1 // Copyright 2013 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_FAKE_SHILL_SERVICE_CLIENT_H_
6 #define CHROMEOS_DBUS_SHILL_FAKE_SHILL_SERVICE_CLIENT_H_
7
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "base/callback.h"
14 #include "base/component_export.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/values.h"
18 #include "chromeos/dbus/shill/shill_service_client.h"
19
20 namespace chromeos {
21
22 // A fake implementation of ShillServiceClient. This works in close coordination
23 // with FakeShillManagerClient and is not intended to be used independently.
COMPONENT_EXPORT(SHILL_CLIENT)24 class COMPONENT_EXPORT(SHILL_CLIENT) FakeShillServiceClient
25 : public ShillServiceClient,
26 public ShillServiceClient::TestInterface {
27 public:
28 FakeShillServiceClient();
29 ~FakeShillServiceClient() override;
30
31 // ShillServiceClient overrides
32 void AddPropertyChangedObserver(
33 const dbus::ObjectPath& service_path,
34 ShillPropertyChangedObserver* observer) override;
35 void RemovePropertyChangedObserver(
36 const dbus::ObjectPath& service_path,
37 ShillPropertyChangedObserver* observer) override;
38 void GetProperties(const dbus::ObjectPath& service_path,
39 DBusMethodCallback<base::Value> callback) override;
40 void SetProperty(const dbus::ObjectPath& service_path,
41 const std::string& name,
42 const base::Value& value,
43 base::OnceClosure callback,
44 ErrorCallback error_callback) override;
45 void SetProperties(const dbus::ObjectPath& service_path,
46 const base::Value& properties,
47 base::OnceClosure callback,
48 ErrorCallback error_callback) override;
49 void ClearProperty(const dbus::ObjectPath& service_path,
50 const std::string& name,
51 base::OnceClosure callback,
52 ErrorCallback error_callback) override;
53 void ClearProperties(const dbus::ObjectPath& service_path,
54 const std::vector<std::string>& names,
55 ListValueCallback callback,
56 ErrorCallback error_callback) override;
57 void Connect(const dbus::ObjectPath& service_path,
58 base::OnceClosure callback,
59 ErrorCallback error_callback) override;
60 void Disconnect(const dbus::ObjectPath& service_path,
61 base::OnceClosure callback,
62 ErrorCallback error_callback) override;
63 void Remove(const dbus::ObjectPath& service_path,
64 base::OnceClosure callback,
65 ErrorCallback error_callback) override;
66 void CompleteCellularActivation(const dbus::ObjectPath& service_path,
67 base::OnceClosure callback,
68 ErrorCallback error_callback) override;
69 void GetLoadableProfileEntries(
70 const dbus::ObjectPath& service_path,
71 DBusMethodCallback<base::Value> callback) override;
72 void GetWiFiPassphrase(const dbus::ObjectPath& service_path,
73 StringCallback callback,
74 ErrorCallback error_callback) override;
75 ShillServiceClient::TestInterface* GetTestInterface() override;
76
77 // ShillServiceClient::TestInterface overrides.
78 void AddService(const std::string& service_path,
79 const std::string& guid,
80 const std::string& name,
81 const std::string& type,
82 const std::string& state,
83 bool visible) override;
84 void AddServiceWithIPConfig(const std::string& service_path,
85 const std::string& guid,
86 const std::string& name,
87 const std::string& type,
88 const std::string& state,
89 const std::string& ipconfig_path,
90 bool visible) override;
91 base::Value* SetServiceProperties(const std::string& service_path,
92 const std::string& guid,
93 const std::string& name,
94 const std::string& type,
95 const std::string& state,
96 bool visible) override;
97 void RemoveService(const std::string& service_path) override;
98 bool SetServiceProperty(const std::string& service_path,
99 const std::string& property,
100 const base::Value& value) override;
101 const base::Value* GetServiceProperties(
102 const std::string& service_path) const override;
103 bool ClearConfiguredServiceProperties(
104 const std::string& service_path) override;
105 std::string FindServiceMatchingGUID(const std::string& guid) override;
106 std::string FindSimilarService(
107 const base::Value& template_service_properties) override;
108 void ClearServices() override;
109 void SetConnectBehavior(const std::string& service_path,
110 const base::RepeatingClosure& behavior) override;
111 void SetHoldBackServicePropertyUpdates(bool hold_back) override;
112
113 private:
114 typedef base::ObserverList<ShillPropertyChangedObserver>::Unchecked
115 PropertyObserverList;
116
117 void NotifyObserversPropertyChanged(const dbus::ObjectPath& service_path,
118 const std::string& property);
119 base::Value* GetModifiableServiceProperties(const std::string& service_path,
120 bool create_if_missing);
121 PropertyObserverList& GetObserverList(const dbus::ObjectPath& device_path);
122 void SetOtherServicesOffline(const std::string& service_path);
123 void SetCellularActivated(const dbus::ObjectPath& service_path,
124 ErrorCallback error_callback);
125 void ContinueConnect(const std::string& service_path);
126
127 base::Value stub_services_{base::Value::Type::DICTIONARY};
128
129 // Per network service, stores a closure that is executed on each connection
130 // attempt. The callback can for example modify the services properties in
131 // order to simulate a connection failure.
132 std::map<std::string, base::RepeatingClosure> connect_behavior_;
133
134 // Observer list for each service.
135 std::map<dbus::ObjectPath, std::unique_ptr<PropertyObserverList>>
136 observer_list_;
137
138 // If this is true, the FakeShillServiceClient is recording service property
139 // updates and will only send them when SetHoldBackServicePropertyUpdates is
140 // called with false again.
141 bool hold_back_service_property_updates_ = false;
142
143 // Property updates that were held back while
144 // |hold_back_service_property_updates_| was true.
145 std::vector<base::OnceClosure> recorded_property_updates_;
146
147 // Note: This should remain the last member so it'll be destroyed and
148 // invalidate its weak pointers before any other members are destroyed.
149 base::WeakPtrFactory<FakeShillServiceClient> weak_ptr_factory_{this};
150
151 DISALLOW_COPY_AND_ASSIGN(FakeShillServiceClient);
152 };
153
154 } // namespace chromeos
155
156 #endif // CHROMEOS_DBUS_SHILL_FAKE_SHILL_SERVICE_CLIENT_H_
157