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