1 // Copyright (c) 2012 The Chromium 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 CHROMEOS_DBUS_SHILL_SHILL_PROFILE_CLIENT_H_
6 #define CHROMEOS_DBUS_SHILL_SHILL_PROFILE_CLIENT_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/callback.h"
12 #include "base/component_export.h"
13 #include "base/macros.h"
14 #include "chromeos/dbus/shill/fake_shill_simulated_result.h"
15 #include "chromeos/dbus/shill/shill_client_helper.h"
16
17 namespace dbus {
18 class Bus;
19 class ObjectPath;
20 } // namespace dbus
21
22 namespace chromeos {
23
24 class ShillPropertyChangedObserver;
25
26 // ShillProfileClient is used to communicate with the Shill Profile
27 // service. All methods should be called from the origin thread which
28 // initializes the DBusThreadManager instance.
COMPONENT_EXPORT(SHILL_CLIENT)29 class COMPONENT_EXPORT(SHILL_CLIENT) ShillProfileClient {
30 public:
31 typedef ShillClientHelper::ErrorCallback ErrorCallback;
32
33 // Interface for setting up services for testing. Accessed through
34 // GetTestInterface(), only implemented in the stub implementation.
35 // TODO(stevenjb): remove dependencies on entry_path -> service_path
36 // mappings in some of the TestInterface implementations.
37 class TestInterface {
38 public:
39 virtual void AddProfile(const std::string& profile_path,
40 const std::string& userhash) = 0;
41
42 // Adds an entry to the profile specified by |profile_path|. |properties|
43 // must be a dictionary Value of service properties. |entry_path|
44 // represents a service path and a corresponding entry will be added to the
45 // Manager's kServiceCompleteList property. This will not update the
46 // kServicesProperty (which represents 'visible' services).
47 virtual void AddEntry(const std::string& profile_path,
48 const std::string& entry_path,
49 const base::Value& properties) = 0;
50
51 // Adds a service to the profile, copying properties from the
52 // ShillServiceClient entry matching |service_path|. Returns false if no
53 // Service entry exists or if a Profile entry already exists. Also sets
54 // the Profile property of the service in ShillServiceClient.
55 virtual bool AddService(const std::string& profile_path,
56 const std::string& service_path) = 0;
57
58 // Copies properties from the ShillServiceClient entry matching
59 // |service_path| to the profile entry matching |profile_path|. Returns
60 // false if no Service entry exits or if no Profile entry exists.
61 virtual bool UpdateService(const std::string& profile_path,
62 const std::string& service_path) = 0;
63
64 // Sets |profiles| to the current list of profile paths.
65 virtual void GetProfilePaths(std::vector<std::string>* profiles) = 0;
66
67 // Sets |profiles| to the current list of profile paths that contain an
68 // entry for |service_path|.
69 virtual void GetProfilePathsContainingService(
70 const std::string& service_path,
71 std::vector<std::string>* profiles) = 0;
72
73 // Returns the entry for |service_path| if it exists in any profile and sets
74 // |profile_path| to the path of the profile the service was found in.
75 // Profiles are searched starting with the most recently added profile.
76 // If the service does not exist in any profile, an empty Value is returned.
77 virtual base::Value GetService(const std::string& service_path,
78 std::string* profile_path) = 0;
79
80 // Returns true iff an entry sepcified via |service_path| exists in
81 // any profile.
82 virtual bool HasService(const std::string& service_path) = 0;
83
84 // Remove all profile entries.
85 virtual void ClearProfiles() = 0;
86
87 // Makes DeleteEntry succeed, fail, or timeout.
88 virtual void SetSimulateDeleteResult(
89 FakeShillSimulatedResult delete_result) = 0;
90
91 protected:
92 virtual ~TestInterface() {}
93 };
94
95 // Creates and initializes the global instance. |bus| must not be null.
96 static void Initialize(dbus::Bus* bus);
97
98 // Creates the global instance with a fake implementation.
99 static void InitializeFake();
100
101 // Destroys the global instance which must have been initialized.
102 static void Shutdown();
103
104 // Returns the global instance if initialized. May return null.
105 static ShillProfileClient* Get();
106
107 // Returns the shared profile path.
108 static std::string GetSharedProfilePath();
109
110 // Adds a property changed |observer| for the profile at |profile_path|.
111 virtual void AddPropertyChangedObserver(
112 const dbus::ObjectPath& profile_path,
113 ShillPropertyChangedObserver* observer) = 0;
114
115 // Removes a property changed |observer| for the profile at |profile_path|.
116 virtual void RemovePropertyChangedObserver(
117 const dbus::ObjectPath& profile_path,
118 ShillPropertyChangedObserver* observer) = 0;
119
120 // Calls the GetProperties DBus method and invokes |callback| on success or
121 // |error_callback| on failure. On success |callback| receives a dictionary
122 // Value containing the Profile properties.
123 virtual void GetProperties(
124 const dbus::ObjectPath& profile_path,
125 base::OnceCallback<void(base::Value result)> callback,
126 ErrorCallback error_callback) = 0;
127
128 // Calls GetEntry method.
129 // |callback| is called after the method call succeeds.
130 virtual void GetEntry(const dbus::ObjectPath& profile_path,
131 const std::string& entry_path,
132 base::OnceCallback<void(base::Value result)> callback,
133 ErrorCallback error_callback) = 0;
134
135 // Calls DeleteEntry method.
136 // |callback| is called after the method call succeeds.
137 virtual void DeleteEntry(const dbus::ObjectPath& profile_path,
138 const std::string& entry_path,
139 base::OnceClosure callback,
140 ErrorCallback error_callback) = 0;
141
142 // Returns an interface for testing (stub only), or returns null.
143 virtual TestInterface* GetTestInterface() = 0;
144
145 protected:
146 friend class ShillProfileClientTest;
147
148 // Initialize/Shutdown should be used instead.
149 ShillProfileClient();
150 virtual ~ShillProfileClient();
151
152 private:
153 DISALLOW_COPY_AND_ASSIGN(ShillProfileClient);
154 };
155
156 } // namespace chromeos
157
158 #endif // CHROMEOS_DBUS_SHILL_SHILL_PROFILE_CLIENT_H_
159