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