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_profile_client.h"
6
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_property_changed_observer.h"
13 #include "dbus/bus.h"
14 #include "dbus/message.h"
15 #include "dbus/object_path.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 class ShillProfileClientImpl : public ShillProfileClient {
24 public:
25 ShillProfileClientImpl();
26
AddPropertyChangedObserver(const dbus::ObjectPath & profile_path,ShillPropertyChangedObserver * observer)27 virtual void AddPropertyChangedObserver(
28 const dbus::ObjectPath& profile_path,
29 ShillPropertyChangedObserver* observer) OVERRIDE {
30 GetHelper(profile_path)->AddPropertyChangedObserver(observer);
31 }
32
RemovePropertyChangedObserver(const dbus::ObjectPath & profile_path,ShillPropertyChangedObserver * observer)33 virtual void RemovePropertyChangedObserver(
34 const dbus::ObjectPath& profile_path,
35 ShillPropertyChangedObserver* observer) OVERRIDE {
36 GetHelper(profile_path)->RemovePropertyChangedObserver(observer);
37 }
38
39 virtual void GetProperties(
40 const dbus::ObjectPath& profile_path,
41 const DictionaryValueCallbackWithoutStatus& callback,
42 const ErrorCallback& error_callback) OVERRIDE;
43 virtual void GetEntry(const dbus::ObjectPath& profile_path,
44 const std::string& entry_path,
45 const DictionaryValueCallbackWithoutStatus& callback,
46 const ErrorCallback& error_callback) OVERRIDE;
47 virtual void DeleteEntry(const dbus::ObjectPath& profile_path,
48 const std::string& entry_path,
49 const base::Closure& callback,
50 const ErrorCallback& error_callback) OVERRIDE;
51
GetTestInterface()52 virtual TestInterface* GetTestInterface() OVERRIDE {
53 return NULL;
54 }
55
56 protected:
Init(dbus::Bus * bus)57 virtual void Init(dbus::Bus* bus) OVERRIDE {
58 bus_ = bus;
59 }
60
61 private:
62 typedef std::map<std::string, ShillClientHelper*> HelperMap;
63
64 // Returns the corresponding ShillClientHelper for the profile.
65 ShillClientHelper* GetHelper(const dbus::ObjectPath& profile_path);
66
67 dbus::Bus* bus_;
68 HelperMap helpers_;
69 STLValueDeleter<HelperMap> helpers_deleter_;
70
71 DISALLOW_COPY_AND_ASSIGN(ShillProfileClientImpl);
72 };
73
ShillProfileClientImpl()74 ShillProfileClientImpl::ShillProfileClientImpl()
75 : bus_(NULL),
76 helpers_deleter_(&helpers_) {
77 }
78
GetHelper(const dbus::ObjectPath & profile_path)79 ShillClientHelper* ShillProfileClientImpl::GetHelper(
80 const dbus::ObjectPath& profile_path) {
81 HelperMap::iterator it = helpers_.find(profile_path.value());
82 if (it != helpers_.end())
83 return it->second;
84
85 // There is no helper for the profile, create it.
86 dbus::ObjectProxy* object_proxy =
87 bus_->GetObjectProxy(shill::kFlimflamServiceName, profile_path);
88 ShillClientHelper* helper = new ShillClientHelper(object_proxy);
89 helper->MonitorPropertyChanged(shill::kFlimflamProfileInterface);
90 helpers_.insert(HelperMap::value_type(profile_path.value(), helper));
91 return helper;
92 }
93
GetProperties(const dbus::ObjectPath & profile_path,const DictionaryValueCallbackWithoutStatus & callback,const ErrorCallback & error_callback)94 void ShillProfileClientImpl::GetProperties(
95 const dbus::ObjectPath& profile_path,
96 const DictionaryValueCallbackWithoutStatus& callback,
97 const ErrorCallback& error_callback) {
98 dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
99 shill::kGetPropertiesFunction);
100 GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
101 &method_call, callback, error_callback);
102 }
103
GetEntry(const dbus::ObjectPath & profile_path,const std::string & entry_path,const DictionaryValueCallbackWithoutStatus & callback,const ErrorCallback & error_callback)104 void ShillProfileClientImpl::GetEntry(
105 const dbus::ObjectPath& profile_path,
106 const std::string& entry_path,
107 const DictionaryValueCallbackWithoutStatus& callback,
108 const ErrorCallback& error_callback) {
109 dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
110 shill::kGetEntryFunction);
111 dbus::MessageWriter writer(&method_call);
112 writer.AppendString(entry_path);
113 GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
114 &method_call, callback, error_callback);
115 }
116
DeleteEntry(const dbus::ObjectPath & profile_path,const std::string & entry_path,const base::Closure & callback,const ErrorCallback & error_callback)117 void ShillProfileClientImpl::DeleteEntry(
118 const dbus::ObjectPath& profile_path,
119 const std::string& entry_path,
120 const base::Closure& callback,
121 const ErrorCallback& error_callback) {
122 dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
123 shill::kDeleteEntryFunction);
124 dbus::MessageWriter writer(&method_call);
125 writer.AppendString(entry_path);
126 GetHelper(profile_path)->CallVoidMethodWithErrorCallback(
127 &method_call, callback, error_callback);
128 }
129
130 } // namespace
131
ShillProfileClient()132 ShillProfileClient::ShillProfileClient() {}
133
~ShillProfileClient()134 ShillProfileClient::~ShillProfileClient() {}
135
136 // static
Create()137 ShillProfileClient* ShillProfileClient::Create() {
138 return new ShillProfileClientImpl();
139 }
140
141 } // namespace chromeos
142