Track active references in ShillClientHelper (Take 2)
[chromium-blink-merge.git] / chromeos / dbus / shill_profile_client.cc
blob0e6335443d2128e9ad2601349a8db8685d8ae245
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.
5 #include "chromeos/dbus/shill_profile_client.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/stl_util.h"
10 #include "base/values.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/shill_profile_client_stub.h"
13 #include "chromeos/dbus/shill_property_changed_observer.h"
14 #include "dbus/bus.h"
15 #include "dbus/message.h"
16 #include "dbus/object_path.h"
17 #include "dbus/values_util.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h"
20 namespace chromeos {
22 namespace {
24 class ShillProfileClientImpl : public ShillProfileClient {
25 public:
26 ShillProfileClientImpl();
28 virtual void AddPropertyChangedObserver(
29 const dbus::ObjectPath& profile_path,
30 ShillPropertyChangedObserver* observer) OVERRIDE {
31 GetHelper(profile_path)->AddPropertyChangedObserver(observer);
34 virtual void RemovePropertyChangedObserver(
35 const dbus::ObjectPath& profile_path,
36 ShillPropertyChangedObserver* observer) OVERRIDE {
37 GetHelper(profile_path)->RemovePropertyChangedObserver(observer);
40 virtual void GetProperties(
41 const dbus::ObjectPath& profile_path,
42 const DictionaryValueCallbackWithoutStatus& callback,
43 const ErrorCallback& error_callback) OVERRIDE;
44 virtual void GetEntry(const dbus::ObjectPath& profile_path,
45 const std::string& entry_path,
46 const DictionaryValueCallbackWithoutStatus& callback,
47 const ErrorCallback& error_callback) OVERRIDE;
48 virtual void DeleteEntry(const dbus::ObjectPath& profile_path,
49 const std::string& entry_path,
50 const base::Closure& callback,
51 const ErrorCallback& error_callback) OVERRIDE;
53 virtual TestInterface* GetTestInterface() OVERRIDE {
54 return NULL;
57 protected:
58 virtual void Init(dbus::Bus* bus) OVERRIDE {
59 bus_ = bus;
62 private:
63 typedef std::map<std::string, ShillClientHelper*> HelperMap;
65 // Returns the corresponding ShillClientHelper for the profile.
66 ShillClientHelper* GetHelper(const dbus::ObjectPath& profile_path);
68 dbus::Bus* bus_;
69 HelperMap helpers_;
70 STLValueDeleter<HelperMap> helpers_deleter_;
72 DISALLOW_COPY_AND_ASSIGN(ShillProfileClientImpl);
75 ShillProfileClientImpl::ShillProfileClientImpl()
76 : bus_(NULL),
77 helpers_deleter_(&helpers_) {
80 ShillClientHelper* ShillProfileClientImpl::GetHelper(
81 const dbus::ObjectPath& profile_path) {
82 HelperMap::iterator it = helpers_.find(profile_path.value());
83 if (it != helpers_.end())
84 return it->second;
86 // There is no helper for the profile, create it.
87 dbus::ObjectProxy* object_proxy =
88 bus_->GetObjectProxy(shill::kFlimflamServiceName, profile_path);
89 ShillClientHelper* helper = new ShillClientHelper(object_proxy);
90 helper->MonitorPropertyChanged(shill::kFlimflamProfileInterface);
91 helpers_.insert(HelperMap::value_type(profile_path.value(), helper));
92 return helper;
95 void ShillProfileClientImpl::GetProperties(
96 const dbus::ObjectPath& profile_path,
97 const DictionaryValueCallbackWithoutStatus& callback,
98 const ErrorCallback& error_callback) {
99 dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
100 shill::kGetPropertiesFunction);
101 GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
102 &method_call, callback, error_callback);
105 void ShillProfileClientImpl::GetEntry(
106 const dbus::ObjectPath& profile_path,
107 const std::string& entry_path,
108 const DictionaryValueCallbackWithoutStatus& callback,
109 const ErrorCallback& error_callback) {
110 dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
111 shill::kGetEntryFunction);
112 dbus::MessageWriter writer(&method_call);
113 writer.AppendString(entry_path);
114 GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
115 &method_call, callback, error_callback);
118 void ShillProfileClientImpl::DeleteEntry(
119 const dbus::ObjectPath& profile_path,
120 const std::string& entry_path,
121 const base::Closure& callback,
122 const ErrorCallback& error_callback) {
123 dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
124 shill::kDeleteEntryFunction);
125 dbus::MessageWriter writer(&method_call);
126 writer.AppendString(entry_path);
127 GetHelper(profile_path)->CallVoidMethodWithErrorCallback(
128 &method_call, callback, error_callback);
131 } // namespace
133 ShillProfileClient::ShillProfileClient() {}
135 ShillProfileClient::~ShillProfileClient() {}
137 // static
138 ShillProfileClient* ShillProfileClient::Create(
139 DBusClientImplementationType type) {
140 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
141 return new ShillProfileClientImpl();
142 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
143 return new ShillProfileClientStub();
146 } // namespace chromeos