mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 2 | // 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 Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 8 | #include <string> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | #include <vector> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 10 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 11 | #include <base/file_path.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 12 | #include <base/memory/ref_counted.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 15 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 16 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 17 | #include "shill/device.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 18 | #include "shill/device_info.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 19 | #include "shill/event_dispatcher.h" |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 20 | #include "shill/modem_info.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 21 | #include "shill/property_store.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 22 | #include "shill/service.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 23 | #include "shill/wifi.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 24 | |
| 25 | namespace shill { |
| 26 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 27 | class ControlInterface; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 28 | class Error; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 29 | class EventDispatcher; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 30 | class ManagerAdaptorInterface; |
| 31 | class Metrics; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 32 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 33 | class Manager { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 34 | public: |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 35 | struct Properties { |
| 36 | public: |
| 37 | Properties() : offline_mode(false) {} |
| 38 | bool offline_mode; |
| 39 | std::string check_portal_list; |
| 40 | std::string country; |
| 41 | std::string portal_url; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 42 | std::string host_name; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 45 | Manager(ControlInterface *control_interface, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 46 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 47 | Metrics *metrics, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 48 | GLib *glib, |
| 49 | const std::string &run_directory, |
| 50 | const std::string &storage_directory, |
| 51 | const std::string &user_storage_format); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 52 | virtual ~Manager(); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 53 | |
| 54 | void AddDeviceToBlackList(const std::string &device_name); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 55 | void Start(); |
| 56 | void Stop(); |
| 57 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 58 | const ProfileRefPtr &ActiveProfile() const; |
| 59 | bool IsActiveProfile(const ProfileRefPtr &profile) const; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 60 | bool MoveServiceToProfile(const ServiceRefPtr &to_move, |
| 61 | const ProfileRefPtr &destination); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 62 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 63 | // Called via RPC call on Service (|to_set|) to set the "Profile" property. |
| 64 | void SetProfileForService(const ServiceRefPtr &to_set, |
| 65 | const std::string &profile, |
| 66 | Error *error); |
| 67 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 68 | void RegisterDevice(const DeviceRefPtr &to_manage); |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 69 | void DeregisterDevice(const DeviceRefPtr &to_forget); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 70 | |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 71 | virtual bool HasService(const ServiceRefPtr &service); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 72 | // Register a Service with the Manager. Manager may choose to |
| 73 | // connect to it immediately. |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 74 | virtual void RegisterService(const ServiceRefPtr &to_manage); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 75 | // Deregister a Service from the Manager. Caller is responsible |
| 76 | // for disconnecting the Service before-hand. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 77 | virtual void DeregisterService(const ServiceRefPtr &to_forget); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 78 | virtual void UpdateService(const ServiceRefPtr &to_update); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 79 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 80 | void FilterByTechnology(Technology::Identifier tech, |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 81 | std::vector<DeviceRefPtr> *found); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 82 | |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 83 | ServiceRefPtr FindService(const std::string& name); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 84 | std::vector<std::string> EnumerateAvailableServices(Error *error); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 85 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 86 | // called via RPC (e.g., from ManagerDBusAdaptor) |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 87 | WiFiServiceRefPtr GetWifiService(const KeyValueStore &args, Error *error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 88 | void RequestScan(const std::string &technology, Error *error); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 89 | std::string GetTechnologyOrder(); |
| 90 | void SetTechnologyOrder(const std::string &order, Error *error); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 91 | // Set up the profile list starting with a default profile along with |
| 92 | // an (optional) list of startup profiles. |
| 93 | void InitializeProfiles(); |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 94 | // Create a profile. This does not affect the profile stack. Returns |
| 95 | // the RPC path of the created profile in |path|. |
| 96 | void CreateProfile(const std::string &name, std::string *path, Error *error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 97 | // Pushes existing profile with name |name| onto stack of managed profiles. |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 98 | // Returns the RPC path of the pushed profile in |path|. |
| 99 | void PushProfile(const std::string &name, std::string *path, Error *error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 100 | // Pops profile named |name| off the top of the stack of managed profiles. |
| 101 | void PopProfile(const std::string &name, Error *error); |
| 102 | // Remove the active profile. |
| 103 | void PopAnyProfile(Error *error); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 104 | // Handle the event where a profile is about to remove a profile entry. |
| 105 | // Any Services that are dependent on this storage identifier will need |
| 106 | // to find new profiles. Return true if any service has been moved to a new |
| 107 | // profile. Any such services will have had the profile group removed from |
| 108 | // the profile. |
| 109 | virtual bool HandleProfileEntryDeletion(const ProfileRefPtr &profile, |
| 110 | const std::string &entry_name); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 111 | // Find a service that is both the member of |profile| and has a |
| 112 | // storage identifier that matches |entry_name|. This function is |
| 113 | // called by the Profile in order to return a profile entry's properties. |
| 114 | virtual ServiceRefPtr GetServiceWithStorageIdentifier( |
| 115 | const ProfileRefPtr &profile, |
| 116 | const std::string &entry_name, |
| 117 | Error *error); |
| 118 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 119 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 120 | virtual DeviceInfo *device_info() { return &device_info_; } |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 121 | ModemInfo *modem_info() { return &modem_info_; } |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 122 | PropertyStore *mutable_store() { return &store_; } |
| 123 | virtual const PropertyStore &store() const { return store_; } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 124 | |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 125 | std::vector<DeviceRefPtr>::iterator devices_begin() { |
| 126 | return devices_.begin(); |
| 127 | } |
| 128 | std::vector<DeviceRefPtr>::iterator devices_end() { return devices_.end(); } |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 129 | void set_startup_profiles(const std::vector<std::string> &startup_profiles) { |
| 130 | startup_profiles_ = startup_profiles; |
| 131 | } |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 132 | virtual const std::string &GetHostName() { return props_.host_name; } |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 133 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 134 | private: |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 135 | friend class ManagerAdaptorInterface; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 136 | friend class ManagerTest; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 137 | FRIEND_TEST(ManagerTest, AvailableTechnologies); |
| 138 | FRIEND_TEST(ManagerTest, ConnectedTechnologies); |
| 139 | FRIEND_TEST(ManagerTest, DefaultTechnology); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 140 | FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart); |
| 141 | FRIEND_TEST(ManagerTest, EnumerateProfiles); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 142 | FRIEND_TEST(ManagerTest, SortServices); |
| 143 | FRIEND_TEST(ManagerTest, SortServicesWithConnection); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 144 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 145 | static const char kManagerErrorNoDevice[]; |
| 146 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 147 | std::string CalculateState(Error *error); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 148 | void AutoConnect(); |
| 149 | void AutoConnectTask(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 150 | std::vector<std::string> AvailableTechnologies(Error *error); |
| 151 | std::vector<std::string> ConnectedTechnologies(Error *error); |
| 152 | std::string DefaultTechnology(Error *error); |
| 153 | std::vector<std::string> EnabledTechnologies(Error *error); |
| 154 | std::vector<std::string> EnumerateDevices(Error *error); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 155 | std::vector<std::string> EnumerateProfiles(Error *error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 156 | // TODO(cmasone): This should be implemented by filtering |services_|. |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 157 | std::vector<std::string> EnumerateWatchedServices(Error *error); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 158 | std::string GetActiveProfileRpcIdentifier(Error *error); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 159 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 160 | void HelpRegisterDerivedString( |
| 161 | const std::string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 162 | std::string(Manager::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 163 | void(Manager::*set)(const std::string&, Error *)); |
| 164 | void HelpRegisterDerivedStrings( |
| 165 | const std::string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 166 | Strings(Manager::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 167 | void(Manager::*set)(const Strings&, Error *)); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 168 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 169 | void PopProfileInternal(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 170 | bool OrderServices(ServiceRefPtr a, ServiceRefPtr b); |
| 171 | void SortServices(); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 172 | bool MatchProfileWithService(const ServiceRefPtr &service); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 173 | |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 174 | // For unit testing. |
| 175 | void set_metrics(Metrics *metrics) { metrics_ = metrics; } |
| 176 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 177 | EventDispatcher *dispatcher_; |
| 178 | ScopedRunnableMethodFactory<Manager> task_factory_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 179 | const FilePath run_path_; |
| 180 | const FilePath storage_path_; |
| 181 | const std::string user_storage_format_; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 182 | scoped_ptr<ManagerAdaptorInterface> adaptor_; |
| 183 | DeviceInfo device_info_; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 184 | ModemInfo modem_info_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 185 | bool running_; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 186 | // Used to facilitate unit tests which can't use RPC. |
| 187 | bool connect_profiles_to_rpc_; |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 188 | std::vector<DeviceRefPtr> devices_; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 189 | // We store Services in a vector, because we want to keep them sorted. |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 190 | std::vector<ServiceRefPtr> services_; |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 191 | // List of startup profile names to push on the profile stack on startup. |
| 192 | std::vector<std::string> startup_profiles_; |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 193 | std::vector<ProfileRefPtr> profiles_; |
| 194 | ProfileRefPtr ephemeral_profile_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 195 | ControlInterface *control_interface_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 196 | Metrics *metrics_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 197 | GLib *glib_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 198 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 199 | // The priority order of technologies |
| 200 | std::vector<Technology::Identifier> technology_order_; |
| 201 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 202 | // Properties to be get/set via PropertyStore calls. |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 203 | Properties props_; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 204 | PropertyStore store_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | } // namespace shill |
| 208 | |
| 209 | #endif // SHILL_MANAGER_ |