blob: 475441eb7bd20a8cce16aef79a2461a5c2865d3b [file] [log] [blame]
Paul Stewarte6927402012-01-23 16:11:30 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone88cbd5f2011-07-03 14:30:04 -07002// 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 Masone877ff982011-09-21 16:18:24 -07007#include <vector>
8
Chris Masone2ae797d2011-08-23 20:41:00 -07009#include <base/file_path.h>
Paul Stewartc681fa02012-03-02 19:40:04 -080010#include <base/string_number_conversions.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070011#include <base/stringprintf.h>
Chris Masone88cbd5f2011-07-03 14:30:04 -070012#include <chromeos/dbus/service_constants.h>
13
14#include "shill/adaptor_interfaces.h"
15#include "shill/control_interface.h"
Paul Stewart036dba02012-08-07 12:34:41 -070016#include "shill/link_monitor.h"
Chris Masone88cbd5f2011-07-03 14:30:04 -070017#include "shill/manager.h"
Paul Stewarte6927402012-01-23 16:11:30 -080018#include "shill/portal_detector.h"
Paul Stewartbf667612012-06-29 14:49:54 -070019#include "shill/resolver.h"
Chris Masoneaa482372011-09-14 16:40:37 -070020#include "shill/store_interface.h"
Wade Guthrie60a37062013-04-02 11:39:09 -070021#include "shill/wifi_provider.h"
Chris Masone88cbd5f2011-07-03 14:30:04 -070022
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080023using base::FilePath;
Paul Stewartd0a3b812012-03-28 22:48:22 -070024using std::string;
Chris Masone877ff982011-09-21 16:18:24 -070025using std::vector;
26
Chris Masone88cbd5f2011-07-03 14:30:04 -070027namespace shill {
Chris Masoneaa482372011-09-14 16:40:37 -070028// static
Chris Masone7df0c672011-07-15 10:24:54 -070029const char DefaultProfile::kDefaultId[] = "default";
Chris Masoneaa482372011-09-14 16:40:37 -070030// static
31const char DefaultProfile::kStorageId[] = "global";
32// static
Paul Stewartd408fdf2012-05-07 17:15:57 -070033const char DefaultProfile::kStorageArpGateway[] = "ArpGateway";
34// static
Chris Masoneaa482372011-09-14 16:40:37 -070035const char DefaultProfile::kStorageCheckPortalList[] = "CheckPortalList";
36// static
Paul Stewartd32f4842012-01-11 16:08:13 -080037const char DefaultProfile::kStorageHostName[] = "HostName";
38// static
Paul Stewart4d5efb72012-09-17 12:24:34 -070039const char DefaultProfile::kStorageIgnoredDNSSearchPaths[] =
40 "IgnoredDNSSearchPaths";
41// static
Paul Stewart036dba02012-08-07 12:34:41 -070042const char DefaultProfile::kStorageLinkMonitorTechnologies[] =
43 "LinkMonitorTechnologies";
44// static
Chris Masoneaa482372011-09-14 16:40:37 -070045const char DefaultProfile::kStorageName[] = "Name";
46// static
47const char DefaultProfile::kStorageOfflineMode[] = "OfflineMode";
Paul Stewarte6927402012-01-23 16:11:30 -080048// static
49const char DefaultProfile::kStoragePortalURL[] = "PortalURL";
Paul Stewartc681fa02012-03-02 19:40:04 -080050// static
51const char DefaultProfile::kStoragePortalCheckInterval[] =
52 "PortalCheckInterval";
Chris Masone88cbd5f2011-07-03 14:30:04 -070053
Chris Masone2ae797d2011-08-23 20:41:00 -070054DefaultProfile::DefaultProfile(ControlInterface *control,
Thieu Le5133b712013-02-19 14:47:21 -080055 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070056 Manager *manager,
Chris Masone2ae797d2011-08-23 20:41:00 -070057 const FilePath &storage_path,
Paul Stewartd0a3b812012-03-28 22:48:22 -070058 const string &profile_id,
Chris Masone88cbd5f2011-07-03 14:30:04 -070059 const Manager::Properties &manager_props)
Thieu Le5133b712013-02-19 14:47:21 -080060 : Profile(control, metrics, manager, Identifier(profile_id), "", true),
Chris Masoneaa482372011-09-14 16:40:37 -070061 storage_path_(storage_path),
Paul Stewartd0a3b812012-03-28 22:48:22 -070062 profile_id_(profile_id),
Chris Masoneaa482372011-09-14 16:40:37 -070063 props_(manager_props) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070064 PropertyStore *store = this->mutable_store();
Paul Stewartd408fdf2012-05-07 17:15:57 -070065 store->RegisterConstBool(flimflam::kArpGatewayProperty,
66 &manager_props.arp_gateway);
Paul Stewartac4ac002011-08-26 12:04:26 -070067 store->RegisterConstString(flimflam::kCheckPortalListProperty,
Chris Masone88cbd5f2011-07-03 14:30:04 -070068 &manager_props.check_portal_list);
Paul Stewartac4ac002011-08-26 12:04:26 -070069 store->RegisterConstString(flimflam::kCountryProperty,
Chris Masone88cbd5f2011-07-03 14:30:04 -070070 &manager_props.country);
Paul Stewart4d5efb72012-09-17 12:24:34 -070071 store->RegisterConstString(shill::kIgnoredDNSSearchPathsProperty,
72 &manager_props.ignored_dns_search_paths);
Paul Stewart036dba02012-08-07 12:34:41 -070073 store->RegisterConstString(shill::kLinkMonitorTechnologiesProperty,
74 &manager_props.link_monitor_technologies);
Paul Stewartac4ac002011-08-26 12:04:26 -070075 store->RegisterConstBool(flimflam::kOfflineModeProperty,
Chris Masone88cbd5f2011-07-03 14:30:04 -070076 &manager_props.offline_mode);
Paul Stewartac4ac002011-08-26 12:04:26 -070077 store->RegisterConstString(flimflam::kPortalURLProperty,
Chris Masone88cbd5f2011-07-03 14:30:04 -070078 &manager_props.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -080079 store->RegisterConstInt32(shill::kPortalCheckIntervalProperty,
80 &manager_props.portal_check_interval_seconds);
Chris Masone88cbd5f2011-07-03 14:30:04 -070081}
82
83DefaultProfile::~DefaultProfile() {}
84
Paul Stewart870523b2012-01-11 17:00:42 -080085bool DefaultProfile::LoadManagerProperties(Manager::Properties *manager_props) {
Paul Stewartd408fdf2012-05-07 17:15:57 -070086 storage()->GetBool(kStorageId, kStorageArpGateway,
87 &manager_props->arp_gateway);
Paul Stewartd32f4842012-01-11 16:08:13 -080088 storage()->GetString(kStorageId, kStorageHostName, &manager_props->host_name);
Paul Stewart870523b2012-01-11 17:00:42 -080089 storage()->GetBool(kStorageId, kStorageOfflineMode,
90 &manager_props->offline_mode);
Paul Stewartf555cf82012-03-15 14:42:43 -070091 if (!storage()->GetString(kStorageId,
92 kStorageCheckPortalList,
93 &manager_props->check_portal_list)) {
94 manager_props->check_portal_list = PortalDetector::kDefaultCheckPortalList;
95 }
Paul Stewart036dba02012-08-07 12:34:41 -070096 if (!storage()->GetString(kStorageId,
Paul Stewart4d5efb72012-09-17 12:24:34 -070097 kStorageIgnoredDNSSearchPaths,
98 &manager_props->ignored_dns_search_paths)) {
99 manager_props->ignored_dns_search_paths =
100 Resolver::kDefaultIgnoredSearchList;
101 }
102 if (!storage()->GetString(kStorageId,
Paul Stewart036dba02012-08-07 12:34:41 -0700103 kStorageLinkMonitorTechnologies,
104 &manager_props->link_monitor_technologies)) {
105 manager_props->link_monitor_technologies =
106 LinkMonitor::kDefaultLinkMonitorTechnologies;
107 }
Paul Stewarte6927402012-01-23 16:11:30 -0800108 if (!storage()->GetString(kStorageId, kStoragePortalURL,
109 &manager_props->portal_url)) {
110 manager_props->portal_url = PortalDetector::kDefaultURL;
111 }
Paul Stewartc681fa02012-03-02 19:40:04 -0800112 std::string check_interval;
113 if (!storage()->GetString(kStorageId, kStoragePortalCheckInterval,
114 &check_interval) ||
115 !base::StringToInt(check_interval,
116 &manager_props->portal_check_interval_seconds)) {
117 manager_props->portal_check_interval_seconds =
118 PortalDetector::kDefaultCheckIntervalSeconds;
119 }
Paul Stewart870523b2012-01-11 17:00:42 -0800120 return true;
121}
122
Paul Stewartf284a232012-05-01 10:24:37 -0700123bool DefaultProfile::ConfigureService(const ServiceRefPtr &service) {
124 if (Profile::ConfigureService(service)) {
125 return true;
126 }
127 if (service->technology() == Technology::kEthernet) {
128 // Ethernet services should have an affinity towards the default profile,
129 // so even if a new Ethernet service has no known configuration, accept
130 // it anyway.
131 UpdateService(service);
Philipp Neubeck79173602012-11-13 21:10:09 +0100132 service->SetProfile(this);
Paul Stewartf284a232012-05-01 10:24:37 -0700133 return true;
134 }
135 return false;
136}
137
Chris Masoneb9c00592011-10-06 13:10:39 -0700138bool DefaultProfile::Save() {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700139 storage()->SetBool(kStorageId, kStorageArpGateway, props_.arp_gateway);
Paul Stewartd32f4842012-01-11 16:08:13 -0800140 storage()->SetString(kStorageId, kStorageHostName, props_.host_name);
Chris Masoneb9c00592011-10-06 13:10:39 -0700141 storage()->SetString(kStorageId, kStorageName, GetFriendlyName());
142 storage()->SetBool(kStorageId, kStorageOfflineMode, props_.offline_mode);
143 storage()->SetString(kStorageId,
144 kStorageCheckPortalList,
145 props_.check_portal_list);
Paul Stewarte6927402012-01-23 16:11:30 -0800146 storage()->SetString(kStorageId,
Paul Stewart4d5efb72012-09-17 12:24:34 -0700147 kStorageIgnoredDNSSearchPaths,
148 props_.ignored_dns_search_paths);
149 storage()->SetString(kStorageId,
Paul Stewart036dba02012-08-07 12:34:41 -0700150 kStorageLinkMonitorTechnologies,
151 props_.link_monitor_technologies);
152 storage()->SetString(kStorageId,
Paul Stewarte6927402012-01-23 16:11:30 -0800153 kStoragePortalURL,
154 props_.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -0800155 storage()->SetString(kStorageId,
156 kStoragePortalCheckInterval,
157 base::IntToString(props_.portal_check_interval_seconds));
Chris Masoneb9c00592011-10-06 13:10:39 -0700158 return Profile::Save();
Chris Masoneaa482372011-09-14 16:40:37 -0700159}
160
Darin Petkove7c6ad32012-06-29 10:22:09 +0200161bool DefaultProfile::UpdateDevice(const DeviceRefPtr &device) {
162 return device->Save(storage()) && storage()->Flush();
163}
164
Wade Guthrie60a37062013-04-02 11:39:09 -0700165bool DefaultProfile::UpdateWiFiProvider(const WiFiProvider &wifi_provider) {
166 return wifi_provider.Save(storage()) && storage()->Flush();
167}
168
Chris Masone2ae797d2011-08-23 20:41:00 -0700169bool DefaultProfile::GetStoragePath(FilePath *path) {
Paul Stewartd0a3b812012-03-28 22:48:22 -0700170 *path = storage_path_.Append(base::StringPrintf("%s.profile",
171 profile_id_.c_str()));
Chris Masone2ae797d2011-08-23 20:41:00 -0700172 return true;
173}
174
Chris Masone88cbd5f2011-07-03 14:30:04 -0700175} // namespace shill