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