Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -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 | |
| 5 | #include "shill/default_profile.h" |
| 6 | |
Alex Vakulenko | 37c5d94 | 2014-06-17 18:17:43 -0700 | [diff] [blame] | 7 | #include <random> |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 10 | #include <base/files/file_path.h> |
| 11 | #include <base/strings/string_number_conversions.h> |
| 12 | #include <base/strings/stringprintf.h> |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
| 14 | |
| 15 | #include "shill/adaptor_interfaces.h" |
Rebecca Silberstein | 3d49ea4 | 2014-08-21 11:20:50 -0700 | [diff] [blame] | 16 | #include "shill/connectivity_trial.h" |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 17 | #include "shill/control_interface.h" |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 18 | #include "shill/link_monitor.h" |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 19 | #include "shill/manager.h" |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 20 | #include "shill/portal_detector.h" |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 21 | #include "shill/resolver.h" |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 22 | #include "shill/store_interface.h" |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 23 | |
| 24 | #if !defined(DISABLE_WIFI) |
Ben Chan | d6a8b51 | 2014-11-18 10:45:15 -0800 | [diff] [blame] | 25 | #include "shill/wifi/wifi_provider.h" |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 26 | #endif // DISABLE_WIFI |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 27 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 28 | using base::FilePath; |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 29 | using std::string; |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 30 | using std::vector; |
| 31 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 32 | namespace shill { |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 33 | // static |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 34 | const char DefaultProfile::kDefaultId[] = "default"; |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 35 | // static |
| 36 | const char DefaultProfile::kStorageId[] = "global"; |
| 37 | // static |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 38 | const char DefaultProfile::kStorageArpGateway[] = "ArpGateway"; |
| 39 | // static |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 40 | const char DefaultProfile::kStorageCheckPortalList[] = "CheckPortalList"; |
| 41 | // static |
Paul Stewart | ff6be29 | 2014-11-24 17:05:03 -0800 | [diff] [blame] | 42 | const char DefaultProfile::kStorageConnectionIdSalt[] = "ConnectionIdSalt"; |
| 43 | // static |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 44 | const char DefaultProfile::kStorageHostName[] = "HostName"; |
| 45 | // static |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 46 | const char DefaultProfile::kStorageIgnoredDNSSearchPaths[] = |
| 47 | "IgnoredDNSSearchPaths"; |
| 48 | // static |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 49 | const char DefaultProfile::kStorageLinkMonitorTechnologies[] = |
| 50 | "LinkMonitorTechnologies"; |
| 51 | // static |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 52 | const char DefaultProfile::kStorageName[] = "Name"; |
| 53 | // static |
Ben Chan | 8e6b8ef | 2014-07-14 21:50:18 -0700 | [diff] [blame] | 54 | const char DefaultProfile::kStorageNoAutoConnectTechnologies[] = |
| 55 | "NoAutoConnectTechnologies"; |
| 56 | // static |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 57 | const char DefaultProfile::kStorageOfflineMode[] = "OfflineMode"; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 58 | // static |
| 59 | const char DefaultProfile::kStoragePortalURL[] = "PortalURL"; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 60 | // static |
| 61 | const char DefaultProfile::kStoragePortalCheckInterval[] = |
| 62 | "PortalCheckInterval"; |
Peter Qiu | 9d58193 | 2014-04-14 16:37:37 -0700 | [diff] [blame] | 63 | // static |
Paul Stewart | ff6be29 | 2014-11-24 17:05:03 -0800 | [diff] [blame] | 64 | const char DefaultProfile::kStorageProhibitedTechnologies[] = |
| 65 | "ProhibitedTechnologies"; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 66 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 67 | DefaultProfile::DefaultProfile(ControlInterface* control, |
| 68 | Metrics* metrics, |
| 69 | Manager* manager, |
| 70 | const FilePath& storage_path, |
| 71 | const string& profile_id, |
| 72 | const Manager::Properties& manager_props) |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 73 | : Profile(control, metrics, manager, Identifier(profile_id), "", true), |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 74 | storage_path_(storage_path), |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 75 | profile_id_(profile_id), |
Alex Vakulenko | 37c5d94 | 2014-06-17 18:17:43 -0700 | [diff] [blame] | 76 | props_(manager_props), |
| 77 | random_engine_(time(nullptr)) { |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 78 | PropertyStore* store = this->mutable_store(); |
Ben Chan | 5dc5881 | 2013-09-20 12:53:25 -0700 | [diff] [blame] | 79 | store->RegisterConstBool(kArpGatewayProperty, &manager_props.arp_gateway); |
| 80 | store->RegisterConstString(kCheckPortalListProperty, |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 81 | &manager_props.check_portal_list); |
Ben Chan | 5dc5881 | 2013-09-20 12:53:25 -0700 | [diff] [blame] | 82 | store->RegisterConstString(kCountryProperty, &manager_props.country); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 83 | store->RegisterConstString(kIgnoredDNSSearchPathsProperty, |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 84 | &manager_props.ignored_dns_search_paths); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 85 | store->RegisterConstString(kLinkMonitorTechnologiesProperty, |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 86 | &manager_props.link_monitor_technologies); |
Ben Chan | 8e6b8ef | 2014-07-14 21:50:18 -0700 | [diff] [blame] | 87 | store->RegisterConstString(kNoAutoConnectTechnologiesProperty, |
| 88 | &manager_props.no_auto_connect_technologies); |
Ben Chan | 5dc5881 | 2013-09-20 12:53:25 -0700 | [diff] [blame] | 89 | store->RegisterConstBool(kOfflineModeProperty, &manager_props.offline_mode); |
| 90 | store->RegisterConstString(kPortalURLProperty, &manager_props.portal_url); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 91 | store->RegisterConstInt32(kPortalCheckIntervalProperty, |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 92 | &manager_props.portal_check_interval_seconds); |
Paul Stewart | ff6be29 | 2014-11-24 17:05:03 -0800 | [diff] [blame] | 93 | store->RegisterConstString(kProhibitedTechnologiesProperty, |
| 94 | &manager_props.prohibited_technologies); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | DefaultProfile::~DefaultProfile() {} |
| 98 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 99 | void DefaultProfile::LoadManagerProperties(Manager::Properties* manager_props) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 100 | storage()->GetBool(kStorageId, kStorageArpGateway, |
| 101 | &manager_props->arp_gateway); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 102 | storage()->GetString(kStorageId, kStorageHostName, &manager_props->host_name); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 103 | storage()->GetBool(kStorageId, kStorageOfflineMode, |
| 104 | &manager_props->offline_mode); |
Paul Stewart | f555cf8 | 2012-03-15 14:42:43 -0700 | [diff] [blame] | 105 | if (!storage()->GetString(kStorageId, |
| 106 | kStorageCheckPortalList, |
| 107 | &manager_props->check_portal_list)) { |
| 108 | manager_props->check_portal_list = PortalDetector::kDefaultCheckPortalList; |
| 109 | } |
Paul Stewart | ff6be29 | 2014-11-24 17:05:03 -0800 | [diff] [blame] | 110 | if (!storage()->GetInt(kStorageId, kStorageConnectionIdSalt, |
| 111 | &manager_props->connection_id_salt)) { |
| 112 | manager_props->connection_id_salt = |
| 113 | std::uniform_int_distribution<int>()(random_engine_); |
| 114 | } |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 115 | if (!storage()->GetString(kStorageId, |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 116 | kStorageIgnoredDNSSearchPaths, |
| 117 | &manager_props->ignored_dns_search_paths)) { |
| 118 | manager_props->ignored_dns_search_paths = |
| 119 | Resolver::kDefaultIgnoredSearchList; |
| 120 | } |
| 121 | if (!storage()->GetString(kStorageId, |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 122 | kStorageLinkMonitorTechnologies, |
| 123 | &manager_props->link_monitor_technologies)) { |
| 124 | manager_props->link_monitor_technologies = |
| 125 | LinkMonitor::kDefaultLinkMonitorTechnologies; |
| 126 | } |
Ben Chan | 8e6b8ef | 2014-07-14 21:50:18 -0700 | [diff] [blame] | 127 | if (!storage()->GetString(kStorageId, |
| 128 | kStorageNoAutoConnectTechnologies, |
| 129 | &manager_props->no_auto_connect_technologies)) { |
| 130 | manager_props->no_auto_connect_technologies = ""; |
| 131 | } |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 132 | if (!storage()->GetString(kStorageId, kStoragePortalURL, |
| 133 | &manager_props->portal_url)) { |
Rebecca Silberstein | 3d49ea4 | 2014-08-21 11:20:50 -0700 | [diff] [blame] | 134 | manager_props->portal_url = ConnectivityTrial::kDefaultURL; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 135 | } |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 136 | std::string check_interval; |
| 137 | if (!storage()->GetString(kStorageId, kStoragePortalCheckInterval, |
| 138 | &check_interval) || |
| 139 | !base::StringToInt(check_interval, |
| 140 | &manager_props->portal_check_interval_seconds)) { |
| 141 | manager_props->portal_check_interval_seconds = |
| 142 | PortalDetector::kDefaultCheckIntervalSeconds; |
| 143 | } |
Paul Stewart | ff6be29 | 2014-11-24 17:05:03 -0800 | [diff] [blame] | 144 | if (!storage()->GetString(kStorageId, |
| 145 | kStorageProhibitedTechnologies, |
| 146 | &manager_props->prohibited_technologies)) { |
| 147 | manager_props->prohibited_technologies = ""; |
Peter Qiu | 9d58193 | 2014-04-14 16:37:37 -0700 | [diff] [blame] | 148 | } |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 151 | bool DefaultProfile::ConfigureService(const ServiceRefPtr& service) { |
Paul Stewart | f284a23 | 2012-05-01 10:24:37 -0700 | [diff] [blame] | 152 | if (Profile::ConfigureService(service)) { |
| 153 | return true; |
| 154 | } |
| 155 | if (service->technology() == Technology::kEthernet) { |
| 156 | // Ethernet services should have an affinity towards the default profile, |
| 157 | // so even if a new Ethernet service has no known configuration, accept |
| 158 | // it anyway. |
| 159 | UpdateService(service); |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 160 | service->SetProfile(this); |
Paul Stewart | f284a23 | 2012-05-01 10:24:37 -0700 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 166 | bool DefaultProfile::Save() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 167 | storage()->SetBool(kStorageId, kStorageArpGateway, props_.arp_gateway); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 168 | storage()->SetString(kStorageId, kStorageHostName, props_.host_name); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 169 | storage()->SetString(kStorageId, kStorageName, GetFriendlyName()); |
| 170 | storage()->SetBool(kStorageId, kStorageOfflineMode, props_.offline_mode); |
| 171 | storage()->SetString(kStorageId, |
| 172 | kStorageCheckPortalList, |
| 173 | props_.check_portal_list); |
Paul Stewart | ff6be29 | 2014-11-24 17:05:03 -0800 | [diff] [blame] | 174 | storage()->SetInt(kStorageId, kStorageConnectionIdSalt, |
| 175 | props_.connection_id_salt); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 176 | storage()->SetString(kStorageId, |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 177 | kStorageIgnoredDNSSearchPaths, |
| 178 | props_.ignored_dns_search_paths); |
| 179 | storage()->SetString(kStorageId, |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 180 | kStorageLinkMonitorTechnologies, |
| 181 | props_.link_monitor_technologies); |
| 182 | storage()->SetString(kStorageId, |
Ben Chan | 8e6b8ef | 2014-07-14 21:50:18 -0700 | [diff] [blame] | 183 | kStorageNoAutoConnectTechnologies, |
| 184 | props_.no_auto_connect_technologies); |
| 185 | storage()->SetString(kStorageId, |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 186 | kStoragePortalURL, |
| 187 | props_.portal_url); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 188 | storage()->SetString(kStorageId, |
| 189 | kStoragePortalCheckInterval, |
| 190 | base::IntToString(props_.portal_check_interval_seconds)); |
Paul Stewart | ff6be29 | 2014-11-24 17:05:03 -0800 | [diff] [blame] | 191 | storage()->SetString(kStorageId, |
| 192 | kStorageProhibitedTechnologies, |
| 193 | props_.prohibited_technologies); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 194 | return Profile::Save(); |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 197 | bool DefaultProfile::UpdateDevice(const DeviceRefPtr& device) { |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 198 | return device->Save(storage()) && storage()->Flush(); |
| 199 | } |
| 200 | |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 201 | #if !defined(DISABLE_WIFI) |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 202 | bool DefaultProfile::UpdateWiFiProvider(const WiFiProvider& wifi_provider) { |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 203 | return wifi_provider.Save(storage()) && storage()->Flush(); |
| 204 | } |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 205 | #endif // DISABLE_WIFI |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 206 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 207 | bool DefaultProfile::GetStoragePath(FilePath* path) { |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 208 | *path = storage_path_.Append(base::StringPrintf("%s.profile", |
| 209 | profile_id_.c_str())); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 213 | } // namespace shill |