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