blob: a6e5abd8ae45d3d26c468e9874cd87807e110d51 [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
5#ifndef SHILL_PROFILE_
6#define SHILL_PROFILE_
7
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>
Darin Petkova4766822011-07-07 10:42:22 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Chris Masone52cd19b2011-06-29 17:23:04 -070014
Paul Stewart26b327e2011-10-19 11:38:09 -070015#include "shill/event_dispatcher.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070016#include "shill/property_store.h"
17#include "shill/refptr_types.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070018
Paul Stewart5ad16062013-02-21 18:10:48 -080019namespace base {
20
Darin Petkova4766822011-07-07 10:42:22 -070021class FilePath;
22
Paul Stewart5ad16062013-02-21 18:10:48 -080023} // namespace base
24
Chris Masone52cd19b2011-06-29 17:23:04 -070025namespace shill {
26
27class ControlInterface;
28class Error;
Chris Masoneb9c00592011-10-06 13:10:39 -070029class GLib;
Chris Masone6791a432011-07-12 13:23:19 -070030class Manager;
Thieu Le5133b712013-02-19 14:47:21 -080031class Metrics;
Chris Masone52cd19b2011-06-29 17:23:04 -070032class ProfileAdaptorInterface;
Chris Masone9d779932011-08-25 16:33:41 -070033class StoreInterface;
Chris Masone52cd19b2011-06-29 17:23:04 -070034
Chris Masone7aa5f902011-07-11 11:13:35 -070035class Profile : public base::RefCounted<Profile> {
Chris Masone52cd19b2011-06-29 17:23:04 -070036 public:
Paul Stewart5dc40aa2011-10-28 19:43:43 -070037 enum InitStorageOption {
38 kOpenExisting,
39 kCreateNew,
40 kCreateOrOpenExisting
41 };
Darin Petkova4766822011-07-07 10:42:22 -070042 struct Identifier {
Chris Masone7df0c672011-07-15 10:24:54 -070043 Identifier() {}
44 explicit Identifier(const std::string &i) : identifier(i) {}
45 Identifier(const std::string &u, const std::string &i)
46 : user(u),
47 identifier(i) {
48 }
Darin Petkova4766822011-07-07 10:42:22 -070049 std::string user; // Empty for global.
50 std::string identifier;
51 };
52
Chris Masone7df0c672011-07-15 10:24:54 -070053 Profile(ControlInterface *control_interface,
Thieu Le5133b712013-02-19 14:47:21 -080054 Metrics *metrics,
Chris Masone7df0c672011-07-15 10:24:54 -070055 Manager *manager,
56 const Identifier &name,
Chris Masone2ae797d2011-08-23 20:41:00 -070057 const std::string &user_storage_format,
Chris Masone7df0c672011-07-15 10:24:54 -070058 bool connect_to_rpc);
Chris Masoneb9c00592011-10-06 13:10:39 -070059
Chris Masone52cd19b2011-06-29 17:23:04 -070060 virtual ~Profile();
61
Chris Masoneb9c00592011-10-06 13:10:39 -070062 // Set up persistent storage for this Profile.
Paul Stewart5dc40aa2011-10-28 19:43:43 -070063 bool InitStorage(GLib *glib,
64 InitStorageOption storage_option,
65 Error *error);
Chris Masoneb9c00592011-10-06 13:10:39 -070066
Paul Stewarte73d05c2012-03-29 16:26:05 -070067 // Remove the persisitent storage for this Profile. It is an error to
68 // do so while the underlying storage is open via InitStorage() or
69 // set_storage().
70 bool RemoveStorage(GLib *glib, Error *error);
71
Chris Masone7df0c672011-07-15 10:24:54 -070072 std::string GetFriendlyName();
73
Paul Stewart1b1a7f22012-01-06 16:24:06 -080074 virtual std::string GetRpcIdentifier();
Chris Masone7aa5f902011-07-11 11:13:35 -070075
mukesh agrawalde29fa82011-09-16 16:16:36 -070076 PropertyStore *mutable_store() { return &store_; }
77 const PropertyStore &store() const { return store_; }
Chris Masone52cd19b2011-06-29 17:23:04 -070078
Paul Stewarte73d05c2012-03-29 16:26:05 -070079 // Set the storage inteface. This is used for testing purposes. It
80 // takes ownership of |storage|.
81 void set_storage(StoreInterface *storage);
Chris Masoneb9c00592011-10-06 13:10:39 -070082
Chris Masone6791a432011-07-12 13:23:19 -070083 // Begin managing the persistence of |service|.
84 // Returns true if |service| is new to this profile and was added,
85 // false if the |service| already existed.
Chris Masone6515aab2011-10-12 16:19:09 -070086 virtual bool AdoptService(const ServiceRefPtr &service);
Chris Masone7aa5f902011-07-11 11:13:35 -070087
Chris Masone6515aab2011-10-12 16:19:09 -070088 // Cease managing the persistence of the Service |service|.
89 // Returns true if |service| was found and abandoned, or not found.
90 // Returns false if can't be abandoned.
91 virtual bool AbandonService(const ServiceRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -070092
Chris Masone6515aab2011-10-12 16:19:09 -070093 // Clobbers persisted notion of |service| with data from |service|.
94 // Returns true if |service| was found and updated, false if not found.
Paul Stewart7f61e522012-03-22 11:13:45 -070095 virtual bool UpdateService(const ServiceRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -070096
Paul Stewartbba6a5b2011-11-02 18:45:59 -070097 // Ask |service| if it can configure itself from the profile. If it can,
Paul Stewart2c575d22012-12-07 12:28:57 -080098 // ask |service| to perform the configuration and return true. If not,
99 // return false.
100 virtual bool LoadService(const ServiceRefPtr &service);
101
102 // Perform LoadService() on |service|. If this succeeds, change
103 // the service to point at this profile and return true. If not, return
104 // false.
Paul Stewarta41e38d2011-11-11 07:47:29 -0800105 virtual bool ConfigureService(const ServiceRefPtr &service);
106
107 // Allow the device to configure itself from this profile. Returns
108 // true if the device succeeded in finding its configuration. If not,
109 // return false.
110 virtual bool ConfigureDevice(const DeviceRefPtr &device);
Chris Masone6791a432011-07-12 13:23:19 -0700111
Paul Stewart75225512012-01-26 22:51:33 -0800112 // Remove a named entry from the profile. This includes detaching
113 // any service that uses this profile entry.
114 virtual void DeleteEntry(const std::string &entry_name, Error *error);
115
Paul Stewart0756db92012-01-27 08:34:47 -0800116 // Return a service configured from the given profile entry.
117 virtual ServiceRefPtr GetServiceFromEntry(const std::string &entry_name,
118 Error *error);
119
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700120 // Return whether |service| can configure itself from the profile.
Chris Masone6515aab2011-10-12 16:19:09 -0700121 bool ContainsService(const ServiceConstRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -0700122
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800123 std::vector<std::string> EnumerateAvailableServices(Error *error);
124 std::vector<std::string> EnumerateEntries(Error *error);
Chris Masone6791a432011-07-12 13:23:19 -0700125
Darin Petkove7c6ad32012-06-29 10:22:09 +0200126 // Clobbers persisted notion of |device| with data from |device|. Returns true
127 // if |device| was found and updated, false otherwise. The base implementation
128 // always returns false -- currently devices are persisted only in
129 // DefaultProfile.
130 virtual bool UpdateDevice(const DeviceRefPtr &device);
131
Chris Masoneb9c00592011-10-06 13:10:39 -0700132 // Write all in-memory state to disk via |storage_|.
133 virtual bool Save();
Chris Masone9d779932011-08-25 16:33:41 -0700134
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700135 // Parses a profile identifier. There're two acceptable forms of the |raw|
136 // identifier: "identifier" and "~user/identifier". Both "user" and
137 // "identifier" must be suitable for use in a D-Bus object path. Returns true
138 // on success.
139 static bool ParseIdentifier(const std::string &raw, Identifier *parsed);
140
141 // Returns whether |name| matches this Profile's |name_|.
142 virtual bool MatchesIdentifier(const Identifier &name) const;
Darin Petkova4766822011-07-07 10:42:22 -0700143
Paul Stewartd0a3b812012-03-28 22:48:22 -0700144 // Returns the username component of the profile identifier.
145 const std::string &GetUser() const { return name_.user; }
146
Paul Stewart0756db92012-01-27 08:34:47 -0800147 // Returns a read-only copy of the backing storage of the profile.
Paul Stewart66815332012-04-09 18:09:36 -0700148 virtual const StoreInterface *GetConstStorage() const {
149 return storage_.get();
150 }
Paul Stewart0756db92012-01-27 08:34:47 -0800151
Chris Masone88cbd5f2011-07-03 14:30:04 -0700152 protected:
Paul Stewartac4ac002011-08-26 12:04:26 -0700153 // Protected getters
Thieu Le5133b712013-02-19 14:47:21 -0800154 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700155 Manager *manager() const { return manager_; }
Chris Masoneb9c00592011-10-06 13:10:39 -0700156 StoreInterface *storage() { return storage_.get(); }
Chris Masone7aa5f902011-07-11 11:13:35 -0700157
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700158 // Sets |path| to the persistent store file path for a profile identified by
159 // |name_|. Returns true on success, and false if unable to determine an
160 // appropriate file location. |name_| must be a valid identifier,
161 // possibly parsed and validated through Profile::ParseIdentifier.
162 //
163 // In the default implementation, |name_.user| cannot be empty, because
164 // all regular profiles should be associated with a user.
Paul Stewart5ad16062013-02-21 18:10:48 -0800165 virtual bool GetStoragePath(base::FilePath *path);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700166
Chris Masone52cd19b2011-06-29 17:23:04 -0700167 private:
Chris Masone88cbd5f2011-07-03 14:30:04 -0700168 friend class ProfileAdaptorInterface;
Paul Stewart75225512012-01-26 22:51:33 -0800169 FRIEND_TEST(ProfileTest, DeleteEntry);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700170 FRIEND_TEST(ProfileTest, GetStoragePath);
Darin Petkova4766822011-07-07 10:42:22 -0700171 FRIEND_TEST(ProfileTest, IsValidIdentifierToken);
172
173 static bool IsValidIdentifierToken(const std::string &token);
Chris Masone88cbd5f2011-07-03 14:30:04 -0700174
mukesh agrawalffa3d042011-10-06 15:26:10 -0700175 void HelpRegisterDerivedStrings(
176 const std::string &name,
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800177 Strings(Profile::*get)(Error *error),
178 void(Profile::*set)(const Strings&, Error *error));
Chris Masone6791a432011-07-12 13:23:19 -0700179
Paul Stewartac4ac002011-08-26 12:04:26 -0700180 // Data members shared with subclasses via getter/setters above in the
181 // protected: section
Thieu Le5133b712013-02-19 14:47:21 -0800182 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700183 Manager *manager_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700184
Chris Masoneb9c00592011-10-06 13:10:39 -0700185 // Shared with |adaptor_| via public getter.
Paul Stewartac4ac002011-08-26 12:04:26 -0700186 PropertyStore store_;
Chris Masone7df0c672011-07-15 10:24:54 -0700187
188 // Properties to be gotten via PropertyStore calls.
189 Identifier name_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700190
Chris Masone2ae797d2011-08-23 20:41:00 -0700191 // Format string used to generate paths to user profile directories.
192 const std::string storage_format_;
193
Chris Masoneb9c00592011-10-06 13:10:39 -0700194 // Allows this profile to be backed with on-disk storage.
195 scoped_ptr<StoreInterface> storage_;
196
Chris Masone7df0c672011-07-15 10:24:54 -0700197 scoped_ptr<ProfileAdaptorInterface> adaptor_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700198
Chris Masone88cbd5f2011-07-03 14:30:04 -0700199 DISALLOW_COPY_AND_ASSIGN(Profile);
Chris Masone52cd19b2011-06-29 17:23:04 -0700200};
201
202} // namespace shill
203
204#endif // SHILL_PROFILE_