Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 52cd19b | 2011-06-29 17:23: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/profile.h" |
| 6 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 7 | #include <set> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 8 | #include <string> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 9 | #include <vector> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 10 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 11 | #include <base/file_util.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | #include <base/stl_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 13 | #include <base/strings/string_split.h> |
| 14 | #include <base/strings/string_util.h> |
| 15 | #include <base/strings/stringprintf.h> |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/adaptor_interfaces.h" |
| 19 | #include "shill/control_interface.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 20 | #include "shill/key_file_store.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 21 | #include "shill/logging.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 22 | #include "shill/manager.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 23 | #include "shill/property_accessor.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 24 | #include "shill/service.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 25 | #include "shill/store_interface.h" |
mukesh agrawal | 92496a4 | 2014-04-08 16:04:43 -0700 | [diff] [blame] | 26 | #include "shill/stub_storage.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 27 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 28 | using base::FilePath; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 29 | using std::set; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 30 | using std::string; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 31 | using std::vector; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 32 | |
| 33 | namespace shill { |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 34 | |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 35 | // static |
| 36 | const char Profile::kUserProfileListPathname[] = |
| 37 | RUNDIR "/loaded_profile_list"; |
| 38 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 39 | Profile::Profile(ControlInterface *control_interface, |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 40 | Metrics *metrics, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 41 | Manager *manager, |
| 42 | const Identifier &name, |
mukesh agrawal | 0a59a5a | 2014-04-24 19:10:46 -0700 | [diff] [blame] | 43 | const string &user_storage_directory, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 44 | bool connect_to_rpc) |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 45 | : metrics_(metrics), |
| 46 | manager_(manager), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 47 | name_(name), |
mukesh agrawal | 0a59a5a | 2014-04-24 19:10:46 -0700 | [diff] [blame] | 48 | storage_path_(user_storage_directory) { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 49 | if (connect_to_rpc) |
| 50 | adaptor_.reset(control_interface->CreateProfileAdaptor(this)); |
| 51 | |
Ben Chan | 5dc5881 | 2013-09-20 12:53:25 -0700 | [diff] [blame] | 52 | // kCheckPortalListProperty: Registered in DefaultProfile |
| 53 | // kCountryProperty: Registered in DefaultProfile |
| 54 | store_.RegisterConstString(kNameProperty, &name_.identifier); |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 55 | store_.RegisterConstString(kUserHashProperty, &name_.user_hash); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 56 | |
Ben Chan | 5dc5881 | 2013-09-20 12:53:25 -0700 | [diff] [blame] | 57 | // kOfflineModeProperty: Registered in DefaultProfile |
| 58 | // kPortalURLProperty: Registered in DefaultProfile |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 59 | |
Ben Chan | 5dc5881 | 2013-09-20 12:53:25 -0700 | [diff] [blame] | 60 | HelpRegisterConstDerivedStrings(kServicesProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 61 | &Profile::EnumerateAvailableServices); |
Ben Chan | 5dc5881 | 2013-09-20 12:53:25 -0700 | [diff] [blame] | 62 | HelpRegisterConstDerivedStrings(kEntriesProperty, &Profile::EnumerateEntries); |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 65 | Profile::~Profile() {} |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 66 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 67 | bool Profile::InitStorage(GLib *glib, InitStorageOption storage_option, |
| 68 | Error *error) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 69 | FilePath final_path; |
| 70 | if (!GetStoragePath(&final_path)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 71 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 72 | base::StringPrintf("Could not set up profile storage for %s:%s", |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 73 | name_.user.c_str(), name_.identifier.c_str())); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
| 77 | storage->set_path(final_path); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 78 | bool already_exists = storage->IsNonEmpty(); |
| 79 | if (!already_exists && storage_option != kCreateNew && |
| 80 | storage_option != kCreateOrOpenExisting) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 81 | Error::PopulateAndLog(error, Error::kNotFound, |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 82 | base::StringPrintf("Profile storage for %s:%s does not already exist", |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 83 | name_.user.c_str(), name_.identifier.c_str())); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } else if (already_exists && storage_option != kOpenExisting && |
| 86 | storage_option != kCreateOrOpenExisting) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 87 | Error::PopulateAndLog(error, Error::kAlreadyExists, |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 88 | base::StringPrintf("Profile storage for %s:%s already exists", |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 89 | name_.user.c_str(), name_.identifier.c_str())); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 92 | if (!storage->Open()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 93 | Error::PopulateAndLog(error, Error::kInternalError, |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 94 | base::StringPrintf("Could not open profile storage for %s:%s", |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 95 | name_.user.c_str(), name_.identifier.c_str())); |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 96 | if (already_exists) { |
mukesh agrawal | 92496a4 | 2014-04-08 16:04:43 -0700 | [diff] [blame] | 97 | // The profile contents are corrupt, or we do not have access to |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 98 | // this file. Move this file out of the way so a future open attempt |
| 99 | // will succeed, assuming the failure reason was the former. |
| 100 | storage->MarkAsCorrupted(); |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 101 | metrics_->NotifyCorruptedProfile(); |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 102 | } |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 103 | return false; |
| 104 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 105 | if (!already_exists) { |
| 106 | // Add a descriptive header to the profile so even if nothing is stored |
| 107 | // to it, it still has some content. Completely empty keyfiles are not |
| 108 | // valid for reading. |
| 109 | storage->SetHeader( |
| 110 | base::StringPrintf("Profile %s:%s", name_.user.c_str(), |
| 111 | name_.identifier.c_str())); |
| 112 | } |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 113 | set_storage(storage.release()); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 114 | manager_->OnProfileStorageInitialized(storage_.get()); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
mukesh agrawal | 92496a4 | 2014-04-08 16:04:43 -0700 | [diff] [blame] | 118 | void Profile::InitStubStorage() { |
| 119 | set_storage(new StubStorage()); |
| 120 | } |
| 121 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 122 | bool Profile::RemoveStorage(GLib *glib, Error *error) { |
| 123 | FilePath path; |
| 124 | |
| 125 | CHECK(!storage_.get()); |
| 126 | |
| 127 | if (!GetStoragePath(&path)) { |
| 128 | Error::PopulateAndLog( |
| 129 | error, Error::kInvalidArguments, |
| 130 | base::StringPrintf("Could get the storage path for %s:%s", |
| 131 | name_.user.c_str(), name_.identifier.c_str())); |
| 132 | return false; |
| 133 | } |
| 134 | |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 135 | if (!base::DeleteFile(path, false)) { |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 136 | Error::PopulateAndLog( |
| 137 | error, Error::kOperationFailed, |
| 138 | base::StringPrintf("Could not remove path %s", path.value().c_str())); |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 145 | string Profile::GetFriendlyName() { |
| 146 | return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier; |
| 147 | } |
| 148 | |
| 149 | string Profile::GetRpcIdentifier() { |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 150 | if (!adaptor_.get()) { |
| 151 | // NB: This condition happens in unit tests. |
| 152 | return string(); |
| 153 | } |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 154 | return adaptor_->GetRpcIdentifier(); |
| 155 | } |
| 156 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 157 | void Profile::set_storage(StoreInterface *storage) { |
| 158 | storage_.reset(storage); |
| 159 | } |
| 160 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 161 | bool Profile::AdoptService(const ServiceRefPtr &service) { |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 162 | if (service->profile() == this) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 163 | return false; |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 164 | } |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 165 | service->SetProfile(this); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 166 | return service->Save(storage_.get()) && storage_->Flush(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 169 | bool Profile::AbandonService(const ServiceRefPtr &service) { |
| 170 | if (service->profile() == this) |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 171 | service->SetProfile(NULL); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 172 | return storage_->DeleteGroup(service->GetStorageIdentifier()) && |
| 173 | storage_->Flush(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 176 | bool Profile::UpdateService(const ServiceRefPtr &service) { |
| 177 | return service->Save(storage_.get()) && storage_->Flush(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 180 | bool Profile::LoadService(const ServiceRefPtr &service) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 181 | if (!ContainsService(service)) |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 182 | return false; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 183 | return service->Load(storage_.get()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 186 | bool Profile::ConfigureService(const ServiceRefPtr &service) { |
| 187 | if (!LoadService(service)) |
| 188 | return false; |
| 189 | service->SetProfile(this); |
| 190 | return true; |
| 191 | } |
| 192 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 193 | bool Profile::ConfigureDevice(const DeviceRefPtr &device) { |
| 194 | return device->Load(storage_.get()); |
| 195 | } |
| 196 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 197 | bool Profile::ContainsService(const ServiceConstRefPtr &service) { |
Paul Stewart | e7de294 | 2013-04-25 17:07:31 -0700 | [diff] [blame] | 198 | return service->IsLoadableFrom(*storage_.get()); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 201 | void Profile::DeleteEntry(const std::string &entry_name, Error *error) { |
| 202 | if (!storage_->ContainsGroup(entry_name)) { |
| 203 | Error::PopulateAndLog(error, Error::kNotFound, |
| 204 | base::StringPrintf("Entry %s does not exist in profile", |
| 205 | entry_name.c_str())); |
| 206 | return; |
| 207 | } |
| 208 | if (!manager_->HandleProfileEntryDeletion(this, entry_name)) { |
| 209 | // If HandleProfileEntryDeletion() returns succeeds, DeleteGroup() |
| 210 | // has already been called when AbandonService was called. |
| 211 | // Otherwise, we need to delete the group ourselves. |
| 212 | storage_->DeleteGroup(entry_name); |
| 213 | } |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 214 | Save(); |
| 215 | } |
| 216 | |
| 217 | ServiceRefPtr Profile::GetServiceFromEntry(const std::string &entry_name, |
| 218 | Error *error) { |
| 219 | return manager_->GetServiceWithStorageIdentifier(this, entry_name, error); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 222 | bool Profile::IsValidIdentifierToken(const string &token) { |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 223 | if (token.empty()) { |
| 224 | return false; |
| 225 | } |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 226 | for (auto chr : token) { |
| 227 | if (!IsAsciiAlpha(chr) && !IsAsciiDigit(chr)) { |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 234 | // static |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 235 | bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) { |
| 236 | if (raw.empty()) { |
| 237 | return false; |
| 238 | } |
| 239 | if (raw[0] == '~') { |
| 240 | // Format: "~user/identifier". |
| 241 | size_t slash = raw.find('/'); |
| 242 | if (slash == string::npos) { |
| 243 | return false; |
| 244 | } |
| 245 | string user(raw.begin() + 1, raw.begin() + slash); |
| 246 | string identifier(raw.begin() + slash + 1, raw.end()); |
| 247 | if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) { |
| 248 | return false; |
| 249 | } |
| 250 | parsed->user = user; |
| 251 | parsed->identifier = identifier; |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | // Format: "identifier". |
| 256 | if (!IsValidIdentifierToken(raw)) { |
| 257 | return false; |
| 258 | } |
| 259 | parsed->user = ""; |
| 260 | parsed->identifier = raw; |
| 261 | return true; |
| 262 | } |
| 263 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 264 | // static |
| 265 | string Profile::IdentifierToString(const Identifier &name) { |
| 266 | if (name.user.empty()) { |
| 267 | // Format: "identifier". |
| 268 | return name.identifier; |
| 269 | } |
| 270 | |
| 271 | // Format: "~user/identifier". |
| 272 | return base::StringPrintf( |
| 273 | "~%s/%s", name.user.c_str(), name.identifier.c_str()); |
| 274 | } |
| 275 | |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 276 | // static |
| 277 | vector<Profile::Identifier> Profile::LoadUserProfileList(const FilePath &path) { |
| 278 | vector<Identifier> profile_identifiers; |
| 279 | string profile_data; |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 280 | if (!base::ReadFileToString(path, &profile_data)) { |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 281 | return profile_identifiers; |
| 282 | } |
| 283 | |
| 284 | vector<string> profile_lines; |
| 285 | base::SplitStringDontTrim(profile_data, '\n', &profile_lines); |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 286 | for (const auto &line : profile_lines) { |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 287 | if (line.empty()) { |
| 288 | // This will be the case on the last line, so let's not complain about it. |
| 289 | continue; |
| 290 | } |
| 291 | size_t space = line.find(' '); |
| 292 | if (space == string::npos || space == 0) { |
| 293 | LOG(ERROR) << "Invalid line found in " << path.value() |
| 294 | << ": " << line; |
| 295 | continue; |
| 296 | } |
| 297 | string name(line.begin(), line.begin() + space); |
| 298 | Identifier identifier; |
| 299 | if (!ParseIdentifier(name, &identifier) || identifier.user.empty()) { |
| 300 | LOG(ERROR) << "Invalid profile name found in " << path.value() |
| 301 | << ": " << name; |
| 302 | continue; |
| 303 | } |
| 304 | identifier.user_hash = string(line.begin() + space + 1, line.end()); |
| 305 | profile_identifiers.push_back(identifier); |
| 306 | } |
| 307 | |
| 308 | return profile_identifiers; |
| 309 | } |
| 310 | |
| 311 | // static |
| 312 | bool Profile::SaveUserProfileList(const FilePath &path, |
| 313 | const vector<ProfileRefPtr> &profiles) { |
| 314 | vector<string> lines; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 315 | for (const auto &profile : profiles) { |
| 316 | Identifier &id = profile->name_; |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 317 | if (id.user.empty()) { |
| 318 | continue; |
| 319 | } |
| 320 | lines.push_back(base::StringPrintf("%s %s\n", |
| 321 | IdentifierToString(id).c_str(), |
| 322 | id.user_hash.c_str())); |
| 323 | } |
| 324 | string content = JoinString(lines, ""); |
| 325 | size_t ret = file_util::WriteFile(path, content.c_str(), content.length()); |
| 326 | return ret == content.length(); |
| 327 | } |
| 328 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 329 | bool Profile::MatchesIdentifier(const Identifier &name) const { |
| 330 | return name.user == name_.user && name.identifier == name_.identifier; |
| 331 | } |
| 332 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 333 | bool Profile::Save() { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 334 | return storage_->Flush(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 337 | bool Profile::GetStoragePath(FilePath *path) { |
| 338 | if (name_.user.empty()) { |
| 339 | LOG(ERROR) << "Non-default profiles cannot be stored globally."; |
| 340 | return false; |
| 341 | } |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 342 | // TODO(petkov): Validate the directory permissions, etc. |
mukesh agrawal | 0a59a5a | 2014-04-24 19:10:46 -0700 | [diff] [blame] | 343 | *path = storage_path_.Append( |
| 344 | base::StringPrintf("%s/%s.profile", name_.user.c_str(), |
| 345 | name_.identifier.c_str())); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 349 | vector<string> Profile::EnumerateAvailableServices(Error *error) { |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 350 | // We should return the Manager's service list if this is the active profile. |
| 351 | if (manager_->IsActiveProfile(this)) { |
| 352 | return manager_->EnumerateAvailableServices(error); |
| 353 | } else { |
| 354 | return vector<string>(); |
| 355 | } |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 358 | vector<string> Profile::EnumerateEntries(Error */*error*/) { |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 359 | vector<string> service_groups; |
| 360 | |
| 361 | // Filter this list down to only entries that correspond |
| 362 | // to a technology. (wifi_*, etc) |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 363 | for (const auto &group : storage_->GetGroups()) { |
| 364 | if (Technology::IdentifierFromStorageGroup(group) != Technology::kUnknown) |
| 365 | service_groups.push_back(group); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | return service_groups; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 371 | bool Profile::UpdateDevice(const DeviceRefPtr &device) { |
| 372 | return false; |
| 373 | } |
| 374 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 375 | bool Profile::UpdateWiFiProvider(const WiFiProvider &wifi_provider) { |
| 376 | return false; |
| 377 | } |
| 378 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 379 | void Profile::HelpRegisterConstDerivedStrings( |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 380 | const string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 381 | Strings(Profile::*get)(Error *)) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 382 | store_.RegisterDerivedStrings( |
| 383 | name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 384 | StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, NULL))); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 387 | } // namespace shill |