blob: ac2f4d2fa2772997fa2d8e791036a2aa40dc969f [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_MANAGER_
6#define SHILL_MANAGER_
7
Chris Masone9be4a9d2011-05-16 15:44:09 -07008#include <string>
Paul Stewart75897df2011-04-27 09:05:53 -07009#include <vector>
Chris Masone487b8bf2011-05-13 16:27:57 -070010
Paul Stewarte6132022011-08-16 09:11:02 -070011#include <base/file_path.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070012#include <base/memory/ref_counted.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070013#include <base/memory/scoped_ptr.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050014#include <base/memory/weak_ptr.h>
Chris Masone88cbd5f2011-07-03 14:30:04 -070015#include <chromeos/dbus/service_constants.h>
Paul Stewart22aa71b2011-09-16 12:15:11 -070016#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewart75897df2011-04-27 09:05:53 -070017
Paul Stewart75897df2011-04-27 09:05:53 -070018#include "shill/device.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070019#include "shill/device_info.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070020#include "shill/event_dispatcher.h"
Darin Petkov887f2982011-07-14 16:10:17 -070021#include "shill/modem_info.h"
Gary Morainac1bdb42012-02-16 17:42:29 -080022#include "shill/power_manager.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070023#include "shill/property_store.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070024#include "shill/service.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010025#include "shill/vpn_provider.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070026#include "shill/wifi.h"
Paul Stewart75897df2011-04-27 09:05:53 -070027
28namespace shill {
29
Chris Masoned0ceb8c2011-06-02 10:05:39 -070030class ControlInterface;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070031class Error;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070032class EventDispatcher;
Thieu Le3426c8f2012-01-11 17:35:11 -080033class ManagerAdaptorInterface;
34class Metrics;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070035
Eric Shienbrood3e20a232012-02-16 11:35:56 -050036class Manager : public base::SupportsWeakPtr<Manager> {
Paul Stewart75897df2011-04-27 09:05:53 -070037 public:
Chris Masone88cbd5f2011-07-03 14:30:04 -070038 struct Properties {
39 public:
Paul Stewartc681fa02012-03-02 19:40:04 -080040 Properties()
41 : offline_mode(false), portal_check_interval_seconds(0) {}
Chris Masone88cbd5f2011-07-03 14:30:04 -070042 bool offline_mode;
43 std::string check_portal_list;
44 std::string country;
Paul Stewartc681fa02012-03-02 19:40:04 -080045 int32 portal_check_interval_seconds;
Chris Masone88cbd5f2011-07-03 14:30:04 -070046 std::string portal_url;
Paul Stewartd32f4842012-01-11 16:08:13 -080047 std::string host_name;
Chris Masone88cbd5f2011-07-03 14:30:04 -070048 };
49
Chris Masone9be4a9d2011-05-16 15:44:09 -070050 Manager(ControlInterface *control_interface,
Darin Petkov887f2982011-07-14 16:10:17 -070051 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080052 Metrics *metrics,
Chris Masone2ae797d2011-08-23 20:41:00 -070053 GLib *glib,
54 const std::string &run_directory,
55 const std::string &storage_directory,
56 const std::string &user_storage_format);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070057 virtual ~Manager();
mukesh agrawal8f317b62011-07-15 11:53:23 -070058
59 void AddDeviceToBlackList(const std::string &device_name);
Gary Morainac1bdb42012-02-16 17:42:29 -080060
Thieu Lefb46caf2012-03-08 11:57:15 -080061 virtual void Start();
Paul Stewart75897df2011-04-27 09:05:53 -070062 void Stop();
63
Paul Stewart1b253142012-01-26 14:05:52 -080064 const ProfileRefPtr &ActiveProfile() const;
65 bool IsActiveProfile(const ProfileRefPtr &profile) const;
Eric Shienbrood9a245532012-03-07 14:20:39 -050066 virtual void SaveActiveProfile();
Chris Masone6515aab2011-10-12 16:19:09 -070067 bool MoveServiceToProfile(const ServiceRefPtr &to_move,
68 const ProfileRefPtr &destination);
Paul Stewart7f61e522012-03-22 11:13:45 -070069 ProfileRefPtr LookupProfileByRpcIdentifier(const std::string &profile_rpcid);
Chris Masone7aa5f902011-07-11 11:13:35 -070070
Paul Stewart1b1a7f22012-01-06 16:24:06 -080071 // Called via RPC call on Service (|to_set|) to set the "Profile" property.
72 void SetProfileForService(const ServiceRefPtr &to_set,
73 const std::string &profile,
74 Error *error);
75
Chris Masone2b105542011-06-22 10:58:09 -070076 void RegisterDevice(const DeviceRefPtr &to_manage);
mukesh agrawal5029c6c2011-08-25 11:12:40 -070077 void DeregisterDevice(const DeviceRefPtr &to_forget);
Chris Masone9be4a9d2011-05-16 15:44:09 -070078
mukesh agrawal4eb4d782011-12-05 17:34:37 +000079 virtual bool HasService(const ServiceRefPtr &service);
mukesh agrawal8a3188d2011-12-01 20:56:44 +000080 // Register a Service with the Manager. Manager may choose to
81 // connect to it immediately.
mukesh agrawal32399322011-09-01 10:53:43 -070082 virtual void RegisterService(const ServiceRefPtr &to_manage);
mukesh agrawal8a3188d2011-12-01 20:56:44 +000083 // Deregister a Service from the Manager. Caller is responsible
84 // for disconnecting the Service before-hand.
Chris Masone6515aab2011-10-12 16:19:09 -070085 virtual void DeregisterService(const ServiceRefPtr &to_forget);
mukesh agrawal00917ce2011-11-22 23:56:55 +000086 virtual void UpdateService(const ServiceRefPtr &to_update);
Chris Masone9be4a9d2011-05-16 15:44:09 -070087
Paul Stewartfdd16072011-09-16 12:41:35 -070088 void FilterByTechnology(Technology::Identifier tech,
Chris Masonec1e50412011-06-07 13:04:53 -070089 std::vector<DeviceRefPtr> *found);
Chris Masone9be4a9d2011-05-16 15:44:09 -070090
Chris Masonee0dea762011-06-09 09:06:03 -070091 ServiceRefPtr FindService(const std::string& name);
Gaurav Shah1b7a6162011-11-09 11:41:01 -080092 std::vector<std::string> EnumerateAvailableServices(Error *error);
Chris Masone9be4a9d2011-05-16 15:44:09 -070093
mukesh agrawal32399322011-09-01 10:53:43 -070094 // called via RPC (e.g., from ManagerDBusAdaptor)
Darin Petkovb65c2452012-02-23 15:17:06 +010095 ServiceRefPtr GetService(const KeyValueStore &args, Error *error);
Paul Stewart7f61e522012-03-22 11:13:45 -070096 void ConfigureService(const KeyValueStore &args, Error *error);
97
Paul Stewartc681fa02012-03-02 19:40:04 -080098 // Request portal detection checks on each registered device until a portal
99 // detection attempt starts on one of them.
100 void RecheckPortal(Error *error);
mukesh agrawal32399322011-09-01 10:53:43 -0700101 void RequestScan(const std::string &technology, Error *error);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700102 std::string GetTechnologyOrder();
103 void SetTechnologyOrder(const std::string &order, Error *error);
Gaurav Shah71354762011-11-28 19:22:49 -0800104 // Set up the profile list starting with a default profile along with
105 // an (optional) list of startup profiles.
106 void InitializeProfiles();
Paul Stewart19c871d2011-12-15 16:10:13 -0800107 // Create a profile. This does not affect the profile stack. Returns
108 // the RPC path of the created profile in |path|.
109 void CreateProfile(const std::string &name, std::string *path, Error *error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700110 // Pushes existing profile with name |name| onto stack of managed profiles.
Paul Stewart19c871d2011-12-15 16:10:13 -0800111 // Returns the RPC path of the pushed profile in |path|.
112 void PushProfile(const std::string &name, std::string *path, Error *error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700113 // Pops profile named |name| off the top of the stack of managed profiles.
114 void PopProfile(const std::string &name, Error *error);
115 // Remove the active profile.
116 void PopAnyProfile(Error *error);
Paul Stewart75225512012-01-26 22:51:33 -0800117 // Handle the event where a profile is about to remove a profile entry.
118 // Any Services that are dependent on this storage identifier will need
119 // to find new profiles. Return true if any service has been moved to a new
120 // profile. Any such services will have had the profile group removed from
121 // the profile.
122 virtual bool HandleProfileEntryDeletion(const ProfileRefPtr &profile,
123 const std::string &entry_name);
Paul Stewart13ed2252012-03-21 12:52:46 -0700124 // Find a registered service that contains a GUID property that
125 // matches |guid|.
126 virtual ServiceRefPtr GetServiceWithGUID(const std::string &guid,
127 Error *error);
Paul Stewart0756db92012-01-27 08:34:47 -0800128 // Find a service that is both the member of |profile| and has a
129 // storage identifier that matches |entry_name|. This function is
130 // called by the Profile in order to return a profile entry's properties.
131 virtual ServiceRefPtr GetServiceWithStorageIdentifier(
132 const ProfileRefPtr &profile,
133 const std::string &entry_name,
134 Error *error);
Paul Stewarte2bad7c2012-03-14 08:55:33 -0700135 // Return a reference to the Service associated with the default connection.
136 // If there is no such connection, this function returns a reference to NULL.
137 virtual ServiceRefPtr GetDefaultService() const;
Paul Stewart0756db92012-01-27 08:34:47 -0800138
Eric Shienbrood9a245532012-03-07 14:20:39 -0500139 // Enable all devices for the named technology.
140 void EnableTechnology(const std::string &technology_name,
141 Error *error,
142 const ResultCallback &callback);
143 // Disable all devices for the named technology.
144 void DisableTechnology(const std::string &technology_name,
145 Error *error,
146 const ResultCallback &callback);
Paul Stewart20088d82012-02-16 06:58:55 -0800147 // Return whether a technology is marked as enabled for portal detection.
148 virtual bool IsPortalDetectionEnabled(Technology::Identifier tech);
149
Paul Stewartc681fa02012-03-02 19:40:04 -0800150 virtual int GetPortalCheckInterval() const {
151 return props_.portal_check_interval_seconds;
152 }
Paul Stewart20088d82012-02-16 06:58:55 -0800153 virtual const std::string &GetPortalCheckURL() const {
154 return props_.portal_url;
155 }
mukesh agrawal32399322011-09-01 10:53:43 -0700156
Chris Masone626719f2011-08-18 16:58:48 -0700157 virtual DeviceInfo *device_info() { return &device_info_; }
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100158 ModemInfo *modem_info() { return &modem_info_; }
Paul Stewartca6abd42012-03-01 15:45:29 -0800159 VPNProvider *vpn_provider() { return &vpn_provider_; }
mukesh agrawalde29fa82011-09-16 16:16:36 -0700160 PropertyStore *mutable_store() { return &store_; }
161 virtual const PropertyStore &store() const { return store_; }
Darin Petkov36a3ace2012-03-06 17:22:14 +0100162 GLib *glib() const { return glib_; }
Chris Masoneb925cc82011-06-22 15:39:57 -0700163
Chris Masone877ff982011-09-21 16:18:24 -0700164 std::vector<DeviceRefPtr>::iterator devices_begin() {
165 return devices_.begin();
166 }
167 std::vector<DeviceRefPtr>::iterator devices_end() { return devices_.end(); }
Gaurav Shah71354762011-11-28 19:22:49 -0800168 void set_startup_profiles(const std::vector<std::string> &startup_profiles) {
169 startup_profiles_ = startup_profiles;
170 }
Paul Stewartd32f4842012-01-11 16:08:13 -0800171 virtual const std::string &GetHostName() { return props_.host_name; }
Chris Masone877ff982011-09-21 16:18:24 -0700172
Eric Shienbrood9a245532012-03-07 14:20:39 -0500173 virtual void UpdateEnabledTechnologies();
Gary Morainac1bdb42012-02-16 17:42:29 -0800174 PowerManager *power_manager() const { return power_manager_.get(); }
175
Paul Stewart75897df2011-04-27 09:05:53 -0700176 private:
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700177 friend class ManagerAdaptorInterface;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700178 friend class ManagerTest;
Gary Morainac1bdb42012-02-16 17:42:29 -0800179 friend class WiFiMainTest;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800180 FRIEND_TEST(ManagerTest, AvailableTechnologies);
181 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
182 FRIEND_TEST(ManagerTest, DefaultTechnology);
Paul Stewart1b253142012-01-26 14:05:52 -0800183 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
184 FRIEND_TEST(ManagerTest, EnumerateProfiles);
Paul Stewart1b253142012-01-26 14:05:52 -0800185 FRIEND_TEST(ManagerTest, SortServices);
186 FRIEND_TEST(ManagerTest, SortServicesWithConnection);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700187
Darin Petkovb65c2452012-02-23 15:17:06 +0100188 static const char kErrorNoDevice[];
189 static const char kErrorTypeRequired[];
190 static const char kErrorUnsupportedServiceType[];
191
192 WiFiServiceRefPtr GetWifiService(const KeyValueStore &args, Error *error);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700193
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800194 std::string CalculateState(Error *error);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000195 void AutoConnect();
196 void AutoConnectTask();
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800197 std::vector<std::string> AvailableTechnologies(Error *error);
198 std::vector<std::string> ConnectedTechnologies(Error *error);
199 std::string DefaultTechnology(Error *error);
200 std::vector<std::string> EnabledTechnologies(Error *error);
201 std::vector<std::string> EnumerateDevices(Error *error);
Paul Stewart1b253142012-01-26 14:05:52 -0800202 std::vector<std::string> EnumerateProfiles(Error *error);
Chris Masone6791a432011-07-12 13:23:19 -0700203 // TODO(cmasone): This should be implemented by filtering |services_|.
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800204 std::vector<std::string> EnumerateWatchedServices(Error *error);
Paul Stewart1b253142012-01-26 14:05:52 -0800205 std::string GetActiveProfileRpcIdentifier(Error *error);
Chris Masoneb925cc82011-06-22 15:39:57 -0700206
mukesh agrawal2366eed2012-03-20 18:21:50 -0700207 void HelpRegisterConstDerivedRpcIdentifiers(
208 const std::string &name,
209 RpcIdentifiers(Manager::*get)(Error *));
mukesh agrawalffa3d042011-10-06 15:26:10 -0700210 void HelpRegisterDerivedString(
211 const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800212 std::string(Manager::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700213 void(Manager::*set)(const std::string&, Error *));
214 void HelpRegisterDerivedStrings(
215 const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800216 Strings(Manager::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700217 void(Manager::*set)(const Strings&, Error *));
Chris Masone27c4aa52011-07-02 13:10:14 -0700218
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700219 void PopProfileInternal();
Paul Stewart22aa71b2011-09-16 12:15:11 -0700220 bool OrderServices(ServiceRefPtr a, ServiceRefPtr b);
221 void SortServices();
Paul Stewart75225512012-01-26 22:51:33 -0800222 bool MatchProfileWithService(const ServiceRefPtr &service);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700223
Thieu Lea20cbc22012-01-09 22:01:43 +0000224 // For unit testing.
225 void set_metrics(Metrics *metrics) { metrics_ = metrics; }
226
Gary Morainac1bdb42012-02-16 17:42:29 -0800227 // Used by tests to set a mock PowerManager. Takes ownership of
228 // power_manager.
229 void set_power_manager(PowerManager *power_manager) {
230 power_manager_.reset(power_manager);
231 }
232
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000233 EventDispatcher *dispatcher_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700234 const FilePath run_path_;
235 const FilePath storage_path_;
236 const std::string user_storage_format_;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700237 scoped_ptr<ManagerAdaptorInterface> adaptor_;
238 DeviceInfo device_info_;
Darin Petkov887f2982011-07-14 16:10:17 -0700239 ModemInfo modem_info_;
Darin Petkov33af05c2012-02-28 10:10:30 +0100240 VPNProvider vpn_provider_;
Paul Stewart75897df2011-04-27 09:05:53 -0700241 bool running_;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700242 // Used to facilitate unit tests which can't use RPC.
243 bool connect_profiles_to_rpc_;
Chris Masonec1e50412011-06-07 13:04:53 -0700244 std::vector<DeviceRefPtr> devices_;
Chris Masone6791a432011-07-12 13:23:19 -0700245 // We store Services in a vector, because we want to keep them sorted.
Chris Masonec1e50412011-06-07 13:04:53 -0700246 std::vector<ServiceRefPtr> services_;
Gaurav Shah71354762011-11-28 19:22:49 -0800247 // List of startup profile names to push on the profile stack on startup.
248 std::vector<std::string> startup_profiles_;
Chris Masone7aa5f902011-07-11 11:13:35 -0700249 std::vector<ProfileRefPtr> profiles_;
250 ProfileRefPtr ephemeral_profile_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700251 ControlInterface *control_interface_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800252 Metrics *metrics_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700253 GLib *glib_;
Gary Morainac1bdb42012-02-16 17:42:29 -0800254 scoped_ptr<PowerManager> power_manager_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700255
Paul Stewart22aa71b2011-09-16 12:15:11 -0700256 // The priority order of technologies
257 std::vector<Technology::Identifier> technology_order_;
258
Chris Masoneb925cc82011-06-22 15:39:57 -0700259 // Properties to be get/set via PropertyStore calls.
Chris Masone88cbd5f2011-07-03 14:30:04 -0700260 Properties props_;
Chris Masone27c4aa52011-07-02 13:10:14 -0700261 PropertyStore store_;
Paul Stewart75897df2011-04-27 09:05:53 -0700262};
263
264} // namespace shill
265
266#endif // SHILL_MANAGER_