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