blob: e03c09e76535e845855611a42d50d17ebce6c565 [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
7#include <string>
8
9#include <base/logging.h>
Darin Petkova4766822011-07-07 10:42:22 -070010#include <base/string_util.h>
Chris Masone88cbd5f2011-07-03 14:30:04 -070011#include <chromeos/dbus/service_constants.h>
Chris Masone52cd19b2011-06-29 17:23:04 -070012
13#include "shill/adaptor_interfaces.h"
14#include "shill/control_interface.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070015#include "shill/entry.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070016#include "shill/property_accessor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070017#include "shill/service.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070018
19using std::string;
20
21namespace shill {
Darin Petkova4766822011-07-07 10:42:22 -070022
23const char Profile::kGlobalStorageDir[] = "/var/cache/flimflam";
24const char Profile::kUserStorageDirFormat[] = "/home/%s/user/flimflam";
25
26Profile::Profile(ControlInterface *control_interface,
27 GLib *glib)
28 : adaptor_(control_interface->CreateProfileAdaptor(this)),
29 storage_(glib) {
Chris Masone88cbd5f2011-07-03 14:30:04 -070030 // 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 Masone52cd19b2011-06-29 17:23:04 -070044}
45
46Profile::~Profile() {}
47
Chris Masone7aa5f902011-07-11 11:13:35 -070048void Profile::AdoptEntry(const std::string &entry_name,
49 const EntryRefPtr &entry) {
50 entry->profile_name = name();
51 entries_[entry_name] = entry;
52}
53
Darin Petkova4766822011-07-07 10:42:22 -070054bool 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
66bool 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
95string Profile::GetRpcPath(const Identifier &identifier) {
96 string user = identifier.user.empty() ? "" : identifier.user + "/";
97 return "/profile/" + user + identifier.identifier;
98}
99
100bool 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 Masone52cd19b2011-06-29 17:23:04 -0700111} // namespace shill