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" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 24 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 25 | #include "shill/control_interface.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 26 | #include "shill/dbus_adaptor.h" |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 27 | #include "shill/dbus_manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 28 | #include "shill/default_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 29 | #include "shill/device.h" |
| 30 | #include "shill/device_info.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 31 | #include "shill/ephemeral_profile.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 32 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 33 | #include "shill/event_dispatcher.h" |
Gaurav Shah | b790aa2 | 2012-10-23 12:51:12 -0700 | [diff] [blame] | 34 | #include "shill/geolocation_info.h" |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 35 | #include "shill/hook_table.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 36 | #include "shill/key_file_store.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 37 | #include "shill/logging.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 38 | #include "shill/profile.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 39 | #include "shill/property_accessor.h" |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 40 | #include "shill/proxy_factory.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 41 | #include "shill/resolver.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 42 | #include "shill/service.h" |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 43 | #include "shill/service_sorter.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 44 | #include "shill/vpn_service.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 45 | #include "shill/wifi.h" |
| 46 | #include "shill/wifi_service.h" |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 47 | #include "shill/wimax_service.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 48 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 49 | using base::Bind; |
| 50 | using base::StringPrintf; |
| 51 | using base::Unretained; |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 52 | using std::map; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 53 | using std::set; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 54 | using std::string; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 55 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 56 | |
| 57 | namespace shill { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 58 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 59 | // statics |
| 60 | const char Manager::kErrorNoDevice[] = "no wifi devices available"; |
| 61 | const char Manager::kErrorTypeRequired[] = "must specify service type"; |
| 62 | const char Manager::kErrorUnsupportedServiceType[] = |
| 63 | "service type is unsupported"; |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 64 | // This timeout should be less than the upstart job timeout, otherwise |
| 65 | // stats for termination actions might be lost. |
| 66 | const int Manager::kTerminationActionsTimeoutMilliseconds = 4500; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 67 | const char Manager::kPowerManagerKey[] = "manager"; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 68 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 69 | Manager::Manager(ControlInterface *control_interface, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 70 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 71 | Metrics *metrics, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 72 | GLib *glib, |
| 73 | const string &run_directory, |
| 74 | const string &storage_directory, |
| 75 | const string &user_storage_format) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 76 | : dispatcher_(dispatcher), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 77 | run_path_(FilePath(run_directory)), |
| 78 | storage_path_(FilePath(storage_directory)), |
| 79 | user_storage_format_(user_storage_format), |
| 80 | adaptor_(control_interface->CreateManagerAdaptor(this)), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 81 | device_info_(control_interface, dispatcher, metrics, this), |
| 82 | modem_info_(control_interface, dispatcher, metrics, this, glib), |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 83 | vpn_provider_(control_interface, dispatcher, metrics, this), |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 84 | wimax_provider_(control_interface, dispatcher, metrics, this), |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 85 | resolver_(Resolver::GetInstance()), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 86 | running_(false), |
| 87 | connect_profiles_to_rpc_(true), |
| 88 | ephemeral_profile_(new EphemeralProfile(control_interface, this)), |
| 89 | control_interface_(control_interface), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 90 | metrics_(metrics), |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 91 | glib_(glib), |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 92 | use_startup_portal_list_(false), |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 93 | termination_actions_(dispatcher), |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 94 | suspend_delay_registered_(false), |
| 95 | suspend_delay_id_(0), |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 96 | default_service_callback_tag_(0) { |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 97 | HelpRegisterDerivedString(flimflam::kActiveProfileProperty, |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 98 | &Manager::GetActiveProfileRpcIdentifier, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 99 | NULL); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 100 | store_.RegisterBool(flimflam::kArpGatewayProperty, &props_.arp_gateway); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 101 | HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty, |
| 102 | &Manager::AvailableTechnologies, |
| 103 | NULL); |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 104 | HelpRegisterDerivedString(flimflam::kCheckPortalListProperty, |
| 105 | &Manager::GetCheckPortalList, |
| 106 | &Manager::SetCheckPortalList); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 107 | HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty, |
| 108 | &Manager::ConnectedTechnologies, |
| 109 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 110 | store_.RegisterString(flimflam::kCountryProperty, &props_.country); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 111 | HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty, |
| 112 | &Manager::DefaultTechnology, |
| 113 | NULL); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 114 | HelpRegisterConstDerivedRpcIdentifier( |
| 115 | shill::kDefaultServiceProperty, |
| 116 | &Manager::GetDefaultServiceRpcIdentifier); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 117 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kDevicesProperty, |
| 118 | &Manager::EnumerateDevices); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 119 | HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty, |
| 120 | &Manager::EnabledTechnologies, |
| 121 | NULL); |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 122 | HelpRegisterDerivedString(shill::kIgnoredDNSSearchPathsProperty, |
| 123 | &Manager::GetIgnoredDNSSearchPaths, |
| 124 | &Manager::SetIgnoredDNSSearchPaths); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 125 | store_.RegisterString(shill::kLinkMonitorTechnologiesProperty, |
| 126 | &props_.link_monitor_technologies); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 127 | store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode); |
| 128 | store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 129 | store_.RegisterInt32(kPortalCheckIntervalProperty, |
| 130 | &props_.portal_check_interval_seconds); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 131 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kProfilesProperty, |
| 132 | &Manager::EnumerateProfiles); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 133 | store_.RegisterString(kHostNameProperty, &props_.host_name); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 134 | HelpRegisterDerivedString(flimflam::kStateProperty, |
| 135 | &Manager::CalculateState, |
| 136 | NULL); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 137 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kServicesProperty, |
| 138 | &Manager::EnumerateAvailableServices); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 139 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kServiceWatchListProperty, |
| 140 | &Manager::EnumerateWatchedServices); |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 141 | store_.RegisterString(shill::kShortDNSTimeoutTechnologiesProperty, |
| 142 | &props_.short_dns_timeout_technologies); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 143 | |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 144 | // Set default technology order "by hand", to avoid invoking side |
| 145 | // effects of SetTechnologyOrder. |
| 146 | technology_order_.push_back( |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 147 | Technology::IdentifierFromName(flimflam::kTypeVPN)); |
| 148 | technology_order_.push_back( |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 149 | Technology::IdentifierFromName(flimflam::kTypeEthernet)); |
| 150 | technology_order_.push_back( |
| 151 | Technology::IdentifierFromName(flimflam::kTypeWifi)); |
| 152 | technology_order_.push_back( |
Ben Chan | 3cafe38 | 2012-11-13 07:51:10 -0800 | [diff] [blame] | 153 | Technology::IdentifierFromName(flimflam::kTypeWimax)); |
| 154 | technology_order_.push_back( |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 155 | Technology::IdentifierFromName(flimflam::kTypeCellular)); |
| 156 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 157 | SLOG(Manager, 2) << "Manager initialized."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 160 | Manager::~Manager() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 161 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 162 | void Manager::AddDeviceToBlackList(const string &device_name) { |
| 163 | device_info_.AddDeviceToBlackList(device_name); |
| 164 | } |
| 165 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 166 | void Manager::Start() { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 167 | LOG(INFO) << "Manager started."; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 168 | |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 169 | dbus_manager_.reset(new DBusManager()); |
| 170 | dbus_manager_->Start(); |
| 171 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 172 | power_manager_.reset( |
| 173 | new PowerManager(dispatcher_, ProxyFactory::GetInstance())); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 174 | power_manager_->AddStateChangeCallback( |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 175 | kPowerManagerKey, |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 176 | Bind(&Manager::OnPowerStateChanged, AsWeakPtr())); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 177 | // TODO(ers): weak ptr for metrics_? |
| 178 | PowerManager::PowerStateCallback cb = |
| 179 | Bind(&Metrics::NotifyPowerStateChange, Unretained(metrics_)); |
Thieu Le | b84ba34 | 2012-03-02 15:15:19 -0800 | [diff] [blame] | 180 | power_manager_->AddStateChangeCallback(Metrics::kMetricPowerManagerKey, cb); |
| 181 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 182 | CHECK(file_util::CreateDirectory(run_path_)) << run_path_.value(); |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 183 | resolver_->set_path(run_path_.Append("resolv.conf")); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 184 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 185 | InitializeProfiles(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 186 | running_ = true; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 187 | adaptor_->UpdateRunning(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 188 | device_info_.Start(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 189 | modem_info_.Start(); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 190 | vpn_provider_.Start(); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 191 | wimax_provider_.Start(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void Manager::Stop() { |
| 195 | running_ = false; |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 196 | // Persist device information to disk; |
| 197 | vector<DeviceRefPtr>::iterator devices_it; |
| 198 | for (devices_it = devices_.begin(); devices_it != devices_.end(); |
| 199 | ++devices_it) { |
| 200 | UpdateDevice(*devices_it); |
| 201 | } |
| 202 | |
| 203 | // Persist profile, service information to disk. |
| 204 | vector<ProfileRefPtr>::iterator profiles_it; |
| 205 | for (profiles_it = profiles_.begin(); profiles_it != profiles_.end(); |
| 206 | ++profiles_it) { |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 207 | // Since this happens in a loop, the current manager state is stored to |
| 208 | // all default profiles in the stack. This is acceptable because the |
| 209 | // only time multiple default profiles are loaded are during autotests. |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 210 | (*profiles_it)->Save(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 211 | } |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 212 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 213 | vector<ServiceRefPtr>::iterator services_it; |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 214 | Error e; |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 215 | for (services_it = services_.begin(); services_it != services_.end(); |
| 216 | ++services_it) { |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 217 | (*services_it)->Disconnect(&e); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 220 | adaptor_->UpdateRunning(); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 221 | wimax_provider_.Stop(); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 222 | vpn_provider_.Stop(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 223 | modem_info_.Stop(); |
| 224 | device_info_.Stop(); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 225 | sort_services_task_.Cancel(); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 226 | power_manager_.reset(); |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 227 | dbus_manager_.reset(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 230 | void Manager::InitializeProfiles() { |
| 231 | DCHECK(profiles_.empty()); |
| 232 | // The default profile must go first on the stack. |
| 233 | CHECK(file_util::CreateDirectory(storage_path_)) << storage_path_.value(); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 234 | scoped_refptr<DefaultProfile> |
| 235 | default_profile(new DefaultProfile(control_interface_, |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 236 | this, |
| 237 | storage_path_, |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 238 | DefaultProfile::kDefaultId, |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 239 | props_)); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 240 | CHECK(default_profile->InitStorage(glib_, Profile::kCreateOrOpenExisting, |
| 241 | NULL)); |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 242 | CHECK(LoadProperties(default_profile)); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 243 | profiles_.push_back(default_profile.release()); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 244 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 245 | string path; |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 246 | for (vector<string>::iterator it = startup_profiles_.begin(); |
| 247 | it != startup_profiles_.end(); ++it) |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 248 | PushProfile(*it, &path, &error); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 251 | void Manager::CreateProfile(const string &name, string *path, Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 252 | SLOG(Manager, 2) << __func__ << " " << name; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 253 | Profile::Identifier ident; |
| 254 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 255 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 256 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 257 | return; |
| 258 | } |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 259 | |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 260 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 261 | it != profiles_.end(); |
| 262 | ++it) { |
| 263 | if ((*it)->MatchesIdentifier(ident)) { |
| 264 | Error::PopulateAndLog(error, Error::kAlreadyExists, |
| 265 | "Profile name " + name + " is already on stack"); |
| 266 | *path = (*it)->GetRpcIdentifier(); |
| 267 | return; |
| 268 | } |
| 269 | } |
| 270 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 271 | ProfileRefPtr profile; |
| 272 | if (ident.user.empty()) { |
| 273 | profile = new DefaultProfile(control_interface_, |
| 274 | this, |
| 275 | storage_path_, |
| 276 | ident.identifier, |
| 277 | props_); |
| 278 | } else { |
| 279 | profile = new Profile(control_interface_, |
| 280 | this, |
| 281 | ident, |
| 282 | user_storage_format_, |
Gary Morain | b672d35 | 2012-04-25 09:19:06 -0700 | [diff] [blame] | 283 | true); |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 286 | if (!profile->InitStorage(glib_, Profile::kCreateNew, error)) { |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 287 | // |error| will have been populated by InitStorage(). |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | // Save profile data out, and then let the scoped pointer fall out of scope. |
| 292 | if (!profile->Save()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 293 | Error::PopulateAndLog(error, Error::kInternalError, |
| 294 | "Profile name " + name + " could not be saved"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 295 | return; |
| 296 | } |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 297 | |
| 298 | *path = profile->GetRpcIdentifier(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 301 | void Manager::PushProfile(const string &name, string *path, Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 302 | SLOG(Manager, 2) << __func__ << " " << name; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 303 | Profile::Identifier ident; |
| 304 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 305 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 306 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 307 | return; |
| 308 | } |
| 309 | |
| 310 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 311 | it != profiles_.end(); |
| 312 | ++it) { |
| 313 | if ((*it)->MatchesIdentifier(ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 314 | Error::PopulateAndLog(error, Error::kAlreadyExists, |
| 315 | "Profile name " + name + " is already on stack"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | } |
| 319 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 320 | ProfileRefPtr profile; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 321 | if (ident.user.empty()) { |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 322 | // Allow a machine-wide-profile to be pushed on the stack only if the |
| 323 | // profile stack is empty, or if the topmost profile on the stack is |
| 324 | // also a machine-wide (non-user) profile. |
| 325 | if (!profiles_.empty() && !profiles_.back()->GetUser().empty()) { |
| 326 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 327 | "Cannot load non-default global profile " + name + |
| 328 | " on top of a user profile"); |
| 329 | return; |
| 330 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 331 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 332 | scoped_refptr<DefaultProfile> |
| 333 | default_profile(new DefaultProfile(control_interface_, |
| 334 | this, |
| 335 | storage_path_, |
| 336 | ident.identifier, |
| 337 | props_)); |
| 338 | if (!default_profile->InitStorage(glib_, Profile::kOpenExisting, error)) { |
| 339 | // |error| will have been populated by InitStorage(). |
| 340 | return; |
| 341 | } |
| 342 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 343 | if (!LoadProperties(default_profile)) { |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 344 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 345 | "Could not load Manager properties from profile " + |
| 346 | name); |
| 347 | return; |
| 348 | } |
| 349 | profile = default_profile; |
| 350 | } else { |
| 351 | profile = new Profile(control_interface_, |
| 352 | this, |
| 353 | ident, |
| 354 | user_storage_format_, |
| 355 | connect_profiles_to_rpc_); |
| 356 | if (!profile->InitStorage(glib_, Profile::kOpenExisting, error)) { |
| 357 | // |error| will have been populated by InitStorage(). |
| 358 | return; |
| 359 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 362 | profiles_.push_back(profile); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 363 | |
| 364 | // Offer each registered Service the opportunity to join this new Profile. |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 365 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 366 | it != services_.end(); ++it) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 367 | profile->ConfigureService(*it); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 370 | // Shop the Profile contents around to Devices which can create |
| 371 | // non-visible services. |
| 372 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 373 | it != devices_.end(); ++it) { |
| 374 | profile->ConfigureDevice(*it); |
| 375 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 376 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 377 | // Offer the Profile contents to the service/device providers which will |
| 378 | // create new services if necessary. |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 379 | vpn_provider_.CreateServicesFromProfile(profile); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 380 | wimax_provider_.CreateServicesFromProfile(profile); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 381 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 382 | *path = profile->GetRpcIdentifier(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 383 | SortServices(); |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 384 | |
| 385 | Error unused_error; |
| 386 | adaptor_->EmitStringsChanged(flimflam::kProfilesProperty, |
| 387 | EnumerateProfiles(&unused_error)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void Manager::PopProfileInternal() { |
| 391 | CHECK(!profiles_.empty()); |
| 392 | ProfileRefPtr active_profile = profiles_.back(); |
| 393 | profiles_.pop_back(); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 394 | vector<ServiceRefPtr>::iterator it; |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 395 | for (it = services_.begin(); it != services_.end();) { |
| 396 | if ((*it)->profile().get() != active_profile.get() || |
| 397 | MatchProfileWithService(*it) || |
| 398 | !UnloadService(&it)) { |
| 399 | LOG(ERROR) << "Skipping unload of service"; |
| 400 | ++it; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 403 | SortServices(); |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 404 | |
| 405 | Error unused_error; |
| 406 | adaptor_->EmitStringsChanged(flimflam::kProfilesProperty, |
| 407 | EnumerateProfiles(&unused_error)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 410 | void Manager::PopProfile(const string &name, Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 411 | SLOG(Manager, 2) << __func__ << " " << name; |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 412 | // This signal is sent when a user logs out of a session. Regardless of |
| 413 | // whether we find their profile to remove, lets clear the network related |
| 414 | // logs. |
| 415 | MemoryLog::GetInstance()->Clear(); |
| 416 | LOG(INFO) << "Cleared the memory log on logout event."; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 417 | Profile::Identifier ident; |
| 418 | if (profiles_.empty()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 419 | Error::PopulateAndLog(error, Error::kNotFound, "Profile stack is empty"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 420 | return; |
| 421 | } |
| 422 | ProfileRefPtr active_profile = profiles_.back(); |
| 423 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 424 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 425 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | if (!active_profile->MatchesIdentifier(ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 429 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 430 | name + " is not the active profile"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | PopProfileInternal(); |
| 434 | } |
| 435 | |
| 436 | void Manager::PopAnyProfile(Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 437 | SLOG(Manager, 2) << __func__; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 438 | Profile::Identifier ident; |
| 439 | if (profiles_.empty()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 440 | Error::PopulateAndLog(error, Error::kNotFound, "Profile stack is empty"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 441 | return; |
| 442 | } |
| 443 | PopProfileInternal(); |
| 444 | } |
| 445 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 446 | void Manager::RemoveProfile(const string &name, Error *error) { |
| 447 | Profile::Identifier ident; |
| 448 | if (!Profile::ParseIdentifier(name, &ident)) { |
| 449 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 450 | "Invalid profile name " + name); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 455 | it != profiles_.end(); |
| 456 | ++it) { |
| 457 | if ((*it)->MatchesIdentifier(ident)) { |
| 458 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 459 | "Cannot remove profile name " + name + |
| 460 | " since it is on stack"); |
| 461 | return; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | ProfileRefPtr profile; |
| 466 | if (ident.user.empty()) { |
| 467 | profile = new DefaultProfile(control_interface_, |
| 468 | this, |
| 469 | storage_path_, |
| 470 | ident.identifier, |
| 471 | props_); |
| 472 | } else { |
| 473 | profile = new Profile(control_interface_, |
| 474 | this, |
| 475 | ident, |
| 476 | user_storage_format_, |
| 477 | false); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | // |error| will have been populated if RemoveStorage fails. |
| 482 | profile->RemoveStorage(glib_, error); |
| 483 | |
| 484 | return; |
| 485 | } |
| 486 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 487 | bool Manager::HandleProfileEntryDeletion(const ProfileRefPtr &profile, |
| 488 | const std::string &entry_name) { |
| 489 | bool moved_services = false; |
| 490 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 491 | it != services_.end();) { |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 492 | if ((*it)->profile().get() == profile.get() && |
| 493 | (*it)->GetStorageIdentifier() == entry_name) { |
| 494 | profile->AbandonService(*it); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 495 | if (MatchProfileWithService(*it) || |
| 496 | !UnloadService(&it)) { |
| 497 | ++it; |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 498 | } |
| 499 | moved_services = true; |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 500 | } else { |
| 501 | ++it; |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | return moved_services; |
| 505 | } |
| 506 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 507 | ServiceRefPtr Manager::GetServiceWithStorageIdentifier( |
| 508 | const ProfileRefPtr &profile, const std::string &entry_name, Error *error) { |
| 509 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 510 | it != services_.end(); ++it) { |
| 511 | if ((*it)->profile().get() == profile.get() && |
| 512 | (*it)->GetStorageIdentifier() == entry_name) { |
| 513 | return *it; |
| 514 | } |
| 515 | } |
| 516 | |
Paul Stewart | 76a89cb | 2012-09-14 06:06:48 -0700 | [diff] [blame] | 517 | string error_string( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 518 | StringPrintf("Entry %s is not registered in the manager", |
| 519 | entry_name.c_str())); |
Paul Stewart | 76a89cb | 2012-09-14 06:06:48 -0700 | [diff] [blame] | 520 | if (error) { |
| 521 | error->Populate(Error::kNotFound, error_string); |
| 522 | } |
| 523 | SLOG(Manager, 2) << error_string; |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 524 | return NULL; |
| 525 | } |
| 526 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 527 | ServiceRefPtr Manager::GetServiceWithGUID( |
| 528 | const std::string &guid, Error *error) { |
| 529 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 530 | it != services_.end(); ++it) { |
| 531 | if ((*it)->guid() == guid) { |
| 532 | return *it; |
| 533 | } |
| 534 | } |
| 535 | |
Paul Stewart | 76a89cb | 2012-09-14 06:06:48 -0700 | [diff] [blame] | 536 | string error_string( |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 537 | StringPrintf("Service wth GUID %s is not registered in the manager", |
| 538 | guid.c_str())); |
Paul Stewart | 76a89cb | 2012-09-14 06:06:48 -0700 | [diff] [blame] | 539 | if (error) { |
| 540 | error->Populate(Error::kNotFound, error_string); |
| 541 | } |
| 542 | SLOG(Manager, 2) << error_string; |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 543 | return NULL; |
| 544 | } |
| 545 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 546 | ServiceRefPtr Manager::GetDefaultService() const { |
Arman Uguray | 32c7640 | 2012-11-27 14:01:13 -0800 | [diff] [blame] | 547 | SLOG(Manager, 2) << __func__; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 548 | if (services_.empty() || !services_[0]->connection().get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 549 | SLOG(Manager, 2) << "In " << __func__ << ": No default connection exists."; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 550 | return NULL; |
| 551 | } |
| 552 | return services_[0]; |
| 553 | } |
| 554 | |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 555 | RpcIdentifier Manager::GetDefaultServiceRpcIdentifier(Error */*error*/) { |
| 556 | ServiceRefPtr default_service = GetDefaultService(); |
| 557 | return default_service ? default_service->GetRpcIdentifier() : "/"; |
| 558 | } |
| 559 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 560 | bool Manager::IsTechnologyInList(const string &technology_list, |
| 561 | Technology::Identifier tech) const { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 562 | Error error; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 563 | vector<Technology::Identifier> technologies; |
| 564 | return Technology::GetTechnologyVectorFromString(technology_list, |
| 565 | &technologies, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 566 | &error) && |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 567 | std::find(technologies.begin(), technologies.end(), tech) != |
| 568 | technologies.end(); |
| 569 | } |
| 570 | |
| 571 | bool Manager::IsPortalDetectionEnabled(Technology::Identifier tech) { |
| 572 | return IsTechnologyInList(GetCheckPortalList(NULL), tech); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 575 | void Manager::SetStartupPortalList(const string &portal_list) { |
| 576 | startup_portal_list_ = portal_list; |
| 577 | use_startup_portal_list_ = true; |
| 578 | } |
| 579 | |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 580 | bool Manager::IsServiceEphemeral(const ServiceConstRefPtr &service) const { |
| 581 | return service->profile() == ephemeral_profile_; |
| 582 | } |
| 583 | |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 584 | bool Manager::IsTechnologyShortDNSTimeoutEnabled( |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 585 | Technology::Identifier technology) const { |
| 586 | return IsTechnologyInList(props_.short_dns_timeout_technologies, technology); |
| 587 | } |
| 588 | |
| 589 | bool Manager::IsTechnologyLinkMonitorEnabled( |
| 590 | Technology::Identifier technology) const { |
| 591 | return IsTechnologyInList(props_.link_monitor_technologies, technology); |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 594 | const ProfileRefPtr &Manager::ActiveProfile() const { |
Eric Shienbrood | c74cf9c | 2012-03-02 15:00:35 -0500 | [diff] [blame] | 595 | DCHECK_NE(profiles_.size(), 0U); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 596 | return profiles_.back(); |
| 597 | } |
| 598 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 599 | bool Manager::IsActiveProfile(const ProfileRefPtr &profile) const { |
| 600 | return (profiles_.size() > 0 && |
| 601 | ActiveProfile().get() == profile.get()); |
| 602 | } |
| 603 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 604 | bool Manager::MoveServiceToProfile(const ServiceRefPtr &to_move, |
| 605 | const ProfileRefPtr &destination) { |
| 606 | const ProfileRefPtr from = to_move->profile(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 607 | SLOG(Manager, 2) << "Moving service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 608 | << to_move->unique_name() |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 609 | << " to profile " |
| 610 | << destination->GetFriendlyName() |
| 611 | << " from " |
| 612 | << from->GetFriendlyName(); |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 613 | return destination->AdoptService(to_move) && from->AbandonService(to_move); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 616 | ProfileRefPtr Manager::LookupProfileByRpcIdentifier( |
| 617 | const string &profile_rpcid) { |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 618 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 619 | it != profiles_.end(); |
| 620 | ++it) { |
| 621 | if (profile_rpcid == (*it)->GetRpcIdentifier()) { |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 622 | return *it; |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 623 | } |
| 624 | } |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 625 | return NULL; |
| 626 | } |
| 627 | |
| 628 | void Manager::SetProfileForService(const ServiceRefPtr &to_set, |
| 629 | const string &profile_rpcid, |
| 630 | Error *error) { |
| 631 | ProfileRefPtr profile = LookupProfileByRpcIdentifier(profile_rpcid); |
| 632 | if (!profile) { |
| 633 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 634 | StringPrintf("Unknown Profile %s requested for " |
| 635 | "Service", profile_rpcid.c_str())); |
| 636 | return; |
| 637 | } |
| 638 | |
Paul Stewart | 649f3a4 | 2012-04-24 23:22:16 -0700 | [diff] [blame] | 639 | if (!to_set->profile()) { |
| 640 | // We are being asked to set the profile property of a service that |
| 641 | // has never been registered. Now is a good time to register it. |
| 642 | RegisterService(to_set); |
| 643 | } |
| 644 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 645 | if (to_set->profile().get() == profile.get()) { |
| 646 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 647 | "Service is already connected to this profile"); |
| 648 | } else if (!MoveServiceToProfile(to_set, profile)) { |
| 649 | Error::PopulateAndLog(error, Error::kInternalError, |
| 650 | "Unable to move service to profile"); |
| 651 | } |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 654 | void Manager::EnableTechnology(const std::string &technology_name, |
| 655 | Error *error, |
| 656 | const ResultCallback &callback) { |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 657 | CHECK(error != NULL); |
| 658 | DCHECK(error->IsOngoing()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 659 | Technology::Identifier id = Technology::IdentifierFromName(technology_name); |
| 660 | if (id == Technology::kUnknown) { |
| 661 | error->Populate(Error::kInvalidArguments, "Unknown technology"); |
| 662 | return; |
| 663 | } |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 664 | bool deferred = false; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 665 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 666 | it != devices_.end(); ++it) { |
| 667 | DeviceRefPtr device = *it; |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 668 | if (device->technology() == id && !device->enabled()) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 669 | device->SetEnabledPersistent(true, error, callback); |
| 670 | // Continue with other devices even if one fails |
| 671 | // TODO(ers): Decide whether an error should be returned |
| 672 | // for the overall EnableTechnology operation if some |
| 673 | // devices succeed and some fail. |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 674 | if (error->IsOngoing()) |
| 675 | deferred = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 676 | } |
| 677 | } |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 678 | // If no device has deferred work, then clear the error to |
| 679 | // communicate to the caller that all work is done. |
| 680 | if (!deferred) |
| 681 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void Manager::DisableTechnology(const std::string &technology_name, |
| 685 | Error *error, |
| 686 | const ResultCallback &callback) { |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 687 | CHECK(error != NULL); |
| 688 | DCHECK(error->IsOngoing()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 689 | Technology::Identifier id = Technology::IdentifierFromName(technology_name); |
| 690 | if (id == Technology::kUnknown) { |
| 691 | error->Populate(Error::kInvalidArguments, "Unknown technology"); |
| 692 | return; |
| 693 | } |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 694 | bool deferred = false; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 695 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 696 | it != devices_.end(); ++it) { |
| 697 | DeviceRefPtr device = *it; |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 698 | if (device->technology() == id && device->enabled()) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 699 | device->SetEnabledPersistent(false, error, callback); |
| 700 | // Continue with other devices even if one fails |
| 701 | // TODO(ers): Decide whether an error should be returned |
| 702 | // for the overall DisableTechnology operation if some |
| 703 | // devices succeed and some fail. |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 704 | if (error->IsOngoing()) |
| 705 | deferred = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 706 | } |
| 707 | } |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 708 | // If no device has deferred work, then clear the error to |
| 709 | // communicate to the caller that all work is done. |
| 710 | if (!deferred) |
| 711 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | void Manager::UpdateEnabledTechnologies() { |
| 715 | Error error; |
| 716 | adaptor_->EmitStringsChanged(flimflam::kEnabledTechnologiesProperty, |
| 717 | EnabledTechnologies(&error)); |
| 718 | } |
| 719 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 720 | void Manager::RegisterDevice(const DeviceRefPtr &to_manage) { |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 721 | LOG(INFO) << "Device " << to_manage->FriendlyName() << " registered."; |
| 722 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 723 | it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 724 | if (to_manage.get() == it->get()) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 725 | return; |
| 726 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 727 | devices_.push_back(to_manage); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 728 | |
Paul Stewart | 87a4ae8 | 2012-10-26 15:49:32 -0700 | [diff] [blame] | 729 | LoadDeviceFromProfiles(to_manage); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 730 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 731 | // If |to_manage| is new, it needs to be persisted. |
| 732 | UpdateDevice(to_manage); |
| 733 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 734 | // In normal usage, running_ will always be true when we are here, however |
| 735 | // unit tests sometimes do things in otherwise invalid states. |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 736 | if (running_ && (to_manage->enabled_persistent() || |
| 737 | to_manage->IsUnderlyingDeviceEnabled())) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 738 | to_manage->SetEnabled(true); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 739 | |
Eric Shienbrood | 8839a89 | 2012-03-29 10:33:48 -0400 | [diff] [blame] | 740 | EmitDeviceProperties(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 741 | } |
| 742 | |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 743 | void Manager::DeregisterDevice(const DeviceRefPtr &to_forget) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 744 | SLOG(Manager, 2) << __func__ << "(" << to_forget->FriendlyName() << ")"; |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 745 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 746 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 747 | if (to_forget.get() == it->get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 748 | SLOG(Manager, 2) << "Deregistered device: " << to_forget->UniqueName(); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 749 | UpdateDevice(to_forget); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 750 | to_forget->SetEnabled(false); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 751 | devices_.erase(it); |
Eric Shienbrood | 8839a89 | 2012-03-29 10:33:48 -0400 | [diff] [blame] | 752 | EmitDeviceProperties(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 756 | SLOG(Manager, 2) << __func__ << " unknown device: " |
| 757 | << to_forget->UniqueName(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Paul Stewart | 87a4ae8 | 2012-10-26 15:49:32 -0700 | [diff] [blame] | 760 | void Manager::LoadDeviceFromProfiles(const DeviceRefPtr &device) { |
| 761 | // We are applying device properties from the DefaultProfile, and adding the |
| 762 | // union of hidden services in all loaded profiles to the device. |
| 763 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 764 | it != profiles_.end(); ++it) { |
| 765 | // Load device configuration, if any exists, as well as hidden services. |
| 766 | (*it)->ConfigureDevice(device); |
| 767 | } |
| 768 | } |
| 769 | |
Eric Shienbrood | 8839a89 | 2012-03-29 10:33:48 -0400 | [diff] [blame] | 770 | void Manager::EmitDeviceProperties() { |
| 771 | vector<DeviceRefPtr>::iterator it; |
| 772 | vector<string> device_paths; |
| 773 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 774 | device_paths.push_back((*it)->GetRpcIdentifier()); |
| 775 | } |
| 776 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kDevicesProperty, |
| 777 | device_paths); |
| 778 | Error error; |
| 779 | adaptor_->EmitStringsChanged(flimflam::kAvailableTechnologiesProperty, |
| 780 | AvailableTechnologies(&error)); |
| 781 | adaptor_->EmitStringsChanged(flimflam::kEnabledTechnologiesProperty, |
| 782 | EnabledTechnologies(&error)); |
| 783 | } |
| 784 | |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 785 | bool Manager::HasService(const ServiceRefPtr &service) { |
| 786 | vector<ServiceRefPtr>::iterator it; |
| 787 | for (it = services_.begin(); it != services_.end(); ++it) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 788 | if ((*it)->unique_name() == service->unique_name()) |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 789 | return true; |
| 790 | } |
| 791 | return false; |
| 792 | } |
| 793 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 794 | void Manager::RegisterService(const ServiceRefPtr &to_manage) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 795 | SLOG(Manager, 2) << "Registering service " << to_manage->unique_name(); |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 796 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 797 | MatchProfileWithService(to_manage); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 798 | |
| 799 | // Now add to OUR list. |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 800 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 801 | for (it = services_.begin(); it != services_.end(); ++it) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 802 | CHECK(to_manage->unique_name() != (*it)->unique_name()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 803 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 804 | services_.push_back(to_manage); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 805 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 808 | void Manager::DeregisterService(const ServiceRefPtr &to_forget) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 809 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 810 | for (it = services_.begin(); it != services_.end(); ++it) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 811 | if (to_forget->unique_name() == (*it)->unique_name()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 812 | DCHECK(!(*it)->connection()); |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 813 | (*it)->Unload(); |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 814 | (*it)->SetProfile(NULL); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 815 | services_.erase(it); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 816 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 817 | return; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 822 | bool Manager::UnloadService(vector<ServiceRefPtr>::iterator *service_iterator) { |
| 823 | if (!(**service_iterator)->Unload()) { |
| 824 | return false; |
| 825 | } |
| 826 | |
| 827 | DCHECK(!(**service_iterator)->connection()); |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 828 | (**service_iterator)->SetProfile(NULL); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 829 | *service_iterator = services_.erase(*service_iterator); |
| 830 | |
| 831 | return true; |
| 832 | } |
| 833 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 834 | void Manager::UpdateService(const ServiceRefPtr &to_update) { |
| 835 | CHECK(to_update); |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 836 | LOG(INFO) << "Service " << to_update->unique_name() << " updated;" |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 837 | << " state: " << Service::ConnectStateToString(to_update->state()) |
| 838 | << " failure: " |
| 839 | << Service::ConnectFailureToString(to_update->failure()); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 840 | SLOG(Manager, 2) << "IsConnected(): " << to_update->IsConnected(); |
| 841 | SLOG(Manager, 2) << "IsConnecting(): " << to_update->IsConnecting(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 842 | if (to_update->IsConnected()) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 843 | to_update->MakeFavorite(); |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 844 | // Persists the updated favorite setting in the profile. |
| 845 | SaveServiceToProfile(to_update); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 846 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 847 | SortServices(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 850 | void Manager::UpdateDevice(const DeviceRefPtr &to_update) { |
| 851 | LOG(INFO) << "Device " << to_update->link_name() << " updated: " |
| 852 | << (to_update->enabled_persistent() ? "enabled" : "disabled"); |
| 853 | // Saves the device to the topmost profile that accepts it. Normally, this |
| 854 | // would be the only DefaultProfile at the bottom of the stack except in |
| 855 | // autotests that push a second test-only DefaultProfile. |
| 856 | for (vector<ProfileRefPtr>::reverse_iterator rit = profiles_.rbegin(); |
| 857 | rit != profiles_.rend(); ++rit) { |
| 858 | if ((*rit)->UpdateDevice(to_update)) { |
| 859 | return; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 864 | void Manager::SaveServiceToProfile(const ServiceRefPtr &to_update) { |
| 865 | if (IsServiceEphemeral(to_update)) { |
| 866 | if (profiles_.empty()) { |
| 867 | LOG(ERROR) << "Cannot assign profile to service: no profiles exist!"; |
| 868 | } else { |
| 869 | MoveServiceToProfile(to_update, profiles_.back()); |
| 870 | } |
| 871 | } else { |
| 872 | to_update->profile()->UpdateService(to_update); |
| 873 | } |
| 874 | } |
| 875 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 876 | bool Manager::LoadProperties(const scoped_refptr<DefaultProfile> &profile) { |
| 877 | if (!profile->LoadManagerProperties(&props_)) { |
| 878 | return false; |
| 879 | } |
| 880 | SetIgnoredDNSSearchPaths(props_.ignored_dns_search_paths, NULL); |
| 881 | return true; |
| 882 | } |
| 883 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 884 | void Manager::AddTerminationAction(const string &name, |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 885 | const base::Closure &start) { |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 886 | if (termination_actions_.IsEmpty() && power_manager_.get()) { |
| 887 | power_manager_->AddSuspendDelayCallback( |
| 888 | kPowerManagerKey, |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 889 | Bind(&Manager::OnSuspendImminent, AsWeakPtr())); |
| 890 | CHECK(!suspend_delay_registered_); |
| 891 | suspend_delay_registered_ = power_manager_->RegisterSuspendDelay( |
| 892 | base::TimeDelta::FromMilliseconds( |
| 893 | kTerminationActionsTimeoutMilliseconds), |
| 894 | &suspend_delay_id_); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 895 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 896 | termination_actions_.Add(name, start); |
| 897 | } |
| 898 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 899 | void Manager::TerminationActionComplete(const string &name) { |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 900 | termination_actions_.ActionComplete(name); |
| 901 | } |
| 902 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 903 | void Manager::RemoveTerminationAction(const string &name) { |
| 904 | if (termination_actions_.IsEmpty()) { |
| 905 | return; |
| 906 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 907 | termination_actions_.Remove(name); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 908 | if (termination_actions_.IsEmpty() && power_manager_.get()) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 909 | if (suspend_delay_registered_) { |
| 910 | power_manager_->UnregisterSuspendDelay(suspend_delay_id_); |
| 911 | suspend_delay_registered_ = false; |
| 912 | suspend_delay_id_ = 0; |
| 913 | } |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 914 | power_manager_->RemoveSuspendDelayCallback(kPowerManagerKey); |
| 915 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | void Manager::RunTerminationActions( |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 919 | const base::Callback<void(const Error &)> &done) { |
| 920 | LOG(INFO) << "Running termination actions."; |
| 921 | termination_actions_.Run(kTerminationActionsTimeoutMilliseconds, done); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 924 | bool Manager::RunTerminationActionsAndNotifyMetrics( |
| 925 | const base::Callback<void(const Error &)> &done, |
| 926 | Metrics::TerminationActionReason reason) { |
| 927 | if (termination_actions_.IsEmpty()) |
| 928 | return false; |
| 929 | |
| 930 | metrics_->NotifyTerminationActionsStarted(reason); |
| 931 | RunTerminationActions(done); |
| 932 | return true; |
| 933 | } |
| 934 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 935 | int Manager::RegisterDefaultServiceCallback(const ServiceCallback &callback) { |
| 936 | default_service_callbacks_[++default_service_callback_tag_] = callback; |
| 937 | return default_service_callback_tag_; |
| 938 | } |
| 939 | |
| 940 | void Manager::DeregisterDefaultServiceCallback(int tag) { |
| 941 | default_service_callbacks_.erase(tag); |
| 942 | } |
| 943 | |
| 944 | void Manager::NotifyDefaultServiceChanged(const ServiceRefPtr &service) { |
| 945 | for (map<int, ServiceCallback>::const_iterator it = |
| 946 | default_service_callbacks_.begin(); |
| 947 | it != default_service_callbacks_.end(); ++it) { |
| 948 | it->second.Run(service); |
| 949 | } |
| 950 | metrics_->NotifyDefaultServiceChanged(service); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 951 | EmitDefaultService(); |
| 952 | } |
| 953 | |
| 954 | void Manager::EmitDefaultService() { |
| 955 | RpcIdentifier rpc_identifier = GetDefaultServiceRpcIdentifier(NULL); |
| 956 | if (rpc_identifier != default_service_rpc_identifier_) { |
| 957 | adaptor_->EmitRpcIdentifierChanged(shill::kDefaultServiceProperty, |
| 958 | rpc_identifier); |
| 959 | default_service_rpc_identifier_ = rpc_identifier; |
| 960 | } |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 961 | } |
| 962 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 963 | void Manager::OnPowerStateChanged( |
| 964 | PowerManagerProxyDelegate::SuspendState power_state) { |
| 965 | if (power_state == PowerManagerProxyDelegate::kOn) { |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 966 | vector<ServiceRefPtr>::iterator sit; |
| 967 | for (sit = services_.begin(); sit != services_.end(); ++sit) { |
| 968 | (*sit)->OnAfterResume(); |
| 969 | } |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 970 | SortServices(); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 971 | vector<DeviceRefPtr>::iterator it; |
| 972 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 973 | (*it)->OnAfterResume(); |
| 974 | } |
| 975 | } else if (power_state == PowerManagerProxyDelegate::kMem) { |
| 976 | vector<DeviceRefPtr>::iterator it; |
| 977 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 978 | (*it)->OnBeforeSuspend(); |
| 979 | } |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 983 | void Manager::OnSuspendImminent(int suspend_id) { |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 984 | if (!RunTerminationActionsAndNotifyMetrics( |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 985 | Bind(&Manager::OnSuspendActionsComplete, AsWeakPtr(), suspend_id), |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 986 | Metrics::kTerminationActionReasonSuspend)) { |
| 987 | LOG(INFO) << "No suspend actions were run."; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 988 | power_manager_->ReportSuspendReadiness(suspend_delay_id_, suspend_id); |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 989 | } |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 990 | } |
| 991 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 992 | void Manager::OnSuspendActionsComplete(int suspend_id, const Error &error) { |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 993 | LOG(INFO) << "Finished suspend actions. Result: " << error; |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 994 | metrics_->NotifyTerminationActionsCompleted( |
| 995 | Metrics::kTerminationActionReasonSuspend, error.IsSuccess()); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 996 | power_manager_->ReportSuspendReadiness(suspend_delay_id_, suspend_id); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 997 | } |
| 998 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 999 | void Manager::FilterByTechnology(Technology::Identifier tech, |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 1000 | vector<DeviceRefPtr> *found) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1001 | CHECK(found); |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 1002 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1003 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 1004 | if ((*it)->technology() == tech) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1005 | found->push_back(*it); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1009 | ServiceRefPtr Manager::FindService(const string &name) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 1010 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1011 | for (it = services_.begin(); it != services_.end(); ++it) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1012 | if (name == (*it)->unique_name()) |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 1013 | return *it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1014 | } |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 1015 | return NULL; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 1018 | void Manager::HelpRegisterConstDerivedRpcIdentifier( |
| 1019 | const string &name, |
| 1020 | RpcIdentifier(Manager::*get)(Error *error)) { |
| 1021 | store_.RegisterDerivedRpcIdentifier( |
| 1022 | name, |
| 1023 | RpcIdentifierAccessor( |
| 1024 | new CustomAccessor<Manager, RpcIdentifier>(this, get, NULL))); |
| 1025 | } |
| 1026 | |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 1027 | void Manager::HelpRegisterConstDerivedRpcIdentifiers( |
| 1028 | const string &name, |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 1029 | RpcIdentifiers(Manager::*get)(Error *error)) { |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 1030 | store_.RegisterDerivedRpcIdentifiers( |
| 1031 | name, |
| 1032 | RpcIdentifiersAccessor( |
| 1033 | new CustomAccessor<Manager, RpcIdentifiers>(this, get, NULL))); |
| 1034 | } |
| 1035 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 1036 | void Manager::HelpRegisterDerivedString( |
| 1037 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1038 | string(Manager::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 1039 | void(Manager::*set)(const string&, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 1040 | store_.RegisterDerivedString( |
| 1041 | name, |
| 1042 | StringAccessor(new CustomAccessor<Manager, string>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 1045 | void Manager::HelpRegisterDerivedStrings( |
| 1046 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1047 | Strings(Manager::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 1048 | void(Manager::*set)(const Strings &, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 1049 | store_.RegisterDerivedStrings( |
| 1050 | name, |
| 1051 | StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1054 | void Manager::SortServices() { |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1055 | // We might be called in the middle of a series of events that |
| 1056 | // may result in multiple calls to Manager::SortServices, or within |
| 1057 | // an outer loop that may also be traversing the services_ list. |
| 1058 | // Defer this work to the event loop. |
| 1059 | if (sort_services_task_.IsCancelled()) { |
| 1060 | sort_services_task_.Reset(Bind(&Manager::SortServicesTask, AsWeakPtr())); |
| 1061 | dispatcher_->PostTask(sort_services_task_.callback()); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | void Manager::SortServicesTask() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1066 | SLOG(Manager, 4) << "In " << __func__; |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1067 | sort_services_task_.Cancel(); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1068 | ServiceRefPtr default_service; |
| 1069 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1070 | if (!services_.empty()) { |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1071 | // Keep track of the service that is the candidate for the default |
| 1072 | // service. We have not yet tested to see if this service has a |
| 1073 | // connection. |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1074 | default_service = services_[0]; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1075 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1076 | sort(services_.begin(), services_.end(), ServiceSorter(technology_order_)); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1077 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1078 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 1079 | EnumerateAvailableServices(NULL)); |
| 1080 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServiceWatchListProperty, |
| 1081 | EnumerateWatchedServices(NULL)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1082 | |
| 1083 | Error error; |
| 1084 | adaptor_->EmitStringsChanged(flimflam::kConnectedTechnologiesProperty, |
| 1085 | ConnectedTechnologies(&error)); |
| 1086 | adaptor_->EmitStringChanged(flimflam::kDefaultTechnologyProperty, |
| 1087 | DefaultTechnology(&error)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1088 | |
| 1089 | if (!services_.empty()) { |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1090 | ConnectionRefPtr default_connection = default_service->connection(); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1091 | if (default_connection && |
| 1092 | services_[0]->connection() != default_connection) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1093 | default_connection->SetIsDefault(false); |
| 1094 | } |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1095 | if (services_[0]->connection()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1096 | services_[0]->connection()->SetIsDefault(true); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1097 | default_service = services_[0]; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1098 | } else { |
| 1099 | default_service = NULL; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1100 | } |
| 1101 | } |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1102 | NotifyDefaultServiceChanged(default_service); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1103 | AutoConnect(); |
| 1104 | } |
| 1105 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 1106 | bool Manager::MatchProfileWithService(const ServiceRefPtr &service) { |
| 1107 | vector<ProfileRefPtr>::reverse_iterator it; |
| 1108 | for (it = profiles_.rbegin(); it != profiles_.rend(); ++it) { |
| 1109 | if ((*it)->ConfigureService(service)) { |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | if (it == profiles_.rend()) { |
| 1114 | ephemeral_profile_->AdoptService(service); |
| 1115 | return false; |
| 1116 | } |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1120 | void Manager::AutoConnect() { |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 1121 | if (!running_) { |
| 1122 | LOG(INFO) << "Auto-connect suppressed -- not running."; |
| 1123 | return; |
| 1124 | } |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 1125 | if (power_manager_.get() && |
| 1126 | power_manager_->power_state() != PowerManagerProxyDelegate::kOn && |
| 1127 | power_manager_->power_state() != PowerManagerProxyDelegate::kUnknown) { |
| 1128 | LOG(INFO) << "Auto-connect suppressed -- power state is not 'on'."; |
| 1129 | return; |
| 1130 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1131 | if (services_.empty()) { |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 1132 | LOG(INFO) << "Auto-connect suppressed -- no services."; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1136 | if (SLOG_IS_ON(Manager, 4)) { |
| 1137 | SLOG(Manager, 4) << "Sorted service list: "; |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 1138 | for (size_t i = 0; i < services_.size(); ++i) { |
| 1139 | ServiceRefPtr service = services_[i]; |
| 1140 | const char *compare_reason = NULL; |
| 1141 | if (i + 1 < services_.size()) { |
| 1142 | Service::Compare( |
| 1143 | service, services_[i+1], technology_order_, &compare_reason); |
| 1144 | } else { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1145 | compare_reason = "last"; |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 1146 | } |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1147 | SLOG(Manager, 4) << "Service " << service->unique_name() |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1148 | << " IsConnected: " << service->IsConnected() |
| 1149 | << " IsConnecting: " << service->IsConnecting() |
| 1150 | << " IsFailed: " << service->IsFailed() |
| 1151 | << " connectable: " << service->connectable() |
| 1152 | << " auto_connect: " << service->auto_connect() |
| 1153 | << " favorite: " << service->favorite() |
| 1154 | << " priority: " << service->priority() |
| 1155 | << " security_level: " << service->security_level() |
| 1156 | << " strength: " << service->strength() |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1157 | << " sorted: " << compare_reason; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1161 | // Perform auto-connect. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1162 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 1163 | it != services_.end(); ++it) { |
| 1164 | if ((*it)->auto_connect()) { |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1165 | (*it)->AutoConnect(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1166 | } |
| 1167 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1170 | string Manager::CalculateState(Error */*error*/) { |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 1171 | // |services_| is sorted such that connected services are first. |
| 1172 | if (!services_.empty() && |
| 1173 | services_.front()->IsConnected()) { |
| 1174 | return flimflam::kStateOnline; |
| 1175 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1176 | return flimflam::kStateOffline; |
| 1177 | } |
| 1178 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1179 | vector<string> Manager::AvailableTechnologies(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1180 | set<string> unique_technologies; |
| 1181 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1182 | it != devices_.end(); ++it) { |
| 1183 | unique_technologies.insert( |
| 1184 | Technology::NameFromIdentifier((*it)->technology())); |
| 1185 | } |
| 1186 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1189 | vector<string> Manager::ConnectedTechnologies(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1190 | set<string> unique_technologies; |
| 1191 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1192 | it != devices_.end(); ++it) { |
| 1193 | if ((*it)->IsConnected()) |
| 1194 | unique_technologies.insert( |
| 1195 | Technology::NameFromIdentifier((*it)->technology())); |
| 1196 | } |
| 1197 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 1200 | string Manager::DefaultTechnology(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1201 | return (!services_.empty() && services_[0]->IsConnected()) ? |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 1202 | services_[0]->GetTechnologyString() : ""; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1205 | vector<string> Manager::EnabledTechnologies(Error */*error*/) { |
| 1206 | set<string> unique_technologies; |
| 1207 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1208 | it != devices_.end(); ++it) { |
| 1209 | if ((*it)->enabled()) |
| 1210 | unique_technologies.insert( |
| 1211 | Technology::NameFromIdentifier((*it)->technology())); |
| 1212 | } |
| 1213 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 1216 | RpcIdentifiers Manager::EnumerateDevices(Error */*error*/) { |
| 1217 | RpcIdentifiers device_rpc_ids; |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 1218 | for (vector<DeviceRefPtr>::const_iterator it = devices_.begin(); |
| 1219 | it != devices_.end(); |
| 1220 | ++it) { |
| 1221 | device_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 1222 | } |
| 1223 | return device_rpc_ids; |
| 1224 | } |
| 1225 | |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 1226 | RpcIdentifiers Manager::EnumerateProfiles(Error */*error*/) { |
| 1227 | RpcIdentifiers profile_rpc_ids; |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1228 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 1229 | it != profiles_.end(); |
| 1230 | ++it) { |
| 1231 | profile_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 1232 | } |
| 1233 | return profile_rpc_ids; |
| 1234 | } |
| 1235 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1236 | vector<string> Manager::EnumerateAvailableServices(Error */*error*/) { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 1237 | vector<string> service_rpc_ids; |
| 1238 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 1239 | it != services_.end(); |
| 1240 | ++it) { |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 1241 | if ((*it)->IsVisible()) { |
| 1242 | service_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 1243 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 1244 | } |
| 1245 | return service_rpc_ids; |
| 1246 | } |
| 1247 | |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 1248 | RpcIdentifiers Manager::EnumerateWatchedServices(Error */*error*/) { |
| 1249 | vector<string> service_rpc_ids; |
| 1250 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 1251 | it != services_.end(); |
| 1252 | ++it) { |
| 1253 | if ((*it)->IsVisible() && (*it)->IsActive(NULL)) { |
| 1254 | service_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 1255 | } |
| 1256 | } |
| 1257 | return service_rpc_ids; |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1260 | string Manager::GetActiveProfileRpcIdentifier(Error */*error*/) { |
| 1261 | return ActiveProfile()->GetRpcIdentifier(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 1264 | string Manager::GetCheckPortalList(Error */*error*/) { |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 1265 | return use_startup_portal_list_ ? startup_portal_list_ : |
| 1266 | props_.check_portal_list; |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | void Manager::SetCheckPortalList(const string &portal_list, Error *error) { |
| 1270 | props_.check_portal_list = portal_list; |
| 1271 | use_startup_portal_list_ = false; |
| 1272 | } |
| 1273 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 1274 | string Manager::GetIgnoredDNSSearchPaths(Error */*error*/) { |
| 1275 | return props_.ignored_dns_search_paths; |
| 1276 | } |
| 1277 | |
| 1278 | void Manager::SetIgnoredDNSSearchPaths(const string &ignored_paths, |
| 1279 | Error */*error*/) { |
| 1280 | props_.ignored_dns_search_paths = ignored_paths; |
| 1281 | vector<string> ignored_path_list; |
| 1282 | if (!ignored_paths.empty()) { |
| 1283 | base::SplitString(ignored_paths, ',', &ignored_path_list); |
| 1284 | } |
| 1285 | resolver_->set_ignored_search_list(ignored_path_list); |
| 1286 | } |
| 1287 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1288 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1289 | ServiceRefPtr Manager::GetService(const KeyValueStore &args, Error *error) { |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1290 | if (args.ContainsString(flimflam::kTypeProperty) && |
| 1291 | args.GetString(flimflam::kTypeProperty) == flimflam::kTypeVPN) { |
| 1292 | // GetService on a VPN service should actually perform ConfigureService. |
| 1293 | // TODO(pstew): Remove this hack and change Chrome to use ConfigureService |
| 1294 | // instead, when we no longer need to support flimflam. crosbug.com/31523 |
| 1295 | return ConfigureService(args, error); |
| 1296 | } |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1297 | |
| 1298 | ServiceRefPtr service = GetServiceInner(args, error); |
| 1299 | if (service) { |
| 1300 | // Configures the service using the rest of the passed-in arguments. |
| 1301 | service->Configure(args, error); |
| 1302 | } |
| 1303 | |
| 1304 | return service; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | ServiceRefPtr Manager::GetServiceInner(const KeyValueStore &args, |
| 1308 | Error *error) { |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1309 | if (args.ContainsString(flimflam::kGuidProperty)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1310 | SLOG(Manager, 2) << __func__ << ": searching by GUID"; |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1311 | ServiceRefPtr service = |
| 1312 | GetServiceWithGUID(args.GetString(flimflam::kGuidProperty), NULL); |
| 1313 | if (service) { |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1314 | return service; |
| 1315 | } |
| 1316 | } |
| 1317 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1318 | if (!args.ContainsString(flimflam::kTypeProperty)) { |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1319 | Error::PopulateAndLog(error, Error::kInvalidArguments, kErrorTypeRequired); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1320 | return NULL; |
| 1321 | } |
| 1322 | |
| 1323 | string type = args.GetString(flimflam::kTypeProperty); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1324 | if (type == flimflam::kTypeVPN) { |
| 1325 | SLOG(Manager, 2) << __func__ << ": getting VPN Service"; |
| 1326 | return vpn_provider_.GetService(args, error); |
| 1327 | } |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1328 | if (type == flimflam::kTypeWifi) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1329 | SLOG(Manager, 2) << __func__ << ": getting WiFi Service"; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1330 | return GetWifiService(args, error); |
| 1331 | } |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1332 | if (type == flimflam::kTypeWimax) { |
| 1333 | SLOG(Manager, 2) << __func__ << ": getting WiMAX Service"; |
| 1334 | return wimax_provider_.GetService(args, error); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1335 | } |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1336 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 1337 | kErrorUnsupportedServiceType); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1338 | return NULL; |
| 1339 | } |
| 1340 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1341 | WiFiServiceRefPtr Manager::GetWifiService(const KeyValueStore &args, |
| 1342 | Error *error) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1343 | vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1344 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1345 | if (wifi_devices.empty()) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1346 | Error::PopulateAndLog(error, Error::kInvalidArguments, kErrorNoDevice); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1347 | return NULL; |
| 1348 | } else { |
| 1349 | WiFi *wifi = dynamic_cast<WiFi *>(wifi_devices.front().get()); |
| 1350 | CHECK(wifi); |
| 1351 | return wifi->GetService(args, error); |
| 1352 | } |
| 1353 | } |
| 1354 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1355 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1356 | ServiceRefPtr Manager::ConfigureService(const KeyValueStore &args, |
| 1357 | Error *error) { |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1358 | ProfileRefPtr profile = ActiveProfile(); |
| 1359 | bool profile_specified = args.ContainsString(flimflam::kProfileProperty); |
| 1360 | if (profile_specified) { |
| 1361 | string profile_rpcid = args.GetString(flimflam::kProfileProperty); |
| 1362 | profile = LookupProfileByRpcIdentifier(profile_rpcid); |
| 1363 | if (!profile) { |
| 1364 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 1365 | "Invalid profile name " + profile_rpcid); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1366 | return NULL; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1370 | ServiceRefPtr service = GetServiceInner(args, error); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1371 | if (error->IsFailure() || !service) { |
| 1372 | LOG(ERROR) << "GetService failed; returning upstream error."; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1373 | return NULL; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1376 | // First pull in any stored configuration associated with the service. |
| 1377 | if (service->profile() == profile) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1378 | SLOG(Manager, 2) << __func__ << ": service " << service->unique_name() |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1379 | << " is already a member of profile " |
| 1380 | << profile->GetFriendlyName() |
| 1381 | << " so a load is not necessary."; |
| 1382 | } else if (profile->LoadService(service)) { |
| 1383 | SLOG(Manager, 2) << __func__ << ": applied stored information from profile " |
| 1384 | << profile->GetFriendlyName() |
| 1385 | << " into service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1386 | << service->unique_name(); |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1387 | } else { |
| 1388 | SLOG(Manager, 2) << __func__ << ": no previous information in profile " |
| 1389 | << profile->GetFriendlyName() |
| 1390 | << " exists for service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1391 | << service->unique_name(); |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | // Overlay this with the passed-in configuration parameters. |
| 1395 | service->Configure(args, error); |
| 1396 | |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1397 | // Overwrite the profile data with the resulting configured service. |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1398 | if (!profile->UpdateService(service)) { |
| 1399 | Error::PopulateAndLog(error, Error::kInternalError, |
| 1400 | "Unable to save service to profile"); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1401 | return NULL; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | if (HasService(service)) { |
| 1405 | // If the service has been registered (it may not be -- as is the case |
| 1406 | // with invisible WiFi networks), we can now transfer the service between |
| 1407 | // profiles. |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 1408 | if (IsServiceEphemeral(service) || |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1409 | (profile_specified && service->profile() != profile)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1410 | SLOG(Manager, 2) << "Moving service to profile " |
| 1411 | << profile->GetFriendlyName(); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1412 | if (!MoveServiceToProfile(service, profile)) { |
| 1413 | Error::PopulateAndLog(error, Error::kInternalError, |
| 1414 | "Unable to move service to profile"); |
| 1415 | } |
| 1416 | } |
| 1417 | } |
Paul Stewart | 4357f4e | 2012-04-26 17:39:26 -0700 | [diff] [blame] | 1418 | |
| 1419 | // Notify the service that a profile has been configured for it. |
| 1420 | service->OnProfileConfigured(); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1421 | |
| 1422 | return service; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Gaurav Shah | b790aa2 | 2012-10-23 12:51:12 -0700 | [diff] [blame] | 1425 | map<string, GeolocationInfos> Manager::GetNetworksForGeolocation() { |
| 1426 | map<string, GeolocationInfos> networks; |
| 1427 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1428 | it != devices_.end(); ++it) { |
| 1429 | switch((*it)->technology()) { |
| 1430 | // TODO(gauravsh): crosbug.com/35736 Need a strategy for combining |
| 1431 | // geolocation objects from multiple devices of the same technolgy. |
| 1432 | // Currently, we just pick the geolocation objects from the first found |
| 1433 | // device of each supported technology type. |
| 1434 | case Technology::kWifi: |
| 1435 | if (!ContainsKey(networks, kGeoWifiAccessPointsProperty)) |
| 1436 | networks[kGeoWifiAccessPointsProperty] = |
| 1437 | (*it)->GetGeolocationObjects(); |
| 1438 | break; |
| 1439 | case Technology::kCellular: |
| 1440 | if (!ContainsKey(networks, kGeoCellTowersProperty)) |
| 1441 | networks[kGeoCellTowersProperty] = (*it)->GetGeolocationObjects(); |
| 1442 | break; |
| 1443 | default: |
| 1444 | // Ignore other technologies. |
| 1445 | break; |
| 1446 | }; |
| 1447 | } |
| 1448 | return networks; |
| 1449 | }; |
| 1450 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1451 | void Manager::RecheckPortal(Error */*error*/) { |
| 1452 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1453 | it != devices_.end(); ++it) { |
| 1454 | if ((*it)->RequestPortalDetection()) { |
| 1455 | // Only start Portal Detection on the device with the default connection. |
| 1456 | // We will get a "true" return value when we've found that device, and |
| 1457 | // can end our loop early as a result. |
| 1458 | break; |
| 1459 | } |
| 1460 | } |
| 1461 | } |
| 1462 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1463 | void Manager::RecheckPortalOnService(const ServiceRefPtr &service) { |
| 1464 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 1465 | it != devices_.end(); ++it) { |
| 1466 | if ((*it)->IsConnectedToService(service)) { |
| 1467 | // As opposed to RecheckPortal() above, we explicitly stop and then |
| 1468 | // restart portal detection, since the service to recheck was explicitly |
| 1469 | // specified. |
| 1470 | (*it)->RestartPortalDetection(); |
| 1471 | break; |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1476 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1477 | void Manager::RequestScan(const string &technology, Error *error) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1478 | if (technology == flimflam::kTypeWifi || technology == "") { |
| 1479 | vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 1480 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1481 | |
| 1482 | for (vector<DeviceRefPtr>::iterator it = wifi_devices.begin(); |
| 1483 | it != wifi_devices.end(); |
| 1484 | ++it) { |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 1485 | (*it)->Scan(error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1486 | } |
| 1487 | } else { |
| 1488 | // TODO(quiche): support scanning for other technologies? |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 1489 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 1490 | "Unrecognized technology " + technology); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1494 | string Manager::GetTechnologyOrder() { |
| 1495 | vector<string> technology_names; |
| 1496 | for (vector<Technology::Identifier>::iterator it = technology_order_.begin(); |
| 1497 | it != technology_order_.end(); |
| 1498 | ++it) { |
| 1499 | technology_names.push_back(Technology::NameFromIdentifier(*it)); |
| 1500 | } |
| 1501 | |
| 1502 | return JoinString(technology_names, ','); |
| 1503 | } |
| 1504 | |
| 1505 | void Manager::SetTechnologyOrder(const string &order, Error *error) { |
| 1506 | vector<Technology::Identifier> new_order; |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1507 | SLOG(Manager, 2) << "Setting technology order to " << order; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1508 | if (!Technology::GetTechnologyVectorFromString(order, &new_order, error)) { |
| 1509 | return; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | technology_order_ = new_order; |
| 1513 | SortServices(); |
| 1514 | } |
| 1515 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1516 | } // namespace shill |