blob: 1f7ce715912f8267f4e0ab95c06ace36dd4b82ed [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"
16#include "shill/manager.h"
Paul Stewarte6927402012-01-23 16:11:30 -080017#include "shill/portal_detector.h"
Chris Masoneaa482372011-09-14 16:40:37 -070018#include "shill/store_interface.h"
Chris Masone88cbd5f2011-07-03 14:30:04 -070019
Paul Stewartd0a3b812012-03-28 22:48:22 -070020using std::string;
Chris Masone877ff982011-09-21 16:18:24 -070021using std::vector;
22
Chris Masone88cbd5f2011-07-03 14:30:04 -070023namespace shill {
Chris Masoneaa482372011-09-14 16:40:37 -070024// static
Chris Masone7df0c672011-07-15 10:24:54 -070025const char DefaultProfile::kDefaultId[] = "default";
Chris Masoneaa482372011-09-14 16:40:37 -070026// static
27const char DefaultProfile::kStorageId[] = "global";
28// static
Paul Stewartd408fdf2012-05-07 17:15:57 -070029const char DefaultProfile::kStorageArpGateway[] = "ArpGateway";
30// static
Chris Masoneaa482372011-09-14 16:40:37 -070031const char DefaultProfile::kStorageCheckPortalList[] = "CheckPortalList";
32// static
Paul Stewartd32f4842012-01-11 16:08:13 -080033const char DefaultProfile::kStorageHostName[] = "HostName";
34// static
Chris Masoneaa482372011-09-14 16:40:37 -070035const char DefaultProfile::kStorageName[] = "Name";
36// static
37const char DefaultProfile::kStorageOfflineMode[] = "OfflineMode";
Paul Stewarte6927402012-01-23 16:11:30 -080038// static
39const char DefaultProfile::kStoragePortalURL[] = "PortalURL";
Paul Stewartc681fa02012-03-02 19:40:04 -080040// static
41const char DefaultProfile::kStoragePortalCheckInterval[] =
42 "PortalCheckInterval";
Chris Masone88cbd5f2011-07-03 14:30:04 -070043
Chris Masone2ae797d2011-08-23 20:41:00 -070044DefaultProfile::DefaultProfile(ControlInterface *control,
Chris Masone6791a432011-07-12 13:23:19 -070045 Manager *manager,
Chris Masone2ae797d2011-08-23 20:41:00 -070046 const FilePath &storage_path,
Paul Stewartd0a3b812012-03-28 22:48:22 -070047 const string &profile_id,
Chris Masone88cbd5f2011-07-03 14:30:04 -070048 const Manager::Properties &manager_props)
Paul Stewartd0a3b812012-03-28 22:48:22 -070049 : Profile(control, manager, Identifier(profile_id), "", true),
Chris Masoneaa482372011-09-14 16:40:37 -070050 storage_path_(storage_path),
Paul Stewartd0a3b812012-03-28 22:48:22 -070051 profile_id_(profile_id),
Chris Masoneaa482372011-09-14 16:40:37 -070052 props_(manager_props) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070053 PropertyStore *store = this->mutable_store();
Paul Stewartd408fdf2012-05-07 17:15:57 -070054 store->RegisterConstBool(flimflam::kArpGatewayProperty,
55 &manager_props.arp_gateway);
Paul Stewartac4ac002011-08-26 12:04:26 -070056 store->RegisterConstString(flimflam::kCheckPortalListProperty,
Chris Masone88cbd5f2011-07-03 14:30:04 -070057 &manager_props.check_portal_list);
Paul Stewartac4ac002011-08-26 12:04:26 -070058 store->RegisterConstString(flimflam::kCountryProperty,
Chris Masone88cbd5f2011-07-03 14:30:04 -070059 &manager_props.country);
Paul Stewartac4ac002011-08-26 12:04:26 -070060 store->RegisterConstBool(flimflam::kOfflineModeProperty,
Chris Masone88cbd5f2011-07-03 14:30:04 -070061 &manager_props.offline_mode);
Paul Stewartac4ac002011-08-26 12:04:26 -070062 store->RegisterConstString(flimflam::kPortalURLProperty,
Chris Masone88cbd5f2011-07-03 14:30:04 -070063 &manager_props.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -080064 store->RegisterConstInt32(shill::kPortalCheckIntervalProperty,
65 &manager_props.portal_check_interval_seconds);
Chris Masone88cbd5f2011-07-03 14:30:04 -070066}
67
68DefaultProfile::~DefaultProfile() {}
69
Paul Stewart870523b2012-01-11 17:00:42 -080070bool DefaultProfile::LoadManagerProperties(Manager::Properties *manager_props) {
Paul Stewartd408fdf2012-05-07 17:15:57 -070071 storage()->GetBool(kStorageId, kStorageArpGateway,
72 &manager_props->arp_gateway);
Paul Stewartd32f4842012-01-11 16:08:13 -080073 storage()->GetString(kStorageId, kStorageHostName, &manager_props->host_name);
Paul Stewart870523b2012-01-11 17:00:42 -080074 storage()->GetBool(kStorageId, kStorageOfflineMode,
75 &manager_props->offline_mode);
Paul Stewartf555cf82012-03-15 14:42:43 -070076 if (!storage()->GetString(kStorageId,
77 kStorageCheckPortalList,
78 &manager_props->check_portal_list)) {
79 manager_props->check_portal_list = PortalDetector::kDefaultCheckPortalList;
80 }
Paul Stewarte6927402012-01-23 16:11:30 -080081 if (!storage()->GetString(kStorageId, kStoragePortalURL,
82 &manager_props->portal_url)) {
83 manager_props->portal_url = PortalDetector::kDefaultURL;
84 }
Paul Stewartc681fa02012-03-02 19:40:04 -080085 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 Stewart870523b2012-01-11 17:00:42 -080093 return true;
94}
95
Paul Stewartf284a232012-05-01 10:24:37 -070096bool 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 Masoneb9c00592011-10-06 13:10:39 -0700111bool DefaultProfile::Save() {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700112 storage()->SetBool(kStorageId, kStorageArpGateway, props_.arp_gateway);
Paul Stewartd32f4842012-01-11 16:08:13 -0800113 storage()->SetString(kStorageId, kStorageHostName, props_.host_name);
Chris Masoneb9c00592011-10-06 13:10:39 -0700114 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 Stewarte6927402012-01-23 16:11:30 -0800119 storage()->SetString(kStorageId,
120 kStoragePortalURL,
121 props_.portal_url);
Paul Stewartc681fa02012-03-02 19:40:04 -0800122 storage()->SetString(kStorageId,
123 kStoragePortalCheckInterval,
124 base::IntToString(props_.portal_check_interval_seconds));
Chris Masone877ff982011-09-21 16:18:24 -0700125 vector<DeviceRefPtr>::iterator it;
126 for (it = manager()->devices_begin(); it != manager()->devices_end(); ++it) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700127 if (!(*it)->Save(storage())) {
Chris Masone877ff982011-09-21 16:18:24 -0700128 LOG(ERROR) << "Could not save " << (*it)->UniqueName();
129 return false;
130 }
131 }
Chris Masoneb9c00592011-10-06 13:10:39 -0700132 return Profile::Save();
Chris Masoneaa482372011-09-14 16:40:37 -0700133}
134
Chris Masone2ae797d2011-08-23 20:41:00 -0700135bool DefaultProfile::GetStoragePath(FilePath *path) {
Paul Stewartd0a3b812012-03-28 22:48:22 -0700136 *path = storage_path_.Append(base::StringPrintf("%s.profile",
137 profile_id_.c_str()));
Chris Masone2ae797d2011-08-23 20:41:00 -0700138 return true;
139}
140
Chris Masone88cbd5f2011-07-03 14:30:04 -0700141} // namespace shill