blob: ef5182bea039c75f2575e553c3a7583f7f88894a [file] [log] [blame]
Chris Masone52cd19b2011-06-29 17:23:04 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
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/profile.h"
6
Chris Masone6791a432011-07-12 13:23:19 -07007#include <map>
Chris Masone52cd19b2011-06-29 17:23:04 -07008#include <string>
Chris Masone6791a432011-07-12 13:23:19 -07009#include <vector>
Chris Masone52cd19b2011-06-29 17:23:04 -070010
11#include <base/logging.h>
Chris Masone6791a432011-07-12 13:23:19 -070012#include <base/stl_util-inl.h>
Darin Petkova4766822011-07-07 10:42:22 -070013#include <base/string_util.h>
Chris Masone6791a432011-07-12 13:23:19 -070014#include <base/stringprintf.h>
Chris Masone88cbd5f2011-07-03 14:30:04 -070015#include <chromeos/dbus/service_constants.h>
Chris Masone52cd19b2011-06-29 17:23:04 -070016
17#include "shill/adaptor_interfaces.h"
18#include "shill/control_interface.h"
Chris Masone6791a432011-07-12 13:23:19 -070019#include "shill/manager.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070020#include "shill/property_accessor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070021#include "shill/service.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070022
Chris Masone6791a432011-07-12 13:23:19 -070023using std::map;
Chris Masone52cd19b2011-06-29 17:23:04 -070024using std::string;
Chris Masone6791a432011-07-12 13:23:19 -070025using std::vector;
Chris Masone52cd19b2011-06-29 17:23:04 -070026
27namespace shill {
Darin Petkova4766822011-07-07 10:42:22 -070028
29const char Profile::kGlobalStorageDir[] = "/var/cache/flimflam";
30const char Profile::kUserStorageDirFormat[] = "/home/%s/user/flimflam";
31
32Profile::Profile(ControlInterface *control_interface,
Chris Masone6791a432011-07-12 13:23:19 -070033 GLib *glib,
Chris Masone7df0c672011-07-15 10:24:54 -070034 Manager *manager,
35 const Identifier &name,
36 bool connect_to_rpc)
Chris Masone6791a432011-07-12 13:23:19 -070037 : manager_(manager),
Chris Masone7df0c672011-07-15 10:24:54 -070038 name_(name),
Darin Petkova4766822011-07-07 10:42:22 -070039 storage_(glib) {
Chris Masone7df0c672011-07-15 10:24:54 -070040 if (connect_to_rpc)
41 adaptor_.reset(control_interface->CreateProfileAdaptor(this));
42
Chris Masone88cbd5f2011-07-03 14:30:04 -070043 // flimflam::kCheckPortalListProperty: Registered in DefaultProfile
44 // flimflam::kCountryProperty: Registered in DefaultProfile
Chris Masone7df0c672011-07-15 10:24:54 -070045 store_.RegisterConstString(flimflam::kNameProperty, &name_.identifier);
Chris Masone88cbd5f2011-07-03 14:30:04 -070046
47 // flimflam::kOfflineModeProperty: Registered in DefaultProfile
48 // flimflam::kPortalURLProperty: Registered in DefaultProfile
49
Chris Masone6791a432011-07-12 13:23:19 -070050 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
51 &Profile::EnumerateAvailableServices,
52 NULL);
Chris Masone7df0c672011-07-15 10:24:54 -070053 HelpRegisterDerivedStrings(flimflam::kEntriesProperty,
54 &Profile::EnumerateEntries,
55 NULL);
Chris Masone52cd19b2011-06-29 17:23:04 -070056}
57
58Profile::~Profile() {}
59
Chris Masone7df0c672011-07-15 10:24:54 -070060string Profile::GetFriendlyName() {
61 return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier;
62}
63
64string Profile::GetRpcIdentifier() {
65 return adaptor_->GetRpcIdentifier();
66}
67
Chris Masone6791a432011-07-12 13:23:19 -070068bool Profile::AdoptService(const ServiceRefPtr &service) {
69 if (ContainsKey(services_, service->UniqueName()))
70 return false;
71 service->set_profile(this);
72 services_[service->UniqueName()] = service;
73 return true;
74}
75
76bool Profile::AbandonService(const string &name) {
77 map<string, ServiceRefPtr>::iterator to_abandon = services_.find(name);
78 if (to_abandon != services_.end()) {
79 services_.erase(to_abandon);
80 return true;
81 }
82 return false;
83}
84
85bool Profile::DemoteService(const string &name) {
86 map<string, ServiceRefPtr>::iterator to_demote = services_.find(name);
87 if (to_demote == services_.end())
88 return false;
89 return true; // TODO(cmasone): mark |to_demote| as inactive or something.
90}
91
92bool Profile::MergeService(const ServiceRefPtr &service) {
93 map<string, ServiceRefPtr>::iterator it;
94 for (it = services_.begin(); it != services_.end(); ++it) {
95 if (Mergeable(it->second, service))
96 return true; // TODO(cmasone): Perform merge.
97 }
98 return false;
99}
100
101ServiceRefPtr Profile::FindService(const std::string& name) {
102 if (ContainsKey(services_, name))
103 return services_[name];
104 return NULL;
105}
106
107void Profile::Finalize() {
108 // TODO(cmasone): Flush all of |services_| to disk if needed.
109 services_.clear();
Chris Masone7aa5f902011-07-11 11:13:35 -0700110}
111
Darin Petkova4766822011-07-07 10:42:22 -0700112bool Profile::IsValidIdentifierToken(const std::string &token) {
113 if (token.empty()) {
114 return false;
115 }
116 for (string::const_iterator it = token.begin(); it != token.end(); ++it) {
117 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it)) {
118 return false;
119 }
120 }
121 return true;
122}
123
124bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) {
125 if (raw.empty()) {
126 return false;
127 }
128 if (raw[0] == '~') {
129 // Format: "~user/identifier".
130 size_t slash = raw.find('/');
131 if (slash == string::npos) {
132 return false;
133 }
134 string user(raw.begin() + 1, raw.begin() + slash);
135 string identifier(raw.begin() + slash + 1, raw.end());
136 if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) {
137 return false;
138 }
139 parsed->user = user;
140 parsed->identifier = identifier;
141 return true;
142 }
143
144 // Format: "identifier".
145 if (!IsValidIdentifierToken(raw)) {
146 return false;
147 }
148 parsed->user = "";
149 parsed->identifier = raw;
150 return true;
151}
152
Darin Petkova4766822011-07-07 10:42:22 -0700153bool Profile::GetStoragePath(const Identifier &identifier, FilePath *path) {
154 FilePath dir(
155 identifier.user.empty() ?
156 kGlobalStorageDir :
157 base::StringPrintf(kUserStorageDirFormat, identifier.user.c_str()));
158 // TODO(petkov): Validate the directory permissions, etc.
159 *path = dir.Append(base::StringPrintf("%s.profile",
160 identifier.identifier.c_str()));
161 return true;
162}
163
Chris Masone6791a432011-07-12 13:23:19 -0700164vector<string> Profile::EnumerateAvailableServices() {
165 return manager_->EnumerateAvailableServices();
166}
167
168vector<string> Profile::EnumerateEntries() {
169 vector<string> rpc_ids;
170 map<string, ServiceRefPtr>::const_iterator it;
171 for (it = services_.begin(); it != services_.end(); ++it) {
172 rpc_ids.push_back(it->second->GetRpcIdentifier());
173 }
174 return rpc_ids;
175}
176
177void Profile::HelpRegisterDerivedStrings(const string &name,
178 Strings(Profile::*get)(void),
179 bool(Profile::*set)(const Strings&)) {
180 store_.RegisterDerivedStrings(
181 name,
182 StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, set)));
183}
184
Chris Masone52cd19b2011-06-29 17:23:04 -0700185} // namespace shill