1 // Copyright 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/fake_shill_profile_client.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop/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_property_changed_observer.h"
14 #include "chromeos/dbus/shill_service_client.h"
16 #include "dbus/message.h"
17 #include "dbus/object_path.h"
18 #include "dbus/values_util.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h"
23 struct FakeShillProfileClient::ProfileProperties
{
24 base::DictionaryValue entries
; // Dictionary of Service Dictionaries
25 base::DictionaryValue properties
; // Dictionary of Profile properties
31 const ShillProfileClient::DictionaryValueCallbackWithoutStatus
& callback
,
32 const base::DictionaryValue
* dictionary
) {
33 callback
.Run(*dictionary
);
38 FakeShillProfileClient::FakeShillProfileClient() {
41 FakeShillProfileClient::~FakeShillProfileClient() {
42 STLDeleteValues(&profiles_
);
45 void FakeShillProfileClient::Init(dbus::Bus
* bus
) {
48 void FakeShillProfileClient::AddPropertyChangedObserver(
49 const dbus::ObjectPath
& profile_path
,
50 ShillPropertyChangedObserver
* observer
) {
53 void FakeShillProfileClient::RemovePropertyChangedObserver(
54 const dbus::ObjectPath
& profile_path
,
55 ShillPropertyChangedObserver
* observer
) {
58 void FakeShillProfileClient::GetProperties(
59 const dbus::ObjectPath
& profile_path
,
60 const DictionaryValueCallbackWithoutStatus
& callback
,
61 const ErrorCallback
& error_callback
) {
62 ProfileProperties
* profile
= GetProfile(profile_path
, error_callback
);
66 scoped_ptr
<base::DictionaryValue
> properties(profile
->properties
.DeepCopy());
67 base::ListValue
* entry_paths
= new base::ListValue
;
68 properties
->SetWithoutPathExpansion(shill::kEntriesProperty
, entry_paths
);
69 for (base::DictionaryValue::Iterator
it(profile
->entries
); !it
.IsAtEnd();
71 entry_paths
->AppendString(it
.key());
74 base::MessageLoop::current()->PostTask(
76 base::Bind(&PassDictionary
, callback
, base::Owned(properties
.release())));
79 void FakeShillProfileClient::GetEntry(
80 const dbus::ObjectPath
& profile_path
,
81 const std::string
& entry_path
,
82 const DictionaryValueCallbackWithoutStatus
& callback
,
83 const ErrorCallback
& error_callback
) {
84 ProfileProperties
* profile
= GetProfile(profile_path
, error_callback
);
88 base::DictionaryValue
* entry
= NULL
;
89 profile
->entries
.GetDictionaryWithoutPathExpansion(entry_path
, &entry
);
91 error_callback
.Run("Error.InvalidProfileEntry", "Invalid profile entry");
95 base::MessageLoop::current()->PostTask(
97 base::Bind(&PassDictionary
, callback
, base::Owned(entry
->DeepCopy())));
100 void FakeShillProfileClient::DeleteEntry(const dbus::ObjectPath
& profile_path
,
101 const std::string
& entry_path
,
102 const base::Closure
& callback
,
103 const ErrorCallback
& error_callback
) {
104 ProfileProperties
* profile
= GetProfile(profile_path
, error_callback
);
108 if (!profile
->entries
.RemoveWithoutPathExpansion(entry_path
, NULL
)) {
109 error_callback
.Run("Error.InvalidProfileEntry", entry_path
);
113 base::StringValue
profile_path_value("");
114 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()->
115 SetServiceProperty(entry_path
,
116 shill::kProfileProperty
,
119 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
122 ShillProfileClient::TestInterface
* FakeShillProfileClient::GetTestInterface() {
126 void FakeShillProfileClient::AddProfile(const std::string
& profile_path
,
127 const std::string
& userhash
) {
128 if (GetProfile(dbus::ObjectPath(profile_path
), ErrorCallback()))
131 ProfileProperties
* profile
= new ProfileProperties
;
132 profile
->properties
.SetStringWithoutPathExpansion(shill::kUserHashProperty
,
134 profiles_
[profile_path
] = profile
;
135 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
136 AddProfile(profile_path
);
139 void FakeShillProfileClient::AddEntry(const std::string
& profile_path
,
140 const std::string
& entry_path
,
141 const base::DictionaryValue
& properties
) {
142 ProfileProperties
* profile
= GetProfile(dbus::ObjectPath(profile_path
),
145 profile
->entries
.SetWithoutPathExpansion(entry_path
, properties
.DeepCopy());
146 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
147 AddManagerService(entry_path
, true);
150 bool FakeShillProfileClient::AddService(const std::string
& profile_path
,
151 const std::string
& service_path
) {
152 ProfileProperties
* profile
= GetProfile(dbus::ObjectPath(profile_path
),
155 LOG(ERROR
) << "AddService: No matching profile: " << profile_path
156 << " for: " << service_path
;
159 if (profile
->entries
.HasKey(service_path
))
161 return AddOrUpdateServiceImpl(profile_path
, service_path
, profile
);
164 bool FakeShillProfileClient::UpdateService(const std::string
& profile_path
,
165 const std::string
& service_path
) {
166 ProfileProperties
* profile
= GetProfile(dbus::ObjectPath(profile_path
),
169 LOG(ERROR
) << "UpdateService: No matching profile: " << profile_path
170 << " for: " << service_path
;
173 if (!profile
->entries
.HasKey(service_path
)) {
174 LOG(ERROR
) << "UpdateService: Profile: " << profile_path
175 << " does not contain Service: " << service_path
;
178 return AddOrUpdateServiceImpl(profile_path
, service_path
, profile
);
181 bool FakeShillProfileClient::AddOrUpdateServiceImpl(
182 const std::string
& profile_path
,
183 const std::string
& service_path
,
184 ProfileProperties
* profile
) {
185 ShillServiceClient::TestInterface
* service_test
=
186 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
187 const base::DictionaryValue
* service_properties
=
188 service_test
->GetServiceProperties(service_path
);
189 if (!service_properties
) {
190 LOG(ERROR
) << "No matching service: " << service_path
;
193 std::string service_profile_path
;
194 service_properties
->GetStringWithoutPathExpansion(shill::kProfileProperty
,
195 &service_profile_path
);
196 if (service_profile_path
.empty()) {
197 base::StringValue
profile_path_value(profile_path
);
198 service_test
->SetServiceProperty(service_path
,
199 shill::kProfileProperty
,
201 } else if (service_profile_path
!= profile_path
) {
202 LOG(ERROR
) << "Service has non matching profile path: "
203 << service_profile_path
;
207 profile
->entries
.SetWithoutPathExpansion(service_path
,
208 service_properties
->DeepCopy());
212 void FakeShillProfileClient::GetProfilePaths(
213 std::vector
<std::string
>* profiles
) {
214 for (ProfileMap::iterator iter
= profiles_
.begin();
215 iter
!= profiles_
.end(); ++iter
) {
216 profiles
->push_back(iter
->first
);
220 bool FakeShillProfileClient::GetService(const std::string
& service_path
,
221 std::string
* profile_path
,
222 base::DictionaryValue
* properties
) {
224 for (ProfileMap::const_iterator iter
= profiles_
.begin();
225 iter
!= profiles_
.end(); ++iter
) {
226 const ProfileProperties
* profile
= iter
->second
;
227 const base::DictionaryValue
* entry
;
228 if (!profile
->entries
.GetDictionaryWithoutPathExpansion(
229 service_path
, &entry
)) {
232 *profile_path
= iter
->first
;
233 properties
->MergeDictionary(entry
);
239 void FakeShillProfileClient::ClearProfiles() {
240 STLDeleteValues(&profiles_
);
243 FakeShillProfileClient::ProfileProperties
* FakeShillProfileClient::GetProfile(
244 const dbus::ObjectPath
& profile_path
,
245 const ErrorCallback
& error_callback
) {
246 ProfileMap::const_iterator found
= profiles_
.find(profile_path
.value());
247 if (found
== profiles_
.end()) {
248 if (!error_callback
.is_null())
249 error_callback
.Run("Error.InvalidProfile", "Invalid profile");
253 return found
->second
;
256 } // namespace chromeos