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 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 5 | #include "shill/manager.h" |
| 6 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | #include <time.h> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 9 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 10 | #include <algorithm> |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 11 | #include <set> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 12 | #include <string> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 13 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 14 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | #include <base/bind.h> |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 16 | #include <base/file_util.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 17 | #include <base/memory/ref_counted.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 18 | #include <base/stringprintf.h> |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 19 | #include <base/string_split.h> |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 20 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 21 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 22 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 23 | #include "shill/adaptor_interfaces.h" |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 24 | #include "shill/callbacks.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 25 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 26 | #include "shill/control_interface.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 27 | #include "shill/dbus_adaptor.h" |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 28 | #include "shill/dbus_manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 29 | #include "shill/default_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 30 | #include "shill/device.h" |
| 31 | #include "shill/device_info.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 32 | #include "shill/ephemeral_profile.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 33 | #include "shill/error.h" |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 34 | #include "shill/ethernet_eap_provider.h" |
| 35 | #include "shill/ethernet_eap_service.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 36 | #include "shill/event_dispatcher.h" |
Gaurav Shah | b790aa2 | 2012-10-23 12:51:12 -0700 | [diff] [blame] | 37 | #include "shill/geolocation_info.h" |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 38 | #include "shill/hook_table.h" |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 39 | #include "shill/ip_address_store.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 40 | #include "shill/key_file_store.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 41 | #include "shill/logging.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 42 | #include "shill/profile.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 43 | #include "shill/property_accessor.h" |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 44 | #include "shill/proxy_factory.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 45 | #include "shill/resolver.h" |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 46 | #include "shill/result_aggregator.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 47 | #include "shill/service.h" |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 48 | #include "shill/service_sorter.h" |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 49 | #include "shill/vpn_provider.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 50 | #include "shill/vpn_service.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 51 | #include "shill/wifi.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 52 | #include "shill/wifi_provider.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 53 | #include "shill/wifi_service.h" |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 54 | #include "shill/wimax_service.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 55 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 56 | using base::Bind; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 57 | using base::FilePath; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 58 | using base::StringPrintf; |
| 59 | using base::Unretained; |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 60 | using std::map; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 61 | using std::set; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 62 | using std::string; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 63 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 64 | |
| 65 | namespace shill { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 66 | |
Daniel Erat | f975367 | 2013-01-24 10:17:02 -0800 | [diff] [blame] | 67 | namespace { |
| 68 | |
| 69 | // Human-readable string describing the suspend delay that is registered |
| 70 | // with the power manager. |
| 71 | const char kSuspendDelayDescription[] = "shill"; |
| 72 | |
| 73 | } // namespace |
| 74 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 75 | // statics |
| 76 | const char Manager::kErrorNoDevice[] = "no wifi devices available"; |
| 77 | const char Manager::kErrorTypeRequired[] = "must specify service type"; |
| 78 | const char Manager::kErrorUnsupportedServiceType[] = |
| 79 | "service type is unsupported"; |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 80 | // This timeout should be less than the upstart job timeout, otherwise |
| 81 | // stats for termination actions might be lost. |
| 82 | const int Manager::kTerminationActionsTimeoutMilliseconds = 4500; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 83 | const char Manager::kPowerManagerKey[] = "manager"; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 84 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 85 | Manager::Manager(ControlInterface *control_interface, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 86 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 87 | Metrics *metrics, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 88 | GLib *glib, |
| 89 | const string &run_directory, |
| 90 | const string &storage_directory, |
| 91 | const string &user_storage_format) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 92 | : dispatcher_(dispatcher), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 93 | run_path_(FilePath(run_directory)), |
| 94 | storage_path_(FilePath(storage_directory)), |
| 95 | user_storage_format_(user_storage_format), |
mukesh agrawal | d142fd6 | 2013-05-01 16:50:57 -0700 | [diff] [blame] | 96 | user_profile_list_path_(FilePath(Profile::kUserProfileListPathname)), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 97 | adaptor_(control_interface->CreateManagerAdaptor(this)), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 98 | device_info_(control_interface, dispatcher, metrics, this), |
| 99 | modem_info_(control_interface, dispatcher, metrics, this, glib), |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 100 | ethernet_eap_provider_( |
| 101 | new EthernetEapProvider( |
| 102 | control_interface, dispatcher, metrics, this)), |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 103 | vpn_provider_( |
| 104 | new VPNProvider(control_interface, dispatcher, metrics, this)), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 105 | wifi_provider_( |
| 106 | new WiFiProvider(control_interface, dispatcher, metrics, this)), |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 107 | wimax_provider_(control_interface, dispatcher, metrics, this), |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 108 | resolver_(Resolver::GetInstance()), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 109 | running_(false), |
| 110 | connect_profiles_to_rpc_(true), |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 111 | ephemeral_profile_( |
| 112 | new EphemeralProfile(control_interface, metrics, this)), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 113 | control_interface_(control_interface), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 114 | metrics_(metrics), |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 115 | glib_(glib), |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 116 | use_startup_portal_list_(false), |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 117 | termination_actions_(dispatcher), |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 118 | suspend_delay_registered_(false), |
| 119 | suspend_delay_id_(0), |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 120 | default_service_callback_tag_(0), |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 121 | crypto_util_proxy_(new CryptoUtilProxy(dispatcher, glib)), |
| 122 | health_checker_remote_ips_(new IPAddressStore()) { |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 123 | HelpRegisterDerivedString(flimflam::kActiveProfileProperty, |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 124 | &Manager::GetActiveProfileRpcIdentifier, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 125 | NULL); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 126 | store_.RegisterBool(flimflam::kArpGatewayProperty, &props_.arp_gateway); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 127 | HelpRegisterConstDerivedStrings(flimflam::kAvailableTechnologiesProperty, |
| 128 | &Manager::AvailableTechnologies); |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 129 | HelpRegisterDerivedString(flimflam::kCheckPortalListProperty, |
| 130 | &Manager::GetCheckPortalList, |
| 131 | &Manager::SetCheckPortalList); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 132 | HelpRegisterConstDerivedStrings(flimflam::kConnectedTechnologiesProperty, |
| 133 | &Manager::ConnectedTechnologies); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 134 | store_.RegisterString(flimflam::kCountryProperty, &props_.country); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 135 | HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty, |
| 136 | &Manager::DefaultTechnology, |
| 137 | NULL); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 138 | HelpRegisterConstDerivedRpcIdentifier( |
| 139 | shill::kDefaultServiceProperty, |
| 140 | &Manager::GetDefaultServiceRpcIdentifier); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 141 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kDevicesProperty, |
| 142 | &Manager::EnumerateDevices); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 143 | HelpRegisterConstDerivedStrings(flimflam::kEnabledTechnologiesProperty, |
| 144 | &Manager::EnabledTechnologies); |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 145 | HelpRegisterDerivedString(shill::kIgnoredDNSSearchPathsProperty, |
| 146 | &Manager::GetIgnoredDNSSearchPaths, |
| 147 | &Manager::SetIgnoredDNSSearchPaths); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 148 | store_.RegisterString(shill::kLinkMonitorTechnologiesProperty, |
| 149 | &props_.link_monitor_technologies); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 150 | store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode); |
| 151 | store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 152 | store_.RegisterInt32(kPortalCheckIntervalProperty, |
| 153 | &props_.portal_check_interval_seconds); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 154 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kProfilesProperty, |
| 155 | &Manager::EnumerateProfiles); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 156 | store_.RegisterString(kHostNameProperty, &props_.host_name); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 157 | HelpRegisterDerivedString(flimflam::kStateProperty, |
| 158 | &Manager::CalculateState, |
| 159 | NULL); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 160 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kServicesProperty, |
| 161 | &Manager::EnumerateAvailableServices); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 162 | HelpRegisterConstDerivedRpcIdentifiers(shill::kServiceCompleteListProperty, |
| 163 | &Manager::EnumerateCompleteServices); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 164 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kServiceWatchListProperty, |
| 165 | &Manager::EnumerateWatchedServices); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 166 | HelpRegisterConstDerivedStrings(kUninitializedTechnologiesProperty, |
| 167 | &Manager::UninitializedTechnologies); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 168 | |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 169 | // Set default technology order "by hand", to avoid invoking side |
| 170 | // effects of SetTechnologyOrder. |
| 171 | technology_order_.push_back( |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 172 | Technology::IdentifierFromName(flimflam::kTypeVPN)); |
| 173 | technology_order_.push_back( |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 174 | Technology::IdentifierFromName(flimflam::kTypeEthernet)); |
| 175 | technology_order_.push_back( |
| 176 | Technology::IdentifierFromName(flimflam::kTypeWifi)); |
| 177 | technology_order_.push_back( |
Ben Chan | 3cafe38 | 2012-11-13 07:51:10 -0800 | [diff] [blame] | 178 | Technology::IdentifierFromName(flimflam::kTypeWimax)); |
| 179 | technology_order_.push_back( |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 180 | Technology::IdentifierFromName(flimflam::kTypeCellular)); |
| 181 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 182 | SLOG(Manager, 2) << "Manager initialized."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 185 | Manager::~Manager() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 186 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 187 | void Manager::AddDeviceToBlackList(const string &device_name) { |
| 188 | device_info_.AddDeviceToBlackList(device_name); |
| 189 | } |
| 190 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 191 | void Manager::Start() { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 192 | LOG(INFO) << "Manager started."; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 193 | |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 194 | dbus_manager_.reset(new DBusManager()); |
| 195 | dbus_manager_->Start(); |
| 196 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 197 | power_manager_.reset( |
| 198 | new PowerManager(dispatcher_, ProxyFactory::GetInstance())); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 199 | power_manager_->AddStateChangeCallback( |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 200 | kPowerManagerKey, |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 201 | Bind(&Manager::OnPowerStateChanged, AsWeakPtr())); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 202 | // TODO(ers): weak ptr for metrics_? |
| 203 | PowerManager::PowerStateCallback cb = |
| 204 | Bind(&Metrics::NotifyPowerStateChange, Unretained(metrics_)); |
Thieu Le | b84ba34 | 2012-03-02 15:15:19 -0800 | [diff] [blame] | 205 | power_manager_->AddStateChangeCallback(Metrics::kMetricPowerManagerKey, cb); |
| 206 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 207 | CHECK(file_util::CreateDirectory(run_path_)) << run_path_.value(); |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 208 | resolver_->set_path(run_path_.Append("resolv.conf")); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 209 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 210 | InitializeProfiles(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 211 | running_ = true; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 212 | adaptor_->UpdateRunning(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 213 | device_info_.Start(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 214 | modem_info_.Start(); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 215 | ethernet_eap_provider_->Start(); |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 216 | vpn_provider_->Start(); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 217 | wifi_provider_->Start(); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 218 | wimax_provider_.Start(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void Manager::Stop() { |
| 222 | running_ = false; |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 223 | // Persist device information to disk; |
| 224 | vector<DeviceRefPtr>::iterator devices_it; |
| 225 | for (devices_it = devices_.begin(); devices_it != devices_.end(); |
| 226 | ++devices_it) { |
| 227 | UpdateDevice(*devices_it); |
| 228 | } |
| 229 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 230 | UpdateWiFiProvider(); |
| 231 | |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 232 | // Persist profile, service information to disk. |
| 233 | vector<ProfileRefPtr>::iterator profiles_it; |
| 234 | for (profiles_it = profiles_.begin(); profiles_it != profiles_.end(); |
| 235 | ++profiles_it) { |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 236 | // Since this happens in a loop, the current manager state is stored to |
| 237 | // all default profiles in the stack. This is acceptable because the |
| 238 | // only time multiple default profiles are loaded are during autotests. |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 239 | (*profiles_it)->Save(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 240 | } |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 241 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 242 | vector<ServiceRefPtr>::iterator services_it; |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 243 | Error e; |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 244 | for (services_it = services_.begin(); services_it != services_.end(); |
| 245 | ++services_it) { |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 246 | (*services_it)->Disconnect(&e); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 249 | adaptor_->UpdateRunning(); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 250 | wimax_provider_.Stop(); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 251 | wifi_provider_->Stop(); |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 252 | vpn_provider_->Stop(); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 253 | ethernet_eap_provider_->Stop(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 254 | modem_info_.Stop(); |
| 255 | device_info_.Stop(); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 256 | sort_services_task_.Cancel(); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 257 | power_manager_.reset(); |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 258 | dbus_manager_.reset(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 261 | void Manager::InitializeProfiles() { |
mukesh agrawal | d142fd6 | 2013-05-01 16:50:57 -0700 | [diff] [blame] | 262 | DCHECK(profiles_.empty()); // The default profile must go first on stack. |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 263 | CHECK(file_util::CreateDirectory(storage_path_)) << storage_path_.value(); |
mukesh agrawal | d142fd6 | 2013-05-01 16:50:57 -0700 | [diff] [blame] | 264 | |
| 265 | // Ensure that we have storage for the default profile, and that |
| 266 | // the persistent copy of the default profile is not corrupt. |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 267 | scoped_refptr<DefaultProfile> |
| 268 | default_profile(new DefaultProfile(control_interface_, |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 269 | metrics_, |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 270 | this, |
| 271 | storage_path_, |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 272 | DefaultProfile::kDefaultId, |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 273 | props_)); |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 274 | // The default profile may fail to initialize if it's corrupted. |
| 275 | // If so, recreate the default profile. |
| 276 | if (!default_profile->InitStorage( |
| 277 | glib_, Profile::kCreateOrOpenExisting, NULL)) |
| 278 | CHECK(default_profile->InitStorage(glib_, Profile::kCreateNew, |
| 279 | NULL)); |
mukesh agrawal | 0075253 | 2013-05-03 15:46:55 -0700 | [diff] [blame] | 280 | // In case we created a new profile, initialize its default values, |
| 281 | // and then save. This is required for properties such as |
| 282 | // PortalDetector::kDefaultCheckPortalList to be initialized correctly. |
| 283 | LoadProperties(default_profile); |
mukesh agrawal | d142fd6 | 2013-05-01 16:50:57 -0700 | [diff] [blame] | 284 | default_profile->Save(); |
| 285 | default_profile = NULL; // PushProfileInternal will re-create. |
| 286 | |
| 287 | // Read list of user profiles. This must be done before pushing the |
| 288 | // default profile, because modifying the profile stack updates the |
| 289 | // user profile list. |
| 290 | vector<Profile::Identifier> identifiers = |
| 291 | Profile::LoadUserProfileList(user_profile_list_path_); |
| 292 | |
| 293 | // Push the default profile onto the stack. |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 294 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 295 | string path; |
mukesh agrawal | d142fd6 | 2013-05-01 16:50:57 -0700 | [diff] [blame] | 296 | Profile::Identifier default_profile_id; |
| 297 | CHECK(Profile::ParseIdentifier( |
| 298 | DefaultProfile::kDefaultId, &default_profile_id)); |
| 299 | PushProfileInternal(default_profile_id, &path, &error); |
| 300 | CHECK(error.IsSuccess()); // Must have a default profile. |
| 301 | |
| 302 | // Push user profiles onto the stack. |
| 303 | for (const auto &profile_id : identifiers) { |
| 304 | PushProfileInternal(profile_id, &path, &error); |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 305 | } |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 308 | void Manager::CreateProfile(const string &name, string *path, Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 309 | SLOG(Manager, 2) << __func__ << " " << name; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 310 | Profile::Identifier ident; |
| 311 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 312 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 313 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 314 | return; |
| 315 | } |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 316 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 317 | if (HasProfile(ident)) { |
| 318 | Error::PopulateAndLog(error, Error::kAlreadyExists, |
| 319 | "Profile name " + name + " is already on stack"); |
| 320 | return; |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 323 | ProfileRefPtr profile; |
| 324 | if (ident.user.empty()) { |
| 325 | profile = new DefaultProfile(control_interface_, |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 326 | metrics_, |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 327 | this, |
| 328 | storage_path_, |
| 329 | ident.identifier, |
| 330 | props_); |
| 331 | } else { |
| 332 | profile = new Profile(control_interface_, |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 333 | metrics_, |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 334 | this, |
| 335 | ident, |
| 336 | user_storage_format_, |
Gary Morain | b672d35 | 2012-04-25 09:19:06 -0700 | [diff] [blame] | 337 | true); |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 340 | if (!profile->InitStorage(glib_, Profile::kCreateNew, error)) { |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 341 | // |error| will have been populated by InitStorage(). |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
| 345 | // Save profile data out, and then let the scoped pointer fall out of scope. |
| 346 | if (!profile->Save()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 347 | Error::PopulateAndLog(error, Error::kInternalError, |
| 348 | "Profile name " + name + " could not be saved"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 349 | return; |
| 350 | } |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 351 | |
| 352 | *path = profile->GetRpcIdentifier(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 355 | bool Manager::HasProfile(const Profile::Identifier &ident) { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 356 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 357 | it != profiles_.end(); |
| 358 | ++it) { |
| 359 | if ((*it)->MatchesIdentifier(ident)) { |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 360 | return true; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 363 | return false; |
| 364 | } |
| 365 | |
| 366 | void Manager::PushProfileInternal( |
| 367 | const Profile::Identifier &ident, string *path, Error *error) { |
| 368 | if (HasProfile(ident)) { |
| 369 | Error::PopulateAndLog(error, Error::kAlreadyExists, |
| 370 | "Profile name " + Profile::IdentifierToString(ident) + |
| 371 | " is already on stack"); |
| 372 | return; |
| 373 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 374 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 375 | ProfileRefPtr profile; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 376 | if (ident.user.empty()) { |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 377 | // Allow a machine-wide-profile to be pushed on the stack only if the |
| 378 | // profile stack is empty, or if the topmost profile on the stack is |
| 379 | // also a machine-wide (non-user) profile. |
| 380 | if (!profiles_.empty() && !profiles_.back()->GetUser().empty()) { |
| 381 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 382 | "Cannot load non-default global profile " + |
| 383 | Profile::IdentifierToString(ident) + |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 384 | " on top of a user profile"); |
| 385 | return; |
| 386 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 387 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 388 | scoped_refptr<DefaultProfile> |
| 389 | default_profile(new DefaultProfile(control_interface_, |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 390 | metrics_, |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 391 | this, |
| 392 | storage_path_, |
| 393 | ident.identifier, |
| 394 | props_)); |
| 395 | if (!default_profile->InitStorage(glib_, Profile::kOpenExisting, error)) { |
| 396 | // |error| will have been populated by InitStorage(). |
| 397 | return; |
| 398 | } |
| 399 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 400 | if (!LoadProperties(default_profile)) { |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 401 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 402 | "Could not load Manager properties from profile " + |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 403 | Profile::IdentifierToString(ident)); |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | profile = default_profile; |
| 407 | } else { |
| 408 | profile = new Profile(control_interface_, |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 409 | metrics_, |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 410 | this, |
| 411 | ident, |
| 412 | user_storage_format_, |
| 413 | connect_profiles_to_rpc_); |
| 414 | if (!profile->InitStorage(glib_, Profile::kOpenExisting, error)) { |
| 415 | // |error| will have been populated by InitStorage(). |
| 416 | return; |
| 417 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 420 | profiles_.push_back(profile); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 421 | |
| 422 | // Offer each registered Service the opportunity to join this new Profile. |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 423 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 424 | it != services_.end(); ++it) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 425 | profile->ConfigureService(*it); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 428 | // Shop the Profile contents around to Devices which may have configuration |
| 429 | // stored in these profiles. |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 430 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 431 | it != devices_.end(); ++it) { |
| 432 | profile->ConfigureDevice(*it); |
| 433 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 434 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 435 | // Offer the Profile contents to the service/device providers which will |
| 436 | // create new services if necessary. |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 437 | vpn_provider_->CreateServicesFromProfile(profile); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 438 | wifi_provider_->CreateServicesFromProfile(profile); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 439 | wimax_provider_.CreateServicesFromProfile(profile); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 440 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 441 | *path = profile->GetRpcIdentifier(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 442 | SortServices(); |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 443 | OnProfilesChanged(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 446 | void Manager::PushProfile(const string &name, string *path, Error *error) { |
| 447 | SLOG(Manager, 2) << __func__ << " " << name; |
| 448 | Profile::Identifier ident; |
| 449 | if (!Profile::ParseIdentifier(name, &ident)) { |
| 450 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 451 | "Invalid profile name " + name); |
| 452 | return; |
| 453 | } |
| 454 | PushProfileInternal(ident, path, error); |
| 455 | } |
| 456 | |
| 457 | void Manager::InsertUserProfile(const string &name, |
| 458 | const string &user_hash, |
| 459 | string *path, |
| 460 | Error *error) { |
| 461 | SLOG(Manager, 2) << __func__ << " " << name; |
| 462 | Profile::Identifier ident; |
| 463 | if (!Profile::ParseIdentifier(name, &ident) || |
| 464 | ident.user.empty()) { |
| 465 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 466 | "Invalid user profile name " + name); |
| 467 | return; |
| 468 | } |
| 469 | ident.user_hash = user_hash; |
| 470 | PushProfileInternal(ident, path, error); |
| 471 | } |
| 472 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 473 | void Manager::PopProfileInternal() { |
| 474 | CHECK(!profiles_.empty()); |
| 475 | ProfileRefPtr active_profile = profiles_.back(); |
| 476 | profiles_.pop_back(); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 477 | vector<ServiceRefPtr>::iterator it; |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 478 | for (it = services_.begin(); it != services_.end();) { |
| 479 | if ((*it)->profile().get() != active_profile.get() || |
| 480 | MatchProfileWithService(*it) || |
| 481 | !UnloadService(&it)) { |
| 482 | LOG(ERROR) << "Skipping unload of service"; |
| 483 | ++it; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 486 | SortServices(); |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 487 | OnProfilesChanged(); |
| 488 | } |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 489 | |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 490 | void Manager::OnProfilesChanged() { |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 491 | Error unused_error; |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 492 | |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 493 | adaptor_->EmitStringsChanged(flimflam::kProfilesProperty, |
| 494 | EnumerateProfiles(&unused_error)); |
mukesh agrawal | d142fd6 | 2013-05-01 16:50:57 -0700 | [diff] [blame] | 495 | Profile::SaveUserProfileList(user_profile_list_path_, profiles_); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 498 | void Manager::PopProfile(const string &name, Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 499 | SLOG(Manager, 2) << __func__ << " " << name; |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 500 | // This signal is sent when a user logs out of a session. Regardless of |
| 501 | // whether we find their profile to remove, lets clear the network related |
| 502 | // logs. |
| 503 | MemoryLog::GetInstance()->Clear(); |
| 504 | LOG(INFO) << "Cleared the memory log on logout event."; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 505 | Profile::Identifier ident; |
| 506 | if (profiles_.empty()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 507 | Error::PopulateAndLog(error, Error::kNotFound, "Profile stack is empty"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 508 | return; |
| 509 | } |
| 510 | ProfileRefPtr active_profile = profiles_.back(); |
| 511 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 512 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 513 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 514 | return; |
| 515 | } |
| 516 | if (!active_profile->MatchesIdentifier(ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 517 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 518 | name + " is not the active profile"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 519 | return; |
| 520 | } |
| 521 | PopProfileInternal(); |
| 522 | } |
| 523 | |
| 524 | void Manager::PopAnyProfile(Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 525 | SLOG(Manager, 2) << __func__; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 526 | Profile::Identifier ident; |
| 527 | if (profiles_.empty()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 528 | Error::PopulateAndLog(error, Error::kNotFound, "Profile stack is empty"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | PopProfileInternal(); |
| 532 | } |
| 533 | |
Paul Stewart | 307c250 | 2013-03-23 12:32:10 -0700 | [diff] [blame] | 534 | void Manager::PopAllUserProfiles(Error */*error*/) { |
| 535 | SLOG(Manager, 2) << __func__; |
| 536 | // This signal is sent when a user logs out of a session. Regardless of |
| 537 | // whether we find their profile to remove, lets clear the network related |
| 538 | // logs. |
| 539 | MemoryLog::GetInstance()->Clear(); |
| 540 | LOG(INFO) << "Cleared the memory log on logout event."; |
| 541 | while (!profiles_.empty() && !profiles_.back()->GetUser().empty()) { |
| 542 | PopProfileInternal(); |
| 543 | } |
| 544 | } |
| 545 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 546 | void Manager::RemoveProfile(const string &name, Error *error) { |
| 547 | Profile::Identifier ident; |
| 548 | if (!Profile::ParseIdentifier(name, &ident)) { |
| 549 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 550 | "Invalid profile name " + name); |
| 551 | return; |
| 552 | } |
| 553 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 554 | if (HasProfile(ident)) { |
| 555 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 556 | "Cannot remove profile name " + name + |
| 557 | " since it is on stack"); |
| 558 | return; |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | ProfileRefPtr profile; |
| 562 | if (ident.user.empty()) { |
| 563 | profile = new DefaultProfile(control_interface_, |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 564 | metrics_, |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 565 | this, |
| 566 | storage_path_, |
| 567 | ident.identifier, |
| 568 | props_); |
| 569 | } else { |
| 570 | profile = new Profile(control_interface_, |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 571 | metrics_, |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 572 | this, |
| 573 | ident, |
| 574 | user_storage_format_, |
| 575 | false); |
| 576 | } |
| 577 | |
| 578 | |
| 579 | // |error| will have been populated if RemoveStorage fails. |
| 580 | profile->RemoveStorage(glib_, error); |
| 581 | |
| 582 | return; |
| 583 | } |
| 584 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 585 | bool Manager::HandleProfileEntryDeletion(const ProfileRefPtr &profile, |
| 586 | const std::string &entry_name) { |
| 587 | bool moved_services = false; |
| 588 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 589 | it != services_.end();) { |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 590 | if ((*it)->profile().get() == profile.get() && |
| 591 | (*it)->GetStorageIdentifier() == entry_name) { |
| 592 | profile->AbandonService(*it); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 593 | if (MatchProfileWithService(*it) || |
| 594 | !UnloadService(&it)) { |
| 595 | ++it; |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 596 | } |
| 597 | moved_services = true; |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 598 | } else { |
| 599 | ++it; |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | return moved_services; |
| 603 | } |
| 604 | |
Paul Stewart | 967eaeb | 2013-04-25 19:53:07 -0700 | [diff] [blame] | 605 | map<string, string> Manager::GetLoadableProfileEntriesForService( |
| 606 | const ServiceConstRefPtr &service) { |
| 607 | map<string, string> profile_entries; |
| 608 | for (const auto &profile : profiles_) { |
| 609 | string entry_name = service->GetLoadableStorageIdentifier( |
| 610 | *profile->GetConstStorage()); |
| 611 | if (!entry_name.empty()) { |
| 612 | profile_entries[profile->GetRpcIdentifier()] = entry_name; |
| 613 | } |
| 614 | } |
| 615 | return profile_entries; |
| 616 | } |
| 617 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 618 | ServiceRefPtr Manager::GetServiceWithStorageIdentifier( |
| 619 | const ProfileRefPtr &profile, const std::string &entry_name, Error *error) { |
| 620 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 621 | it != services_.end(); ++it) { |
| 622 | if ((*it)->profile().get() == profile.get() && |
| 623 | (*it)->GetStorageIdentifier() == entry_name) { |
| 624 | return *it; |
| 625 | } |
| 626 | } |
| 627 | |
Paul Stewart | 76a89cb | 2012-09-14 06:06:48 -0700 | [diff] [blame] | 628 | string error_string( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 629 | StringPrintf("Entry %s is not registered in the manager", |
| 630 | entry_name.c_str())); |
Paul Stewart | 76a89cb | 2012-09-14 06:06:48 -0700 | [diff] [blame] | 631 | if (error) { |
| 632 | error->Populate(Error::kNotFound, error_string); |
| 633 | } |
| 634 | SLOG(Manager, 2) << error_string; |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 635 | return NULL; |
| 636 | } |
| 637 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 638 | ServiceRefPtr Manager::GetServiceWithGUID( |
| 639 | const std::string &guid, Error *error) { |
| 640 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 641 | it != services_.end(); ++it) { |
| 642 | if ((*it)->guid() == guid) { |
| 643 | return *it; |
| 644 | } |
| 645 | } |
| 646 | |
Paul Stewart | 76a89cb | 2012-09-14 06:06:48 -0700 | [diff] [blame] | 647 | string error_string( |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 648 | StringPrintf("Service wth GUID %s is not registered in the manager", |
| 649 | guid.c_str())); |
Paul Stewart | 76a89cb | 2012-09-14 06:06:48 -0700 | [diff] [blame] | 650 | if (error) { |
| 651 | error->Populate(Error::kNotFound, error_string); |
| 652 | } |
| 653 | SLOG(Manager, 2) << error_string; |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 654 | return NULL; |
| 655 | } |
| 656 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 657 | ServiceRefPtr Manager::GetDefaultService() const { |
Arman Uguray | 32c7640 | 2012-11-27 14:01:13 -0800 | [diff] [blame] | 658 | SLOG(Manager, 2) << __func__; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 659 | if (services_.empty() || !services_[0]->connection().get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 660 | SLOG(Manager, 2) << "In " << __func__ << ": No default connection exists."; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 661 | return NULL; |
| 662 | } |
| 663 | return services_[0]; |
| 664 | } |
| 665 | |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 666 | RpcIdentifier Manager::GetDefaultServiceRpcIdentifier(Error */*error*/) { |
| 667 | ServiceRefPtr default_service = GetDefaultService(); |
| 668 | return default_service ? default_service->GetRpcIdentifier() : "/"; |
| 669 | } |
| 670 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 671 | bool Manager::IsTechnologyInList(const string &technology_list, |
| 672 | Technology::Identifier tech) const { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 673 | Error error; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 674 | vector<Technology::Identifier> technologies; |
| 675 | return Technology::GetTechnologyVectorFromString(technology_list, |
| 676 | &technologies, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 677 | &error) && |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 678 | std::find(technologies.begin(), technologies.end(), tech) != |
| 679 | technologies.end(); |
| 680 | } |
| 681 | |
| 682 | bool Manager::IsPortalDetectionEnabled(Technology::Identifier tech) { |
| 683 | return IsTechnologyInList(GetCheckPortalList(NULL), tech); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 686 | void Manager::SetStartupPortalList(const string &portal_list) { |
| 687 | startup_portal_list_ = portal_list; |
| 688 | use_startup_portal_list_ = true; |
| 689 | } |
| 690 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 691 | bool Manager::IsProfileBefore(const ProfileRefPtr &a, |
| 692 | const ProfileRefPtr &b) const { |
| 693 | DCHECK(a != b); |
| 694 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 695 | it != profiles_.end(); |
| 696 | ++it) { |
| 697 | if (*it == a) { |
| 698 | return true; |
| 699 | } |
| 700 | if (*it == b) { |
| 701 | return false; |
| 702 | } |
| 703 | } |
| 704 | NOTREACHED() << "We should have found both profiles in the profiles_ list!"; |
| 705 | return false; |
| 706 | } |
| 707 | |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 708 | bool Manager::IsServiceEphemeral(const ServiceConstRefPtr &service) const { |
| 709 | return service->profile() == ephemeral_profile_; |
| 710 | } |
| 711 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 712 | bool Manager::IsTechnologyLinkMonitorEnabled( |
| 713 | Technology::Identifier technology) const { |
| 714 | return IsTechnologyInList(props_.link_monitor_technologies, technology); |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 717 | bool Manager::IsDefaultProfile(StoreInterface *storage) { |
| 718 | return profiles_.empty() || storage == profiles_.front()->GetConstStorage(); |
| 719 | } |
| 720 | |
| 721 | void Manager::OnProfileStorageInitialized(StoreInterface *storage) { |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 722 | wifi_provider_->LoadAndFixupServiceEntries(storage, |
| 723 | IsDefaultProfile(storage)); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | DeviceRefPtr Manager::GetEnabledDeviceWithTechnology( |
| 727 | Technology::Identifier technology) const { |
| 728 | vector<DeviceRefPtr> devices; |
| 729 | FilterByTechnology(technology, &devices); |
| 730 | for (vector<DeviceRefPtr>::const_iterator it = devices.begin(); |
| 731 | it != devices.end(); ++it) { |
| 732 | if ((*it)->enabled()) { |
| 733 | return *it; |
| 734 | } |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 735 | } |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 736 | return NULL; |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 739 | const ProfileRefPtr &Manager::ActiveProfile() const { |
Eric Shienbrood | c74cf9c | 2012-03-02 15:00:35 -0500 | [diff] [blame] | 740 | DCHECK_NE(profiles_.size(), 0U); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 741 | return profiles_.back(); |
| 742 | } |
| 743 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 744 | bool Manager::IsActiveProfile(const ProfileRefPtr &profile) const { |
| 745 | return (profiles_.size() > 0 && |
| 746 | ActiveProfile().get() == profile.get()); |
| 747 | } |
| 748 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 749 | bool Manager::MoveServiceToProfile(const ServiceRefPtr &to_move, |
| 750 | const ProfileRefPtr &destination) { |
| 751 | const ProfileRefPtr from = to_move->profile(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 752 | SLOG(Manager, 2) << "Moving service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 753 | << to_move->unique_name() |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 754 | << " to profile " |
| 755 | << destination->GetFriendlyName() |
| 756 | << " from " |
| 757 | << from->GetFriendlyName(); |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 758 | return destination->AdoptService(to_move) && from->AbandonService(to_move); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 761 | ProfileRefPtr Manager::LookupProfileByRpcIdentifier( |
| 762 | const string &profile_rpcid) { |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 763 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 764 | it != profiles_.end(); |
| 765 | ++it) { |
| 766 | if (profile_rpcid == (*it)->GetRpcIdentifier()) { |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 767 | return *it; |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 768 | } |
| 769 | } |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | void Manager::SetProfileForService(const ServiceRefPtr &to_set, |
| 774 | const string &profile_rpcid, |
| 775 | Error *error) { |
| 776 | ProfileRefPtr profile = LookupProfileByRpcIdentifier(profile_rpcid); |
| 777 | if (!profile) { |
| 778 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 779 | StringPrintf("Unknown Profile %s requested for " |
| 780 | "Service", profile_rpcid.c_str())); |
| 781 | return; |
| 782 | } |
| 783 | |
Paul Stewart | 649f3a4 | 2012-04-24 23:22:16 -0700 | [diff] [blame] | 784 | if (!to_set->profile()) { |
| 785 | // We are being asked to set the profile property of a service that |
| 786 | // has never been registered. Now is a good time to register it. |
| 787 | RegisterService(to_set); |
| 788 | } |
| 789 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 790 | if (to_set->profile().get() == profile.get()) { |
| 791 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 792 | "Service is already connected to this profile"); |
| 793 | } else if (!MoveServiceToProfile(to_set, profile)) { |
| 794 | Error::PopulateAndLog(error, Error::kInternalError, |
| 795 | "Unable to move service to profile"); |
| 796 | } |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 797 | } |
| 798 | |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 799 | void Manager::SetEnabledStateForTechnology(const std::string &technology_name, |
| 800 | bool enabled_state, |
| 801 | Error *error, |
| 802 | const ResultCallback &callback) { |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 803 | CHECK(error != NULL); |
| 804 | DCHECK(error->IsOngoing()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 805 | Technology::Identifier id = Technology::IdentifierFromName(technology_name); |
| 806 | if (id == Technology::kUnknown) { |
| 807 | error->Populate(Error::kInvalidArguments, "Unknown technology"); |
| 808 | return; |
| 809 | } |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 810 | bool deferred = false; |
mukesh agrawal | b711ba2 | 2013-07-17 10:21:00 -0700 | [diff] [blame] | 811 | auto result_aggregator(make_scoped_refptr(new ResultAggregator(callback))); |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 812 | for (auto &device : devices_) { |
| 813 | if (device->technology() != id || device->enabled() == enabled_state) |
| 814 | continue; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 815 | |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 816 | Error device_error(Error::kOperationInitiated); |
| 817 | ResultCallback aggregator_callback( |
| 818 | Bind(&ResultAggregator::ReportResult, result_aggregator)); |
| 819 | device->SetEnabledPersistent( |
| 820 | enabled_state, &device_error, aggregator_callback); |
| 821 | if (device_error.IsOngoing()) { |
| 822 | deferred = true; |
| 823 | } else if (!error->IsFailure()) { // Report first failure. |
| 824 | error->CopyFrom(device_error); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 825 | } |
| 826 | } |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 827 | if (deferred) { |
| 828 | // Some device is handling this change asynchronously. Clobber any error |
| 829 | // from another device, so that we can indicate the operation is still in |
| 830 | // progress. |
| 831 | error->Populate(Error::kOperationInitiated); |
| 832 | } else if (error->IsOngoing()) { |
| 833 | // |error| IsOngoing at entry to this method, but no device |
| 834 | // |deferred|. Reset |error|, to indicate we're done. |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 835 | error->Reset(); |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 836 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | void Manager::UpdateEnabledTechnologies() { |
| 840 | Error error; |
| 841 | adaptor_->EmitStringsChanged(flimflam::kEnabledTechnologiesProperty, |
| 842 | EnabledTechnologies(&error)); |
| 843 | } |
| 844 | |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 845 | void Manager::UpdateUninitializedTechnologies() { |
| 846 | Error error; |
| 847 | adaptor_->EmitStringsChanged(kUninitializedTechnologiesProperty, |
| 848 | UninitializedTechnologies(&error)); |
| 849 | } |
| 850 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 851 | void Manager::RegisterDevice(const DeviceRefPtr &to_manage) { |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 852 | LOG(INFO) << "Device " << to_manage->FriendlyName() << " registered."; |
| 853 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 854 | it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 855 | if (to_manage.get() == it->get()) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 856 | return; |
| 857 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 858 | devices_.push_back(to_manage); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 859 | |
Paul Stewart | 87a4ae8 | 2012-10-26 15:49:32 -0700 | [diff] [blame] | 860 | LoadDeviceFromProfiles(to_manage); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 861 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 862 | // If |to_manage| is new, it needs to be persisted. |
| 863 | UpdateDevice(to_manage); |
| 864 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 865 | // In normal usage, running_ will always be true when we are here, however |
| 866 | // unit tests sometimes do things in otherwise invalid states. |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 867 | if (running_ && (to_manage->enabled_persistent() || |
| 868 | to_manage->IsUnderlyingDeviceEnabled())) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 869 | to_manage->SetEnabled(true); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 870 | |
Eric Shienbrood | 8839a89 | 2012-03-29 10:33:48 -0400 | [diff] [blame] | 871 | EmitDeviceProperties(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 872 | } |
| 873 | |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 874 | void Manager::DeregisterDevice(const DeviceRefPtr &to_forget) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 875 | SLOG(Manager, 2) << __func__ << "(" << to_forget->FriendlyName() << ")"; |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 876 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 877 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 878 | if (to_forget.get() == it->get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 879 | SLOG(Manager, 2) << "Deregistered device: " << to_forget->UniqueName(); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 880 | UpdateDevice(to_forget); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 881 | to_forget->SetEnabled(false); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 882 | devices_.erase(it); |
Eric Shienbrood | 8839a89 | 2012-03-29 10:33:48 -0400 | [diff] [blame] | 883 | EmitDeviceProperties(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 884 | return; |
| 885 | } |
| 886 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 887 | SLOG(Manager, 2) << __func__ << " unknown device: " |
| 888 | << to_forget->UniqueName(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Paul Stewart | 87a4ae8 | 2012-10-26 15:49:32 -0700 | [diff] [blame] | 891 | void Manager::LoadDeviceFromProfiles(const DeviceRefPtr &device) { |
| 892 | // We are applying device properties from the DefaultProfile, and adding the |
| 893 | // union of hidden services in all loaded profiles to the device. |
| 894 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 895 | it != profiles_.end(); ++it) { |
| 896 | // Load device configuration, if any exists, as well as hidden services. |
| 897 | (*it)->ConfigureDevice(device); |
| 898 | } |
| 899 | } |
| 900 | |
Eric Shienbrood | 8839a89 | 2012-03-29 10:33:48 -0400 | [diff] [blame] | 901 | void Manager::EmitDeviceProperties() { |
| 902 | vector<DeviceRefPtr>::iterator it; |
| 903 | vector<string> device_paths; |
| 904 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 905 | device_paths.push_back((*it)->GetRpcIdentifier()); |
| 906 | } |
| 907 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kDevicesProperty, |
| 908 | device_paths); |
| 909 | Error error; |
| 910 | adaptor_->EmitStringsChanged(flimflam::kAvailableTechnologiesProperty, |
| 911 | AvailableTechnologies(&error)); |
| 912 | adaptor_->EmitStringsChanged(flimflam::kEnabledTechnologiesProperty, |
| 913 | EnabledTechnologies(&error)); |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 914 | adaptor_->EmitStringsChanged(kUninitializedTechnologiesProperty, |
| 915 | UninitializedTechnologies(&error)); |
Eric Shienbrood | 8839a89 | 2012-03-29 10:33:48 -0400 | [diff] [blame] | 916 | } |
| 917 | |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 918 | bool Manager::HasService(const ServiceRefPtr &service) { |
| 919 | vector<ServiceRefPtr>::iterator it; |
| 920 | for (it = services_.begin(); it != services_.end(); ++it) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 921 | if ((*it)->unique_name() == service->unique_name()) |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 922 | return true; |
| 923 | } |
| 924 | return false; |
| 925 | } |
| 926 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 927 | void Manager::RegisterService(const ServiceRefPtr &to_manage) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 928 | SLOG(Manager, 2) << "Registering service " << to_manage->unique_name(); |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 929 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 930 | MatchProfileWithService(to_manage); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 931 | |
| 932 | // Now add to OUR list. |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 933 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 934 | for (it = services_.begin(); it != services_.end(); ++it) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 935 | CHECK(to_manage->unique_name() != (*it)->unique_name()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 936 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 937 | services_.push_back(to_manage); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 938 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 941 | void Manager::DeregisterService(const ServiceRefPtr &to_forget) { |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 942 | for (auto it = services_.begin(); it != services_.end(); ++it) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 943 | if (to_forget->unique_name() == (*it)->unique_name()) { |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 944 | DLOG_IF(FATAL, (*it)->connection()) |
| 945 | << "Service " << (*it)->unique_name() |
| 946 | << " still has a connection (in call to " << __func__ << ")"; |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 947 | (*it)->Unload(); |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 948 | (*it)->SetProfile(NULL); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 949 | services_.erase(it); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 950 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 951 | return; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 956 | bool Manager::UnloadService(vector<ServiceRefPtr>::iterator *service_iterator) { |
| 957 | if (!(**service_iterator)->Unload()) { |
| 958 | return false; |
| 959 | } |
| 960 | |
| 961 | DCHECK(!(**service_iterator)->connection()); |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 962 | (**service_iterator)->SetProfile(NULL); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 963 | *service_iterator = services_.erase(*service_iterator); |
| 964 | |
| 965 | return true; |
| 966 | } |
| 967 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 968 | void Manager::UpdateService(const ServiceRefPtr &to_update) { |
| 969 | CHECK(to_update); |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 970 | LOG(INFO) << "Service " << to_update->unique_name() << " updated;" |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 971 | << " state: " << Service::ConnectStateToString(to_update->state()) |
| 972 | << " failure: " |
| 973 | << Service::ConnectFailureToString(to_update->failure()); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 974 | SLOG(Manager, 2) << "IsConnected(): " << to_update->IsConnected(); |
| 975 | SLOG(Manager, 2) << "IsConnecting(): " << to_update->IsConnecting(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 976 | if (to_update->IsConnected()) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 977 | to_update->MakeFavorite(); |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 978 | // Persists the updated favorite setting in the profile. |
| 979 | SaveServiceToProfile(to_update); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 980 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 981 | SortServices(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 984 | void Manager::UpdateDevice(const DeviceRefPtr &to_update) { |
| 985 | LOG(INFO) << "Device " << to_update->link_name() << " updated: " |
| 986 | << (to_update->enabled_persistent() ? "enabled" : "disabled"); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 987 | // Saves the device to the topmost profile that accepts it (ordinary |
| 988 | // profiles don't update but default profiles do). Normally, the topmost |
| 989 | // updating profile would be the DefaultProfile at the bottom of the stack. |
| 990 | // Autotests, differ from the normal scenario, however, in that they push a |
| 991 | // second test-only DefaultProfile. |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 992 | for (vector<ProfileRefPtr>::reverse_iterator rit = profiles_.rbegin(); |
| 993 | rit != profiles_.rend(); ++rit) { |
| 994 | if ((*rit)->UpdateDevice(to_update)) { |
| 995 | return; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 1000 | void Manager::UpdateWiFiProvider() { |
| 1001 | // Saves |wifi_provider_| to the topmost profile that accepts it (ordinary |
| 1002 | // profiles don't update but default profiles do). Normally, the topmost |
| 1003 | // updating profile would be the DefaultProfile at the bottom of the stack. |
| 1004 | // Autotests, differ from the normal scenario, however, in that they push a |
| 1005 | // second test-only DefaultProfile. |
| 1006 | for (vector<ProfileRefPtr>::reverse_iterator rit = profiles_.rbegin(); |
| 1007 | rit != profiles_.rend(); ++rit) { |
| 1008 | if ((*rit)->UpdateWiFiProvider(*wifi_provider_)) { |
| 1009 | return; |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 1014 | void Manager::SaveServiceToProfile(const ServiceRefPtr &to_update) { |
| 1015 | if (IsServiceEphemeral(to_update)) { |
| 1016 | if (profiles_.empty()) { |
| 1017 | LOG(ERROR) << "Cannot assign profile to service: no profiles exist!"; |
| 1018 | } else { |
| 1019 | MoveServiceToProfile(to_update, profiles_.back()); |
| 1020 | } |
| 1021 | } else { |
| 1022 | to_update->profile()->UpdateService(to_update); |
| 1023 | } |
| 1024 | } |
| 1025 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 1026 | bool Manager::LoadProperties(const scoped_refptr<DefaultProfile> &profile) { |
| 1027 | if (!profile->LoadManagerProperties(&props_)) { |
| 1028 | return false; |
| 1029 | } |
| 1030 | SetIgnoredDNSSearchPaths(props_.ignored_dns_search_paths, NULL); |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1034 | void Manager::AddTerminationAction(const string &name, |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 1035 | const base::Closure &start) { |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1036 | if (termination_actions_.IsEmpty() && power_manager_.get()) { |
| 1037 | power_manager_->AddSuspendDelayCallback( |
| 1038 | kPowerManagerKey, |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 1039 | Bind(&Manager::OnSuspendImminent, AsWeakPtr())); |
| 1040 | CHECK(!suspend_delay_registered_); |
| 1041 | suspend_delay_registered_ = power_manager_->RegisterSuspendDelay( |
| 1042 | base::TimeDelta::FromMilliseconds( |
| 1043 | kTerminationActionsTimeoutMilliseconds), |
Daniel Erat | f975367 | 2013-01-24 10:17:02 -0800 | [diff] [blame] | 1044 | kSuspendDelayDescription, |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 1045 | &suspend_delay_id_); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1046 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 1047 | termination_actions_.Add(name, start); |
| 1048 | } |
| 1049 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1050 | void Manager::TerminationActionComplete(const string &name) { |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 1051 | termination_actions_.ActionComplete(name); |
| 1052 | } |
| 1053 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1054 | void Manager::RemoveTerminationAction(const string &name) { |
| 1055 | if (termination_actions_.IsEmpty()) { |
| 1056 | return; |
| 1057 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 1058 | termination_actions_.Remove(name); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1059 | if (termination_actions_.IsEmpty() && power_manager_.get()) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 1060 | if (suspend_delay_registered_) { |
| 1061 | power_manager_->UnregisterSuspendDelay(suspend_delay_id_); |
| 1062 | suspend_delay_registered_ = false; |
| 1063 | suspend_delay_id_ = 0; |
| 1064 | } |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1065 | power_manager_->RemoveSuspendDelayCallback(kPowerManagerKey); |
| 1066 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | void Manager::RunTerminationActions( |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1070 | const base::Callback<void(const Error &)> &done) { |
| 1071 | LOG(INFO) << "Running termination actions."; |
| 1072 | termination_actions_.Run(kTerminationActionsTimeoutMilliseconds, done); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 1075 | bool Manager::RunTerminationActionsAndNotifyMetrics( |
| 1076 | const base::Callback<void(const Error &)> &done, |
| 1077 | Metrics::TerminationActionReason reason) { |
| 1078 | if (termination_actions_.IsEmpty()) |
| 1079 | return false; |
| 1080 | |
| 1081 | metrics_->NotifyTerminationActionsStarted(reason); |
| 1082 | RunTerminationActions(done); |
| 1083 | return true; |
| 1084 | } |
| 1085 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1086 | int Manager::RegisterDefaultServiceCallback(const ServiceCallback &callback) { |
| 1087 | default_service_callbacks_[++default_service_callback_tag_] = callback; |
| 1088 | return default_service_callback_tag_; |
| 1089 | } |
| 1090 | |
| 1091 | void Manager::DeregisterDefaultServiceCallback(int tag) { |
| 1092 | default_service_callbacks_.erase(tag); |
| 1093 | } |
| 1094 | |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 1095 | void Manager::VerifyDestination(const string &certificate, |
| 1096 | const string &public_key, |
| 1097 | const string &nonce, |
| 1098 | const string &signed_data, |
| 1099 | const string &destination_udn, |
Christopher Wiley | cdde79f | 2013-05-01 14:26:56 -0700 | [diff] [blame] | 1100 | const string &hotspot_ssid, |
| 1101 | const string &hotspot_bssid, |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 1102 | const ResultBoolCallback &cb, |
| 1103 | Error *error) { |
Christopher Wiley | cdde79f | 2013-05-01 14:26:56 -0700 | [diff] [blame] | 1104 | if (hotspot_bssid.length() > 32) { |
| 1105 | error->Populate(Error::kOperationFailed, |
| 1106 | "Invalid SSID given for verification."); |
| 1107 | return; |
| 1108 | } |
Christopher Wiley | 8388986 | 2013-05-02 15:55:09 -0700 | [diff] [blame] | 1109 | vector<uint8_t> ssid; |
| 1110 | string bssid; |
Christopher Wiley | cdde79f | 2013-05-01 14:26:56 -0700 | [diff] [blame] | 1111 | if (hotspot_ssid.length() || hotspot_bssid.length()) { |
Christopher Wiley | 8388986 | 2013-05-02 15:55:09 -0700 | [diff] [blame] | 1112 | // If Chrome thinks this destination is already configured, service |
| 1113 | // will be an AP that both we and the destination are connected |
| 1114 | // to, and not the thing we should verify against. |
Christopher Wiley | cdde79f | 2013-05-01 14:26:56 -0700 | [diff] [blame] | 1115 | ssid.assign(hotspot_ssid.begin(), hotspot_ssid.end()); |
| 1116 | bssid = hotspot_bssid; |
Christopher Wiley | 8388986 | 2013-05-02 15:55:09 -0700 | [diff] [blame] | 1117 | } else { |
| 1118 | // For now, we only support a single connected WiFi service. If we change |
| 1119 | // that, we'll need to revisit this. |
| 1120 | bool found_one = false; |
| 1121 | for (const auto &service : services_) { |
| 1122 | if (service->technology() == Technology::kWifi && |
| 1123 | service->IsConnected()) { |
| 1124 | WiFiService *wifi = reinterpret_cast<WiFiService *>(&(*service)); |
| 1125 | bssid = wifi->bssid(); |
| 1126 | ssid = wifi->ssid(); |
| 1127 | found_one = true; |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | if (!found_one) { |
| 1132 | error->Populate(Error::kOperationFailed, |
| 1133 | "Unable to find connected WiFi service."); |
| 1134 | return; |
| 1135 | |
| 1136 | } |
Christopher Wiley | cdde79f | 2013-05-01 14:26:56 -0700 | [diff] [blame] | 1137 | } |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 1138 | crypto_util_proxy_->VerifyDestination(certificate, public_key, nonce, |
| 1139 | signed_data, destination_udn, |
Christopher Wiley | 8388986 | 2013-05-02 15:55:09 -0700 | [diff] [blame] | 1140 | ssid, bssid, cb, error); |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | void Manager::VerifyToEncryptLink(string public_key, |
| 1144 | string data, |
| 1145 | ResultStringCallback cb, |
| 1146 | const Error &error, |
| 1147 | bool success) { |
| 1148 | if (!success || !error.IsSuccess()) { |
| 1149 | CHECK(error.IsFailure()) << "Return code from CryptoUtilProxy " |
| 1150 | << "inconsistent with error code."; |
| 1151 | cb.Run(error, ""); |
| 1152 | return; |
| 1153 | } |
| 1154 | Error encrypt_error; |
| 1155 | if (!crypto_util_proxy_->EncryptData(public_key, data, cb, &encrypt_error)) { |
Christopher Wiley | b3e70d2 | 2013-04-26 17:28:37 -0700 | [diff] [blame] | 1156 | CHECK(encrypt_error.IsFailure()) << "CryptoUtilProxy::EncryptData returned " |
| 1157 | << "inconsistently."; |
| 1158 | cb.Run(encrypt_error, ""); |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 1159 | } |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | void Manager::VerifyAndEncryptData(const string &certificate, |
| 1163 | const string &public_key, |
| 1164 | const string &nonce, |
| 1165 | const string &signed_data, |
| 1166 | const string &destination_udn, |
Christopher Wiley | cdde79f | 2013-05-01 14:26:56 -0700 | [diff] [blame] | 1167 | const string &hotspot_ssid, |
| 1168 | const string &hotspot_bssid, |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 1169 | const string &data, |
| 1170 | const ResultStringCallback &cb, |
| 1171 | Error *error) { |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 1172 | ResultBoolCallback on_verification_success = Bind( |
| 1173 | &Manager::VerifyToEncryptLink, AsWeakPtr(), public_key, data, cb); |
| 1174 | VerifyDestination(certificate, public_key, nonce, signed_data, |
Christopher Wiley | cdde79f | 2013-05-01 14:26:56 -0700 | [diff] [blame] | 1175 | destination_udn, hotspot_ssid, hotspot_bssid, |
| 1176 | on_verification_success, error); |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | void Manager::VerifyAndEncryptCredentials(const string &certificate, |
| 1180 | const string &public_key, |
| 1181 | const string &nonce, |
| 1182 | const string &signed_data, |
| 1183 | const string &destination_udn, |
Christopher Wiley | cdde79f | 2013-05-01 14:26:56 -0700 | [diff] [blame] | 1184 | const string &hotspot_ssid, |
| 1185 | const string &hotspot_bssid, |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 1186 | const string &network_path, |
| 1187 | const ResultStringCallback &cb, |
| 1188 | Error *error) { |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 1189 | // This is intentionally left unimplemented until we have a security review. |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 1190 | error->Populate(Error::kNotImplemented, "Not implemented"); |
| 1191 | } |
| 1192 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1193 | void Manager::NotifyDefaultServiceChanged(const ServiceRefPtr &service) { |
| 1194 | for (map<int, ServiceCallback>::const_iterator it = |
| 1195 | default_service_callbacks_.begin(); |
| 1196 | it != default_service_callbacks_.end(); ++it) { |
| 1197 | it->second.Run(service); |
| 1198 | } |
| 1199 | metrics_->NotifyDefaultServiceChanged(service); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 1200 | EmitDefaultService(); |
| 1201 | } |
| 1202 | |
| 1203 | void Manager::EmitDefaultService() { |
| 1204 | RpcIdentifier rpc_identifier = GetDefaultServiceRpcIdentifier(NULL); |
| 1205 | if (rpc_identifier != default_service_rpc_identifier_) { |
| 1206 | adaptor_->EmitRpcIdentifierChanged(shill::kDefaultServiceProperty, |
| 1207 | rpc_identifier); |
| 1208 | default_service_rpc_identifier_ = rpc_identifier; |
| 1209 | } |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 1212 | void Manager::OnPowerStateChanged( |
| 1213 | PowerManagerProxyDelegate::SuspendState power_state) { |
| 1214 | if (power_state == PowerManagerProxyDelegate::kOn) { |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 1215 | vector<ServiceRefPtr>::iterator sit; |
| 1216 | for (sit = services_.begin(); sit != services_.end(); ++sit) { |
| 1217 | (*sit)->OnAfterResume(); |
| 1218 | } |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 1219 | SortServices(); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 1220 | vector<DeviceRefPtr>::iterator it; |
| 1221 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 1222 | (*it)->OnAfterResume(); |
| 1223 | } |
| 1224 | } else if (power_state == PowerManagerProxyDelegate::kMem) { |
| 1225 | vector<DeviceRefPtr>::iterator it; |
| 1226 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 1227 | (*it)->OnBeforeSuspend(); |
| 1228 | } |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 1232 | void Manager::OnSuspendImminent(int suspend_id) { |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 1233 | if (!RunTerminationActionsAndNotifyMetrics( |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 1234 | Bind(&Manager::OnSuspendActionsComplete, AsWeakPtr(), suspend_id), |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 1235 | Metrics::kTerminationActionReasonSuspend)) { |
| 1236 | LOG(INFO) << "No suspend actions were run."; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 1237 | power_manager_->ReportSuspendReadiness(suspend_delay_id_, suspend_id); |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 1238 | } |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 1241 | void Manager::OnSuspendActionsComplete(int suspend_id, const Error &error) { |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1242 | LOG(INFO) << "Finished suspend actions. Result: " << error; |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 1243 | metrics_->NotifyTerminationActionsCompleted( |
| 1244 | Metrics::kTerminationActionReasonSuspend, error.IsSuccess()); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 1245 | power_manager_->ReportSuspendReadiness(suspend_delay_id_, suspend_id); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 1246 | } |
| 1247 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 1248 | void Manager::FilterByTechnology(Technology::Identifier tech, |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1249 | vector<DeviceRefPtr> *found) const { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1250 | CHECK(found); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1251 | vector<DeviceRefPtr>::const_iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1252 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 1253 | if ((*it)->technology() == tech) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1254 | found->push_back(*it); |
| 1255 | } |
| 1256 | } |
| 1257 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1258 | ServiceRefPtr Manager::FindService(const string &name) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 1259 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1260 | for (it = services_.begin(); it != services_.end(); ++it) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1261 | if (name == (*it)->unique_name()) |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 1262 | return *it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1263 | } |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 1264 | return NULL; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 1267 | void Manager::HelpRegisterConstDerivedRpcIdentifier( |
| 1268 | const string &name, |
| 1269 | RpcIdentifier(Manager::*get)(Error *error)) { |
| 1270 | store_.RegisterDerivedRpcIdentifier( |
| 1271 | name, |
| 1272 | RpcIdentifierAccessor( |
| 1273 | new CustomAccessor<Manager, RpcIdentifier>(this, get, NULL))); |
| 1274 | } |
| 1275 | |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 1276 | void Manager::HelpRegisterConstDerivedRpcIdentifiers( |
| 1277 | const string &name, |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 1278 | RpcIdentifiers(Manager::*get)(Error *error)) { |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 1279 | store_.RegisterDerivedRpcIdentifiers( |
| 1280 | name, |
| 1281 | RpcIdentifiersAccessor( |
| 1282 | new CustomAccessor<Manager, RpcIdentifiers>(this, get, NULL))); |
| 1283 | } |
| 1284 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 1285 | void Manager::HelpRegisterDerivedString( |
| 1286 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1287 | string(Manager::*get)(Error *), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1288 | bool(Manager::*set)(const string&, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 1289 | store_.RegisterDerivedString( |
| 1290 | name, |
| 1291 | StringAccessor(new CustomAccessor<Manager, string>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1294 | void Manager::HelpRegisterConstDerivedStrings( |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 1295 | const string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1296 | Strings(Manager::*get)(Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 1297 | store_.RegisterDerivedStrings( |
| 1298 | name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1299 | StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, NULL))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1302 | void Manager::SortServices() { |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1303 | // We might be called in the middle of a series of events that |
| 1304 | // may result in multiple calls to Manager::SortServices, or within |
| 1305 | // an outer loop that may also be traversing the services_ list. |
| 1306 | // Defer this work to the event loop. |
| 1307 | if (sort_services_task_.IsCancelled()) { |
| 1308 | sort_services_task_.Reset(Bind(&Manager::SortServicesTask, AsWeakPtr())); |
| 1309 | dispatcher_->PostTask(sort_services_task_.callback()); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | void Manager::SortServicesTask() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1314 | SLOG(Manager, 4) << "In " << __func__; |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1315 | sort_services_task_.Cancel(); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1316 | ServiceRefPtr default_service; |
| 1317 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1318 | if (!services_.empty()) { |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1319 | // Keep track of the service that is the candidate for the default |
| 1320 | // service. We have not yet tested to see if this service has a |
| 1321 | // connection. |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1322 | default_service = services_[0]; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1323 | } |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 1324 | const bool kCompareConnectivityState = true; |
| 1325 | sort(services_.begin(), services_.end(), |
| 1326 | ServiceSorter(kCompareConnectivityState, technology_order_)); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1327 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1328 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 1329 | EnumerateAvailableServices(NULL)); |
| 1330 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServiceWatchListProperty, |
| 1331 | EnumerateWatchedServices(NULL)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1332 | |
| 1333 | Error error; |
| 1334 | adaptor_->EmitStringsChanged(flimflam::kConnectedTechnologiesProperty, |
| 1335 | ConnectedTechnologies(&error)); |
| 1336 | adaptor_->EmitStringChanged(flimflam::kDefaultTechnologyProperty, |
| 1337 | DefaultTechnology(&error)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1338 | |
| 1339 | if (!services_.empty()) { |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1340 | ConnectionRefPtr default_connection = default_service->connection(); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1341 | if (default_connection && |
| 1342 | services_[0]->connection() != default_connection) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1343 | default_connection->SetIsDefault(false); |
| 1344 | } |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1345 | if (services_[0]->connection()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1346 | services_[0]->connection()->SetIsDefault(true); |
Paul Stewart | 8596f9f | 2013-03-14 07:58:26 -0700 | [diff] [blame] | 1347 | if (default_service != services_[0]) { |
| 1348 | default_service = services_[0]; |
| 1349 | LOG(INFO) << "Default service is now " |
| 1350 | << default_service->unique_name(); |
| 1351 | } |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1352 | } else { |
| 1353 | default_service = NULL; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1354 | } |
| 1355 | } |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1356 | NotifyDefaultServiceChanged(default_service); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1357 | AutoConnect(); |
| 1358 | } |
| 1359 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 1360 | bool Manager::MatchProfileWithService(const ServiceRefPtr &service) { |
| 1361 | vector<ProfileRefPtr>::reverse_iterator it; |
| 1362 | for (it = profiles_.rbegin(); it != profiles_.rend(); ++it) { |
| 1363 | if ((*it)->ConfigureService(service)) { |
| 1364 | break; |
| 1365 | } |
| 1366 | } |
| 1367 | if (it == profiles_.rend()) { |
| 1368 | ephemeral_profile_->AdoptService(service); |
| 1369 | return false; |
| 1370 | } |
| 1371 | return true; |
| 1372 | } |
| 1373 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1374 | void Manager::AutoConnect() { |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 1375 | if (!running_) { |
| 1376 | LOG(INFO) << "Auto-connect suppressed -- not running."; |
| 1377 | return; |
| 1378 | } |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 1379 | if (power_manager_.get() && |
| 1380 | power_manager_->power_state() != PowerManagerProxyDelegate::kOn && |
| 1381 | power_manager_->power_state() != PowerManagerProxyDelegate::kUnknown) { |
| 1382 | LOG(INFO) << "Auto-connect suppressed -- power state is not 'on'."; |
| 1383 | return; |
| 1384 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1385 | if (services_.empty()) { |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 1386 | LOG(INFO) << "Auto-connect suppressed -- no services."; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1387 | return; |
| 1388 | } |
| 1389 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1390 | if (SLOG_IS_ON(Manager, 4)) { |
| 1391 | SLOG(Manager, 4) << "Sorted service list: "; |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 1392 | for (size_t i = 0; i < services_.size(); ++i) { |
| 1393 | ServiceRefPtr service = services_[i]; |
| 1394 | const char *compare_reason = NULL; |
| 1395 | if (i + 1 < services_.size()) { |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 1396 | const bool kCompareConnectivityState = true; |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 1397 | Service::Compare( |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 1398 | service, services_[i+1], kCompareConnectivityState, |
| 1399 | technology_order_, &compare_reason); |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 1400 | } else { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1401 | compare_reason = "last"; |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 1402 | } |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1403 | SLOG(Manager, 4) << "Service " << service->unique_name() |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1404 | << " IsConnected: " << service->IsConnected() |
| 1405 | << " IsConnecting: " << service->IsConnecting() |
| 1406 | << " IsFailed: " << service->IsFailed() |
| 1407 | << " connectable: " << service->connectable() |
| 1408 | << " auto_connect: " << service->auto_connect() |
| 1409 | << " favorite: " << service->favorite() |
| 1410 | << " priority: " << service->priority() |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 1411 | << " crypto_algorithm: " << service->crypto_algorithm() |
| 1412 | << " key_rotation: " << service->key_rotation() |
| 1413 | << " endpoint_auth: " << service->endpoint_auth() |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1414 | << " strength: " << service->strength() |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1415 | << " sorted: " << compare_reason; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1416 | } |
| 1417 | } |
| 1418 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1419 | // Perform auto-connect. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1420 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 1421 | it != services_.end(); ++it) { |
| 1422 | if ((*it)->auto_connect()) { |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1423 | (*it)->AutoConnect(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1424 | } |
| 1425 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 1428 | void Manager::ConnectToBestServices(Error */*error*/) { |
| 1429 | dispatcher_->PostTask(Bind(&Manager::ConnectToBestServicesTask, AsWeakPtr())); |
| 1430 | } |
| 1431 | |
| 1432 | void Manager::ConnectToBestServicesTask() { |
| 1433 | vector<ServiceRefPtr> services_copy = services_; |
| 1434 | const bool kCompareConnectivityState = false; |
| 1435 | sort(services_copy.begin(), services_copy.end(), |
| 1436 | ServiceSorter(kCompareConnectivityState, technology_order_)); |
| 1437 | set<Technology::Identifier> connecting_technologies; |
| 1438 | for (vector<ServiceRefPtr>::const_iterator it = services_copy.begin(); |
| 1439 | it != services_copy.end(); |
| 1440 | ++it) { |
| 1441 | if (!(*it)->connectable()) { |
| 1442 | // Due to service sort order, it is guaranteed that no services beyond |
| 1443 | // this one will be connectable either. |
| 1444 | break; |
| 1445 | } |
Paul Stewart | f7d7d3f | 2013-04-24 14:47:44 -0700 | [diff] [blame] | 1446 | if (!(*it)->auto_connect() || !(*it)->IsVisible()) { |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 1447 | continue; |
| 1448 | } |
| 1449 | Technology::Identifier technology = (*it)->technology(); |
| 1450 | if (!Technology::IsPrimaryConnectivityTechnology(technology) && |
| 1451 | !IsOnline()) { |
| 1452 | // Non-primary services need some other service connected first. |
| 1453 | continue; |
| 1454 | } |
| 1455 | if (ContainsKey(connecting_technologies, technology)) { |
| 1456 | // We have already started a connection for this technology. |
| 1457 | continue; |
| 1458 | } |
| 1459 | connecting_technologies.insert(technology); |
| 1460 | if (!(*it)->IsConnected() && !(*it)->IsConnecting()) { |
| 1461 | // At first blush, it may seem that using Service::AutoConnect might |
| 1462 | // be the right choice, however Service::IsAutoConnectable and its |
| 1463 | // overridden implementations consider a host of conditions which |
| 1464 | // prevent it from attempting a connection which we'd like to ignore |
| 1465 | // for the purposes of this user-initiated action. |
| 1466 | Error error; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 1467 | (*it)->Connect(&error, __func__); |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 1468 | if (error.IsFailure()) { |
| 1469 | LOG(ERROR) << "Connection failed: " << error.message(); |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 1475 | bool Manager::IsOnline() const { |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 1476 | // |services_| is sorted such that connected services are first. |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 1477 | return !services_.empty() && services_.front()->IsConnected(); |
| 1478 | } |
| 1479 | |
| 1480 | string Manager::CalculateState(Error */*error*/) { |
| 1481 | return IsOnline() ? flimflam::kStateOnline : flimflam::kStateOffline; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1484 | vector<string> Manager::AvailableTechnologies(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1485 | set<string> unique_technologies; |
| 1486 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1487 | it != devices_.end(); ++it) { |
| 1488 | unique_technologies.insert( |
| 1489 | Technology::NameFromIdentifier((*it)->technology())); |
| 1490 | } |
| 1491 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1494 | vector<string> Manager::ConnectedTechnologies(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1495 | set<string> unique_technologies; |
| 1496 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1497 | it != devices_.end(); ++it) { |
| 1498 | if ((*it)->IsConnected()) |
| 1499 | unique_technologies.insert( |
| 1500 | Technology::NameFromIdentifier((*it)->technology())); |
| 1501 | } |
| 1502 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1505 | bool Manager::IsTechnologyConnected(Technology::Identifier technology) const { |
| 1506 | for (vector<DeviceRefPtr>::const_iterator it = devices_.begin(); |
| 1507 | it != devices_.end(); ++it) { |
| 1508 | if ((*it)->technology() == technology && (*it)->IsConnected()) |
| 1509 | return true; |
| 1510 | } |
| 1511 | return false; |
| 1512 | } |
| 1513 | |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 1514 | string Manager::DefaultTechnology(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1515 | return (!services_.empty() && services_[0]->IsConnected()) ? |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 1516 | services_[0]->GetTechnologyString() : ""; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1519 | vector<string> Manager::EnabledTechnologies(Error */*error*/) { |
| 1520 | set<string> unique_technologies; |
| 1521 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1522 | it != devices_.end(); ++it) { |
| 1523 | if ((*it)->enabled()) |
| 1524 | unique_technologies.insert( |
| 1525 | Technology::NameFromIdentifier((*it)->technology())); |
| 1526 | } |
| 1527 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 1530 | vector<string> Manager::UninitializedTechnologies(Error */*error*/) { |
| 1531 | return device_info_.GetUninitializedTechnologies(); |
| 1532 | } |
| 1533 | |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 1534 | RpcIdentifiers Manager::EnumerateDevices(Error */*error*/) { |
| 1535 | RpcIdentifiers device_rpc_ids; |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 1536 | for (vector<DeviceRefPtr>::const_iterator it = devices_.begin(); |
| 1537 | it != devices_.end(); |
| 1538 | ++it) { |
| 1539 | device_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 1540 | } |
| 1541 | return device_rpc_ids; |
| 1542 | } |
| 1543 | |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 1544 | RpcIdentifiers Manager::EnumerateProfiles(Error */*error*/) { |
| 1545 | RpcIdentifiers profile_rpc_ids; |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1546 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 1547 | it != profiles_.end(); |
| 1548 | ++it) { |
| 1549 | profile_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 1550 | } |
| 1551 | return profile_rpc_ids; |
| 1552 | } |
| 1553 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1554 | vector<string> Manager::EnumerateAvailableServices(Error */*error*/) { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 1555 | vector<string> service_rpc_ids; |
| 1556 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 1557 | it != services_.end(); |
| 1558 | ++it) { |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 1559 | if ((*it)->IsVisible()) { |
| 1560 | service_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 1561 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 1562 | } |
| 1563 | return service_rpc_ids; |
| 1564 | } |
| 1565 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1566 | RpcIdentifiers Manager::EnumerateCompleteServices(Error */*error*/) { |
| 1567 | vector<string> service_rpc_ids; |
| 1568 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 1569 | it != services_.end(); |
| 1570 | ++it) { |
| 1571 | service_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 1572 | } |
| 1573 | return service_rpc_ids; |
| 1574 | } |
| 1575 | |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 1576 | RpcIdentifiers Manager::EnumerateWatchedServices(Error */*error*/) { |
| 1577 | vector<string> service_rpc_ids; |
| 1578 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 1579 | it != services_.end(); |
| 1580 | ++it) { |
| 1581 | if ((*it)->IsVisible() && (*it)->IsActive(NULL)) { |
| 1582 | service_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 1583 | } |
| 1584 | } |
| 1585 | return service_rpc_ids; |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1588 | string Manager::GetActiveProfileRpcIdentifier(Error */*error*/) { |
| 1589 | return ActiveProfile()->GetRpcIdentifier(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 1592 | string Manager::GetCheckPortalList(Error */*error*/) { |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 1593 | return use_startup_portal_list_ ? startup_portal_list_ : |
| 1594 | props_.check_portal_list; |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1597 | bool Manager::SetCheckPortalList(const string &portal_list, Error *error) { |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 1598 | use_startup_portal_list_ = false; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1599 | if (props_.check_portal_list == portal_list) { |
| 1600 | return false; |
| 1601 | } |
| 1602 | props_.check_portal_list = portal_list; |
| 1603 | return true; |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 1606 | string Manager::GetIgnoredDNSSearchPaths(Error */*error*/) { |
| 1607 | return props_.ignored_dns_search_paths; |
| 1608 | } |
| 1609 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1610 | bool Manager::SetIgnoredDNSSearchPaths(const string &ignored_paths, |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 1611 | Error */*error*/) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1612 | if (props_.ignored_dns_search_paths == ignored_paths) { |
| 1613 | return false; |
| 1614 | } |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 1615 | vector<string> ignored_path_list; |
| 1616 | if (!ignored_paths.empty()) { |
| 1617 | base::SplitString(ignored_paths, ',', &ignored_path_list); |
| 1618 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1619 | props_.ignored_dns_search_paths = ignored_paths; |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 1620 | resolver_->set_ignored_search_list(ignored_path_list); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1621 | return true; |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1624 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1625 | ServiceRefPtr Manager::GetService(const KeyValueStore &args, Error *error) { |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1626 | if (args.ContainsString(flimflam::kTypeProperty) && |
| 1627 | args.GetString(flimflam::kTypeProperty) == flimflam::kTypeVPN) { |
| 1628 | // GetService on a VPN service should actually perform ConfigureService. |
| 1629 | // TODO(pstew): Remove this hack and change Chrome to use ConfigureService |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 1630 | // instead, when we no longer need to support flimflam. crbug.com/213802 |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1631 | return ConfigureService(args, error); |
| 1632 | } |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1633 | |
| 1634 | ServiceRefPtr service = GetServiceInner(args, error); |
| 1635 | if (service) { |
| 1636 | // Configures the service using the rest of the passed-in arguments. |
| 1637 | service->Configure(args, error); |
| 1638 | } |
| 1639 | |
| 1640 | return service; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | ServiceRefPtr Manager::GetServiceInner(const KeyValueStore &args, |
| 1644 | Error *error) { |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1645 | if (args.ContainsString(flimflam::kGuidProperty)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1646 | SLOG(Manager, 2) << __func__ << ": searching by GUID"; |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1647 | ServiceRefPtr service = |
| 1648 | GetServiceWithGUID(args.GetString(flimflam::kGuidProperty), NULL); |
| 1649 | if (service) { |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1650 | return service; |
| 1651 | } |
| 1652 | } |
| 1653 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1654 | if (!args.ContainsString(flimflam::kTypeProperty)) { |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1655 | Error::PopulateAndLog(error, Error::kInvalidArguments, kErrorTypeRequired); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1656 | return NULL; |
| 1657 | } |
| 1658 | |
| 1659 | string type = args.GetString(flimflam::kTypeProperty); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 1660 | if (type == kTypeEthernetEap) { |
| 1661 | SLOG(Manager, 2) << __func__ << ": getting Ethernet EAP Service"; |
| 1662 | return ethernet_eap_provider_->service(); |
| 1663 | } |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1664 | if (type == flimflam::kTypeVPN) { |
| 1665 | SLOG(Manager, 2) << __func__ << ": getting VPN Service"; |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 1666 | return vpn_provider_->GetService(args, error); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1667 | } |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1668 | if (type == flimflam::kTypeWifi) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1669 | SLOG(Manager, 2) << __func__ << ": getting WiFi Service"; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1670 | return wifi_provider_->GetService(args, error); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1671 | } |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1672 | if (type == flimflam::kTypeWimax) { |
| 1673 | SLOG(Manager, 2) << __func__ << ": getting WiMAX Service"; |
| 1674 | return wimax_provider_.GetService(args, error); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1675 | } |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1676 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 1677 | kErrorUnsupportedServiceType); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1678 | return NULL; |
| 1679 | } |
| 1680 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1681 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1682 | ServiceRefPtr Manager::ConfigureService(const KeyValueStore &args, |
| 1683 | Error *error) { |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1684 | ProfileRefPtr profile = ActiveProfile(); |
| 1685 | bool profile_specified = args.ContainsString(flimflam::kProfileProperty); |
| 1686 | if (profile_specified) { |
| 1687 | string profile_rpcid = args.GetString(flimflam::kProfileProperty); |
| 1688 | profile = LookupProfileByRpcIdentifier(profile_rpcid); |
| 1689 | if (!profile) { |
| 1690 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 1691 | "Invalid profile name " + profile_rpcid); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1692 | return NULL; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1696 | ServiceRefPtr service = GetServiceInner(args, error); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1697 | if (error->IsFailure() || !service) { |
| 1698 | LOG(ERROR) << "GetService failed; returning upstream error."; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1699 | return NULL; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1702 | // First pull in any stored configuration associated with the service. |
| 1703 | if (service->profile() == profile) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1704 | SLOG(Manager, 2) << __func__ << ": service " << service->unique_name() |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1705 | << " is already a member of profile " |
| 1706 | << profile->GetFriendlyName() |
| 1707 | << " so a load is not necessary."; |
| 1708 | } else if (profile->LoadService(service)) { |
| 1709 | SLOG(Manager, 2) << __func__ << ": applied stored information from profile " |
| 1710 | << profile->GetFriendlyName() |
| 1711 | << " into service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1712 | << service->unique_name(); |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1713 | } else { |
| 1714 | SLOG(Manager, 2) << __func__ << ": no previous information in profile " |
| 1715 | << profile->GetFriendlyName() |
| 1716 | << " exists for service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1717 | << service->unique_name(); |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | // Overlay this with the passed-in configuration parameters. |
| 1721 | service->Configure(args, error); |
| 1722 | |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1723 | // Overwrite the profile data with the resulting configured service. |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1724 | if (!profile->UpdateService(service)) { |
| 1725 | Error::PopulateAndLog(error, Error::kInternalError, |
| 1726 | "Unable to save service to profile"); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1727 | return NULL; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | if (HasService(service)) { |
| 1731 | // If the service has been registered (it may not be -- as is the case |
| 1732 | // with invisible WiFi networks), we can now transfer the service between |
| 1733 | // profiles. |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 1734 | if (IsServiceEphemeral(service) || |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1735 | (profile_specified && service->profile() != profile)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1736 | SLOG(Manager, 2) << "Moving service to profile " |
| 1737 | << profile->GetFriendlyName(); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1738 | if (!MoveServiceToProfile(service, profile)) { |
| 1739 | Error::PopulateAndLog(error, Error::kInternalError, |
| 1740 | "Unable to move service to profile"); |
| 1741 | } |
| 1742 | } |
| 1743 | } |
Paul Stewart | 4357f4e | 2012-04-26 17:39:26 -0700 | [diff] [blame] | 1744 | |
| 1745 | // Notify the service that a profile has been configured for it. |
| 1746 | service->OnProfileConfigured(); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1747 | |
| 1748 | return service; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 1751 | // called via RPC (e.g., from ManagerDBusAdaptor) |
| 1752 | ServiceRefPtr Manager::ConfigureServiceForProfile( |
| 1753 | const string &profile_rpcid, const KeyValueStore &args, Error *error) { |
| 1754 | if (args.LookupString(flimflam::kTypeProperty, "") != flimflam::kTypeWifi) { |
| 1755 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 1756 | "This method only supports WiFi services"); |
| 1757 | return NULL; |
| 1758 | } |
| 1759 | |
| 1760 | ProfileRefPtr profile = LookupProfileByRpcIdentifier(profile_rpcid); |
| 1761 | if (!profile) { |
| 1762 | Error::PopulateAndLog(error, Error::kNotFound, |
| 1763 | "Profile specified was not found"); |
| 1764 | return NULL; |
| 1765 | } |
| 1766 | if (args.LookupString(flimflam::kProfileProperty, |
| 1767 | profile_rpcid) != profile_rpcid) { |
| 1768 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 1769 | "Profile argument does not match that in " |
| 1770 | "the configuration arguments"); |
| 1771 | return NULL; |
| 1772 | } |
| 1773 | |
| 1774 | ServiceRefPtr service; |
| 1775 | if (args.ContainsString(flimflam::kGuidProperty)) { |
| 1776 | SLOG(Manager, 2) << __func__ << ": searching by GUID"; |
| 1777 | service = GetServiceWithGUID(args.GetString(flimflam::kGuidProperty), NULL); |
| 1778 | if (service && service->technology() != Technology::kWifi) { |
| 1779 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 1780 | "This GUID matches a non-WiFi service"); |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | if (!service) { |
| 1786 | Error find_error; |
| 1787 | service = wifi_provider_->FindSimilarService(args, &find_error); |
| 1788 | } |
| 1789 | |
| 1790 | // If no matching service exists, create a new service in the specified |
| 1791 | // profile using ConfigureService(). |
| 1792 | if (!service) { |
| 1793 | KeyValueStore configure_args; |
| 1794 | configure_args.CopyFrom(args); |
| 1795 | configure_args.SetString(flimflam::kProfileProperty, profile_rpcid); |
| 1796 | return ConfigureService(configure_args, error); |
| 1797 | } |
| 1798 | |
| 1799 | // The service already exists and is set to the desired profile, |
| 1800 | // the service is in the ephemeral profile, or the current profile |
| 1801 | // for the service appears before the desired profile, we need to |
| 1802 | // reassign the service to the new profile if necessary, leaving |
| 1803 | // the old profile intact (i.e, not calling Profile::AbandonService()). |
| 1804 | // Then, configure the properties on the service as well as its newly |
| 1805 | // associated profile. |
| 1806 | if (service->profile() == profile || |
| 1807 | IsServiceEphemeral(service) || |
| 1808 | IsProfileBefore(service->profile(), profile)) { |
| 1809 | SetupServiceInProfile(service, profile, args, error); |
| 1810 | return service; |
| 1811 | } |
| 1812 | |
| 1813 | // The current profile for the service appears after the desired |
| 1814 | // profile. We must create a temporary service specifically for |
| 1815 | // the task of creating configuration data. This service will |
| 1816 | // neither inherit properties from the visible service, nor will |
| 1817 | // it exist after this function returns. |
| 1818 | service = wifi_provider_->CreateTemporaryService(args, error); |
| 1819 | if (!service || !error->IsSuccess()) { |
| 1820 | // Service::CreateTemporaryService() failed, and has set the error |
| 1821 | // appropriately. |
| 1822 | return NULL; |
| 1823 | } |
| 1824 | |
| 1825 | // The profile may already have configuration for this service. |
| 1826 | profile->ConfigureService(service); |
| 1827 | |
| 1828 | SetupServiceInProfile(service, profile, args, error); |
| 1829 | |
| 1830 | // Although we have succeeded, this service will not exist, so its |
| 1831 | // path is of no use to the caller. |
| 1832 | DCHECK(service->HasOneRef()); |
| 1833 | return NULL; |
| 1834 | } |
| 1835 | |
| 1836 | void Manager::SetupServiceInProfile(ServiceRefPtr service, |
| 1837 | ProfileRefPtr profile, |
| 1838 | const KeyValueStore &args, |
| 1839 | Error *error) { |
| 1840 | service->SetProfile(profile); |
| 1841 | service->Configure(args, error); |
| 1842 | profile->UpdateService(service); |
| 1843 | } |
| 1844 | |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 1845 | ServiceRefPtr Manager::FindMatchingService(const KeyValueStore &args, |
| 1846 | Error *error) { |
| 1847 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 1848 | it != services_.end(); ++it) { |
| 1849 | if ((*it)->DoPropertiesMatch(args)) { |
| 1850 | return *it; |
| 1851 | } |
| 1852 | } |
| 1853 | error->Populate(Error::kNotFound, "Matching service was not found"); |
| 1854 | return NULL; |
| 1855 | } |
| 1856 | |
Gaurav Shah | b790aa2 | 2012-10-23 12:51:12 -0700 | [diff] [blame] | 1857 | map<string, GeolocationInfos> Manager::GetNetworksForGeolocation() { |
| 1858 | map<string, GeolocationInfos> networks; |
| 1859 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1860 | it != devices_.end(); ++it) { |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 1861 | switch ((*it)->technology()) { |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 1862 | // TODO(gauravsh): crbug.com/217833 Need a strategy for combining |
Gaurav Shah | b790aa2 | 2012-10-23 12:51:12 -0700 | [diff] [blame] | 1863 | // geolocation objects from multiple devices of the same technolgy. |
| 1864 | // Currently, we just pick the geolocation objects from the first found |
| 1865 | // device of each supported technology type. |
| 1866 | case Technology::kWifi: |
| 1867 | if (!ContainsKey(networks, kGeoWifiAccessPointsProperty)) |
| 1868 | networks[kGeoWifiAccessPointsProperty] = |
| 1869 | (*it)->GetGeolocationObjects(); |
| 1870 | break; |
| 1871 | case Technology::kCellular: |
| 1872 | if (!ContainsKey(networks, kGeoCellTowersProperty)) |
| 1873 | networks[kGeoCellTowersProperty] = (*it)->GetGeolocationObjects(); |
| 1874 | break; |
| 1875 | default: |
| 1876 | // Ignore other technologies. |
| 1877 | break; |
| 1878 | }; |
| 1879 | } |
| 1880 | return networks; |
| 1881 | }; |
| 1882 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1883 | void Manager::RecheckPortal(Error */*error*/) { |
| 1884 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1885 | it != devices_.end(); ++it) { |
| 1886 | if ((*it)->RequestPortalDetection()) { |
| 1887 | // Only start Portal Detection on the device with the default connection. |
| 1888 | // We will get a "true" return value when we've found that device, and |
| 1889 | // can end our loop early as a result. |
| 1890 | break; |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1895 | void Manager::RecheckPortalOnService(const ServiceRefPtr &service) { |
| 1896 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1897 | it != devices_.end(); ++it) { |
| 1898 | if ((*it)->IsConnectedToService(service)) { |
| 1899 | // As opposed to RecheckPortal() above, we explicitly stop and then |
| 1900 | // restart portal detection, since the service to recheck was explicitly |
| 1901 | // specified. |
| 1902 | (*it)->RestartPortalDetection(); |
| 1903 | break; |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 1908 | void Manager::RequestScan(Device::ScanType scan_type, |
| 1909 | const string &technology, Error *error) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1910 | if (technology == flimflam::kTypeWifi || technology == "") { |
| 1911 | vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 1912 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1913 | for (vector<DeviceRefPtr>::iterator it = wifi_devices.begin(); |
| 1914 | it != wifi_devices.end(); |
| 1915 | ++it) { |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 1916 | (*it)->Scan(scan_type, error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1917 | } |
| 1918 | } else { |
| 1919 | // TODO(quiche): support scanning for other technologies? |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 1920 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 1921 | "Unrecognized technology " + technology); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1922 | } |
| 1923 | } |
| 1924 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1925 | string Manager::GetTechnologyOrder() { |
| 1926 | vector<string> technology_names; |
| 1927 | for (vector<Technology::Identifier>::iterator it = technology_order_.begin(); |
| 1928 | it != technology_order_.end(); |
| 1929 | ++it) { |
| 1930 | technology_names.push_back(Technology::NameFromIdentifier(*it)); |
| 1931 | } |
| 1932 | |
| 1933 | return JoinString(technology_names, ','); |
| 1934 | } |
| 1935 | |
| 1936 | void Manager::SetTechnologyOrder(const string &order, Error *error) { |
| 1937 | vector<Technology::Identifier> new_order; |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1938 | SLOG(Manager, 2) << "Setting technology order to " << order; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1939 | if (!Technology::GetTechnologyVectorFromString(order, &new_order, error)) { |
| 1940 | return; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | technology_order_ = new_order; |
| 1944 | SortServices(); |
| 1945 | } |
| 1946 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1947 | } // namespace shill |