blob: cf0b1996a34bdaba4f00f4990e2324ffb80f8ffc [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
Chris Masone7df0c672011-07-15 10:24:54 -070061 std::string GetFriendlyName();
62
Paul Stewart1b1a7f22012-01-06 16:24:06 -080063 virtual std::string GetRpcIdentifier();
Chris Masone7aa5f902011-07-11 11:13:35 -070064
mukesh agrawalde29fa82011-09-16 16:16:36 -070065 PropertyStore *mutable_store() { return &store_; }
66 const PropertyStore &store() const { return store_; }
Chris Masone52cd19b2011-06-29 17:23:04 -070067
Chris Masoneb9c00592011-10-06 13:10:39 -070068 void set_storage(StoreInterface *storage); // Takes ownership of |storage|.
69
Chris Masone6791a432011-07-12 13:23:19 -070070 // 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 Masone6515aab2011-10-12 16:19:09 -070073 virtual bool AdoptService(const ServiceRefPtr &service);
Chris Masone7aa5f902011-07-11 11:13:35 -070074
Chris Masone6515aab2011-10-12 16:19:09 -070075 // 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 Masone6791a432011-07-12 13:23:19 -070079
Chris Masone6515aab2011-10-12 16:19:09 -070080 // 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 Masone6791a432011-07-12 13:23:19 -070083
Paul Stewartbba6a5b2011-11-02 18:45:59 -070084 // 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.
Paul Stewarta41e38d2011-11-11 07:47:29 -080087 virtual bool ConfigureService(const ServiceRefPtr &service);
88
89 // Allow the device to configure itself from this profile. Returns
90 // true if the device succeeded in finding its configuration. If not,
91 // return false.
92 virtual bool ConfigureDevice(const DeviceRefPtr &device);
Chris Masone6791a432011-07-12 13:23:19 -070093
Paul Stewartbba6a5b2011-11-02 18:45:59 -070094 // Return whether |service| can configure itself from the profile.
Chris Masone6515aab2011-10-12 16:19:09 -070095 bool ContainsService(const ServiceConstRefPtr &service);
Chris Masone6791a432011-07-12 13:23:19 -070096
Gaurav Shah1b7a6162011-11-09 11:41:01 -080097 std::vector<std::string> EnumerateAvailableServices(Error *error);
98 std::vector<std::string> EnumerateEntries(Error *error);
Chris Masone6791a432011-07-12 13:23:19 -070099
Chris Masoneb9c00592011-10-06 13:10:39 -0700100 // Write all in-memory state to disk via |storage_|.
101 virtual bool Save();
Chris Masone9d779932011-08-25 16:33:41 -0700102
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700103 // Parses a profile identifier. There're two acceptable forms of the |raw|
104 // identifier: "identifier" and "~user/identifier". Both "user" and
105 // "identifier" must be suitable for use in a D-Bus object path. Returns true
106 // on success.
107 static bool ParseIdentifier(const std::string &raw, Identifier *parsed);
108
109 // Returns whether |name| matches this Profile's |name_|.
110 virtual bool MatchesIdentifier(const Identifier &name) const;
Darin Petkova4766822011-07-07 10:42:22 -0700111
Chris Masone88cbd5f2011-07-03 14:30:04 -0700112 protected:
Paul Stewartac4ac002011-08-26 12:04:26 -0700113 // Protected getters
114 Manager *manager() const { return manager_; }
Chris Masoneb9c00592011-10-06 13:10:39 -0700115 StoreInterface *storage() { return storage_.get(); }
Chris Masone7aa5f902011-07-11 11:13:35 -0700116
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700117 // Sets |path| to the persistent store file path for a profile identified by
118 // |name_|. Returns true on success, and false if unable to determine an
119 // appropriate file location. |name_| must be a valid identifier,
120 // possibly parsed and validated through Profile::ParseIdentifier.
121 //
122 // In the default implementation, |name_.user| cannot be empty, because
123 // all regular profiles should be associated with a user.
124 virtual bool GetStoragePath(FilePath *path);
125
Chris Masone52cd19b2011-06-29 17:23:04 -0700126 private:
Chris Masone88cbd5f2011-07-03 14:30:04 -0700127 friend class ProfileAdaptorInterface;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700128 FRIEND_TEST(ProfileTest, GetStoragePath);
Darin Petkova4766822011-07-07 10:42:22 -0700129 FRIEND_TEST(ProfileTest, IsValidIdentifierToken);
130
131 static bool IsValidIdentifierToken(const std::string &token);
Chris Masone88cbd5f2011-07-03 14:30:04 -0700132
mukesh agrawalffa3d042011-10-06 15:26:10 -0700133 void HelpRegisterDerivedStrings(
134 const std::string &name,
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800135 Strings(Profile::*get)(Error *error),
136 void(Profile::*set)(const Strings&, Error *error));
Chris Masone6791a432011-07-12 13:23:19 -0700137
Paul Stewartac4ac002011-08-26 12:04:26 -0700138 // Data members shared with subclasses via getter/setters above in the
139 // protected: section
140 Manager *manager_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700141
Chris Masoneb9c00592011-10-06 13:10:39 -0700142 // Shared with |adaptor_| via public getter.
Paul Stewartac4ac002011-08-26 12:04:26 -0700143 PropertyStore store_;
Chris Masone7df0c672011-07-15 10:24:54 -0700144
145 // Properties to be gotten via PropertyStore calls.
146 Identifier name_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700147
Chris Masone2ae797d2011-08-23 20:41:00 -0700148 // Format string used to generate paths to user profile directories.
149 const std::string storage_format_;
150
Chris Masoneb9c00592011-10-06 13:10:39 -0700151 // Allows this profile to be backed with on-disk storage.
152 scoped_ptr<StoreInterface> storage_;
153
Chris Masone7df0c672011-07-15 10:24:54 -0700154 scoped_ptr<ProfileAdaptorInterface> adaptor_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700155
Chris Masone88cbd5f2011-07-03 14:30:04 -0700156 DISALLOW_COPY_AND_ASSIGN(Profile);
Chris Masone52cd19b2011-06-29 17:23:04 -0700157};
158
159} // namespace shill
160
161#endif // SHILL_PROFILE_