Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 5 | #ifndef SHILL_PROFILE_H_ |
| 6 | #define SHILL_PROFILE_H_ |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 7 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 8 | #include <map> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 14 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 15 | #include "shill/event_dispatcher.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 16 | #include "shill/property_store.h" |
| 17 | #include "shill/refptr_types.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 18 | |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 19 | namespace base { |
| 20 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 21 | class FilePath; |
| 22 | |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 23 | } // namespace base |
| 24 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 25 | namespace shill { |
| 26 | |
| 27 | class ControlInterface; |
| 28 | class Error; |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 29 | class GLib; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 30 | class Manager; |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 31 | class Metrics; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 32 | class ProfileAdaptorInterface; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 33 | class StoreInterface; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 34 | class WiFiProvider; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 35 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 36 | class Profile : public base::RefCounted<Profile> { |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 37 | public: |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 38 | enum InitStorageOption { |
| 39 | kOpenExisting, |
| 40 | kCreateNew, |
| 41 | kCreateOrOpenExisting |
| 42 | }; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 43 | struct Identifier { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 44 | Identifier() {} |
| 45 | explicit Identifier(const std::string &i) : identifier(i) {} |
| 46 | Identifier(const std::string &u, const std::string &i) |
| 47 | : user(u), |
| 48 | identifier(i) { |
| 49 | } |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 50 | std::string user; // Empty for global. |
| 51 | std::string identifier; |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 52 | std::string user_hash; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 55 | // Path to the cached list of inserted user profiles to be loaded at |
| 56 | // startup. |
| 57 | static const char kUserProfileListPathname[]; |
| 58 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 59 | Profile(ControlInterface *control_interface, |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 60 | Metrics *metrics, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 61 | Manager *manager, |
| 62 | const Identifier &name, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 63 | const std::string &user_storage_format, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 64 | bool connect_to_rpc); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 65 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 66 | virtual ~Profile(); |
| 67 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 68 | // Set up persistent storage for this Profile. |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 69 | bool InitStorage(GLib *glib, |
| 70 | InitStorageOption storage_option, |
| 71 | Error *error); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 72 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 73 | // Remove the persisitent storage for this Profile. It is an error to |
| 74 | // do so while the underlying storage is open via InitStorage() or |
| 75 | // set_storage(). |
| 76 | bool RemoveStorage(GLib *glib, Error *error); |
| 77 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 78 | std::string GetFriendlyName(); |
| 79 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 80 | virtual std::string GetRpcIdentifier(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 81 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 82 | PropertyStore *mutable_store() { return &store_; } |
| 83 | const PropertyStore &store() const { return store_; } |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 84 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 85 | // Set the storage inteface. This is used for testing purposes. It |
| 86 | // takes ownership of |storage|. |
| 87 | void set_storage(StoreInterface *storage); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 88 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 89 | // Begin managing the persistence of |service|. |
| 90 | // Returns true if |service| is new to this profile and was added, |
| 91 | // false if the |service| already existed. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 92 | virtual bool AdoptService(const ServiceRefPtr &service); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 93 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 94 | // Cease managing the persistence of the Service |service|. |
| 95 | // Returns true if |service| was found and abandoned, or not found. |
| 96 | // Returns false if can't be abandoned. |
| 97 | virtual bool AbandonService(const ServiceRefPtr &service); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 98 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 99 | // Clobbers persisted notion of |service| with data from |service|. |
| 100 | // Returns true if |service| was found and updated, false if not found. |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 101 | virtual bool UpdateService(const ServiceRefPtr &service); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 102 | |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 103 | // Ask |service| if it can configure itself from the profile. If it can, |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 104 | // ask |service| to perform the configuration and return true. If not, |
| 105 | // return false. |
| 106 | virtual bool LoadService(const ServiceRefPtr &service); |
| 107 | |
| 108 | // Perform LoadService() on |service|. If this succeeds, change |
| 109 | // the service to point at this profile and return true. If not, return |
| 110 | // false. |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 111 | virtual bool ConfigureService(const ServiceRefPtr &service); |
| 112 | |
| 113 | // Allow the device to configure itself from this profile. Returns |
| 114 | // true if the device succeeded in finding its configuration. If not, |
| 115 | // return false. |
| 116 | virtual bool ConfigureDevice(const DeviceRefPtr &device); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 117 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 118 | // Remove a named entry from the profile. This includes detaching |
| 119 | // any service that uses this profile entry. |
| 120 | virtual void DeleteEntry(const std::string &entry_name, Error *error); |
| 121 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 122 | // Return a service configured from the given profile entry. |
| 123 | virtual ServiceRefPtr GetServiceFromEntry(const std::string &entry_name, |
| 124 | Error *error); |
| 125 | |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 126 | // Return whether |service| can configure itself from the profile. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 127 | bool ContainsService(const ServiceConstRefPtr &service); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 128 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 129 | std::vector<std::string> EnumerateAvailableServices(Error *error); |
| 130 | std::vector<std::string> EnumerateEntries(Error *error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 131 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 132 | // Clobbers persisted notion of |device| with data from |device|. Returns true |
| 133 | // if |device| was found and updated, false otherwise. The base implementation |
| 134 | // always returns false -- currently devices are persisted only in |
| 135 | // DefaultProfile. |
| 136 | virtual bool UpdateDevice(const DeviceRefPtr &device); |
| 137 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 138 | // Clobbers persisted notion of |wifi_provider| with data from |
| 139 | // |wifi_provider|. Returns true if |wifi_provider| was found and updated, |
| 140 | // false otherwise. The base implementation always returns false -- currently |
| 141 | // wifi_provider is persisted only in DefaultProfile. |
| 142 | virtual bool UpdateWiFiProvider(const WiFiProvider &wifi_provider); |
| 143 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 144 | // Write all in-memory state to disk via |storage_|. |
| 145 | virtual bool Save(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 146 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 147 | // Parses a profile identifier. There're two acceptable forms of the |raw| |
| 148 | // identifier: "identifier" and "~user/identifier". Both "user" and |
| 149 | // "identifier" must be suitable for use in a D-Bus object path. Returns true |
| 150 | // on success. |
| 151 | static bool ParseIdentifier(const std::string &raw, Identifier *parsed); |
| 152 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 153 | // Returns the composite string identifier for a profile, as would have |
| 154 | // been used in an argument to Manager::PushProfile() in creating this |
| 155 | // profile. It returns a string in the form "identifier", or |
| 156 | // "~user/identifier" depending on whether this profile has a user |
| 157 | // component. |
| 158 | static std::string IdentifierToString(const Identifier &name); |
| 159 | |
Paul Stewart | 78af94c | 2013-04-17 16:02:06 -0700 | [diff] [blame] | 160 | // Load a list of user profile identifiers from a cache file |path|. |
| 161 | // The profiles themselves are not loaded. |
| 162 | static std::vector<Identifier> LoadUserProfileList( |
| 163 | const base::FilePath &path); |
| 164 | |
| 165 | // Save a list of user profile identifiers |profiles| to a cache file |path|. |
| 166 | // Returns true if successful, false otherwise. |
| 167 | static bool SaveUserProfileList(const base::FilePath &path, |
| 168 | const std::vector<ProfileRefPtr> &profiles); |
| 169 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 170 | // Returns whether |name| matches this Profile's |name_|. |
| 171 | virtual bool MatchesIdentifier(const Identifier &name) const; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 172 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 173 | // Returns the username component of the profile identifier. |
| 174 | const std::string &GetUser() const { return name_.user; } |
| 175 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 176 | // Returns the user_hash component of the profile identifier. |
| 177 | const std::string &GetUserHash() const { return name_.user_hash; } |
| 178 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 179 | // Returns a read-only copy of the backing storage of the profile. |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 180 | virtual const StoreInterface *GetConstStorage() const { |
| 181 | return storage_.get(); |
| 182 | } |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 183 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 184 | protected: |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 185 | // Protected getters |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 186 | Metrics *metrics() const { return metrics_; } |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 187 | Manager *manager() const { return manager_; } |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 188 | StoreInterface *storage() { return storage_.get(); } |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 189 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 190 | // Sets |path| to the persistent store file path for a profile identified by |
| 191 | // |name_|. Returns true on success, and false if unable to determine an |
| 192 | // appropriate file location. |name_| must be a valid identifier, |
| 193 | // possibly parsed and validated through Profile::ParseIdentifier. |
| 194 | // |
| 195 | // In the default implementation, |name_.user| cannot be empty, because |
| 196 | // all regular profiles should be associated with a user. |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 197 | virtual bool GetStoragePath(base::FilePath *path); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 198 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 199 | private: |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 200 | friend class ProfileAdaptorInterface; |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 201 | FRIEND_TEST(ProfileTest, DeleteEntry); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 202 | FRIEND_TEST(ProfileTest, GetStoragePath); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 203 | FRIEND_TEST(ProfileTest, IsValidIdentifierToken); |
| 204 | |
| 205 | static bool IsValidIdentifierToken(const std::string &token); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 206 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 207 | void HelpRegisterConstDerivedStrings( |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 208 | const std::string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 209 | Strings(Profile::*get)(Error *error)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 210 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 211 | // Data members shared with subclasses via getter/setters above in the |
| 212 | // protected: section |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 213 | Metrics *metrics_; |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 214 | Manager *manager_; |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 215 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 216 | // Shared with |adaptor_| via public getter. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 217 | PropertyStore store_; |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 218 | |
| 219 | // Properties to be gotten via PropertyStore calls. |
| 220 | Identifier name_; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 221 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 222 | // Format string used to generate paths to user profile directories. |
| 223 | const std::string storage_format_; |
| 224 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 225 | // Allows this profile to be backed with on-disk storage. |
| 226 | scoped_ptr<StoreInterface> storage_; |
| 227 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 228 | scoped_ptr<ProfileAdaptorInterface> adaptor_; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 229 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 230 | DISALLOW_COPY_AND_ASSIGN(Profile); |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | } // namespace shill |
| 234 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 235 | #endif // SHILL_PROFILE_H_ |