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 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/logging.h> |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 10 | #include <base/string_util.h> |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 12 | |
| 13 | #include "shill/adaptor_interfaces.h" |
| 14 | #include "shill/control_interface.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame^] | 15 | #include "shill/entry.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 16 | #include "shill/property_accessor.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame^] | 17 | #include "shill/service.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 18 | |
| 19 | using std::string; |
| 20 | |
| 21 | namespace shill { |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 22 | |
| 23 | const char Profile::kGlobalStorageDir[] = "/var/cache/flimflam"; |
| 24 | const char Profile::kUserStorageDirFormat[] = "/home/%s/user/flimflam"; |
| 25 | |
| 26 | Profile::Profile(ControlInterface *control_interface, |
| 27 | GLib *glib) |
| 28 | : adaptor_(control_interface->CreateProfileAdaptor(this)), |
| 29 | storage_(glib) { |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 30 | // flimflam::kCheckPortalListProperty: Registered in DefaultProfile |
| 31 | // flimflam::kCountryProperty: Registered in DefaultProfile |
| 32 | store_.RegisterConstString(flimflam::kNameProperty, &name_); |
| 33 | |
| 34 | // flimflam::kOfflineModeProperty: Registered in DefaultProfile |
| 35 | // flimflam::kPortalURLProperty: Registered in DefaultProfile |
| 36 | |
| 37 | // TODO(cmasone): Implement these once we figure out where Profiles fit. |
| 38 | // HelpRegisterDerivedStrings(flimflam::kServicesProperty, |
| 39 | // &Manager::EnumerateAvailableServices, |
| 40 | // NULL); |
| 41 | // HelpRegisterDerivedStrings(flimflam::kEntriesProperty, |
| 42 | // &Manager::EnumerateEntries, |
| 43 | // NULL); |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | Profile::~Profile() {} |
| 47 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame^] | 48 | void Profile::AdoptEntry(const std::string &entry_name, |
| 49 | const EntryRefPtr &entry) { |
| 50 | entry->profile_name = name(); |
| 51 | entries_[entry_name] = entry; |
| 52 | } |
| 53 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 54 | bool Profile::IsValidIdentifierToken(const std::string &token) { |
| 55 | if (token.empty()) { |
| 56 | return false; |
| 57 | } |
| 58 | for (string::const_iterator it = token.begin(); it != token.end(); ++it) { |
| 59 | if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it)) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) { |
| 67 | if (raw.empty()) { |
| 68 | return false; |
| 69 | } |
| 70 | if (raw[0] == '~') { |
| 71 | // Format: "~user/identifier". |
| 72 | size_t slash = raw.find('/'); |
| 73 | if (slash == string::npos) { |
| 74 | return false; |
| 75 | } |
| 76 | string user(raw.begin() + 1, raw.begin() + slash); |
| 77 | string identifier(raw.begin() + slash + 1, raw.end()); |
| 78 | if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) { |
| 79 | return false; |
| 80 | } |
| 81 | parsed->user = user; |
| 82 | parsed->identifier = identifier; |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | // Format: "identifier". |
| 87 | if (!IsValidIdentifierToken(raw)) { |
| 88 | return false; |
| 89 | } |
| 90 | parsed->user = ""; |
| 91 | parsed->identifier = raw; |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | string Profile::GetRpcPath(const Identifier &identifier) { |
| 96 | string user = identifier.user.empty() ? "" : identifier.user + "/"; |
| 97 | return "/profile/" + user + identifier.identifier; |
| 98 | } |
| 99 | |
| 100 | bool Profile::GetStoragePath(const Identifier &identifier, FilePath *path) { |
| 101 | FilePath dir( |
| 102 | identifier.user.empty() ? |
| 103 | kGlobalStorageDir : |
| 104 | base::StringPrintf(kUserStorageDirFormat, identifier.user.c_str())); |
| 105 | // TODO(petkov): Validate the directory permissions, etc. |
| 106 | *path = dir.Append(base::StringPrintf("%s.profile", |
| 107 | identifier.identifier.c_str())); |
| 108 | return true; |
| 109 | } |
| 110 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 111 | } // namespace shill |