blob: c1e92a4be99a1059cff3d6efe40c7bb493f04f4f [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 Masone52cd19b2011-06-29 17:23:04 -070015#include "shill/property_accessor.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070016
17using std::string;
18
19namespace shill {
Darin Petkova4766822011-07-07 10:42:22 -070020
21const char Profile::kGlobalStorageDir[] = "/var/cache/flimflam";
22const char Profile::kUserStorageDirFormat[] = "/home/%s/user/flimflam";
23
24Profile::Profile(ControlInterface *control_interface,
25 GLib *glib)
26 : adaptor_(control_interface->CreateProfileAdaptor(this)),
27 storage_(glib) {
Chris Masone88cbd5f2011-07-03 14:30:04 -070028 // flimflam::kCheckPortalListProperty: Registered in DefaultProfile
29 // flimflam::kCountryProperty: Registered in DefaultProfile
30 store_.RegisterConstString(flimflam::kNameProperty, &name_);
31
32 // flimflam::kOfflineModeProperty: Registered in DefaultProfile
33 // flimflam::kPortalURLProperty: Registered in DefaultProfile
34
35 // TODO(cmasone): Implement these once we figure out where Profiles fit.
36 // HelpRegisterDerivedStrings(flimflam::kServicesProperty,
37 // &Manager::EnumerateAvailableServices,
38 // NULL);
39 // HelpRegisterDerivedStrings(flimflam::kEntriesProperty,
40 // &Manager::EnumerateEntries,
41 // NULL);
Chris Masone52cd19b2011-06-29 17:23:04 -070042}
43
44Profile::~Profile() {}
45
Darin Petkova4766822011-07-07 10:42:22 -070046bool Profile::IsValidIdentifierToken(const std::string &token) {
47 if (token.empty()) {
48 return false;
49 }
50 for (string::const_iterator it = token.begin(); it != token.end(); ++it) {
51 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it)) {
52 return false;
53 }
54 }
55 return true;
56}
57
58bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) {
59 if (raw.empty()) {
60 return false;
61 }
62 if (raw[0] == '~') {
63 // Format: "~user/identifier".
64 size_t slash = raw.find('/');
65 if (slash == string::npos) {
66 return false;
67 }
68 string user(raw.begin() + 1, raw.begin() + slash);
69 string identifier(raw.begin() + slash + 1, raw.end());
70 if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) {
71 return false;
72 }
73 parsed->user = user;
74 parsed->identifier = identifier;
75 return true;
76 }
77
78 // Format: "identifier".
79 if (!IsValidIdentifierToken(raw)) {
80 return false;
81 }
82 parsed->user = "";
83 parsed->identifier = raw;
84 return true;
85}
86
87string Profile::GetRpcPath(const Identifier &identifier) {
88 string user = identifier.user.empty() ? "" : identifier.user + "/";
89 return "/profile/" + user + identifier.identifier;
90}
91
92bool Profile::GetStoragePath(const Identifier &identifier, FilePath *path) {
93 FilePath dir(
94 identifier.user.empty() ?
95 kGlobalStorageDir :
96 base::StringPrintf(kUserStorageDirFormat, identifier.user.c_str()));
97 // TODO(petkov): Validate the directory permissions, etc.
98 *path = dir.Append(base::StringPrintf("%s.profile",
99 identifier.identifier.c_str()));
100 return true;
101}
102
Chris Masone52cd19b2011-06-29 17:23:04 -0700103} // namespace shill