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