OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/shill_manager_client.h" | 5 #include "chromeos/dbus/shill_manager_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/chromeos/chromeos_version.h" | 8 #include "base/chromeos/chromeos_version.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 const ErrorCallback& error_callback) OVERRIDE { | 154 const ErrorCallback& error_callback) OVERRIDE { |
155 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | 155 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, |
156 flimflam::kGetServiceFunction); | 156 flimflam::kGetServiceFunction); |
157 dbus::MessageWriter writer(&method_call); | 157 dbus::MessageWriter writer(&method_call); |
158 AppendServicePropertiesDictionary(&writer, properties); | 158 AppendServicePropertiesDictionary(&writer, properties); |
159 helper_.CallObjectPathMethodWithErrorCallback(&method_call, | 159 helper_.CallObjectPathMethodWithErrorCallback(&method_call, |
160 callback, | 160 callback, |
161 error_callback); | 161 error_callback); |
162 } | 162 } |
163 | 163 |
164 virtual TestInterface* GetTestInterface() OVERRIDE { | |
165 return NULL; | |
166 } | |
167 | |
164 private: | 168 private: |
165 dbus::ObjectProxy* proxy_; | 169 dbus::ObjectProxy* proxy_; |
166 ShillClientHelper helper_; | 170 ShillClientHelper helper_; |
167 | 171 |
168 DISALLOW_COPY_AND_ASSIGN(ShillManagerClientImpl); | 172 DISALLOW_COPY_AND_ASSIGN(ShillManagerClientImpl); |
169 }; | 173 }; |
170 | 174 |
171 // A stub implementation of ShillManagerClient. | 175 // A stub implementation of ShillManagerClient. |
172 // Implemented: Stub cellular DeviceList entry for SMS testing. | 176 // Implemented: Stub devices and services for NetworkStateManager tests. |
173 class ShillManagerClientStubImpl : public ShillManagerClient { | 177 // Implemented: Stub cellular device entry for SMS tests. |
178 class ShillManagerClientStubImpl : public ShillManagerClient, | |
179 public ShillManagerClient::TestInterface { | |
174 public: | 180 public: |
175 ShillManagerClientStubImpl() : weak_ptr_factory_(this) { | 181 ShillManagerClientStubImpl() : weak_ptr_factory_(this) { |
176 base::ListValue* device_list = new base::ListValue; | 182 SetDefaultProperties(); |
177 // Note: names match Device stub map. | |
178 const char kStubCellular1[] = "stub_cellular1"; | |
179 const char kStubCellular2[] = "stub_cellular2"; | |
180 device_list->Append(base::Value::CreateStringValue(kStubCellular1)); | |
181 device_list->Append(base::Value::CreateStringValue(kStubCellular2)); | |
182 stub_properties_.Set(flimflam::kDevicesProperty, device_list); | |
183 } | 183 } |
184 | 184 |
185 virtual ~ShillManagerClientStubImpl() {} | 185 virtual ~ShillManagerClientStubImpl() {} |
186 | 186 |
187 ////////////////////////////////// | |
188 // ShillManagerClient overrides. | 187 // ShillManagerClient overrides. |
188 | |
189 virtual void AddPropertyChangedObserver( | 189 virtual void AddPropertyChangedObserver( |
190 ShillPropertyChangedObserver* observer) OVERRIDE {} | 190 ShillPropertyChangedObserver* observer) OVERRIDE { |
191 observer_list_.AddObserver(observer); | |
192 } | |
191 | 193 |
192 virtual void RemovePropertyChangedObserver( | 194 virtual void RemovePropertyChangedObserver( |
193 ShillPropertyChangedObserver* observer) OVERRIDE {} | 195 ShillPropertyChangedObserver* observer) OVERRIDE { |
196 observer_list_.RemoveObserver(observer); | |
197 } | |
194 | 198 |
195 // ShillManagerClient override. | |
196 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { | 199 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { |
197 MessageLoop::current()->PostTask( | 200 MessageLoop::current()->PostTask( |
198 FROM_HERE, base::Bind( | 201 FROM_HERE, base::Bind( |
199 &ShillManagerClientStubImpl::PassStubProperties, | 202 &ShillManagerClientStubImpl::PassStubProperties, |
200 weak_ptr_factory_.GetWeakPtr(), | 203 weak_ptr_factory_.GetWeakPtr(), |
201 callback)); | 204 callback)); |
202 } | 205 } |
203 | 206 |
204 // ShillManagerClient override. | |
205 virtual base::DictionaryValue* CallGetPropertiesAndBlock() OVERRIDE { | 207 virtual base::DictionaryValue* CallGetPropertiesAndBlock() OVERRIDE { |
206 return new base::DictionaryValue; | 208 return stub_properties_.DeepCopy(); |
207 } | 209 } |
208 | 210 |
209 // ShillManagerClient override. | |
210 virtual void SetProperty(const std::string& name, | 211 virtual void SetProperty(const std::string& name, |
211 const base::Value& value, | 212 const base::Value& value, |
212 const base::Closure& callback, | 213 const base::Closure& callback, |
213 const ErrorCallback& error_callback) OVERRIDE { | 214 const ErrorCallback& error_callback) OVERRIDE { |
214 stub_properties_.Set(name, value.DeepCopy()); | 215 stub_properties_.Set(name, value.DeepCopy()); |
215 MessageLoop::current()->PostTask(FROM_HERE, callback); | 216 MessageLoop::current()->PostTask(FROM_HERE, callback); |
216 } | 217 } |
217 | 218 |
218 // ShillManagerClient override. | |
219 virtual void RequestScan(const std::string& type, | 219 virtual void RequestScan(const std::string& type, |
220 const base::Closure& callback, | 220 const base::Closure& callback, |
221 const ErrorCallback& error_callback) OVERRIDE { | 221 const ErrorCallback& error_callback) OVERRIDE { |
222 MessageLoop::current()->PostTask(FROM_HERE, callback); | 222 MessageLoop::current()->PostTask(FROM_HERE, callback); |
223 const int kScanDelaySeconds = 3; | |
224 MessageLoop::current()->PostDelayedTask( | |
225 FROM_HERE, | |
226 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | |
227 weak_ptr_factory_.GetWeakPtr(), | |
228 flimflam::kServicesProperty), | |
229 base::TimeDelta::FromSeconds(kScanDelaySeconds)); | |
223 } | 230 } |
224 | 231 |
225 // ShillManagerClient override. | |
226 virtual void EnableTechnology( | 232 virtual void EnableTechnology( |
227 const std::string& type, | 233 const std::string& type, |
228 const base::Closure& callback, | 234 const base::Closure& callback, |
229 const ErrorCallback& error_callback) OVERRIDE { | 235 const ErrorCallback& error_callback) OVERRIDE { |
hashimoto
2012/11/02 02:06:47
What if |type| is not present in kAvailableTechnol
stevenjb
2012/11/02 02:43:25
It's a stub implementation for testing - it's a lo
| |
236 base::ListValue* enabled_list = NULL; | |
237 if (!stub_properties_.GetListWithoutPathExpansion( | |
238 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { | |
239 MessageLoop::current()->PostTask( | |
240 FROM_HERE, | |
241 base::Bind(error_callback, "StubError", "Property not found")); | |
242 return; | |
243 } | |
230 MessageLoop::current()->PostTask(FROM_HERE, callback); | 244 MessageLoop::current()->PostTask(FROM_HERE, callback); |
245 enabled_list->AppendIfNotPresent(new base::StringValue(type)); | |
246 MessageLoop::current()->PostTask( | |
247 FROM_HERE, | |
248 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | |
249 weak_ptr_factory_.GetWeakPtr(), | |
250 flimflam::kEnabledTechnologiesProperty)); | |
231 } | 251 } |
232 | 252 |
233 // ShillManagerClient override. | |
234 virtual void DisableTechnology( | 253 virtual void DisableTechnology( |
235 const std::string& type, | 254 const std::string& type, |
236 const base::Closure& callback, | 255 const base::Closure& callback, |
237 const ErrorCallback& error_callback) OVERRIDE { | 256 const ErrorCallback& error_callback) OVERRIDE { |
hashimoto
2012/11/02 02:06:47
ditto.
| |
257 base::ListValue* enabled_list = NULL; | |
258 if (!stub_properties_.GetListWithoutPathExpansion( | |
259 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { | |
260 MessageLoop::current()->PostTask( | |
261 FROM_HERE, | |
262 base::Bind(error_callback, "StubError", "Property not found")); | |
263 return; | |
264 } | |
238 MessageLoop::current()->PostTask(FROM_HERE, callback); | 265 MessageLoop::current()->PostTask(FROM_HERE, callback); |
266 base::StringValue type_value(type); | |
267 enabled_list->Remove(type_value, NULL); | |
268 MessageLoop::current()->PostTask( | |
269 FROM_HERE, | |
270 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | |
271 weak_ptr_factory_.GetWeakPtr(), | |
272 flimflam::kEnabledTechnologiesProperty)); | |
239 } | 273 } |
240 | 274 |
241 // ShillManagerClient override. | |
242 virtual void ConfigureService( | 275 virtual void ConfigureService( |
243 const base::DictionaryValue& properties, | 276 const base::DictionaryValue& properties, |
244 const base::Closure& callback, | 277 const base::Closure& callback, |
245 const ErrorCallback& error_callback) OVERRIDE { | 278 const ErrorCallback& error_callback) OVERRIDE { |
246 MessageLoop::current()->PostTask(FROM_HERE, callback); | 279 MessageLoop::current()->PostTask(FROM_HERE, callback); |
247 } | 280 } |
248 | 281 |
249 // ShillManagerClient override. | |
250 virtual void GetService( | 282 virtual void GetService( |
251 const base::DictionaryValue& properties, | 283 const base::DictionaryValue& properties, |
252 const ObjectPathCallback& callback, | 284 const ObjectPathCallback& callback, |
253 const ErrorCallback& error_callback) OVERRIDE { | 285 const ErrorCallback& error_callback) OVERRIDE { |
254 MessageLoop::current()->PostTask(FROM_HERE, | 286 MessageLoop::current()->PostTask( |
255 base::Bind(callback, | 287 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); |
256 dbus::ObjectPath())); | 288 } |
289 | |
290 virtual ShillManagerClient::TestInterface* GetTestInterface() OVERRIDE { | |
291 return this; | |
292 } | |
293 | |
294 // ShillManagerClient::TestInterface overrides. | |
295 | |
296 virtual void AddDevice(const std::string& device_path) OVERRIDE { | |
297 GetListProperty(flimflam::kDevicesProperty)->Append( | |
298 base::Value::CreateStringValue(device_path)); | |
299 } | |
300 | |
301 virtual void RemoveDevice(const std::string& device_path) OVERRIDE { | |
302 base::StringValue device_path_value(device_path); | |
303 GetListProperty(flimflam::kDevicesProperty)->Remove( | |
304 device_path_value, NULL); | |
305 } | |
306 | |
307 virtual void AddService(const std::string& service_path, | |
308 bool add_to_watch_list) OVERRIDE { | |
309 GetListProperty(flimflam::kServicesProperty)->Append( | |
310 base::Value::CreateStringValue(service_path)); | |
311 if (add_to_watch_list) { | |
312 GetListProperty(flimflam::kServiceWatchListProperty)->Append( | |
313 base::Value::CreateStringValue(service_path)); | |
314 } | |
315 } | |
316 | |
317 virtual void RemoveService(const std::string& service_path) OVERRIDE { | |
318 base::StringValue service_path_value(service_path); | |
319 GetListProperty(flimflam::kServicesProperty)->Remove( | |
320 service_path_value, NULL); | |
321 GetListProperty(flimflam::kServiceWatchListProperty)->Remove( | |
322 service_path_value, NULL); | |
323 } | |
324 | |
325 virtual void AddTechnology(const std::string& type, bool enabled) OVERRIDE { | |
326 GetListProperty(flimflam::kAvailableTechnologiesProperty)->Append( | |
327 base::Value::CreateStringValue(type)); | |
328 if (enabled) { | |
329 GetListProperty(flimflam::kEnabledTechnologiesProperty)->Append( | |
330 base::Value::CreateStringValue(type)); | |
331 } | |
332 } | |
333 | |
334 virtual void RemoveTechnology(const std::string& type) OVERRIDE { | |
335 base::StringValue type_value(type); | |
336 GetListProperty(flimflam::kAvailableTechnologiesProperty)->Remove( | |
337 type_value, NULL); | |
338 GetListProperty(flimflam::kEnabledTechnologiesProperty)->Remove( | |
339 type_value, NULL); | |
340 } | |
341 | |
342 virtual void ClearProperties() OVERRIDE { | |
343 stub_properties_.Clear(); | |
257 } | 344 } |
258 | 345 |
259 private: | 346 private: |
347 void SetDefaultProperties() { | |
348 // Stub Devices, Note: names match Device stub map. | |
349 AddDevice("stub_wifi_device1"); | |
350 AddDevice("stub_cellular_device1"); | |
351 | |
352 // Stub Services, Note: names match Service stub map. | |
353 AddService("stub_ethernet", true); | |
354 AddService("stub_wifi1", true); | |
355 AddService("stub_wifi2", true); | |
356 AddService("stub_cellular1", true); | |
357 | |
358 // Stub Technologies | |
359 AddTechnology(flimflam::kTypeEthernet, true); | |
360 AddTechnology(flimflam::kTypeWifi, true); | |
361 AddTechnology(flimflam::kTypeCellular, true); | |
362 } | |
363 | |
260 void PassStubProperties(const DictionaryValueCallback& callback) const { | 364 void PassStubProperties(const DictionaryValueCallback& callback) const { |
261 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); | 365 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); |
262 } | 366 } |
263 | 367 |
368 void NotifyObserversPropertyChanged(const std::string& property) { | |
369 base::Value* value = NULL; | |
370 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { | |
371 LOG(ERROR) << "Notify for unknown property: " << property; | |
372 return; | |
373 } | |
374 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, | |
375 observer_list_, | |
376 OnPropertyChanged(property, *value)); | |
377 } | |
378 | |
379 base::ListValue* GetListProperty(const std::string& property) { | |
380 base::ListValue* list_property = NULL; | |
381 if (!stub_properties_.GetListWithoutPathExpansion( | |
382 property, &list_property)) { | |
383 list_property = new base::ListValue; | |
384 stub_properties_.Set(property, list_property); | |
385 } | |
386 return list_property; | |
387 } | |
388 | |
264 base::DictionaryValue stub_properties_; | 389 base::DictionaryValue stub_properties_; |
390 ObserverList<ShillPropertyChangedObserver> observer_list_; | |
265 | 391 |
266 // Note: This should remain the last member so it'll be destroyed and | 392 // Note: This should remain the last member so it'll be destroyed and |
267 // invalidate its weak pointers before any other members are destroyed. | 393 // invalidate its weak pointers before any other members are destroyed. |
268 base::WeakPtrFactory<ShillManagerClientStubImpl> weak_ptr_factory_; | 394 base::WeakPtrFactory<ShillManagerClientStubImpl> weak_ptr_factory_; |
269 | 395 |
270 DISALLOW_COPY_AND_ASSIGN(ShillManagerClientStubImpl); | 396 DISALLOW_COPY_AND_ASSIGN(ShillManagerClientStubImpl); |
271 }; | 397 }; |
272 | 398 |
273 } // namespace | 399 } // namespace |
274 | 400 |
275 ShillManagerClient::ShillManagerClient() {} | 401 ShillManagerClient::ShillManagerClient() {} |
276 | 402 |
277 ShillManagerClient::~ShillManagerClient() {} | 403 ShillManagerClient::~ShillManagerClient() {} |
278 | 404 |
279 // static | 405 // static |
280 ShillManagerClient* ShillManagerClient::Create( | 406 ShillManagerClient* ShillManagerClient::Create( |
281 DBusClientImplementationType type, | 407 DBusClientImplementationType type, |
282 dbus::Bus* bus) { | 408 dbus::Bus* bus) { |
283 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 409 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
284 return new ShillManagerClientImpl(bus); | 410 return new ShillManagerClientImpl(bus); |
285 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 411 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
286 return new ShillManagerClientStubImpl(); | 412 return new ShillManagerClientStubImpl(); |
287 } | 413 } |
288 | 414 |
289 } // namespace chromeos | 415 } // namespace chromeos |
OLD | NEW |