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"
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"
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"
23 class ShillProfileClientImpl
: public ShillProfileClient
{
25 ShillProfileClientImpl();
27 virtual void AddPropertyChangedObserver(
28 const dbus::ObjectPath
& profile_path
,
29 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
30 GetHelper(profile_path
)->AddPropertyChangedObserver(observer
);
33 virtual void RemovePropertyChangedObserver(
34 const dbus::ObjectPath
& profile_path
,
35 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
36 GetHelper(profile_path
)->RemovePropertyChangedObserver(observer
);
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
;
52 virtual TestInterface
* GetTestInterface() OVERRIDE
{
57 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
62 typedef std::map
<std::string
, ShillClientHelper
*> HelperMap
;
64 // Returns the corresponding ShillClientHelper for the profile.
65 ShillClientHelper
* GetHelper(const dbus::ObjectPath
& profile_path
);
69 STLValueDeleter
<HelperMap
> helpers_deleter_
;
71 DISALLOW_COPY_AND_ASSIGN(ShillProfileClientImpl
);
74 ShillProfileClientImpl::ShillProfileClientImpl()
76 helpers_deleter_(&helpers_
) {
79 ShillClientHelper
* ShillProfileClientImpl::GetHelper(
80 const dbus::ObjectPath
& profile_path
) {
81 HelperMap::iterator it
= helpers_
.find(profile_path
.value());
82 if (it
!= helpers_
.end())
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
));
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
);
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
);
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
);
132 ShillProfileClient::ShillProfileClient() {}
134 ShillProfileClient::~ShillProfileClient() {}
137 ShillProfileClient
* ShillProfileClient::Create() {
138 return new ShillProfileClientImpl();
141 } // namespace chromeos