1 // Copyright (c) 2013 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_stub.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop.h"
10 #include "base/stl_util.h"
11 #include "base/values.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/shill_manager_client.h"
14 #include "chromeos/dbus/shill_property_changed_observer.h"
15 #include "chromeos/dbus/shill_service_client.h"
17 #include "dbus/message.h"
18 #include "dbus/object_path.h"
19 #include "dbus/values_util.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
24 struct ShillProfileClientStub::ProfileProperties
{
25 base::DictionaryValue entries
;
26 base::DictionaryValue properties
;
31 const char kSharedProfilePath
[] = "/profile/default";
34 const ShillProfileClient::DictionaryValueCallbackWithoutStatus
& callback
,
35 const base::DictionaryValue
* dictionary
) {
36 if (callback
.is_null())
38 callback
.Run(*dictionary
);
43 ShillProfileClientStub::ShillProfileClientStub() {
44 AddProfile(kSharedProfilePath
, std::string());
47 ShillProfileClientStub::~ShillProfileClientStub() {
48 STLDeleteValues(&profiles_
);
51 void ShillProfileClientStub::AddPropertyChangedObserver(
52 const dbus::ObjectPath
& profile_path
,
53 ShillPropertyChangedObserver
* observer
) {
56 void ShillProfileClientStub::RemovePropertyChangedObserver(
57 const dbus::ObjectPath
& profile_path
,
58 ShillPropertyChangedObserver
* observer
) {
61 void ShillProfileClientStub::GetProperties(
62 const dbus::ObjectPath
& profile_path
,
63 const DictionaryValueCallbackWithoutStatus
& callback
,
64 const ErrorCallback
& error_callback
) {
65 ProfileProperties
* profile
= GetProfile(profile_path
, error_callback
);
69 scoped_ptr
<base::DictionaryValue
> properties(profile
->properties
.DeepCopy());
70 base::ListValue
* entry_paths
= new base::ListValue
;
71 properties
->SetWithoutPathExpansion(flimflam::kEntriesProperty
, entry_paths
);
72 for (base::DictionaryValue::Iterator
it(profile
->entries
); !it
.IsAtEnd();
74 entry_paths
->AppendString(it
.key());
77 base::MessageLoop::current()->PostTask(
79 base::Bind(&PassDictionary
, callback
, base::Owned(properties
.release())));
82 void ShillProfileClientStub::GetEntry(
83 const dbus::ObjectPath
& profile_path
,
84 const std::string
& entry_path
,
85 const DictionaryValueCallbackWithoutStatus
& callback
,
86 const ErrorCallback
& error_callback
) {
87 ProfileProperties
* profile
= GetProfile(profile_path
, error_callback
);
91 base::DictionaryValue
* entry
= NULL
;
92 profile
->entries
.GetDictionaryWithoutPathExpansion(entry_path
, &entry
);
94 error_callback
.Run("Error.InvalidProfileEntry", "Invalid profile entry");
98 base::MessageLoop::current()->PostTask(
100 base::Bind(&PassDictionary
, callback
, base::Owned(entry
->DeepCopy())));
103 void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath
& profile_path
,
104 const std::string
& entry_path
,
105 const base::Closure
& callback
,
106 const ErrorCallback
& error_callback
) {
107 ProfileProperties
* profile
= GetProfile(profile_path
, error_callback
);
111 if (!profile
->entries
.RemoveWithoutPathExpansion(entry_path
, NULL
)) {
112 error_callback
.Run("Error.InvalidProfileEntry", "Invalid profile entry");
116 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
119 ShillProfileClient::TestInterface
* ShillProfileClientStub::GetTestInterface() {
123 void ShillProfileClientStub::AddProfile(const std::string
& profile_path
,
124 const std::string
& userhash
) {
125 if (GetProfile(dbus::ObjectPath(profile_path
), ErrorCallback()))
128 ProfileProperties
* profile
= new ProfileProperties
;
129 profile
->properties
.SetStringWithoutPathExpansion(shill::kUserHashProperty
,
131 profiles_
[profile_path
] = profile
;
132 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
133 AddProfile(profile_path
);
136 void ShillProfileClientStub::AddEntry(const std::string
& profile_path
,
137 const std::string
& entry_path
,
138 const base::DictionaryValue
& properties
) {
139 ProfileProperties
* profile
= GetProfile(dbus::ObjectPath(profile_path
),
142 profile
->entries
.SetWithoutPathExpansion(entry_path
,
143 properties
.DeepCopy());
146 bool ShillProfileClientStub::AddService(const std::string
& service_path
) {
147 ShillServiceClient::TestInterface
* service_test
=
148 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
149 const base::DictionaryValue
* properties
=
150 service_test
->GetServiceProperties(service_path
);
151 std::string profile_path
;
155 properties
->GetStringWithoutPathExpansion(flimflam::kProfileProperty
,
157 if (profile_path
.empty())
160 AddEntry(profile_path
, service_path
, *properties
);
164 ShillProfileClientStub::ProfileProperties
* ShillProfileClientStub::GetProfile(
165 const dbus::ObjectPath
& profile_path
,
166 const ErrorCallback
& error_callback
) {
167 ProfileMap::const_iterator found
= profiles_
.find(profile_path
.value());
168 if (found
== profiles_
.end()) {
169 if (!error_callback
.is_null())
170 error_callback
.Run("Error.InvalidProfile", "Invalid profile");
174 return found
->second
;
177 } // namespace chromeos