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 const char kSharedProfilePath
[] = "/profile/default";
25 class ShillProfileClientImpl
: public ShillProfileClient
{
27 ShillProfileClientImpl();
29 virtual void AddPropertyChangedObserver(
30 const dbus::ObjectPath
& profile_path
,
31 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
32 GetHelper(profile_path
)->AddPropertyChangedObserver(observer
);
35 virtual void RemovePropertyChangedObserver(
36 const dbus::ObjectPath
& profile_path
,
37 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
38 GetHelper(profile_path
)->RemovePropertyChangedObserver(observer
);
41 virtual void GetProperties(
42 const dbus::ObjectPath
& profile_path
,
43 const DictionaryValueCallbackWithoutStatus
& callback
,
44 const ErrorCallback
& error_callback
) OVERRIDE
;
45 virtual void GetEntry(const dbus::ObjectPath
& profile_path
,
46 const std::string
& entry_path
,
47 const DictionaryValueCallbackWithoutStatus
& callback
,
48 const ErrorCallback
& error_callback
) OVERRIDE
;
49 virtual void DeleteEntry(const dbus::ObjectPath
& profile_path
,
50 const std::string
& entry_path
,
51 const base::Closure
& callback
,
52 const ErrorCallback
& error_callback
) OVERRIDE
;
54 virtual TestInterface
* GetTestInterface() OVERRIDE
{
59 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
64 typedef std::map
<std::string
, ShillClientHelper
*> HelperMap
;
66 // Returns the corresponding ShillClientHelper for the profile.
67 ShillClientHelper
* GetHelper(const dbus::ObjectPath
& profile_path
);
71 STLValueDeleter
<HelperMap
> helpers_deleter_
;
73 DISALLOW_COPY_AND_ASSIGN(ShillProfileClientImpl
);
76 ShillProfileClientImpl::ShillProfileClientImpl()
78 helpers_deleter_(&helpers_
) {
81 ShillClientHelper
* ShillProfileClientImpl::GetHelper(
82 const dbus::ObjectPath
& profile_path
) {
83 HelperMap::iterator it
= helpers_
.find(profile_path
.value());
84 if (it
!= helpers_
.end())
87 // There is no helper for the profile, create it.
88 dbus::ObjectProxy
* object_proxy
=
89 bus_
->GetObjectProxy(shill::kFlimflamServiceName
, profile_path
);
90 ShillClientHelper
* helper
= new ShillClientHelper(object_proxy
);
91 helper
->MonitorPropertyChanged(shill::kFlimflamProfileInterface
);
92 helpers_
.insert(HelperMap::value_type(profile_path
.value(), helper
));
96 void ShillProfileClientImpl::GetProperties(
97 const dbus::ObjectPath
& profile_path
,
98 const DictionaryValueCallbackWithoutStatus
& callback
,
99 const ErrorCallback
& error_callback
) {
100 dbus::MethodCall
method_call(shill::kFlimflamProfileInterface
,
101 shill::kGetPropertiesFunction
);
102 GetHelper(profile_path
)->CallDictionaryValueMethodWithErrorCallback(
103 &method_call
, callback
, error_callback
);
106 void ShillProfileClientImpl::GetEntry(
107 const dbus::ObjectPath
& profile_path
,
108 const std::string
& entry_path
,
109 const DictionaryValueCallbackWithoutStatus
& callback
,
110 const ErrorCallback
& error_callback
) {
111 dbus::MethodCall
method_call(shill::kFlimflamProfileInterface
,
112 shill::kGetEntryFunction
);
113 dbus::MessageWriter
writer(&method_call
);
114 writer
.AppendString(entry_path
);
115 GetHelper(profile_path
)->CallDictionaryValueMethodWithErrorCallback(
116 &method_call
, callback
, error_callback
);
119 void ShillProfileClientImpl::DeleteEntry(
120 const dbus::ObjectPath
& profile_path
,
121 const std::string
& entry_path
,
122 const base::Closure
& callback
,
123 const ErrorCallback
& error_callback
) {
124 dbus::MethodCall
method_call(shill::kFlimflamProfileInterface
,
125 shill::kDeleteEntryFunction
);
126 dbus::MessageWriter
writer(&method_call
);
127 writer
.AppendString(entry_path
);
128 GetHelper(profile_path
)->CallVoidMethodWithErrorCallback(
129 &method_call
, callback
, error_callback
);
134 ShillProfileClient::ShillProfileClient() {}
136 ShillProfileClient::~ShillProfileClient() {}
139 ShillProfileClient
* ShillProfileClient::Create() {
140 return new ShillProfileClientImpl();
144 std::string
ShillProfileClient::GetSharedProfilePath() {
145 return std::string(kSharedProfilePath
);
148 } // namespace chromeos