blob: 0b7395061c98225fb03fb863ba91072553a9eeb5 [file] [log] [blame]
Chris Masone52cd19b2011-06-29 17:23:04 -07001// 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 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
15#include "shill/property_store.h"
16#include "shill/refptr_types.h"
17#include "shill/shill_event.h"
18
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 Masone6791a432011-07-12 13:23:19 -070025class Manager;
Chris Masone52cd19b2011-06-29 17:23:04 -070026class ProfileAdaptorInterface;
Chris Masone9d779932011-08-25 16:33:41 -070027class StoreInterface;
Chris Masone52cd19b2011-06-29 17:23:04 -070028
Chris Masone7aa5f902011-07-11 11:13:35 -070029class Profile : public base::RefCounted<Profile> {
Chris Masone52cd19b2011-06-29 17:23:04 -070030 public:
Darin Petkova4766822011-07-07 10:42:22 -070031 struct Identifier {
Chris Masone7df0c672011-07-15 10:24:54 -070032 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 Petkova4766822011-07-07 10:42:22 -070038 std::string user; // Empty for global.
39 std::string identifier;
40 };
41
Chris Masone7df0c672011-07-15 10:24:54 -070042 Profile(ControlInterface *control_interface,
Chris Masone7df0c672011-07-15 10:24:54 -070043 Manager *manager,
44 const Identifier &name,
Chris Masone2ae797d2011-08-23 20:41:00 -070045 const std::string &user_storage_format,
Chris Masone7df0c672011-07-15 10:24:54 -070046 bool connect_to_rpc);
Chris Masone52cd19b2011-06-29 17:23:04 -070047 virtual ~Profile();
48
Chris Masone7df0c672011-07-15 10:24:54 -070049 std::string GetFriendlyName();
50
51 std::string GetRpcIdentifier();
Chris Masone7aa5f902011-07-11 11:13:35 -070052
mukesh agrawalde29fa82011-09-16 16:16:36 -070053 PropertyStore *mutable_store() { return &store_; }
54 const PropertyStore &store() const { return store_; }
Chris Masone52cd19b2011-06-29 17:23:04 -070055
Chris Masone6791a432011-07-12 13:23:19 -070056 // 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 Masone7aa5f902011-07-11 11:13:35 -070060
Chris Masone6791a432011-07-12 13:23:19 -070061 // 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 Masone9d779932011-08-25 16:33:41 -070080 virtual void Finalize(StoreInterface *storage);
Chris Masone7aa5f902011-07-11 11:13:35 -070081
Darin Petkova4766822011-07-07 10:42:22 -070082 // 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 Masone9d779932011-08-25 16:33:41 -070088 bool Load(StoreInterface *storage);
89
90 virtual bool Save(StoreInterface *storage);
91
Darin Petkova4766822011-07-07 10:42:22 -070092 // Sets |path| to the persistent store file path for a profile identified by
Chris Masone2ae797d2011-08-23 20:41:00 -070093 // |name_|. Returns true on success, and false if unable to determine an
94 // appropriate file location. |name_| must be a valid identifier,
Darin Petkova4766822011-07-07 10:42:22 -070095 // possibly parsed and validated through Profile::ParseIdentifier.
Chris Masone2ae797d2011-08-23 20:41:00 -070096 //
97 // In the default implementation, |name_.user| cannot be empty.
98 virtual bool GetStoragePath(FilePath *path);
Darin Petkova4766822011-07-07 10:42:22 -070099
Chris Masone88cbd5f2011-07-03 14:30:04 -0700100 protected:
Paul Stewartac4ac002011-08-26 12:04:26 -0700101 // Protected getters
102 Manager *manager() const { return manager_; }
103 std::map<std::string, ServiceRefPtr> *services() { return &services_; }
Chris Masone7aa5f902011-07-11 11:13:35 -0700104
Chris Masone52cd19b2011-06-29 17:23:04 -0700105 private:
Chris Masone88cbd5f2011-07-03 14:30:04 -0700106 friend class ProfileAdaptorInterface;
Darin Petkova4766822011-07-07 10:42:22 -0700107 FRIEND_TEST(ProfileTest, IsValidIdentifierToken);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700108 // TODO(cmasone): once we can add services organically, take this out.
109 FRIEND_TEST(ServiceTest, MoveService);
Darin Petkova4766822011-07-07 10:42:22 -0700110
111 static bool IsValidIdentifierToken(const std::string &token);
Chris Masone88cbd5f2011-07-03 14:30:04 -0700112
Chris Masone6791a432011-07-12 13:23:19 -0700113 // Returns true if |candidate| can be merged into |service|.
mukesh agrawal1830fa12011-09-26 14:31:40 -0700114 bool Mergeable(const ServiceRefPtr &/*service*/,
115 const ServiceRefPtr &/*candiate*/) {
Chris Masone6791a432011-07-12 13:23:19 -0700116 return false;
117 }
118
119 void HelpRegisterDerivedStrings(const std::string &name,
120 Strings(Profile::*get)(void),
121 bool(Profile::*set)(const Strings&));
122
Chris Masone9d779932011-08-25 16:33:41 -0700123 // Persists |services_| to disk.
124 bool SaveServices(StoreInterface *storage);
125
Paul Stewartac4ac002011-08-26 12:04:26 -0700126 // 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 Masone7df0c672011-07-15 10:24:54 -0700133
134 // Properties to be gotten via PropertyStore calls.
135 Identifier name_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700136
Chris Masone2ae797d2011-08-23 20:41:00 -0700137 // Format string used to generate paths to user profile directories.
138 const std::string storage_format_;
139
Chris Masone7df0c672011-07-15 10:24:54 -0700140 scoped_ptr<ProfileAdaptorInterface> adaptor_;
Chris Masone52cd19b2011-06-29 17:23:04 -0700141
Chris Masone88cbd5f2011-07-03 14:30:04 -0700142 DISALLOW_COPY_AND_ASSIGN(Profile);
Chris Masone52cd19b2011-06-29 17:23:04 -0700143};
144
145} // namespace shill
146
147#endif // SHILL_PROFILE_