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