blob: 8c390ef3419ee5ddc664514e817586ee284df0be [file] [log] [blame]
Paul Stewart1b1a7f22012-01-06 16:24:06 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone52cd19b2011-06-29 17:23:04 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wade Guthrie60a37062013-04-02 11:39:09 -07005#ifndef SHILL_PROFILE_H_
6#define SHILL_PROFILE_H_
Chris Masone52cd19b2011-06-29 17:23:04 -07007
Chris Masone7aa5f902011-07-11 11:13:35 -07008#include <map>
Chris Masone52cd19b2011-06-29 17:23:04 -07009#include <string>
10#include <vector>
11
12#include <base/memory/scoped_ptr.h>
mukesh agrawal0a59a5a2014-04-24 19:10:46 -070013#include <base/files/file_path.h>
Darin Petkova4766822011-07-07 10:42:22 -070014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Chris Masone52cd19b2011-06-29 17:23:04 -070015
Paul Stewart26b327e2011-10-19 11:38:09 -070016#include "shill/event_dispatcher.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070017#include "shill/property_store.h"
18#include "shill/refptr_types.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070019
Paul Stewart5ad16062013-02-21 18:10:48 -080020namespace base {
21
Darin Petkova4766822011-07-07 10:42:22 -070022class FilePath;
23
Paul Stewart5ad16062013-02-21 18:10:48 -080024} // namespace base
25
Chris Masone52cd19b2011-06-29 17:23:04 -070026namespace shill {
27
28class ControlInterface;
29class Error;
Chris Masoneb9c00592011-10-06 13:10:39 -070030class GLib;
Chris Masone6791a432011-07-12 13:23:19 -070031class Manager;
Thieu Le5133b712013-02-19 14:47:21 -080032class Metrics;
Chris Masone52cd19b2011-06-29 17:23:04 -070033class ProfileAdaptorInterface;
Chris Masone9d779932011-08-25 16:33:41 -070034class StoreInterface;
Wade Guthrie60a37062013-04-02 11:39:09 -070035class WiFiProvider;
Chris Masone52cd19b2011-06-29 17:23:04 -070036
Chris Masone7aa5f902011-07-11 11:13:35 -070037class Profile : public base::RefCounted<Profile> {
Chris Masone52cd19b2011-06-29 17:23:04 -070038 public:
Paul Stewart5dc40aa2011-10-28 19:43:43 -070039 enum InitStorageOption {
40 kOpenExisting,
41 kCreateNew,
42 kCreateOrOpenExisting
43 };
Darin Petkova4766822011-07-07 10:42:22 -070044 struct Identifier {
Chris Masone7df0c672011-07-15 10:24:54 -070045 Identifier() {}
46 explicit Identifier(const std::string &i) : identifier(i) {}
47 Identifier(const std::string &u, const std::string &i)
48 : user(u),
49 identifier(i) {
50 }
Darin Petkova4766822011-07-07 10:42:22 -070051 std::string user; // Empty for global.
52 std::string identifier;
Paul Stewartf3eced92013-04-17 12:18:22 -070053 std::string user_hash;
Darin Petkova4766822011-07-07 10:42:22 -070054 };
55
Paul Stewart78af94c2013-04-17 16:02:06 -070056 // Path to the cached list of inserted user profiles to be loaded at
57 // startup.
58 static const char kUserProfileListPathname[];
59
Chris Masone7df0c672011-07-15 10:24:54 -070060 Profile(ControlInterface *control_interface,
Thieu Le5133b712013-02-19 14:47:21 -080061 Metrics *metrics,
Chris Masone7df0c672011-07-15 10:24:54 -070062 Manager *manager,
63 const Identifier &name,
mukesh agrawal0a59a5a2014-04-24 19:10:46 -070064 const std::string &user_storage_directory,
Chris Masone7df0c672011-07-15 10:24:54 -070065 bool connect_to_rpc);
Chris Masoneb9c00592011-10-06 13:10:39 -070066
Chris Masone52cd19b2011-06-29 17:23:04 -070067 virtual ~Profile();
68
Chris Masoneb9c00592011-10-06 13:10:39 -070069 // Set up persistent storage for this Profile.
Paul Stewart5dc40aa2011-10-28 19:43:43 -070070 bool InitStorage(GLib *glib,
71 InitStorageOption storage_option,
72 Error *error);
Chris Masoneb9c00592011-10-06 13:10:39 -070073
mukesh agrawal92496a42014-04-08 16:04:43 -070074 // Set up stub storage for this Profile. The data will NOT be
75 // persisted. In most cases, you should prefer InitStorage.
76 void InitStubStorage();
77
78 // Remove the persistent storage for this Profile. It is an error to
Paul Stewarte73d05c2012-03-29 16:26:05 -070079 // do so while the underlying storage is open via InitStorage() or
80 // set_storage().
81 bool RemoveStorage(GLib *glib, Error *error);
82
Chris Masone7df0c672011-07-15 10:24:54 -070083 std::string GetFriendlyName();
84
Paul Stewart1b1a7f22012-01-06 16:24:06 -080085 virtual std::string GetRpcIdentifier();
Chris Masone7aa5f902011-07-11 11:13:35 -070086
mukesh agrawalde29fa82011-09-16 16:16:36 -070087 PropertyStore *mutable_store() { return &store_; }
88 const PropertyStore &store() const { return store_; }
Chris Masone52cd19b2011-06-29 17:23:04 -070089
Paul Stewarte73d05c2012-03-29 16:26:05 -070090 // Set the storage inteface. This is used for testing purposes. It
91 // takes ownership of |storage|.
92 void set_storage(StoreInterface *storage);
Chris Masoneb9c00592011-10-06 13:10:39 -070093
Chris Masone6791a432011-07-12 13:23:19 -070094 // Begin managing the persistence of |service|.
95 // Returns true if |service| is new to this profile and was added,
96 // false if the |service| already existed.
Chris Masone6515aab2011-10-12 16:19:09 -070097 virtual bool AdoptService(const ServiceRefPtr &service);
Chris Masone7aa5f902011-07-11 11:13:35 -070098
Chris Masone6515aab2011-10-12 16:19:09 -070099 // Cease managing the persistence of the Service |service|.
100 // Returns true if |service| was found and abandoned, or not found.
101 // Returns false if can't be abandoned.
102 virtual bool AbandonService(const ServiceRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -0700103
Chris Masone6515aab2011-10-12 16:19:09 -0700104 // Clobbers persisted notion of |service| with data from |service|.
105 // Returns true if |service| was found and updated, false if not found.
Paul Stewart7f61e522012-03-22 11:13:45 -0700106 virtual bool UpdateService(const ServiceRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -0700107
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700108 // Ask |service| if it can configure itself from the profile. If it can,
Paul Stewart2c575d22012-12-07 12:28:57 -0800109 // ask |service| to perform the configuration and return true. If not,
110 // return false.
111 virtual bool LoadService(const ServiceRefPtr &service);
112
113 // Perform LoadService() on |service|. If this succeeds, change
114 // the service to point at this profile and return true. If not, return
115 // false.
Paul Stewarta41e38d2011-11-11 07:47:29 -0800116 virtual bool ConfigureService(const ServiceRefPtr &service);
117
118 // Allow the device to configure itself from this profile. Returns
119 // true if the device succeeded in finding its configuration. If not,
120 // return false.
121 virtual bool ConfigureDevice(const DeviceRefPtr &device);
Chris Masone6791a432011-07-12 13:23:19 -0700122
Paul Stewart75225512012-01-26 22:51:33 -0800123 // Remove a named entry from the profile. This includes detaching
124 // any service that uses this profile entry.
125 virtual void DeleteEntry(const std::string &entry_name, Error *error);
126
Paul Stewart0756db92012-01-27 08:34:47 -0800127 // Return a service configured from the given profile entry.
128 virtual ServiceRefPtr GetServiceFromEntry(const std::string &entry_name,
129 Error *error);
130
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700131 // Return whether |service| can configure itself from the profile.
Chris Masone6515aab2011-10-12 16:19:09 -0700132 bool ContainsService(const ServiceConstRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -0700133
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800134 std::vector<std::string> EnumerateAvailableServices(Error *error);
135 std::vector<std::string> EnumerateEntries(Error *error);
Chris Masone6791a432011-07-12 13:23:19 -0700136
Darin Petkove7c6ad32012-06-29 10:22:09 +0200137 // Clobbers persisted notion of |device| with data from |device|. Returns true
138 // if |device| was found and updated, false otherwise. The base implementation
139 // always returns false -- currently devices are persisted only in
140 // DefaultProfile.
141 virtual bool UpdateDevice(const DeviceRefPtr &device);
142
Wade Guthrie60a37062013-04-02 11:39:09 -0700143 // Clobbers persisted notion of |wifi_provider| with data from
144 // |wifi_provider|. Returns true if |wifi_provider| was found and updated,
145 // false otherwise. The base implementation always returns false -- currently
146 // wifi_provider is persisted only in DefaultProfile.
147 virtual bool UpdateWiFiProvider(const WiFiProvider &wifi_provider);
148
Chris Masoneb9c00592011-10-06 13:10:39 -0700149 // Write all in-memory state to disk via |storage_|.
150 virtual bool Save();
Chris Masone9d779932011-08-25 16:33:41 -0700151
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700152 // Parses a profile identifier. There're two acceptable forms of the |raw|
153 // identifier: "identifier" and "~user/identifier". Both "user" and
154 // "identifier" must be suitable for use in a D-Bus object path. Returns true
155 // on success.
156 static bool ParseIdentifier(const std::string &raw, Identifier *parsed);
157
Paul Stewartf3eced92013-04-17 12:18:22 -0700158 // Returns the composite string identifier for a profile, as would have
159 // been used in an argument to Manager::PushProfile() in creating this
160 // profile. It returns a string in the form "identifier", or
161 // "~user/identifier" depending on whether this profile has a user
162 // component.
163 static std::string IdentifierToString(const Identifier &name);
164
Paul Stewart78af94c2013-04-17 16:02:06 -0700165 // Load a list of user profile identifiers from a cache file |path|.
166 // The profiles themselves are not loaded.
167 static std::vector<Identifier> LoadUserProfileList(
168 const base::FilePath &path);
169
170 // Save a list of user profile identifiers |profiles| to a cache file |path|.
171 // Returns true if successful, false otherwise.
172 static bool SaveUserProfileList(const base::FilePath &path,
173 const std::vector<ProfileRefPtr> &profiles);
174
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700175 // Returns whether |name| matches this Profile's |name_|.
176 virtual bool MatchesIdentifier(const Identifier &name) const;
Darin Petkova4766822011-07-07 10:42:22 -0700177
Paul Stewartd0a3b812012-03-28 22:48:22 -0700178 // Returns the username component of the profile identifier.
179 const std::string &GetUser() const { return name_.user; }
180
Paul Stewartf3eced92013-04-17 12:18:22 -0700181 // Returns the user_hash component of the profile identifier.
182 const std::string &GetUserHash() const { return name_.user_hash; }
183
Paul Stewart0756db92012-01-27 08:34:47 -0800184 // Returns a read-only copy of the backing storage of the profile.
Paul Stewart66815332012-04-09 18:09:36 -0700185 virtual const StoreInterface *GetConstStorage() const {
186 return storage_.get();
187 }
Paul Stewart0756db92012-01-27 08:34:47 -0800188
Chris Masone88cbd5f2011-07-03 14:30:04 -0700189 protected:
Paul Stewartac4ac002011-08-26 12:04:26 -0700190 // Protected getters
Thieu Le5133b712013-02-19 14:47:21 -0800191 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700192 Manager *manager() const { return manager_; }
Chris Masoneb9c00592011-10-06 13:10:39 -0700193 StoreInterface *storage() { return storage_.get(); }
Chris Masone7aa5f902011-07-11 11:13:35 -0700194
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700195 // Sets |path| to the persistent store file path for a profile identified by
196 // |name_|. Returns true on success, and false if unable to determine an
197 // appropriate file location. |name_| must be a valid identifier,
198 // possibly parsed and validated through Profile::ParseIdentifier.
199 //
200 // In the default implementation, |name_.user| cannot be empty, because
201 // all regular profiles should be associated with a user.
Paul Stewart5ad16062013-02-21 18:10:48 -0800202 virtual bool GetStoragePath(base::FilePath *path);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700203
Chris Masone52cd19b2011-06-29 17:23:04 -0700204 private:
Chris Masone88cbd5f2011-07-03 14:30:04 -0700205 friend class ProfileAdaptorInterface;
Paul Stewart75225512012-01-26 22:51:33 -0800206 FRIEND_TEST(ProfileTest, DeleteEntry);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700207 FRIEND_TEST(ProfileTest, GetStoragePath);
Darin Petkova4766822011-07-07 10:42:22 -0700208 FRIEND_TEST(ProfileTest, IsValidIdentifierToken);
209
210 static bool IsValidIdentifierToken(const std::string &token);
Chris Masone88cbd5f2011-07-03 14:30:04 -0700211
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700212 void HelpRegisterConstDerivedStrings(
mukesh agrawalffa3d042011-10-06 15:26:10 -0700213 const std::string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700214 Strings(Profile::*get)(Error *error));
Chris Masone6791a432011-07-12 13:23:19 -0700215
Paul Stewartac4ac002011-08-26 12:04:26 -0700216 // Data members shared with subclasses via getter/setters above in the
217 // protected: section
Thieu Le5133b712013-02-19 14:47:21 -0800218 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700219 Manager *manager_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700220
Chris Masoneb9c00592011-10-06 13:10:39 -0700221 // Shared with |adaptor_| via public getter.
Paul Stewartac4ac002011-08-26 12:04:26 -0700222 PropertyStore store_;
Chris Masone7df0c672011-07-15 10:24:54 -0700223
224 // Properties to be gotten via PropertyStore calls.
225 Identifier name_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700226
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700227 // Path to user profile directory.
228 const base::FilePath storage_path_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700229
Chris Masoneb9c00592011-10-06 13:10:39 -0700230 // Allows this profile to be backed with on-disk storage.
231 scoped_ptr<StoreInterface> storage_;
232
Chris Masone7df0c672011-07-15 10:24:54 -0700233 scoped_ptr<ProfileAdaptorInterface> adaptor_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700234
Chris Masone88cbd5f2011-07-03 14:30:04 -0700235 DISALLOW_COPY_AND_ASSIGN(Profile);
Chris Masone52cd19b2011-06-29 17:23:04 -0700236};
237
238} // namespace shill
239
Wade Guthrie60a37062013-04-02 11:39:09 -0700240#endif // SHILL_PROFILE_H_