Update V8 to version 4.6.54.
[chromium-blink-merge.git] / chromeos / dbus / fake_shill_profile_client.cc
blob69f52c08d0238d91403e8109799657fe31fb042d
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"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/stl_util.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/values.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/shill_property_changed_observer.h"
16 #include "chromeos/dbus/shill_service_client.h"
17 #include "dbus/bus.h"
18 #include "dbus/message.h"
19 #include "dbus/object_path.h"
20 #include "dbus/values_util.h"
21 #include "third_party/cros_system_api/dbus/service_constants.h"
23 namespace chromeos {
25 struct FakeShillProfileClient::ProfileProperties {
26 base::DictionaryValue entries; // Dictionary of Service Dictionaries
27 base::DictionaryValue properties; // Dictionary of Profile properties
30 namespace {
32 void PassDictionary(
33 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback,
34 const base::DictionaryValue* dictionary) {
35 callback.Run(*dictionary);
38 } // namespace
40 FakeShillProfileClient::FakeShillProfileClient() {
43 FakeShillProfileClient::~FakeShillProfileClient() {
44 STLDeleteValues(&profiles_);
47 void FakeShillProfileClient::Init(dbus::Bus* bus) {
50 void FakeShillProfileClient::AddPropertyChangedObserver(
51 const dbus::ObjectPath& profile_path,
52 ShillPropertyChangedObserver* observer) {
55 void FakeShillProfileClient::RemovePropertyChangedObserver(
56 const dbus::ObjectPath& profile_path,
57 ShillPropertyChangedObserver* observer) {
60 void FakeShillProfileClient::GetProperties(
61 const dbus::ObjectPath& profile_path,
62 const DictionaryValueCallbackWithoutStatus& callback,
63 const ErrorCallback& error_callback) {
64 ProfileProperties* profile = GetProfile(profile_path, error_callback);
65 if (!profile)
66 return;
68 scoped_ptr<base::DictionaryValue> properties(profile->properties.DeepCopy());
69 base::ListValue* entry_paths = new base::ListValue;
70 properties->SetWithoutPathExpansion(shill::kEntriesProperty, entry_paths);
71 for (base::DictionaryValue::Iterator it(profile->entries); !it.IsAtEnd();
72 it.Advance()) {
73 entry_paths->AppendString(it.key());
76 base::ThreadTaskRunnerHandle::Get()->PostTask(
77 FROM_HERE,
78 base::Bind(&PassDictionary, callback, base::Owned(properties.release())));
81 void FakeShillProfileClient::GetEntry(
82 const dbus::ObjectPath& profile_path,
83 const std::string& entry_path,
84 const DictionaryValueCallbackWithoutStatus& callback,
85 const ErrorCallback& error_callback) {
86 ProfileProperties* profile = GetProfile(profile_path, error_callback);
87 if (!profile)
88 return;
90 base::DictionaryValue* entry = NULL;
91 profile->entries.GetDictionaryWithoutPathExpansion(entry_path, &entry);
92 if (!entry) {
93 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry");
94 return;
97 base::ThreadTaskRunnerHandle::Get()->PostTask(
98 FROM_HERE,
99 base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy())));
102 void FakeShillProfileClient::DeleteEntry(const dbus::ObjectPath& profile_path,
103 const std::string& entry_path,
104 const base::Closure& callback,
105 const ErrorCallback& error_callback) {
106 ProfileProperties* profile = GetProfile(profile_path, error_callback);
107 if (!profile)
108 return;
110 if (!profile->entries.RemoveWithoutPathExpansion(entry_path, NULL)) {
111 error_callback.Run("Error.InvalidProfileEntry", entry_path);
112 return;
115 base::StringValue profile_path_value("");
116 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()->
117 SetServiceProperty(entry_path,
118 shill::kProfileProperty,
119 profile_path_value);
121 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
124 ShillProfileClient::TestInterface* FakeShillProfileClient::GetTestInterface() {
125 return this;
128 void FakeShillProfileClient::AddProfile(const std::string& profile_path,
129 const std::string& userhash) {
130 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback()))
131 return;
133 ProfileProperties* profile = new ProfileProperties;
134 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty,
135 userhash);
136 profiles_[profile_path] = profile;
137 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
138 AddProfile(profile_path);
141 void FakeShillProfileClient::AddEntry(const std::string& profile_path,
142 const std::string& entry_path,
143 const base::DictionaryValue& properties) {
144 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path),
145 ErrorCallback());
146 DCHECK(profile);
147 profile->entries.SetWithoutPathExpansion(entry_path, properties.DeepCopy());
148 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
149 AddManagerService(entry_path, true);
152 bool FakeShillProfileClient::AddService(const std::string& profile_path,
153 const std::string& service_path) {
154 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path),
155 ErrorCallback());
156 if (!profile) {
157 LOG(ERROR) << "AddService: No matching profile: " << profile_path
158 << " for: " << service_path;
159 return false;
161 if (profile->entries.HasKey(service_path))
162 return false;
163 return AddOrUpdateServiceImpl(profile_path, service_path, profile);
166 bool FakeShillProfileClient::UpdateService(const std::string& profile_path,
167 const std::string& service_path) {
168 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path),
169 ErrorCallback());
170 if (!profile) {
171 LOG(ERROR) << "UpdateService: No matching profile: " << profile_path
172 << " for: " << service_path;
173 return false;
175 if (!profile->entries.HasKey(service_path)) {
176 LOG(ERROR) << "UpdateService: Profile: " << profile_path
177 << " does not contain Service: " << service_path;
178 return false;
180 return AddOrUpdateServiceImpl(profile_path, service_path, profile);
183 bool FakeShillProfileClient::AddOrUpdateServiceImpl(
184 const std::string& profile_path,
185 const std::string& service_path,
186 ProfileProperties* profile) {
187 ShillServiceClient::TestInterface* service_test =
188 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
189 const base::DictionaryValue* service_properties =
190 service_test->GetServiceProperties(service_path);
191 if (!service_properties) {
192 LOG(ERROR) << "No matching service: " << service_path;
193 return false;
195 std::string service_profile_path;
196 service_properties->GetStringWithoutPathExpansion(shill::kProfileProperty,
197 &service_profile_path);
198 if (service_profile_path.empty()) {
199 base::StringValue profile_path_value(profile_path);
200 service_test->SetServiceProperty(service_path,
201 shill::kProfileProperty,
202 profile_path_value);
203 } else if (service_profile_path != profile_path) {
204 LOG(ERROR) << "Service has non matching profile path: "
205 << service_profile_path;
206 return false;
209 profile->entries.SetWithoutPathExpansion(service_path,
210 service_properties->DeepCopy());
211 return true;
214 void FakeShillProfileClient::GetProfilePaths(
215 std::vector<std::string>* profiles) {
216 for (ProfileMap::iterator iter = profiles_.begin();
217 iter != profiles_.end(); ++iter) {
218 profiles->push_back(iter->first);
222 bool FakeShillProfileClient::GetService(const std::string& service_path,
223 std::string* profile_path,
224 base::DictionaryValue* properties) {
225 properties->Clear();
226 for (ProfileMap::const_iterator iter = profiles_.begin();
227 iter != profiles_.end(); ++iter) {
228 const ProfileProperties* profile = iter->second;
229 const base::DictionaryValue* entry;
230 if (!profile->entries.GetDictionaryWithoutPathExpansion(
231 service_path, &entry)) {
232 continue;
234 *profile_path = iter->first;
235 properties->MergeDictionary(entry);
236 return true;
238 return false;
241 void FakeShillProfileClient::ClearProfiles() {
242 STLDeleteValues(&profiles_);
245 FakeShillProfileClient::ProfileProperties* FakeShillProfileClient::GetProfile(
246 const dbus::ObjectPath& profile_path,
247 const ErrorCallback& error_callback) {
248 ProfileMap::const_iterator found = profiles_.find(profile_path.value());
249 if (found == profiles_.end()) {
250 if (!error_callback.is_null())
251 error_callback.Run("Error.InvalidProfile", "Invalid profile");
252 return NULL;
255 return found->second;
258 } // namespace chromeos