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 | |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 9 | #include <base/file_path.h> |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 10 | #include <base/string_number_conversions.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 11 | #include <base/stringprintf.h> |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | |
| 14 | #include "shill/adaptor_interfaces.h" |
| 15 | #include "shill/control_interface.h" |
| 16 | #include "shill/manager.h" |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 17 | #include "shill/portal_detector.h" |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 18 | #include "shill/store_interface.h" |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 19 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 20 | using std::string; |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 21 | using std::vector; |
| 22 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 23 | namespace shill { |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 24 | // static |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 25 | const char DefaultProfile::kDefaultId[] = "default"; |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 26 | // static |
| 27 | const char DefaultProfile::kStorageId[] = "global"; |
| 28 | // static |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 29 | const char DefaultProfile::kStorageArpGateway[] = "ArpGateway"; |
| 30 | // static |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 31 | const char DefaultProfile::kStorageCheckPortalList[] = "CheckPortalList"; |
| 32 | // static |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 33 | const char DefaultProfile::kStorageHostName[] = "HostName"; |
| 34 | // static |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 35 | const char DefaultProfile::kStorageName[] = "Name"; |
| 36 | // static |
| 37 | const char DefaultProfile::kStorageOfflineMode[] = "OfflineMode"; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 38 | // static |
| 39 | const char DefaultProfile::kStoragePortalURL[] = "PortalURL"; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 40 | // static |
| 41 | const char DefaultProfile::kStoragePortalCheckInterval[] = |
| 42 | "PortalCheckInterval"; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 43 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 44 | DefaultProfile::DefaultProfile(ControlInterface *control, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 45 | Manager *manager, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 46 | const FilePath &storage_path, |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 47 | const string &profile_id, |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 48 | const Manager::Properties &manager_props) |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 49 | : Profile(control, manager, Identifier(profile_id), "", true), |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 50 | storage_path_(storage_path), |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 51 | profile_id_(profile_id), |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 52 | props_(manager_props) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 53 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 54 | store->RegisterConstBool(flimflam::kArpGatewayProperty, |
| 55 | &manager_props.arp_gateway); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 56 | store->RegisterConstString(flimflam::kCheckPortalListProperty, |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 57 | &manager_props.check_portal_list); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 58 | store->RegisterConstString(flimflam::kCountryProperty, |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 59 | &manager_props.country); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 60 | store->RegisterConstBool(flimflam::kOfflineModeProperty, |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 61 | &manager_props.offline_mode); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 62 | store->RegisterConstString(flimflam::kPortalURLProperty, |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 63 | &manager_props.portal_url); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 64 | store->RegisterConstInt32(shill::kPortalCheckIntervalProperty, |
| 65 | &manager_props.portal_check_interval_seconds); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | DefaultProfile::~DefaultProfile() {} |
| 69 | |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 70 | bool DefaultProfile::LoadManagerProperties(Manager::Properties *manager_props) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 71 | storage()->GetBool(kStorageId, kStorageArpGateway, |
| 72 | &manager_props->arp_gateway); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 73 | storage()->GetString(kStorageId, kStorageHostName, &manager_props->host_name); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 74 | storage()->GetBool(kStorageId, kStorageOfflineMode, |
| 75 | &manager_props->offline_mode); |
Paul Stewart | f555cf8 | 2012-03-15 14:42:43 -0700 | [diff] [blame] | 76 | if (!storage()->GetString(kStorageId, |
| 77 | kStorageCheckPortalList, |
| 78 | &manager_props->check_portal_list)) { |
| 79 | manager_props->check_portal_list = PortalDetector::kDefaultCheckPortalList; |
| 80 | } |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 81 | if (!storage()->GetString(kStorageId, kStoragePortalURL, |
| 82 | &manager_props->portal_url)) { |
| 83 | manager_props->portal_url = PortalDetector::kDefaultURL; |
| 84 | } |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 85 | std::string check_interval; |
| 86 | if (!storage()->GetString(kStorageId, kStoragePortalCheckInterval, |
| 87 | &check_interval) || |
| 88 | !base::StringToInt(check_interval, |
| 89 | &manager_props->portal_check_interval_seconds)) { |
| 90 | manager_props->portal_check_interval_seconds = |
| 91 | PortalDetector::kDefaultCheckIntervalSeconds; |
| 92 | } |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Paul Stewart | f284a23 | 2012-05-01 10:24:37 -0700 | [diff] [blame] | 96 | bool DefaultProfile::ConfigureService(const ServiceRefPtr &service) { |
| 97 | if (Profile::ConfigureService(service)) { |
| 98 | return true; |
| 99 | } |
| 100 | if (service->technology() == Technology::kEthernet) { |
| 101 | // Ethernet services should have an affinity towards the default profile, |
| 102 | // so even if a new Ethernet service has no known configuration, accept |
| 103 | // it anyway. |
| 104 | UpdateService(service); |
| 105 | service->set_profile(this); |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 111 | bool DefaultProfile::Save() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 112 | storage()->SetBool(kStorageId, kStorageArpGateway, props_.arp_gateway); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 113 | storage()->SetString(kStorageId, kStorageHostName, props_.host_name); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 114 | storage()->SetString(kStorageId, kStorageName, GetFriendlyName()); |
| 115 | storage()->SetBool(kStorageId, kStorageOfflineMode, props_.offline_mode); |
| 116 | storage()->SetString(kStorageId, |
| 117 | kStorageCheckPortalList, |
| 118 | props_.check_portal_list); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 119 | storage()->SetString(kStorageId, |
| 120 | kStoragePortalURL, |
| 121 | props_.portal_url); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 122 | storage()->SetString(kStorageId, |
| 123 | kStoragePortalCheckInterval, |
| 124 | base::IntToString(props_.portal_check_interval_seconds)); |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 125 | vector<DeviceRefPtr>::iterator it; |
| 126 | for (it = manager()->devices_begin(); it != manager()->devices_end(); ++it) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 127 | if (!(*it)->Save(storage())) { |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 128 | LOG(ERROR) << "Could not save " << (*it)->UniqueName(); |
| 129 | return false; |
| 130 | } |
| 131 | } |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 132 | return Profile::Save(); |
Chris Masone | aa48237 | 2011-09-14 16:40:37 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 135 | bool DefaultProfile::GetStoragePath(FilePath *path) { |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 136 | *path = storage_path_.Append(base::StringPrintf("%s.profile", |
| 137 | profile_id_.c_str())); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 141 | } // namespace shill |