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_MANAGER_CLIENT_H_
6 #define CHROMEOS_DBUS_SHILL_FAKE_SHILL_MANAGER_CLIENT_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 
12 #include "base/callback.h"
13 #include "base/component_export.h"
14 #include "base/macros.h"
15 #include "base/values.h"
16 #include "chromeos/dbus/shill/shill_manager_client.h"
17 
18 namespace chromeos {
19 
20 // A fake implementation of ShillManagerClient. This works in close coordination
21 // with FakeShillServiceClient. FakeShillDeviceClient, and
22 // FakeShillProfileClient, and is not intended to be used independently.
COMPONENT_EXPORT(SHILL_CLIENT)23 class COMPONENT_EXPORT(SHILL_CLIENT) FakeShillManagerClient
24     : public ShillManagerClient,
25       public ShillManagerClient::TestInterface {
26  public:
27   FakeShillManagerClient();
28   ~FakeShillManagerClient() override;
29 
30   // ShillManagerClient overrides
31   void AddPropertyChangedObserver(
32       ShillPropertyChangedObserver* observer) override;
33   void RemovePropertyChangedObserver(
34       ShillPropertyChangedObserver* observer) override;
35   void GetProperties(DBusMethodCallback<base::Value> callback) override;
36   void GetNetworksForGeolocation(
37       DBusMethodCallback<base::Value> callback) override;
38   void SetProperty(const std::string& name,
39                    const base::Value& value,
40                    base::OnceClosure callback,
41                    ErrorCallback error_callback) override;
42   void RequestScan(const std::string& type,
43                    base::OnceClosure callback,
44                    ErrorCallback error_callback) override;
45   void EnableTechnology(const std::string& type,
46                         base::OnceClosure callback,
47                         ErrorCallback error_callback) override;
48   void DisableTechnology(const std::string& type,
49                          base::OnceClosure callback,
50                          ErrorCallback error_callback) override;
51   void ConfigureService(const base::Value& properties,
52                         ObjectPathCallback callback,
53                         ErrorCallback error_callback) override;
54   void ConfigureServiceForProfile(const dbus::ObjectPath& profile_path,
55                                   const base::Value& properties,
56                                   ObjectPathCallback callback,
57                                   ErrorCallback error_callback) override;
58   void GetService(const base::Value& properties,
59                   ObjectPathCallback callback,
60                   ErrorCallback error_callback) override;
61   void ConnectToBestServices(base::OnceClosure callback,
62                              ErrorCallback error_callback) override;
63   void SetNetworkThrottlingStatus(const NetworkThrottlingStatus& status,
64                                   base::OnceClosure callback,
65                                   ErrorCallback error_callback) override;
66 
67   ShillManagerClient::TestInterface* GetTestInterface() override;
68 
69   // ShillManagerClient::TestInterface overrides.
70   void AddDevice(const std::string& device_path) override;
71   void RemoveDevice(const std::string& device_path) override;
72   void ClearDevices() override;
73   void AddTechnology(const std::string& type, bool enabled) override;
74   void RemoveTechnology(const std::string& type) override;
75   void SetTechnologyInitializing(const std::string& type,
76                                  bool initializing) override;
77   void SetTechnologyProhibited(const std::string& type,
78                                bool prohibited) override;
79   void AddGeoNetwork(const std::string& technology,
80                      const base::Value& network) override;
81   void AddProfile(const std::string& profile_path) override;
82   void ClearProperties() override;
83   void SetManagerProperty(const std::string& key,
84                           const base::Value& value) override;
85   void AddManagerService(const std::string& service_path,
86                          bool notify_observers) override;
87   void RemoveManagerService(const std::string& service_path) override;
88   void ClearManagerServices() override;
89   void ServiceStateChanged(const std::string& service_path,
90                            const std::string& state) override;
91   void SortManagerServices(bool notify) override;
92   void SetupDefaultEnvironment() override;
93   base::TimeDelta GetInteractiveDelay() const override;
94   void SetInteractiveDelay(base::TimeDelta delay) override;
95   void SetBestServiceToConnect(const std::string& service_path) override;
96   const NetworkThrottlingStatus& GetNetworkThrottlingStatus() override;
97   bool GetFastTransitionStatus() override;
98   void SetSimulateConfigurationResult(
99       FakeShillSimulatedResult configuration_result) override;
100   base::Value GetEnabledServiceList() const override;
101 
102   // Constants used for testing.
103   static const char kFakeEthernetNetworkGuid[];
104 
105  private:
106   void SetDefaultProperties();
107   void PassStubProperties(DBusMethodCallback<base::Value> callback) const;
108   void PassStubGeoNetworks(DBusMethodCallback<base::Value> callback) const;
109   void CallNotifyObserversPropertyChanged(const std::string& property);
110   void NotifyObserversPropertyChanged(const std::string& property);
111   base::ListValue* GetListProperty(const std::string& property);
112   bool TechnologyEnabled(const std::string& type) const;
113   void SetTechnologyEnabled(const std::string& type,
114                             base::OnceClosure callback,
115                             bool enabled);
116   void ScanCompleted(const std::string& device_path);
117 
118   // Parses the command line for Shill stub switches and sets initial states.
119   // Uses comma-separated name-value pairs (see SplitStringIntoKeyValuePairs):
120   // interactive={delay} - sets delay in seconds for interactive UI
121   // {wifi,cellular,etc}={on,off,disabled,none} - sets initial state for type
122   void ParseCommandLineSwitch();
123   bool ParseOption(const std::string& arg0, const std::string& arg1);
124   bool SetInitialNetworkState(std::string type_arg,
125                               const std::string& state_arg);
126   std::string GetInitialStateForType(const std::string& type, bool* enabled);
127 
128   // Dictionary of property name -> property value
129   base::Value stub_properties_{base::Value::Type::DICTIONARY};
130 
131   // Dictionary of technology -> list of property dictionaries
132   base::Value stub_geo_networks_{base::Value::Type::DICTIONARY};
133 
134   // Delay for interactive actions
135   base::TimeDelta interactive_delay_;
136 
137   // Initial state for fake services.
138   std::map<std::string, std::string> shill_initial_state_map_;
139 
140   // URL used for cellular activation.
141   std::string cellular_olp_;
142 
143   // Technology type for fake cellular service.
144   std::string cellular_technology_;
145 
146   // Roaming state for fake cellular service.
147   std::string roaming_state_;
148 
149   // Current network throttling status.
150   NetworkThrottlingStatus network_throttling_status_ = {false, 0, 0};
151 
152   typedef std::map<std::string, base::Value> ShillPropertyMap;
153   typedef std::map<std::string, ShillPropertyMap> DevicePropertyMap;
154   DevicePropertyMap shill_device_property_map_;
155 
156   base::ObserverList<ShillPropertyChangedObserver>::Unchecked observer_list_;
157 
158   // Track the default service for signaling Manager.DefaultService.
159   std::string default_service_;
160 
161   // 'Best' service to connect to on ConnectToBestServices() calls.
162   std::string best_service_;
163 
164   FakeShillSimulatedResult simulate_configuration_result_ =
165       FakeShillSimulatedResult::kSuccess;
166 
167   // Note: This should remain the last member so it'll be destroyed and
168   // invalidate its weak pointers before any other members are destroyed.
169   base::WeakPtrFactory<FakeShillManagerClient> weak_ptr_factory_{this};
170 
171   DISALLOW_COPY_AND_ASSIGN(FakeShillManagerClient);
172 };
173 
174 }  // namespace chromeos
175 
176 #endif  // CHROMEOS_DBUS_SHILL_FAKE_SHILL_MANAGER_CLIENT_H_
177