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 void AddPropertyChangedObserver(
30 const dbus::ObjectPath
& profile_path
,
31 ShillPropertyChangedObserver
* observer
) override
{
32 GetHelper(profile_path
)->AddPropertyChangedObserver(observer
);
35 void RemovePropertyChangedObserver(
36 const dbus::ObjectPath
& profile_path
,
37 ShillPropertyChangedObserver
* observer
) override
{
38 GetHelper(profile_path
)->RemovePropertyChangedObserver(observer
);
41 void GetProperties(const dbus::ObjectPath
& profile_path
,
42 const DictionaryValueCallbackWithoutStatus
& callback
,
43 const ErrorCallback
& error_callback
) override
;
44 void GetEntry(const dbus::ObjectPath
& profile_path
,
45 const std::string
& entry_path
,
46 const DictionaryValueCallbackWithoutStatus
& callback
,
47 const ErrorCallback
& error_callback
) override
;
48 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 TestInterface
* GetTestInterface() override
{ return NULL
; }
56 void Init(dbus::Bus
* bus
) override
{ bus_
= bus
; }
59 typedef std::map
<std::string
, ShillClientHelper
*> HelperMap
;
61 // Returns the corresponding ShillClientHelper for the profile.
62 ShillClientHelper
* GetHelper(const dbus::ObjectPath
& profile_path
);
66 STLValueDeleter
<HelperMap
> helpers_deleter_
;
68 DISALLOW_COPY_AND_ASSIGN(ShillProfileClientImpl
);
71 ShillProfileClientImpl::ShillProfileClientImpl()
73 helpers_deleter_(&helpers_
) {
76 ShillClientHelper
* ShillProfileClientImpl::GetHelper(
77 const dbus::ObjectPath
& profile_path
) {
78 HelperMap::iterator it
= helpers_
.find(profile_path
.value());
79 if (it
!= helpers_
.end())
82 // There is no helper for the profile, create it.
83 dbus::ObjectProxy
* object_proxy
=
84 bus_
->GetObjectProxy(shill::kFlimflamServiceName
, profile_path
);
85 ShillClientHelper
* helper
= new ShillClientHelper(object_proxy
);
86 helper
->MonitorPropertyChanged(shill::kFlimflamProfileInterface
);
87 helpers_
.insert(HelperMap::value_type(profile_path
.value(), helper
));
91 void ShillProfileClientImpl::GetProperties(
92 const dbus::ObjectPath
& profile_path
,
93 const DictionaryValueCallbackWithoutStatus
& callback
,
94 const ErrorCallback
& error_callback
) {
95 dbus::MethodCall
method_call(shill::kFlimflamProfileInterface
,
96 shill::kGetPropertiesFunction
);
97 GetHelper(profile_path
)->CallDictionaryValueMethodWithErrorCallback(
98 &method_call
, callback
, error_callback
);
101 void ShillProfileClientImpl::GetEntry(
102 const dbus::ObjectPath
& profile_path
,
103 const std::string
& entry_path
,
104 const DictionaryValueCallbackWithoutStatus
& callback
,
105 const ErrorCallback
& error_callback
) {
106 dbus::MethodCall
method_call(shill::kFlimflamProfileInterface
,
107 shill::kGetEntryFunction
);
108 dbus::MessageWriter
writer(&method_call
);
109 writer
.AppendString(entry_path
);
110 GetHelper(profile_path
)->CallDictionaryValueMethodWithErrorCallback(
111 &method_call
, callback
, error_callback
);
114 void ShillProfileClientImpl::DeleteEntry(
115 const dbus::ObjectPath
& profile_path
,
116 const std::string
& entry_path
,
117 const base::Closure
& callback
,
118 const ErrorCallback
& error_callback
) {
119 dbus::MethodCall
method_call(shill::kFlimflamProfileInterface
,
120 shill::kDeleteEntryFunction
);
121 dbus::MessageWriter
writer(&method_call
);
122 writer
.AppendString(entry_path
);
123 GetHelper(profile_path
)->CallVoidMethodWithErrorCallback(
124 &method_call
, callback
, error_callback
);
129 ShillProfileClient::ShillProfileClient() {}
131 ShillProfileClient::~ShillProfileClient() {}
134 ShillProfileClient
* ShillProfileClient::Create() {
135 return new ShillProfileClientImpl();
139 std::string
ShillProfileClient::GetSharedProfilePath() {
140 return std::string(kSharedProfilePath
);
143 } // namespace chromeos