| OLD | NEW |
| (Empty) |
| 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 #include "chromeos/dbus/shill_network_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/stl_util.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chromeos/dbus/shill_property_changed_observer.h" | |
| 12 #include "dbus/bus.h" | |
| 13 #include "dbus/message.h" | |
| 14 #include "dbus/object_path.h" | |
| 15 #include "dbus/object_proxy.h" | |
| 16 #include "dbus/values_util.h" | |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 18 | |
| 19 namespace chromeos { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // The ShillNetworkClient implementation. | |
| 24 class ShillNetworkClientImpl : public ShillNetworkClient { | |
| 25 public: | |
| 26 explicit ShillNetworkClientImpl(dbus::Bus* bus) | |
| 27 : bus_(bus), | |
| 28 helpers_deleter_(&helpers_) { | |
| 29 } | |
| 30 | |
| 31 ////////////////////////////////////// | |
| 32 // ShillNetworkClient overrides. | |
| 33 virtual void AddPropertyChangedObserver( | |
| 34 const dbus::ObjectPath& network_path, | |
| 35 ShillPropertyChangedObserver* observer) OVERRIDE { | |
| 36 GetHelper(network_path)->AddPropertyChangedObserver(observer); | |
| 37 } | |
| 38 | |
| 39 virtual void RemovePropertyChangedObserver( | |
| 40 const dbus::ObjectPath& network_path, | |
| 41 ShillPropertyChangedObserver* observer) OVERRIDE { | |
| 42 GetHelper(network_path)->RemovePropertyChangedObserver(observer); | |
| 43 } | |
| 44 | |
| 45 virtual void GetProperties(const dbus::ObjectPath& network_path, | |
| 46 const DictionaryValueCallback& callback) OVERRIDE { | |
| 47 dbus::MethodCall method_call(flimflam::kFlimflamNetworkInterface, | |
| 48 flimflam::kGetPropertiesFunction); | |
| 49 GetHelper(network_path)->CallDictionaryValueMethod(&method_call, callback); | |
| 50 } | |
| 51 | |
| 52 virtual base::DictionaryValue* CallGetPropertiesAndBlock( | |
| 53 const dbus::ObjectPath& network_path) OVERRIDE { | |
| 54 dbus::MethodCall method_call(flimflam::kFlimflamNetworkInterface, | |
| 55 flimflam::kGetPropertiesFunction); | |
| 56 return GetHelper(network_path)->CallDictionaryValueMethodAndBlock( | |
| 57 &method_call); | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 typedef std::map<std::string, ShillClientHelper*> HelperMap; | |
| 62 | |
| 63 // Returns the corresponding ShillClientHelper for the profile. | |
| 64 ShillClientHelper* GetHelper(const dbus::ObjectPath& network_path) { | |
| 65 HelperMap::iterator it = helpers_.find(network_path.value()); | |
| 66 if (it != helpers_.end()) | |
| 67 return it->second; | |
| 68 | |
| 69 // There is no helper for the profile, create it. | |
| 70 dbus::ObjectProxy* object_proxy = | |
| 71 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, network_path); | |
| 72 ShillClientHelper* helper = new ShillClientHelper(bus_, object_proxy); | |
| 73 helper->MonitorPropertyChanged(flimflam::kFlimflamNetworkInterface); | |
| 74 helpers_.insert(HelperMap::value_type(network_path.value(), helper)); | |
| 75 return helper; | |
| 76 } | |
| 77 | |
| 78 dbus::Bus* bus_; | |
| 79 HelperMap helpers_; | |
| 80 STLValueDeleter<HelperMap> helpers_deleter_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(ShillNetworkClientImpl); | |
| 83 }; | |
| 84 | |
| 85 // A stub implementation of ShillNetworkClient. | |
| 86 class ShillNetworkClientStubImpl : public ShillNetworkClient { | |
| 87 public: | |
| 88 ShillNetworkClientStubImpl() : weak_ptr_factory_(this) {} | |
| 89 | |
| 90 virtual ~ShillNetworkClientStubImpl() {} | |
| 91 | |
| 92 ///////////////////////////////////// | |
| 93 // ShillNetworkClient overrides. | |
| 94 virtual void AddPropertyChangedObserver( | |
| 95 const dbus::ObjectPath& network_path, | |
| 96 ShillPropertyChangedObserver* observer) OVERRIDE {} | |
| 97 | |
| 98 virtual void RemovePropertyChangedObserver( | |
| 99 const dbus::ObjectPath& network_path, | |
| 100 ShillPropertyChangedObserver* observer) OVERRIDE {} | |
| 101 | |
| 102 virtual void GetProperties(const dbus::ObjectPath& network_path, | |
| 103 const DictionaryValueCallback& callback) OVERRIDE { | |
| 104 MessageLoop::current()->PostTask( | |
| 105 FROM_HERE, | |
| 106 base::Bind(&ShillNetworkClientStubImpl::PassEmptyDictionaryValue, | |
| 107 weak_ptr_factory_.GetWeakPtr(), | |
| 108 callback)); | |
| 109 } | |
| 110 | |
| 111 virtual base::DictionaryValue* CallGetPropertiesAndBlock( | |
| 112 const dbus::ObjectPath& network_path) OVERRIDE { | |
| 113 return new base::DictionaryValue; | |
| 114 } | |
| 115 | |
| 116 private: | |
| 117 void PassEmptyDictionaryValue(const DictionaryValueCallback& callback) const { | |
| 118 base::DictionaryValue dictionary; | |
| 119 callback.Run(DBUS_METHOD_CALL_SUCCESS, dictionary); | |
| 120 } | |
| 121 | |
| 122 // Note: This should remain the last member so it'll be destroyed and | |
| 123 // invalidate its weak pointers before any other members are destroyed. | |
| 124 base::WeakPtrFactory<ShillNetworkClientStubImpl> weak_ptr_factory_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(ShillNetworkClientStubImpl); | |
| 127 }; | |
| 128 | |
| 129 } // namespace | |
| 130 | |
| 131 ShillNetworkClient::ShillNetworkClient() {} | |
| 132 | |
| 133 ShillNetworkClient::~ShillNetworkClient() {} | |
| 134 | |
| 135 // static | |
| 136 ShillNetworkClient* ShillNetworkClient::Create( | |
| 137 DBusClientImplementationType type, | |
| 138 dbus::Bus* bus) { | |
| 139 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
| 140 return new ShillNetworkClientImpl(bus); | |
| 141 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 142 return new ShillNetworkClientStubImpl(); | |
| 143 } | |
| 144 | |
| 145 } // namespace chromeos | |
| OLD | NEW |