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 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 14 | #include <base/file_util.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 15 | #include <base/logging.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 16 | #include <base/memory/ref_counted.h> |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 17 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 18 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 19 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 20 | #include "shill/adaptor_interfaces.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 21 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 22 | #include "shill/control_interface.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 23 | #include "shill/dbus_adaptor.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 24 | #include "shill/default_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 25 | #include "shill/device.h" |
| 26 | #include "shill/device_info.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 27 | #include "shill/ephemeral_profile.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 28 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 29 | #include "shill/event_dispatcher.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 30 | #include "shill/key_file_store.h" |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 31 | #include "shill/metrics.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 32 | #include "shill/profile.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 33 | #include "shill/property_accessor.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 34 | #include "shill/resolver.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 35 | #include "shill/service.h" |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 36 | #include "shill/service_sorter.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 37 | #include "shill/wifi.h" |
| 38 | #include "shill/wifi_service.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 39 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 40 | using std::set; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 41 | using std::string; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 42 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 43 | |
| 44 | namespace shill { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 45 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame^] | 46 | // statics |
| 47 | const char Manager::kErrorNoDevice[] = "no wifi devices available"; |
| 48 | const char Manager::kErrorTypeRequired[] = "must specify service type"; |
| 49 | const char Manager::kErrorUnsupportedServiceType[] = |
| 50 | "service type is unsupported"; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 51 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 52 | Manager::Manager(ControlInterface *control_interface, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 53 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 54 | Metrics *metrics, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 55 | GLib *glib, |
| 56 | const string &run_directory, |
| 57 | const string &storage_directory, |
| 58 | const string &user_storage_format) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 59 | : dispatcher_(dispatcher), |
| 60 | task_factory_(this), |
| 61 | run_path_(FilePath(run_directory)), |
| 62 | storage_path_(FilePath(storage_directory)), |
| 63 | user_storage_format_(user_storage_format), |
| 64 | adaptor_(control_interface->CreateManagerAdaptor(this)), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 65 | device_info_(control_interface, dispatcher, metrics, this), |
| 66 | modem_info_(control_interface, dispatcher, metrics, this, glib), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 67 | running_(false), |
| 68 | connect_profiles_to_rpc_(true), |
| 69 | ephemeral_profile_(new EphemeralProfile(control_interface, this)), |
| 70 | control_interface_(control_interface), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 71 | metrics_(metrics), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 72 | glib_(glib) { |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 73 | HelpRegisterDerivedString(flimflam::kActiveProfileProperty, |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 74 | &Manager::GetActiveProfileRpcIdentifier, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 75 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 76 | HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty, |
| 77 | &Manager::AvailableTechnologies, |
| 78 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 79 | store_.RegisterString(flimflam::kCheckPortalListProperty, |
| 80 | &props_.check_portal_list); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 81 | HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty, |
| 82 | &Manager::ConnectedTechnologies, |
| 83 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 84 | store_.RegisterString(flimflam::kCountryProperty, &props_.country); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 85 | HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty, |
| 86 | &Manager::DefaultTechnology, |
| 87 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 88 | HelpRegisterDerivedStrings(flimflam::kDevicesProperty, |
| 89 | &Manager::EnumerateDevices, |
| 90 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 91 | HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty, |
| 92 | &Manager::EnabledTechnologies, |
| 93 | NULL); |
| 94 | store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode); |
| 95 | store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 96 | HelpRegisterDerivedStrings(flimflam::kProfilesProperty, |
| 97 | &Manager::EnumerateProfiles, |
| 98 | NULL); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 99 | store_.RegisterString(shill::kHostNameProperty, &props_.host_name); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 100 | HelpRegisterDerivedString(flimflam::kStateProperty, |
| 101 | &Manager::CalculateState, |
| 102 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 103 | HelpRegisterDerivedStrings(flimflam::kServicesProperty, |
| 104 | &Manager::EnumerateAvailableServices, |
| 105 | NULL); |
| 106 | HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty, |
| 107 | &Manager::EnumerateWatchedServices, |
| 108 | NULL); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 109 | |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 110 | // Set default technology order "by hand", to avoid invoking side |
| 111 | // effects of SetTechnologyOrder. |
| 112 | technology_order_.push_back( |
| 113 | Technology::IdentifierFromName(flimflam::kTypeEthernet)); |
| 114 | technology_order_.push_back( |
| 115 | Technology::IdentifierFromName(flimflam::kTypeWifi)); |
| 116 | technology_order_.push_back( |
| 117 | Technology::IdentifierFromName(flimflam::kTypeCellular)); |
| 118 | |
Chris Masone | b07006b | 2011-05-14 16:10:04 -0700 | [diff] [blame] | 119 | VLOG(2) << "Manager initialized."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 122 | Manager::~Manager() { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 123 | profiles_.clear(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 124 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 125 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 126 | void Manager::AddDeviceToBlackList(const string &device_name) { |
| 127 | device_info_.AddDeviceToBlackList(device_name); |
| 128 | } |
| 129 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 130 | void Manager::Start() { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 131 | LOG(INFO) << "Manager started."; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 132 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 133 | CHECK(file_util::CreateDirectory(run_path_)) << run_path_.value(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 134 | Resolver::GetInstance()->set_path(run_path_.Append("resolv.conf")); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 135 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 136 | InitializeProfiles(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 137 | running_ = true; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 138 | adaptor_->UpdateRunning(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 139 | device_info_.Start(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 140 | modem_info_.Start(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void Manager::Stop() { |
| 144 | running_ = false; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 145 | // Persist profile, device, service information to disk. |
| 146 | vector<ProfileRefPtr>::iterator it; |
| 147 | for (it = profiles_.begin(); it != profiles_.end(); ++it) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 148 | (*it)->Save(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 149 | } |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 150 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 151 | vector<ServiceRefPtr>::iterator services_it; |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 152 | Error e; |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 153 | for (services_it = services_.begin(); services_it != services_.end(); |
| 154 | ++services_it) { |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 155 | (*services_it)->Disconnect(&e); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 158 | adaptor_->UpdateRunning(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 159 | modem_info_.Stop(); |
| 160 | device_info_.Stop(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 163 | void Manager::InitializeProfiles() { |
| 164 | DCHECK(profiles_.empty()); |
| 165 | // The default profile must go first on the stack. |
| 166 | CHECK(file_util::CreateDirectory(storage_path_)) << storage_path_.value(); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 167 | scoped_refptr<DefaultProfile> |
| 168 | default_profile(new DefaultProfile(control_interface_, |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 169 | this, |
| 170 | storage_path_, |
| 171 | props_)); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 172 | CHECK(default_profile->InitStorage(glib_, Profile::kCreateOrOpenExisting, |
| 173 | NULL)); |
| 174 | CHECK(default_profile->LoadManagerProperties(&props_)); |
| 175 | profiles_.push_back(default_profile.release()); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 176 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 177 | string path; |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 178 | for (vector<string>::iterator it = startup_profiles_.begin(); |
| 179 | it != startup_profiles_.end(); ++it) |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 180 | PushProfile(*it, &path, &error); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 183 | void Manager::CreateProfile(const string &name, string *path, Error *error) { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 184 | Profile::Identifier ident; |
| 185 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 186 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 187 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | ProfileRefPtr profile(new Profile(control_interface_, |
| 191 | this, |
| 192 | ident, |
| 193 | user_storage_format_, |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 194 | connect_profiles_to_rpc_)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 195 | if (!profile->InitStorage(glib_, Profile::kCreateNew, error)) { |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 196 | // |error| will have been populated by InitStorage(). |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
| 200 | // Save profile data out, and then let the scoped pointer fall out of scope. |
| 201 | if (!profile->Save()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 202 | Error::PopulateAndLog(error, Error::kInternalError, |
| 203 | "Profile name " + name + " could not be saved"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 204 | return; |
| 205 | } |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 206 | |
| 207 | *path = profile->GetRpcIdentifier(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 210 | void Manager::PushProfile(const string &name, string *path, Error *error) { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 211 | Profile::Identifier ident; |
| 212 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 213 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 214 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
| 218 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 219 | it != profiles_.end(); |
| 220 | ++it) { |
| 221 | if ((*it)->MatchesIdentifier(ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 222 | Error::PopulateAndLog(error, Error::kAlreadyExists, |
| 223 | "Profile name " + name + " is already on stack"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (ident.user.empty()) { |
| 229 | // The manager will have only one machine-wide profile, and this is the |
| 230 | // DefaultProfile. This means no other profiles can be loaded that do |
| 231 | // not have a user component. |
| 232 | // TODO(pstew): This is all well and good, but WiFi autotests try to |
| 233 | // creating a default profile (by a name other than "default") in order |
| 234 | // to avoid leaving permanent side effects to devices under test. This |
| 235 | // whole thing will need to be reworked in order to allow that to happen, |
| 236 | // or the autotests (or their expectations) will need to change. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 237 | // crosbug.com/24461 |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 238 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 239 | "Cannot load non-default global profile " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
| 243 | ProfileRefPtr profile(new Profile(control_interface_, |
| 244 | this, |
| 245 | ident, |
| 246 | user_storage_format_, |
| 247 | connect_profiles_to_rpc_)); |
| 248 | if (!profile->InitStorage(glib_, Profile::kOpenExisting, error)) { |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 249 | // |error| will have been populated by InitStorage(). |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 253 | profiles_.push_back(profile); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 254 | |
| 255 | // Offer each registered Service the opportunity to join this new Profile. |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 256 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 257 | it != services_.end(); ++it) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 258 | profile->ConfigureService(*it); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 261 | // Shop the Profile contents around to Devices which can create |
| 262 | // non-visible services. |
| 263 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 264 | it != devices_.end(); ++it) { |
| 265 | profile->ConfigureDevice(*it); |
| 266 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 267 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 268 | *path = profile->GetRpcIdentifier(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 269 | SortServices(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void Manager::PopProfileInternal() { |
| 273 | CHECK(!profiles_.empty()); |
| 274 | ProfileRefPtr active_profile = profiles_.back(); |
| 275 | profiles_.pop_back(); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 276 | vector<ServiceRefPtr>::iterator it; |
| 277 | for (it = services_.begin(); it != services_.end(); ++it) { |
| 278 | if ((*it)->profile().get() == active_profile.get() && |
| 279 | !MatchProfileWithService(*it)) { |
| 280 | (*it)->Unload(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 283 | SortServices(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 286 | void Manager::PopProfile(const string &name, Error *error) { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 287 | Profile::Identifier ident; |
| 288 | if (profiles_.empty()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 289 | Error::PopulateAndLog(error, Error::kNotFound, "Profile stack is empty"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | ProfileRefPtr active_profile = profiles_.back(); |
| 293 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 294 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 295 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | if (!active_profile->MatchesIdentifier(ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 299 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 300 | name + " is not the active profile"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | PopProfileInternal(); |
| 304 | } |
| 305 | |
| 306 | void Manager::PopAnyProfile(Error *error) { |
| 307 | Profile::Identifier ident; |
| 308 | if (profiles_.empty()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 309 | Error::PopulateAndLog(error, Error::kNotFound, "Profile stack is empty"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | PopProfileInternal(); |
| 313 | } |
| 314 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 315 | bool Manager::HandleProfileEntryDeletion(const ProfileRefPtr &profile, |
| 316 | const std::string &entry_name) { |
| 317 | bool moved_services = false; |
| 318 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 319 | it != services_.end(); ++it) { |
| 320 | if ((*it)->profile().get() == profile.get() && |
| 321 | (*it)->GetStorageIdentifier() == entry_name) { |
| 322 | profile->AbandonService(*it); |
| 323 | if (!MatchProfileWithService(*it)) { |
| 324 | (*it)->Unload(); |
| 325 | } |
| 326 | moved_services = true; |
| 327 | } |
| 328 | } |
| 329 | return moved_services; |
| 330 | } |
| 331 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 332 | ServiceRefPtr Manager::GetServiceWithStorageIdentifier( |
| 333 | const ProfileRefPtr &profile, const std::string &entry_name, Error *error) { |
| 334 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 335 | it != services_.end(); ++it) { |
| 336 | if ((*it)->profile().get() == profile.get() && |
| 337 | (*it)->GetStorageIdentifier() == entry_name) { |
| 338 | return *it; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | Error::PopulateAndLog(error, Error::kNotFound, |
| 343 | base::StringPrintf("Entry %s is not registered in the manager", |
| 344 | entry_name.c_str())); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 348 | bool Manager::IsPortalDetectionEnabled(Technology::Identifier tech) { |
| 349 | Error error; |
| 350 | vector<Technology::Identifier> portal_technologies; |
| 351 | return Technology::GetTechnologyVectorFromString(props_.check_portal_list, |
| 352 | &portal_technologies, |
| 353 | &error) && |
| 354 | std::find(portal_technologies.begin(), portal_technologies.end(), |
| 355 | tech) != portal_technologies.end(); |
| 356 | } |
| 357 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 358 | const ProfileRefPtr &Manager::ActiveProfile() const { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 359 | DCHECK_NE(profiles_.size(), 0); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 360 | return profiles_.back(); |
| 361 | } |
| 362 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 363 | bool Manager::IsActiveProfile(const ProfileRefPtr &profile) const { |
| 364 | return (profiles_.size() > 0 && |
| 365 | ActiveProfile().get() == profile.get()); |
| 366 | } |
| 367 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 368 | bool Manager::MoveServiceToProfile(const ServiceRefPtr &to_move, |
| 369 | const ProfileRefPtr &destination) { |
| 370 | const ProfileRefPtr from = to_move->profile(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 371 | VLOG(2) << "Moving service " |
| 372 | << to_move->UniqueName() |
| 373 | << " to profile " |
| 374 | << destination->GetFriendlyName() |
| 375 | << " from " |
| 376 | << from->GetFriendlyName(); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 377 | return destination->AdoptService(to_move) && |
| 378 | from->AbandonService(to_move); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 381 | void Manager::SetProfileForService(const ServiceRefPtr &to_set, |
| 382 | const string &profile_rpcid, |
| 383 | Error *error) { |
| 384 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 385 | it != profiles_.end(); |
| 386 | ++it) { |
| 387 | if (profile_rpcid == (*it)->GetRpcIdentifier()) { |
| 388 | if (to_set->profile().get() == it->get()) { |
| 389 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 390 | "Service is already connected to this profile"); |
| 391 | } else if (!MoveServiceToProfile(to_set, *it)) { |
| 392 | Error::PopulateAndLog(error, Error::kInternalError, |
| 393 | "Unable to move service to profile"); |
| 394 | } |
| 395 | return; |
| 396 | } |
| 397 | } |
| 398 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 399 | "Unknown Profile requested for Service"); |
| 400 | } |
| 401 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 402 | void Manager::RegisterDevice(const DeviceRefPtr &to_manage) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 403 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 404 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 405 | if (to_manage.get() == it->get()) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 406 | return; |
| 407 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 408 | devices_.push_back(to_manage); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 409 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 410 | // We are applying device properties from the DefaultProfile, and adding |
| 411 | // the union of hidden services in all loaded profiles to the device. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 412 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 413 | it != profiles_.end(); |
| 414 | ++it) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 415 | // Load device configuration, if any exists, as well as hidden services. |
| 416 | (*it)->ConfigureDevice(to_manage); |
| 417 | |
| 418 | // Currently the only profile for which "Save" is implemented is the |
| 419 | // DefaultProfile. It iterates over all Devices and stores their state. |
| 420 | // We perform the Save now in case the device we have just registered |
| 421 | // is new and needs to be added to the stored DefaultProfile. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 422 | (*it)->Save(); |
| 423 | } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 424 | |
| 425 | // In normal usage, running_ will always be true when we are here, however |
| 426 | // unit tests sometimes do things in otherwise invalid states. |
| 427 | if (running_ && to_manage->powered()) |
| 428 | to_manage->Start(); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 429 | |
| 430 | Error error; |
| 431 | adaptor_->EmitStringsChanged(flimflam::kAvailableTechnologiesProperty, |
| 432 | AvailableTechnologies(&error)); |
| 433 | adaptor_->EmitStringsChanged(flimflam::kEnabledTechnologiesProperty, |
| 434 | EnabledTechnologies(&error)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 435 | } |
| 436 | |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 437 | void Manager::DeregisterDevice(const DeviceRefPtr &to_forget) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 438 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 439 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 440 | if (to_forget.get() == it->get()) { |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 441 | VLOG(2) << "Deregistered device: " << to_forget->UniqueName(); |
| 442 | to_forget->Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 443 | devices_.erase(it); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 444 | Error error; |
| 445 | adaptor_->EmitStringsChanged(flimflam::kAvailableTechnologiesProperty, |
| 446 | AvailableTechnologies(&error)); |
| 447 | adaptor_->EmitStringsChanged(flimflam::kEnabledTechnologiesProperty, |
| 448 | EnabledTechnologies(&error)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 449 | return; |
| 450 | } |
| 451 | } |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 452 | VLOG(2) << __func__ << " unknown device: " << to_forget->UniqueName(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 453 | } |
| 454 | |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 455 | bool Manager::HasService(const ServiceRefPtr &service) { |
| 456 | vector<ServiceRefPtr>::iterator it; |
| 457 | for (it = services_.begin(); it != services_.end(); ++it) { |
| 458 | if ((*it)->UniqueName() == service->UniqueName()) |
| 459 | return true; |
| 460 | } |
| 461 | return false; |
| 462 | } |
| 463 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 464 | void Manager::RegisterService(const ServiceRefPtr &to_manage) { |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 465 | VLOG(2) << "In " << __func__ << "(): Registering service " |
| 466 | << to_manage->UniqueName(); |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 467 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 468 | MatchProfileWithService(to_manage); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 469 | |
| 470 | // Now add to OUR list. |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 471 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 472 | for (it = services_.begin(); it != services_.end(); ++it) { |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 473 | CHECK(to_manage->UniqueName() != (*it)->UniqueName()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 474 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 475 | services_.push_back(to_manage); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 476 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 479 | void Manager::DeregisterService(const ServiceRefPtr &to_forget) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 480 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 481 | for (it = services_.begin(); it != services_.end(); ++it) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 482 | if (to_forget->UniqueName() == (*it)->UniqueName()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 483 | DCHECK(!(*it)->connection()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 484 | services_.erase(it); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 485 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 486 | return; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 491 | void Manager::UpdateService(const ServiceRefPtr &to_update) { |
| 492 | CHECK(to_update); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 493 | LOG(INFO) << "Service " << to_update->UniqueName() << " updated;" |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 494 | << " state: " << Service::ConnectStateToString(to_update->state()) |
| 495 | << " failure: " |
| 496 | << Service::ConnectFailureToString(to_update->failure()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 497 | VLOG(2) << "IsConnected(): " << to_update->IsConnected(); |
| 498 | VLOG(2) << "IsConnecting(): " << to_update->IsConnecting(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 499 | if (to_update->IsConnected()) { |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 500 | bool originally_favorite = to_update->favorite(); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 501 | to_update->MakeFavorite(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 502 | if (to_update->profile().get() == ephemeral_profile_.get()) { |
| 503 | if (profiles_.empty()) { |
| 504 | LOG(ERROR) << "Cannot assign profile to service: no profiles exist!"; |
| 505 | } else { |
| 506 | MoveServiceToProfile(to_update, profiles_.back()); |
| 507 | } |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 508 | } else if (!originally_favorite) { |
| 509 | // Persists the updated favorite setting in the profile. |
| 510 | to_update->SaveToCurrentProfile(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 511 | } |
| 512 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 513 | SortServices(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 516 | void Manager::FilterByTechnology(Technology::Identifier tech, |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 517 | vector<DeviceRefPtr> *found) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 518 | CHECK(found); |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 519 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 520 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 521 | if ((*it)->TechnologyIs(tech)) |
| 522 | found->push_back(*it); |
| 523 | } |
| 524 | } |
| 525 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 526 | ServiceRefPtr Manager::FindService(const string& name) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 527 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 528 | for (it = services_.begin(); it != services_.end(); ++it) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 529 | if (name == (*it)->UniqueName()) |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 530 | return *it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 531 | } |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 532 | return NULL; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 533 | } |
| 534 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 535 | void Manager::HelpRegisterDerivedString( |
| 536 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 537 | string(Manager::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 538 | void(Manager::*set)(const string&, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 539 | store_.RegisterDerivedString( |
| 540 | name, |
| 541 | StringAccessor(new CustomAccessor<Manager, string>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 542 | } |
| 543 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 544 | void Manager::HelpRegisterDerivedStrings( |
| 545 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 546 | Strings(Manager::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 547 | void(Manager::*set)(const Strings &, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 548 | store_.RegisterDerivedStrings( |
| 549 | name, |
| 550 | StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 553 | void Manager::SortServices() { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 554 | VLOG(4) << "In " << __func__; |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 555 | ServiceRefPtr default_service; |
| 556 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 557 | if (!services_.empty()) { |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 558 | // Keep track of the service that was last considered default. |
| 559 | default_service = services_[0]; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 560 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 561 | sort(services_.begin(), services_.end(), ServiceSorter(technology_order_)); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 562 | |
| 563 | vector<string> service_paths; |
| 564 | vector<ServiceRefPtr>::iterator it; |
| 565 | for (it = services_.begin(); it != services_.end(); ++it) { |
| 566 | if ((*it)->IsVisible()) { |
| 567 | service_paths.push_back((*it)->GetRpcIdentifier()); |
| 568 | } |
| 569 | } |
| 570 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, |
| 571 | service_paths); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 572 | |
| 573 | Error error; |
| 574 | adaptor_->EmitStringsChanged(flimflam::kConnectedTechnologiesProperty, |
| 575 | ConnectedTechnologies(&error)); |
| 576 | adaptor_->EmitStringChanged(flimflam::kDefaultTechnologyProperty, |
| 577 | DefaultTechnology(&error)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 578 | |
| 579 | if (!services_.empty()) { |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 580 | ConnectionRefPtr default_connection = default_service->connection(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 581 | if (default_connection.get() && |
| 582 | (services_[0]->connection().get() != default_connection.get())) { |
| 583 | default_connection->SetIsDefault(false); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 584 | default_service = NULL; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 585 | } |
| 586 | if (services_[0]->connection().get()) { |
| 587 | services_[0]->connection()->SetIsDefault(true); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 588 | default_service = services_[0]; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 589 | } |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 590 | metrics_->NotifyDefaultServiceChanged(default_service); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 591 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 592 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 593 | AutoConnect(); |
| 594 | } |
| 595 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 596 | bool Manager::MatchProfileWithService(const ServiceRefPtr &service) { |
| 597 | vector<ProfileRefPtr>::reverse_iterator it; |
| 598 | for (it = profiles_.rbegin(); it != profiles_.rend(); ++it) { |
| 599 | if ((*it)->ConfigureService(service)) { |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | if (it == profiles_.rend()) { |
| 604 | ephemeral_profile_->AdoptService(service); |
| 605 | return false; |
| 606 | } |
| 607 | return true; |
| 608 | } |
| 609 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 610 | void Manager::AutoConnect() { |
| 611 | // We might be called in the middle of another request (e.g., as a |
| 612 | // consequence of Service::SetState calling UpdateService). To avoid |
| 613 | // re-entrancy issues in dbus-c++, defer to the event loop. |
| 614 | dispatcher_->PostTask( |
| 615 | task_factory_.NewRunnableMethod(&Manager::AutoConnectTask)); |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | void Manager::AutoConnectTask() { |
| 620 | if (services_.empty()) { |
| 621 | LOG(INFO) << "No services."; |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | if (VLOG_IS_ON(4)) { |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 626 | VLOG(4) << "Sorted service list: "; |
| 627 | for (size_t i = 0; i < services_.size(); ++i) { |
| 628 | ServiceRefPtr service = services_[i]; |
| 629 | const char *compare_reason = NULL; |
| 630 | if (i + 1 < services_.size()) { |
| 631 | Service::Compare( |
| 632 | service, services_[i+1], technology_order_, &compare_reason); |
| 633 | } else { |
| 634 | compare_reason = ""; |
| 635 | } |
| 636 | VLOG(4) << "Service " << service->friendly_name() |
| 637 | << " IsConnected: " << service->IsConnected() |
| 638 | << " IsConnecting: " << service->IsConnecting() |
| 639 | << " IsFailed: " << service->IsFailed() |
| 640 | << " connectable: " << service->connectable() |
| 641 | << " auto_connect: " << service->auto_connect() |
| 642 | << " favorite: " << service->favorite() |
| 643 | << " priority: " << service->priority() |
| 644 | << " security_level: " << service->security_level() |
| 645 | << " strength: " << service->strength() |
| 646 | << " UniqueName: " << service->UniqueName() |
| 647 | << " " << compare_reason; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 651 | // Perform auto-connect. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 652 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 653 | it != services_.end(); ++it) { |
| 654 | if ((*it)->auto_connect()) { |
mukesh agrawal | 592516d | 2012-01-12 14:01:00 -0800 | [diff] [blame] | 655 | LOG(INFO) << "Requesting autoconnect to service " |
| 656 | << (*it)->friendly_name() << "."; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 657 | (*it)->AutoConnect(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 658 | } |
| 659 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 662 | string Manager::CalculateState(Error */*error*/) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 663 | return flimflam::kStateOffline; |
| 664 | } |
| 665 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 666 | vector<string> Manager::AvailableTechnologies(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 667 | set<string> unique_technologies; |
| 668 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 669 | it != devices_.end(); ++it) { |
| 670 | unique_technologies.insert( |
| 671 | Technology::NameFromIdentifier((*it)->technology())); |
| 672 | } |
| 673 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 676 | vector<string> Manager::ConnectedTechnologies(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 677 | set<string> unique_technologies; |
| 678 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 679 | it != devices_.end(); ++it) { |
| 680 | if ((*it)->IsConnected()) |
| 681 | unique_technologies.insert( |
| 682 | Technology::NameFromIdentifier((*it)->technology())); |
| 683 | } |
| 684 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 687 | string Manager::DefaultTechnology(Error *error) { |
| 688 | return (!services_.empty() && services_[0]->IsConnected()) ? |
| 689 | services_[0]->GetTechnologyString(error) : ""; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 692 | vector<string> Manager::EnabledTechnologies(Error *error) { |
| 693 | // TODO(gauravsh): This must be wired up to the RPC interface to handle |
| 694 | // enabled/disabled devices as set by the user. crosbug.com/23319 |
| 695 | return AvailableTechnologies(error); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 698 | vector<string> Manager::EnumerateDevices(Error */*error*/) { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 699 | vector<string> device_rpc_ids; |
| 700 | for (vector<DeviceRefPtr>::const_iterator it = devices_.begin(); |
| 701 | it != devices_.end(); |
| 702 | ++it) { |
| 703 | device_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 704 | } |
| 705 | return device_rpc_ids; |
| 706 | } |
| 707 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 708 | vector<string> Manager::EnumerateProfiles(Error */*error*/) { |
| 709 | vector<string> profile_rpc_ids; |
| 710 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 711 | it != profiles_.end(); |
| 712 | ++it) { |
| 713 | profile_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 714 | } |
| 715 | return profile_rpc_ids; |
| 716 | } |
| 717 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 718 | vector<string> Manager::EnumerateAvailableServices(Error */*error*/) { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 719 | vector<string> service_rpc_ids; |
| 720 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 721 | it != services_.end(); |
| 722 | ++it) { |
| 723 | service_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 724 | } |
| 725 | return service_rpc_ids; |
| 726 | } |
| 727 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 728 | vector<string> Manager::EnumerateWatchedServices(Error *error) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 729 | // TODO(cmasone): Filter this list for services in appropriate states. |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 730 | return EnumerateAvailableServices(error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 733 | string Manager::GetActiveProfileRpcIdentifier(Error */*error*/) { |
| 734 | return ActiveProfile()->GetRpcIdentifier(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 735 | } |
| 736 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 737 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame^] | 738 | ServiceRefPtr Manager::GetService(const KeyValueStore &args, Error *error) { |
| 739 | if (!args.ContainsString(flimflam::kTypeProperty)) { |
| 740 | error->Populate(Error::kInvalidArguments, kErrorTypeRequired); |
| 741 | return NULL; |
| 742 | } |
| 743 | |
| 744 | string type = args.GetString(flimflam::kTypeProperty); |
| 745 | if (type == flimflam::kTypeWifi) { |
| 746 | return GetWifiService(args, error); |
| 747 | } |
| 748 | if (type == flimflam::kTypeVPN) { |
| 749 | return GetVPNService(args, error); |
| 750 | } |
| 751 | error->Populate(Error::kNotSupported, kErrorUnsupportedServiceType); |
| 752 | return NULL; |
| 753 | } |
| 754 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 755 | WiFiServiceRefPtr Manager::GetWifiService(const KeyValueStore &args, |
| 756 | Error *error) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 757 | vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 758 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 759 | if (wifi_devices.empty()) { |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame^] | 760 | error->Populate(Error::kInvalidArguments, kErrorNoDevice); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 761 | return NULL; |
| 762 | } else { |
| 763 | WiFi *wifi = dynamic_cast<WiFi *>(wifi_devices.front().get()); |
| 764 | CHECK(wifi); |
| 765 | return wifi->GetService(args, error); |
| 766 | } |
| 767 | } |
| 768 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame^] | 769 | ServiceRefPtr Manager::GetVPNService(const KeyValueStore &/*args*/, |
| 770 | Error *error) { |
| 771 | // TODO(petkov): Implement this. |
| 772 | NOTIMPLEMENTED(); |
| 773 | error->Populate(Error::kNotSupported, kErrorUnsupportedServiceType); |
| 774 | return NULL; |
| 775 | } |
| 776 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 777 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 778 | void Manager::RequestScan(const string &technology, Error *error) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 779 | if (technology == flimflam::kTypeWifi || technology == "") { |
| 780 | vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 781 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 782 | |
| 783 | for (vector<DeviceRefPtr>::iterator it = wifi_devices.begin(); |
| 784 | it != wifi_devices.end(); |
| 785 | ++it) { |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 786 | (*it)->Scan(error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 787 | } |
| 788 | } else { |
| 789 | // TODO(quiche): support scanning for other technologies? |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 790 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 791 | "Unrecognized technology " + technology); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 795 | string Manager::GetTechnologyOrder() { |
| 796 | vector<string> technology_names; |
| 797 | for (vector<Technology::Identifier>::iterator it = technology_order_.begin(); |
| 798 | it != technology_order_.end(); |
| 799 | ++it) { |
| 800 | technology_names.push_back(Technology::NameFromIdentifier(*it)); |
| 801 | } |
| 802 | |
| 803 | return JoinString(technology_names, ','); |
| 804 | } |
| 805 | |
| 806 | void Manager::SetTechnologyOrder(const string &order, Error *error) { |
| 807 | vector<Technology::Identifier> new_order; |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 808 | VLOG(2) << "Setting technology order to " << order; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 809 | if (!Technology::GetTechnologyVectorFromString(order, &new_order, error)) { |
| 810 | return; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | technology_order_ = new_order; |
| 814 | SortServices(); |
| 815 | } |
| 816 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 817 | } // namespace shill |