Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 1 | // 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 | #ifndef SHILL_PROFILE_ |
| 6 | #define SHILL_PROFILE_ |
| 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 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 19 | class FilePath; |
| 20 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 21 | namespace shill { |
| 22 | |
| 23 | class ControlInterface; |
| 24 | class Error; |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 25 | class GLib; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 26 | class Manager; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 27 | class ProfileAdaptorInterface; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 28 | class StoreInterface; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 29 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 30 | class Profile : public base::RefCounted<Profile> { |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 31 | public: |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 32 | enum InitStorageOption { |
| 33 | kOpenExisting, |
| 34 | kCreateNew, |
| 35 | kCreateOrOpenExisting |
| 36 | }; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 37 | struct Identifier { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 38 | 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 Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 44 | std::string user; // Empty for global. |
| 45 | std::string identifier; |
| 46 | }; |
| 47 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 48 | Profile(ControlInterface *control_interface, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 49 | Manager *manager, |
| 50 | const Identifier &name, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 51 | const std::string &user_storage_format, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 52 | bool connect_to_rpc); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 53 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 54 | virtual ~Profile(); |
| 55 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 56 | // Set up persistent storage for this Profile. |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 57 | bool InitStorage(GLib *glib, |
| 58 | InitStorageOption storage_option, |
| 59 | Error *error); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 60 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 61 | std::string GetFriendlyName(); |
| 62 | |
| 63 | std::string GetRpcIdentifier(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 64 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 65 | PropertyStore *mutable_store() { return &store_; } |
| 66 | const PropertyStore &store() const { return store_; } |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 67 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 68 | void set_storage(StoreInterface *storage); // Takes ownership of |storage|. |
| 69 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 70 | // Begin managing the persistence of |service|. |
| 71 | // Returns true if |service| is new to this profile and was added, |
| 72 | // false if the |service| already existed. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 73 | virtual bool AdoptService(const ServiceRefPtr &service); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 74 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 75 | // Cease managing the persistence of the Service |service|. |
| 76 | // Returns true if |service| was found and abandoned, or not found. |
| 77 | // Returns false if can't be abandoned. |
| 78 | virtual bool AbandonService(const ServiceRefPtr &service); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 79 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 80 | // Clobbers persisted notion of |service| with data from |service|. |
| 81 | // Returns true if |service| was found and updated, false if not found. |
| 82 | bool UpdateService(const ServiceRefPtr &service); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 83 | |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame^] | 84 | // Ask |service| if it can configure itself from the profile. If it can, |
| 85 | // change the service to point at this profile, ask |service| to perform |
| 86 | // the configuration and return true. If not, return false. |
| 87 | bool ConfigureService(const ServiceRefPtr &service); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 88 | |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame^] | 89 | // Return whether |service| can configure itself from the profile. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 90 | bool ContainsService(const ServiceConstRefPtr &service); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 91 | |
| 92 | std::vector<std::string> EnumerateAvailableServices(); |
| 93 | std::vector<std::string> EnumerateEntries(); |
| 94 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 95 | // Write all in-memory state to disk via |storage_|. |
| 96 | virtual bool Save(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 97 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 98 | // Parses a profile identifier. There're two acceptable forms of the |raw| |
| 99 | // identifier: "identifier" and "~user/identifier". Both "user" and |
| 100 | // "identifier" must be suitable for use in a D-Bus object path. Returns true |
| 101 | // on success. |
| 102 | static bool ParseIdentifier(const std::string &raw, Identifier *parsed); |
| 103 | |
| 104 | // Returns whether |name| matches this Profile's |name_|. |
| 105 | virtual bool MatchesIdentifier(const Identifier &name) const; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 106 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 107 | protected: |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 108 | // Protected getters |
| 109 | Manager *manager() const { return manager_; } |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 110 | StoreInterface *storage() { return storage_.get(); } |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 111 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 112 | // Sets |path| to the persistent store file path for a profile identified by |
| 113 | // |name_|. Returns true on success, and false if unable to determine an |
| 114 | // appropriate file location. |name_| must be a valid identifier, |
| 115 | // possibly parsed and validated through Profile::ParseIdentifier. |
| 116 | // |
| 117 | // In the default implementation, |name_.user| cannot be empty, because |
| 118 | // all regular profiles should be associated with a user. |
| 119 | virtual bool GetStoragePath(FilePath *path); |
| 120 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 121 | private: |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 122 | friend class DefaultProfileTest; |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 123 | friend class ProfileAdaptorInterface; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 124 | FRIEND_TEST(DefaultProfileTest, GetStoragePath); |
| 125 | FRIEND_TEST(ProfileTest, GetStoragePath); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 126 | FRIEND_TEST(ProfileTest, IsValidIdentifierToken); |
| 127 | |
| 128 | static bool IsValidIdentifierToken(const std::string &token); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 129 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 130 | void HelpRegisterDerivedStrings( |
| 131 | const std::string &name, |
| 132 | Strings(Profile::*get)(void), |
| 133 | void(Profile::*set)(const Strings&, Error *)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 134 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 135 | // Data members shared with subclasses via getter/setters above in the |
| 136 | // protected: section |
| 137 | Manager *manager_; |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 138 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 139 | // Shared with |adaptor_| via public getter. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 140 | PropertyStore store_; |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 141 | |
| 142 | // Properties to be gotten via PropertyStore calls. |
| 143 | Identifier name_; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 144 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 145 | // Format string used to generate paths to user profile directories. |
| 146 | const std::string storage_format_; |
| 147 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 148 | // Allows this profile to be backed with on-disk storage. |
| 149 | scoped_ptr<StoreInterface> storage_; |
| 150 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 151 | scoped_ptr<ProfileAdaptorInterface> adaptor_; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 152 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(Profile); |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | } // namespace shill |
| 157 | |
| 158 | #endif // SHILL_PROFILE_ |