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