blob: daa171dd0f8e5df35e65829b6b8a0db1cf47681d [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
Darin Petkova4766822011-07-07 10:42:22 -070019class FilePath;
20
Chris Masone52cd19b2011-06-29 17:23:04 -070021namespace shill {
22
23class ControlInterface;
24class Error;
Chris Masoneb9c00592011-10-06 13:10:39 -070025class GLib;
Chris Masone6791a432011-07-12 13:23:19 -070026class Manager;
Chris Masone52cd19b2011-06-29 17:23:04 -070027class ProfileAdaptorInterface;
Chris Masone9d779932011-08-25 16:33:41 -070028class StoreInterface;
Chris Masone52cd19b2011-06-29 17:23:04 -070029
Chris Masone7aa5f902011-07-11 11:13:35 -070030class Profile : public base::RefCounted<Profile> {
Chris Masone52cd19b2011-06-29 17:23:04 -070031 public:
Paul Stewart5dc40aa2011-10-28 19:43:43 -070032 enum InitStorageOption {
33 kOpenExisting,
34 kCreateNew,
35 kCreateOrOpenExisting
36 };
Darin Petkova4766822011-07-07 10:42:22 -070037 struct Identifier {
Chris Masone7df0c672011-07-15 10:24:54 -070038 Identifier() {}
39 explicit Identifier(const std::string &i) : identifier(i) {}
40 Identifier(const std::string &u, const std::string &i)
41 : user(u),
42 identifier(i) {
43 }
Darin Petkova4766822011-07-07 10:42:22 -070044 std::string user; // Empty for global.
45 std::string identifier;
46 };
47
Chris Masone7df0c672011-07-15 10:24:54 -070048 Profile(ControlInterface *control_interface,
Chris Masone7df0c672011-07-15 10:24:54 -070049 Manager *manager,
50 const Identifier &name,
Chris Masone2ae797d2011-08-23 20:41:00 -070051 const std::string &user_storage_format,
Chris Masone7df0c672011-07-15 10:24:54 -070052 bool connect_to_rpc);
Chris Masoneb9c00592011-10-06 13:10:39 -070053
Chris Masone52cd19b2011-06-29 17:23:04 -070054 virtual ~Profile();
55
Chris Masoneb9c00592011-10-06 13:10:39 -070056 // Set up persistent storage for this Profile.
Paul Stewart5dc40aa2011-10-28 19:43:43 -070057 bool InitStorage(GLib *glib,
58 InitStorageOption storage_option,
59 Error *error);
Chris Masoneb9c00592011-10-06 13:10:39 -070060
Paul Stewarte73d05c2012-03-29 16:26:05 -070061 // Remove the persisitent storage for this Profile. It is an error to
62 // do so while the underlying storage is open via InitStorage() or
63 // set_storage().
64 bool RemoveStorage(GLib *glib, Error *error);
65
Chris Masone7df0c672011-07-15 10:24:54 -070066 std::string GetFriendlyName();
67
Paul Stewart1b1a7f22012-01-06 16:24:06 -080068 virtual std::string GetRpcIdentifier();
Chris Masone7aa5f902011-07-11 11:13:35 -070069
mukesh agrawalde29fa82011-09-16 16:16:36 -070070 PropertyStore *mutable_store() { return &store_; }
71 const PropertyStore &store() const { return store_; }
Chris Masone52cd19b2011-06-29 17:23:04 -070072
Paul Stewarte73d05c2012-03-29 16:26:05 -070073 // Set the storage inteface. This is used for testing purposes. It
74 // takes ownership of |storage|.
75 void set_storage(StoreInterface *storage);
Chris Masoneb9c00592011-10-06 13:10:39 -070076
Chris Masone6791a432011-07-12 13:23:19 -070077 // Begin managing the persistence of |service|.
78 // Returns true if |service| is new to this profile and was added,
79 // false if the |service| already existed.
Chris Masone6515aab2011-10-12 16:19:09 -070080 virtual bool AdoptService(const ServiceRefPtr &service);
Chris Masone7aa5f902011-07-11 11:13:35 -070081
Chris Masone6515aab2011-10-12 16:19:09 -070082 // Cease managing the persistence of the Service |service|.
83 // Returns true if |service| was found and abandoned, or not found.
84 // Returns false if can't be abandoned.
85 virtual bool AbandonService(const ServiceRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -070086
Chris Masone6515aab2011-10-12 16:19:09 -070087 // Clobbers persisted notion of |service| with data from |service|.
88 // Returns true if |service| was found and updated, false if not found.
Paul Stewart7f61e522012-03-22 11:13:45 -070089 virtual bool UpdateService(const ServiceRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -070090
Paul Stewartbba6a5b2011-11-02 18:45:59 -070091 // Ask |service| if it can configure itself from the profile. If it can,
92 // change the service to point at this profile, ask |service| to perform
93 // the configuration and return true. If not, return false.
Paul Stewarta41e38d2011-11-11 07:47:29 -080094 virtual bool ConfigureService(const ServiceRefPtr &service);
95
96 // Allow the device to configure itself from this profile. Returns
97 // true if the device succeeded in finding its configuration. If not,
98 // return false.
99 virtual bool ConfigureDevice(const DeviceRefPtr &device);
Chris Masone6791a432011-07-12 13:23:19 -0700100
Paul Stewart75225512012-01-26 22:51:33 -0800101 // Remove a named entry from the profile. This includes detaching
102 // any service that uses this profile entry.
103 virtual void DeleteEntry(const std::string &entry_name, Error *error);
104
Paul Stewart0756db92012-01-27 08:34:47 -0800105 // Return a service configured from the given profile entry.
106 virtual ServiceRefPtr GetServiceFromEntry(const std::string &entry_name,
107 Error *error);
108
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700109 // Return whether |service| can configure itself from the profile.
Chris Masone6515aab2011-10-12 16:19:09 -0700110 bool ContainsService(const ServiceConstRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -0700111
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800112 std::vector<std::string> EnumerateAvailableServices(Error *error);
113 std::vector<std::string> EnumerateEntries(Error *error);
Chris Masone6791a432011-07-12 13:23:19 -0700114
Chris Masoneb9c00592011-10-06 13:10:39 -0700115 // Write all in-memory state to disk via |storage_|.
116 virtual bool Save();
Chris Masone9d779932011-08-25 16:33:41 -0700117
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700118 // Parses a profile identifier. There're two acceptable forms of the |raw|
119 // identifier: "identifier" and "~user/identifier". Both "user" and
120 // "identifier" must be suitable for use in a D-Bus object path. Returns true
121 // on success.
122 static bool ParseIdentifier(const std::string &raw, Identifier *parsed);
123
124 // Returns whether |name| matches this Profile's |name_|.
125 virtual bool MatchesIdentifier(const Identifier &name) const;
Darin Petkova4766822011-07-07 10:42:22 -0700126
Paul Stewartd0a3b812012-03-28 22:48:22 -0700127 // Returns the username component of the profile identifier.
128 const std::string &GetUser() const { return name_.user; }
129
Paul Stewart0756db92012-01-27 08:34:47 -0800130 // Returns a read-only copy of the backing storage of the profile.
Paul Stewart66815332012-04-09 18:09:36 -0700131 virtual const StoreInterface *GetConstStorage() const {
132 return storage_.get();
133 }
Paul Stewart0756db92012-01-27 08:34:47 -0800134
Chris Masone88cbd5f2011-07-03 14:30:04 -0700135 protected:
Paul Stewartac4ac002011-08-26 12:04:26 -0700136 // Protected getters
137 Manager *manager() const { return manager_; }
Chris Masoneb9c00592011-10-06 13:10:39 -0700138 StoreInterface *storage() { return storage_.get(); }
Chris Masone7aa5f902011-07-11 11:13:35 -0700139
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700140 // Sets |path| to the persistent store file path for a profile identified by
141 // |name_|. Returns true on success, and false if unable to determine an
142 // appropriate file location. |name_| must be a valid identifier,
143 // possibly parsed and validated through Profile::ParseIdentifier.
144 //
145 // In the default implementation, |name_.user| cannot be empty, because
146 // all regular profiles should be associated with a user.
147 virtual bool GetStoragePath(FilePath *path);
148
Chris Masone52cd19b2011-06-29 17:23:04 -0700149 private:
Chris Masone88cbd5f2011-07-03 14:30:04 -0700150 friend class ProfileAdaptorInterface;
Paul Stewart75225512012-01-26 22:51:33 -0800151 FRIEND_TEST(ProfileTest, DeleteEntry);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700152 FRIEND_TEST(ProfileTest, GetStoragePath);
Darin Petkova4766822011-07-07 10:42:22 -0700153 FRIEND_TEST(ProfileTest, IsValidIdentifierToken);
154
155 static bool IsValidIdentifierToken(const std::string &token);
Chris Masone88cbd5f2011-07-03 14:30:04 -0700156
mukesh agrawalffa3d042011-10-06 15:26:10 -0700157 void HelpRegisterDerivedStrings(
158 const std::string &name,
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800159 Strings(Profile::*get)(Error *error),
160 void(Profile::*set)(const Strings&, Error *error));
Chris Masone6791a432011-07-12 13:23:19 -0700161
Paul Stewartac4ac002011-08-26 12:04:26 -0700162 // Data members shared with subclasses via getter/setters above in the
163 // protected: section
164 Manager *manager_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700165
Chris Masoneb9c00592011-10-06 13:10:39 -0700166 // Shared with |adaptor_| via public getter.
Paul Stewartac4ac002011-08-26 12:04:26 -0700167 PropertyStore store_;
Chris Masone7df0c672011-07-15 10:24:54 -0700168
169 // Properties to be gotten via PropertyStore calls.
170 Identifier name_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700171
Chris Masone2ae797d2011-08-23 20:41:00 -0700172 // Format string used to generate paths to user profile directories.
173 const std::string storage_format_;
174
Chris Masoneb9c00592011-10-06 13:10:39 -0700175 // Allows this profile to be backed with on-disk storage.
176 scoped_ptr<StoreInterface> storage_;
177
Chris Masone7df0c672011-07-15 10:24:54 -0700178 scoped_ptr<ProfileAdaptorInterface> adaptor_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700179
Chris Masone88cbd5f2011-07-03 14:30:04 -0700180 DISALLOW_COPY_AND_ASSIGN(Profile);
Chris Masone52cd19b2011-06-29 17:23:04 -0700181};
182
183} // namespace shill
184
185#endif // SHILL_PROFILE_