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 | |
| 15 | #include "shill/property_store.h" |
| 16 | #include "shill/refptr_types.h" |
| 17 | #include "shill/shill_event.h" |
| 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 | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 25 | class Manager; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 26 | class ProfileAdaptorInterface; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 27 | class StoreInterface; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 28 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 29 | class Profile : public base::RefCounted<Profile> { |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 30 | public: |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 31 | struct Identifier { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 32 | Identifier() {} |
| 33 | explicit Identifier(const std::string &i) : identifier(i) {} |
| 34 | Identifier(const std::string &u, const std::string &i) |
| 35 | : user(u), |
| 36 | identifier(i) { |
| 37 | } |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 38 | std::string user; // Empty for global. |
| 39 | std::string identifier; |
| 40 | }; |
| 41 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 42 | Profile(ControlInterface *control_interface, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 43 | Manager *manager, |
| 44 | const Identifier &name, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 45 | const std::string &user_storage_format, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 46 | bool connect_to_rpc); |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 47 | virtual ~Profile(); |
| 48 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 49 | std::string GetFriendlyName(); |
| 50 | |
| 51 | std::string GetRpcIdentifier(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 52 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 53 | PropertyStore *mutable_store() { return &store_; } |
| 54 | const PropertyStore &store() const { return store_; } |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 55 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 56 | // Begin managing the persistence of |service|. |
| 57 | // Returns true if |service| is new to this profile and was added, |
| 58 | // false if the |service| already existed. |
| 59 | bool AdoptService(const ServiceRefPtr &service); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 60 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 61 | // Cease managing the persistence of the Service named |name|. |
| 62 | // Returns true if |name| was found and abandoned, false if not found. |
| 63 | bool AbandonService(const std::string &name); |
| 64 | |
| 65 | // Continue persisting the Service named |name|, but don't consider it |
| 66 | // usable for connectivity. |
| 67 | // Returns true if |name| was found and demoted, false if not found. |
| 68 | bool DemoteService(const std::string &name); |
| 69 | |
| 70 | // Determine if |service| represents a service that's already in |services_|. |
| 71 | // If so, merge them smartly and return true. If not, return false. |
| 72 | bool MergeService(const ServiceRefPtr &service); |
| 73 | |
| 74 | ServiceRefPtr FindService(const std::string& name); |
| 75 | |
| 76 | std::vector<std::string> EnumerateAvailableServices(); |
| 77 | std::vector<std::string> EnumerateEntries(); |
| 78 | |
| 79 | // Flush any pending entry info to disk and stop managing service persistence. |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 80 | virtual void Finalize(StoreInterface *storage); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 81 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 82 | // Parses a profile identifier. There're two acceptable forms of the |raw| |
| 83 | // identifier: "identifier" and "~user/identifier". Both "user" and |
| 84 | // "identifier" must be suitable for use in a D-Bus object path. Returns true |
| 85 | // on success. |
| 86 | static bool ParseIdentifier(const std::string &raw, Identifier *parsed); |
| 87 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 88 | bool Load(StoreInterface *storage); |
| 89 | |
| 90 | virtual bool Save(StoreInterface *storage); |
| 91 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 92 | // Sets |path| to the persistent store file path for a profile identified by |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 93 | // |name_|. Returns true on success, and false if unable to determine an |
| 94 | // appropriate file location. |name_| must be a valid identifier, |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 95 | // possibly parsed and validated through Profile::ParseIdentifier. |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 96 | // |
| 97 | // In the default implementation, |name_.user| cannot be empty. |
| 98 | virtual bool GetStoragePath(FilePath *path); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 99 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 100 | protected: |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 101 | // Protected getters |
| 102 | Manager *manager() const { return manager_; } |
| 103 | std::map<std::string, ServiceRefPtr> *services() { return &services_; } |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 104 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 105 | private: |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 106 | friend class ProfileAdaptorInterface; |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 107 | FRIEND_TEST(ProfileTest, IsValidIdentifierToken); |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 108 | // TODO(cmasone): once we can add services organically, take this out. |
| 109 | FRIEND_TEST(ServiceTest, MoveService); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 110 | |
| 111 | static bool IsValidIdentifierToken(const std::string &token); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 112 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 113 | // Returns true if |candidate| can be merged into |service|. |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 114 | bool Mergeable(const ServiceRefPtr &/*service*/, |
| 115 | const ServiceRefPtr &/*candiate*/) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
| 119 | void HelpRegisterDerivedStrings(const std::string &name, |
| 120 | Strings(Profile::*get)(void), |
| 121 | bool(Profile::*set)(const Strings&)); |
| 122 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 123 | // Persists |services_| to disk. |
| 124 | bool SaveServices(StoreInterface *storage); |
| 125 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 126 | // Data members shared with subclasses via getter/setters above in the |
| 127 | // protected: section |
| 128 | Manager *manager_; |
| 129 | std::map<std::string, ServiceRefPtr> services_; |
| 130 | |
| 131 | // Shared with subclasses via public getter |
| 132 | PropertyStore store_; |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 133 | |
| 134 | // Properties to be gotten via PropertyStore calls. |
| 135 | Identifier name_; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 136 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 137 | // Format string used to generate paths to user profile directories. |
| 138 | const std::string storage_format_; |
| 139 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 140 | scoped_ptr<ProfileAdaptorInterface> adaptor_; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 141 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 142 | DISALLOW_COPY_AND_ASSIGN(Profile); |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | } // namespace shill |
| 146 | |
| 147 | #endif // SHILL_PROFILE_ |