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_service_client.h" | 5 #include "chromeos/dbus/shill_service_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/weak_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
10 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
11 #include "base/values.h" | 10 #include "base/values.h" |
12 #include "chromeos/dbus/shill_property_changed_observer.h" | 11 #include "chromeos/dbus/shill_property_changed_observer.h" |
13 #include "chromeos/dbus/shill_service_client_stub.h" | 12 #include "chromeos/dbus/shill_service_client_stub.h" |
14 #include "chromeos/network/network_event_log.h" | |
15 #include "dbus/bus.h" | 13 #include "dbus/bus.h" |
16 #include "dbus/message.h" | 14 #include "dbus/message.h" |
17 #include "dbus/object_proxy.h" | 15 #include "dbus/object_proxy.h" |
18 #include "third_party/cros_system_api/dbus/service_constants.h" | 16 #include "third_party/cros_system_api/dbus/service_constants.h" |
19 | 17 |
20 namespace chromeos { | 18 namespace chromeos { |
21 | 19 |
22 namespace { | 20 namespace { |
23 | 21 |
24 #ifndef DBUS_ERROR_UNKNOWN_OBJECT | 22 #ifndef DBUS_ERROR_UNKNOWN_OBJECT |
(...skipping 24 matching lines...) Expand all Loading... |
49 | 47 |
50 base::DictionaryValue empty_dictionary; | 48 base::DictionaryValue empty_dictionary; |
51 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); | 49 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); |
52 } | 50 } |
53 | 51 |
54 // The ShillServiceClient implementation. | 52 // The ShillServiceClient implementation. |
55 class ShillServiceClientImpl : public ShillServiceClient { | 53 class ShillServiceClientImpl : public ShillServiceClient { |
56 public: | 54 public: |
57 explicit ShillServiceClientImpl() | 55 explicit ShillServiceClientImpl() |
58 : bus_(NULL), | 56 : bus_(NULL), |
59 helpers_deleter_(&helpers_), | 57 helpers_deleter_(&helpers_) { |
60 weak_ptr_factory_(this) { | |
61 } | 58 } |
62 | 59 |
63 virtual void AddPropertyChangedObserver( | 60 virtual void AddPropertyChangedObserver( |
64 const dbus::ObjectPath& service_path, | 61 const dbus::ObjectPath& service_path, |
65 ShillPropertyChangedObserver* observer) OVERRIDE { | 62 ShillPropertyChangedObserver* observer) OVERRIDE { |
66 GetHelper(service_path)->AddPropertyChangedObserver(observer); | 63 GetHelper(service_path)->AddPropertyChangedObserver(observer); |
67 } | 64 } |
68 | 65 |
69 virtual void RemovePropertyChangedObserver( | 66 virtual void RemovePropertyChangedObserver( |
70 const dbus::ObjectPath& service_path, | 67 const dbus::ObjectPath& service_path, |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 private: | 215 private: |
219 typedef std::map<std::string, ShillClientHelper*> HelperMap; | 216 typedef std::map<std::string, ShillClientHelper*> HelperMap; |
220 | 217 |
221 // Returns the corresponding ShillClientHelper for the profile. | 218 // Returns the corresponding ShillClientHelper for the profile. |
222 ShillClientHelper* GetHelper(const dbus::ObjectPath& service_path) { | 219 ShillClientHelper* GetHelper(const dbus::ObjectPath& service_path) { |
223 HelperMap::iterator it = helpers_.find(service_path.value()); | 220 HelperMap::iterator it = helpers_.find(service_path.value()); |
224 if (it != helpers_.end()) | 221 if (it != helpers_.end()) |
225 return it->second; | 222 return it->second; |
226 | 223 |
227 // There is no helper for the profile, create it. | 224 // There is no helper for the profile, create it. |
228 NET_LOG_DEBUG("AddShillClientHelper", service_path.value()); | |
229 dbus::ObjectProxy* object_proxy = | 225 dbus::ObjectProxy* object_proxy = |
230 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, service_path); | 226 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, service_path); |
231 ShillClientHelper* helper = new ShillClientHelper(object_proxy); | 227 ShillClientHelper* helper = new ShillClientHelper(bus_, object_proxy); |
232 helper->SetReleasedCallback( | |
233 base::Bind(&ShillServiceClientImpl::NotifyReleased, | |
234 weak_ptr_factory_.GetWeakPtr())); | |
235 helper->MonitorPropertyChanged(flimflam::kFlimflamServiceInterface); | 228 helper->MonitorPropertyChanged(flimflam::kFlimflamServiceInterface); |
236 helpers_.insert(HelperMap::value_type(service_path.value(), helper)); | 229 helpers_.insert(HelperMap::value_type(service_path.value(), helper)); |
237 return helper; | 230 return helper; |
238 } | 231 } |
239 | 232 |
240 void NotifyReleased(ShillClientHelper* helper) { | |
241 // New Shill Service DBus objects are created relatively frequently, so | |
242 // remove them when they become inactive (no observers and no active method | |
243 // calls). | |
244 const dbus::ObjectPath& object_path = helper->object_proxy()->object_path(); | |
245 NET_LOG_DEBUG("RemoveShillClientHelper", object_path.value()); | |
246 bus_->RemoveObjectProxy(flimflam::kFlimflamServiceName, | |
247 object_path, base::Bind(&base::DoNothing)); | |
248 helpers_.erase(object_path.value()); | |
249 delete helper; | |
250 } | |
251 | |
252 dbus::Bus* bus_; | 233 dbus::Bus* bus_; |
253 HelperMap helpers_; | 234 HelperMap helpers_; |
254 STLValueDeleter<HelperMap> helpers_deleter_; | 235 STLValueDeleter<HelperMap> helpers_deleter_; |
255 base::WeakPtrFactory<ShillServiceClientImpl> weak_ptr_factory_; | |
256 | 236 |
257 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientImpl); | 237 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientImpl); |
258 }; | 238 }; |
259 | 239 |
260 } // namespace | 240 } // namespace |
261 | 241 |
262 ShillServiceClient::ShillServiceClient() {} | 242 ShillServiceClient::ShillServiceClient() {} |
263 | 243 |
264 ShillServiceClient::~ShillServiceClient() {} | 244 ShillServiceClient::~ShillServiceClient() {} |
265 | 245 |
266 // static | 246 // static |
267 ShillServiceClient* ShillServiceClient::Create( | 247 ShillServiceClient* ShillServiceClient::Create( |
268 DBusClientImplementationType type) { | 248 DBusClientImplementationType type) { |
269 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 249 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
270 return new ShillServiceClientImpl(); | 250 return new ShillServiceClientImpl(); |
271 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 251 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
272 return new ShillServiceClientStub(); | 252 return new ShillServiceClientStub(); |
273 } | 253 } |
274 | 254 |
275 } // namespace chromeos | 255 } // namespace chromeos |
OLD | NEW |