blob: 2025de1651cdfb686039b3f7aa889841fa07519d [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 Masone6515aab2011-10-12 16:19:09 -07007#include <set>
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
Chris Masoneb9c00592011-10-06 13:10:39 -070011#include <base/file_path.h>
Chris Masone52cd19b2011-06-29 17:23:04 -070012#include <base/logging.h>
Chris Masone6791a432011-07-12 13:23:19 -070013#include <base/stl_util-inl.h>
Darin Petkova4766822011-07-07 10:42:22 -070014#include <base/string_util.h>
Chris Masone6791a432011-07-12 13:23:19 -070015#include <base/stringprintf.h>
Chris Masone88cbd5f2011-07-03 14:30:04 -070016#include <chromeos/dbus/service_constants.h>
Chris Masone52cd19b2011-06-29 17:23:04 -070017
18#include "shill/adaptor_interfaces.h"
19#include "shill/control_interface.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070020#include "shill/key_file_store.h"
Chris Masone6791a432011-07-12 13:23:19 -070021#include "shill/manager.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070022#include "shill/property_accessor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070023#include "shill/service.h"
Chris Masone9d779932011-08-25 16:33:41 -070024#include "shill/store_interface.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070025
Chris Masone6515aab2011-10-12 16:19:09 -070026using std::set;
Chris Masone52cd19b2011-06-29 17:23:04 -070027using std::string;
Chris Masone6791a432011-07-12 13:23:19 -070028using std::vector;
Chris Masone52cd19b2011-06-29 17:23:04 -070029
30namespace shill {
Darin Petkova4766822011-07-07 10:42:22 -070031
Darin Petkova4766822011-07-07 10:42:22 -070032Profile::Profile(ControlInterface *control_interface,
Chris Masone7df0c672011-07-15 10:24:54 -070033 Manager *manager,
34 const Identifier &name,
Chris Masone2ae797d2011-08-23 20:41:00 -070035 const string &user_storage_format,
Chris Masone7df0c672011-07-15 10:24:54 -070036 bool connect_to_rpc)
Chris Masone6791a432011-07-12 13:23:19 -070037 : manager_(manager),
Chris Masone7df0c672011-07-15 10:24:54 -070038 name_(name),
Chris Masone2ae797d2011-08-23 20:41:00 -070039 storage_format_(user_storage_format) {
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
Chris Masone6515aab2011-10-12 16:19:09 -070058Profile::~Profile() {}
Chris Masone52cd19b2011-06-29 17:23:04 -070059
Chris Masoneb9c00592011-10-06 13:10:39 -070060bool Profile::InitStorage(GLib *glib) {
61 FilePath final_path;
62 if (!GetStoragePath(&final_path)) {
63 PLOG(ERROR) <<
64 base::StringPrintf("Could not set up profile storage for %s:%s",
65 name_.user.c_str(), name_.identifier.c_str());
66 return false;
67 }
68 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
69 storage->set_path(final_path);
70 if (!storage->Open()) {
71 PLOG(ERROR) <<
72 base::StringPrintf("Could not open profile storage for %s:%s",
73 name_.user.c_str(), name_.identifier.c_str());
74 return false;
75 }
76 set_storage(storage.release());
77 return true;
78}
79
Chris Masone7df0c672011-07-15 10:24:54 -070080string Profile::GetFriendlyName() {
81 return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier;
82}
83
84string Profile::GetRpcIdentifier() {
85 return adaptor_->GetRpcIdentifier();
86}
87
Chris Masoneb9c00592011-10-06 13:10:39 -070088void Profile::set_storage(StoreInterface *storage) {
89 storage_.reset(storage);
90}
91
Chris Masone6791a432011-07-12 13:23:19 -070092bool Profile::AdoptService(const ServiceRefPtr &service) {
Chris Masone6515aab2011-10-12 16:19:09 -070093 if (storage_->ContainsGroup(service->GetStorageIdentifier()))
Chris Masone6791a432011-07-12 13:23:19 -070094 return false;
95 service->set_profile(this);
Chris Masone6515aab2011-10-12 16:19:09 -070096 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -070097}
98
Chris Masone6515aab2011-10-12 16:19:09 -070099bool Profile::AbandonService(const ServiceRefPtr &service) {
100 if (service->profile() == this)
101 service->set_profile(NULL);
102 return storage_->DeleteGroup(service->GetStorageIdentifier()) &&
103 storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700104}
105
Chris Masone6515aab2011-10-12 16:19:09 -0700106bool Profile::UpdateService(const ServiceRefPtr &service) {
107 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700108}
109
110bool Profile::MergeService(const ServiceRefPtr &service) {
Chris Masone6515aab2011-10-12 16:19:09 -0700111 if (!storage_->ContainsGroup(service->GetStorageIdentifier()))
112 return false;
113 service->set_profile(this);
114 return service->Load(storage_.get());
Chris Masone6791a432011-07-12 13:23:19 -0700115}
116
Chris Masone6515aab2011-10-12 16:19:09 -0700117bool Profile::ContainsService(const ServiceConstRefPtr &service) {
118 return storage_->ContainsGroup(service->GetStorageIdentifier());
Chris Masone7aa5f902011-07-11 11:13:35 -0700119}
120
Darin Petkova4766822011-07-07 10:42:22 -0700121bool Profile::IsValidIdentifierToken(const std::string &token) {
122 if (token.empty()) {
123 return false;
124 }
125 for (string::const_iterator it = token.begin(); it != token.end(); ++it) {
126 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it)) {
127 return false;
128 }
129 }
130 return true;
131}
132
133bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) {
134 if (raw.empty()) {
135 return false;
136 }
137 if (raw[0] == '~') {
138 // Format: "~user/identifier".
139 size_t slash = raw.find('/');
140 if (slash == string::npos) {
141 return false;
142 }
143 string user(raw.begin() + 1, raw.begin() + slash);
144 string identifier(raw.begin() + slash + 1, raw.end());
145 if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) {
146 return false;
147 }
148 parsed->user = user;
149 parsed->identifier = identifier;
150 return true;
151 }
152
153 // Format: "identifier".
154 if (!IsValidIdentifierToken(raw)) {
155 return false;
156 }
157 parsed->user = "";
158 parsed->identifier = raw;
159 return true;
160}
161
Chris Masoneb9c00592011-10-06 13:10:39 -0700162bool Profile::Save() {
Chris Masone6515aab2011-10-12 16:19:09 -0700163 return storage_->Flush();
Chris Masone9d779932011-08-25 16:33:41 -0700164}
165
Chris Masone2ae797d2011-08-23 20:41:00 -0700166bool Profile::GetStoragePath(FilePath *path) {
167 if (name_.user.empty()) {
168 LOG(ERROR) << "Non-default profiles cannot be stored globally.";
169 return false;
170 }
171 FilePath dir(base::StringPrintf(storage_format_.c_str(), name_.user.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700172 // TODO(petkov): Validate the directory permissions, etc.
173 *path = dir.Append(base::StringPrintf("%s.profile",
Chris Masone2ae797d2011-08-23 20:41:00 -0700174 name_.identifier.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700175 return true;
176}
177
Chris Masone6791a432011-07-12 13:23:19 -0700178vector<string> Profile::EnumerateAvailableServices() {
179 return manager_->EnumerateAvailableServices();
180}
181
182vector<string> Profile::EnumerateEntries() {
Chris Masone6515aab2011-10-12 16:19:09 -0700183 // TODO(someone): Determine if we care about this wasteful copying; consider
184 // making GetGroups return a vector.
185 set<string> groups(storage_->GetGroups());
186 return vector<string>(groups.begin(), groups.end());
Chris Masone6791a432011-07-12 13:23:19 -0700187}
188
mukesh agrawalffa3d042011-10-06 15:26:10 -0700189void Profile::HelpRegisterDerivedStrings(
190 const string &name,
191 Strings(Profile::*get)(void),
192 void(Profile::*set)(const Strings&, Error *)) {
Chris Masone6791a432011-07-12 13:23:19 -0700193 store_.RegisterDerivedStrings(
194 name,
195 StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, set)));
196}
197
Chris Masone52cd19b2011-06-29 17:23:04 -0700198} // namespace shill