blob: 55d8ae0896d412385eaf79dadaa42f3eb74182a3 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Peter Qiu24b34a02015-06-30 10:22:26 -070016
Samuel Tan59b397b2016-01-20 11:25:21 -080017#ifndef SHILL_DAEMON_TASK_H_
18#define SHILL_DAEMON_TASK_H_
Peter Qiu24b34a02015-06-30 10:22:26 -070019
20#include <memory>
21#include <string>
22#include <vector>
23
Peter Qiu29875ff2015-08-27 21:24:56 -070024#include <base/callback.h>
25
Samuel Tan646c7e82015-12-28 15:43:46 -080026#include "shill/event_dispatcher.h"
Peter Qiu24b34a02015-06-30 10:22:26 -070027#if !defined(DISABLE_WIFI)
28#include "shill/wifi/callback80211_metrics.h"
29#endif // DISABLE_WIFI
30
31namespace shill {
32
33class Config;
34class ControlInterface;
35class DHCPProvider;
36class Error;
37class Manager;
38class Metrics;
Peter Qiudde57ef2015-08-14 09:58:51 -070039class ProcessManager;
Peter Qiu24b34a02015-06-30 10:22:26 -070040class RoutingTable;
41class RTNLHandler;
42
43#if !defined(DISABLE_WIFI)
44class NetlinkManager;
45#endif // DISABLE_WIFI
46
Samuel Tan59b397b2016-01-20 11:25:21 -080047// DaemonTask contains most of the logic used in ShillDaemon (e.g.
48// init/shutdown, start/stop). This class is kept separate from ShillDaemon to
49// ensure that it does not inherit brillo::Daemon. This is necessary for
50// DaemonTask unit tests to run, since the base::ExitManager inherited from
Samuel Tan60ea15d2016-01-19 15:31:26 -080051// brillo::Daemon cannot coexist with the base::ExitManager used by shill's
52// test_runner.cc.
Samuel Tan59b397b2016-01-20 11:25:21 -080053class DaemonTask {
Peter Qiu24b34a02015-06-30 10:22:26 -070054 public:
55 // Run-time settings retrieved from command line.
56 struct Settings {
57 Settings()
58 : ignore_unknown_ethernet(false),
59 minimum_mtu(0),
60 passive_mode(false),
61 use_portal_list(false) {}
62 std::string accept_hostname_from;
63 std::string default_technology_order;
64 std::vector<std::string> device_blacklist;
Christopher Grantc1d44732016-01-15 10:54:45 -050065 std::vector<std::string> device_whitelist;
Peter Qiu24b34a02015-06-30 10:22:26 -070066 std::vector<std::string> dhcpv6_enabled_devices;
67 bool ignore_unknown_ethernet;
68 int minimum_mtu;
69 bool passive_mode;
70 std::string portal_list;
71 std::string prepend_dns_servers;
72 bool use_portal_list;
73 };
74
Samuel Tan59b397b2016-01-20 11:25:21 -080075 DaemonTask(const Settings& settings, Config* config);
76 virtual ~DaemonTask();
Peter Qiu24b34a02015-06-30 10:22:26 -070077
mukesh agrawale6b41922015-09-24 16:52:14 -070078 // Starts the termination actions in the manager. Returns true if
79 // termination actions have completed synchronously, and false
80 // otherwise. Arranges for |completion_callback| to be invoked after
81 // all asynchronous work completes, but ignores
82 // |completion_callback| if no asynchronous work is required.
83 virtual bool Quit(const base::Closure& completion_callback);
Peter Qiu24b34a02015-06-30 10:22:26 -070084
Samuel Tan59b397b2016-01-20 11:25:21 -080085 // Break the termination loop started in DaemonTask::OnShutdown. Invoked
Samuel Tan60ea15d2016-01-19 15:31:26 -080086 // after shill completes its termination tasks during shutdown.
87 void BreakTerminationLoop();
88
Peter Qiu24b34a02015-06-30 10:22:26 -070089 protected:
Samuel Tan60ea15d2016-01-19 15:31:26 -080090 void Init();
Samuel Tan646c7e82015-12-28 15:43:46 -080091
Peter Qiu24b34a02015-06-30 10:22:26 -070092 private:
Samuel Tan59b397b2016-01-20 11:25:21 -080093 friend class DaemonTaskTest;
94 friend class DaemonTaskForTest;
Peter Qiu24b34a02015-06-30 10:22:26 -070095
Samuel Tan60ea15d2016-01-19 15:31:26 -080096 void Start();
97
Peter Qiu24b34a02015-06-30 10:22:26 -070098 // Apply run-time settings to the manager.
Peter Qiudde57ef2015-08-14 09:58:51 -070099 void ApplySettings();
Peter Qiu24b34a02015-06-30 10:22:26 -0700100
101 // Called when the termination actions are completed.
102 void TerminationActionsCompleted(const Error& error);
103
104 // Calls Stop() and then causes the dispatcher message loop to terminate and
105 // return to the main function which started the daemon.
106 void StopAndReturnToMain();
107
108 void Stop();
109
Peter Qiudde57ef2015-08-14 09:58:51 -0700110 Settings settings_;
Peter Qiu24b34a02015-06-30 10:22:26 -0700111 Config* config_;
Samuel Tan646c7e82015-12-28 15:43:46 -0800112 std::unique_ptr<EventDispatcher> dispatcher_;
Peter Qiu24b34a02015-06-30 10:22:26 -0700113 std::unique_ptr<ControlInterface> control_;
Peter Qiu24b34a02015-06-30 10:22:26 -0700114 std::unique_ptr<Metrics> metrics_;
Peter Qiu24b34a02015-06-30 10:22:26 -0700115 RTNLHandler* rtnl_handler_;
116 RoutingTable* routing_table_;
117 DHCPProvider* dhcp_provider_;
Peter Qiudde57ef2015-08-14 09:58:51 -0700118 ProcessManager* process_manager_;
Peter Qiu24b34a02015-06-30 10:22:26 -0700119#if !defined(DISABLE_WIFI)
120 NetlinkManager* netlink_manager_;
Peter Qiudde57ef2015-08-14 09:58:51 -0700121 std::unique_ptr<Callback80211Metrics> callback80211_metrics_;
Peter Qiu24b34a02015-06-30 10:22:26 -0700122#endif // DISABLE_WIFI
123 std::unique_ptr<Manager> manager_;
Peter Qiu5988a852015-08-25 16:45:35 -0700124 base::Closure termination_completed_callback_;
Peter Qiu24b34a02015-06-30 10:22:26 -0700125};
126
127} // namespace shill
128
Samuel Tan59b397b2016-01-20 11:25:21 -0800129#endif // SHILL_DAEMON_TASK_H_