Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 1 | // 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 Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 7 | #include <map> |
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 | |
| 11 | #include <base/logging.h> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 12 | #include <base/stl_util-inl.h> |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 13 | #include <base/string_util.h> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 14 | #include <base/stringprintf.h> |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 15 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 16 | |
| 17 | #include "shill/adaptor_interfaces.h" |
| 18 | #include "shill/control_interface.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 19 | #include "shill/manager.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 20 | #include "shill/property_accessor.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 21 | #include "shill/service.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 22 | #include "shill/store_interface.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 23 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 24 | using std::map; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 25 | using std::string; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 26 | using std::vector; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 27 | |
| 28 | namespace shill { |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 29 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 30 | Profile::Profile(ControlInterface *control_interface, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 31 | Manager *manager, |
| 32 | const Identifier &name, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 33 | const string &user_storage_format, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 34 | bool connect_to_rpc) |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 35 | : manager_(manager), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 36 | name_(name), |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 37 | storage_format_(user_storage_format) { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 38 | if (connect_to_rpc) |
| 39 | adaptor_.reset(control_interface->CreateProfileAdaptor(this)); |
| 40 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 41 | // flimflam::kCheckPortalListProperty: Registered in DefaultProfile |
| 42 | // flimflam::kCountryProperty: Registered in DefaultProfile |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 43 | store_.RegisterConstString(flimflam::kNameProperty, &name_.identifier); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 44 | |
| 45 | // flimflam::kOfflineModeProperty: Registered in DefaultProfile |
| 46 | // flimflam::kPortalURLProperty: Registered in DefaultProfile |
| 47 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 48 | HelpRegisterDerivedStrings(flimflam::kServicesProperty, |
| 49 | &Profile::EnumerateAvailableServices, |
| 50 | NULL); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 51 | HelpRegisterDerivedStrings(flimflam::kEntriesProperty, |
| 52 | &Profile::EnumerateEntries, |
| 53 | NULL); |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 56 | Profile::~Profile() { |
| 57 | DCHECK_EQ(services_.size(), 0); |
| 58 | } |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 59 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 60 | string Profile::GetFriendlyName() { |
| 61 | return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier; |
| 62 | } |
| 63 | |
| 64 | string Profile::GetRpcIdentifier() { |
| 65 | return adaptor_->GetRpcIdentifier(); |
| 66 | } |
| 67 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 68 | bool 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 | |
| 76 | bool 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 | |
| 85 | bool 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 | |
| 92 | bool 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 | |
| 101 | ServiceRefPtr Profile::FindService(const std::string& name) { |
| 102 | if (ContainsKey(services_, name)) |
| 103 | return services_[name]; |
| 104 | return NULL; |
| 105 | } |
| 106 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 107 | void Profile::Finalize(StoreInterface *storage) { |
| 108 | Save(storage); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 109 | services_.clear(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 112 | bool 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 | |
| 124 | bool 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 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 153 | bool Profile::Load(StoreInterface */*storage*/) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | |
| 157 | bool Profile::Save(StoreInterface *storage) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 158 | return SaveServices(storage); |
| 159 | } |
| 160 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 161 | bool Profile::GetStoragePath(FilePath *path) { |
| 162 | if (name_.user.empty()) { |
| 163 | LOG(ERROR) << "Non-default profiles cannot be stored globally."; |
| 164 | return false; |
| 165 | } |
| 166 | FilePath dir(base::StringPrintf(storage_format_.c_str(), name_.user.c_str())); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 167 | // TODO(petkov): Validate the directory permissions, etc. |
| 168 | *path = dir.Append(base::StringPrintf("%s.profile", |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 169 | name_.identifier.c_str())); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 173 | vector<string> Profile::EnumerateAvailableServices() { |
| 174 | return manager_->EnumerateAvailableServices(); |
| 175 | } |
| 176 | |
| 177 | vector<string> Profile::EnumerateEntries() { |
| 178 | vector<string> rpc_ids; |
| 179 | map<string, ServiceRefPtr>::const_iterator it; |
| 180 | for (it = services_.begin(); it != services_.end(); ++it) { |
| 181 | rpc_ids.push_back(it->second->GetRpcIdentifier()); |
| 182 | } |
| 183 | return rpc_ids; |
| 184 | } |
| 185 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 186 | void Profile::HelpRegisterDerivedStrings( |
| 187 | const string &name, |
| 188 | Strings(Profile::*get)(void), |
| 189 | void(Profile::*set)(const Strings&, Error *)) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 190 | store_.RegisterDerivedStrings( |
| 191 | name, |
| 192 | StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, set))); |
| 193 | } |
| 194 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 195 | bool Profile::SaveServices(StoreInterface *storage) { |
| 196 | for (map<string, ServiceRefPtr>::iterator it = services_.begin(); |
| 197 | it != services_.end(); |
| 198 | ++it) { |
| 199 | VLOG(1) << "Saving service named " << it->first; |
| 200 | if (!it->second->Save(storage)) |
| 201 | return false; |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 206 | } // namespace shill |