Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / chromeos / dbus / fake_shill_profile_client.cc
blobfb0d2fc76acb0a1a650d3f5224d1a75deb7de8ab
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/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"
15 #include "dbus/bus.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"
21 namespace chromeos {
23 struct FakeShillProfileClient::ProfileProperties {
24 base::DictionaryValue entries; // Dictionary of Service Dictionaries
25 base::DictionaryValue properties; // Dictionary of Profile properties
28 namespace {
30 void PassDictionary(
31 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback,
32 const base::DictionaryValue* dictionary) {
33 callback.Run(*dictionary);
36 } // namespace
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);
63 if (!profile)
64 return;
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();
70 it.Advance()) {
71 entry_paths->AppendString(it.key());
74 base::MessageLoop::current()->PostTask(
75 FROM_HERE,
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);
85 if (!profile)
86 return;
88 base::DictionaryValue* entry = NULL;
89 profile->entries.GetDictionaryWithoutPathExpansion(entry_path, &entry);
90 if (!entry) {
91 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry");
92 return;
95 base::MessageLoop::current()->PostTask(
96 FROM_HERE,
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);
105 if (!profile)
106 return;
108 if (!profile->entries.RemoveWithoutPathExpansion(entry_path, NULL)) {
109 error_callback.Run("Error.InvalidProfileEntry", entry_path);
110 return;
113 base::StringValue profile_path_value("");
114 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()->
115 SetServiceProperty(entry_path,
116 shill::kProfileProperty,
117 profile_path_value);
119 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
122 ShillProfileClient::TestInterface* FakeShillProfileClient::GetTestInterface() {
123 return this;
126 void FakeShillProfileClient::AddProfile(const std::string& profile_path,
127 const std::string& userhash) {
128 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback()))
129 return;
131 ProfileProperties* profile = new ProfileProperties;
132 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty,
133 userhash);
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),
143 ErrorCallback());
144 DCHECK(profile);
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),
153 ErrorCallback());
154 if (!profile) {
155 LOG(ERROR) << "AddService: No matching profile: " << profile_path
156 << " for: " << service_path;
157 return false;
159 if (profile->entries.HasKey(service_path))
160 return false;
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),
167 ErrorCallback());
168 if (!profile) {
169 LOG(ERROR) << "UpdateService: No matching profile: " << profile_path
170 << " for: " << service_path;
171 return false;
173 if (!profile->entries.HasKey(service_path)) {
174 LOG(ERROR) << "UpdateService: Profile: " << profile_path
175 << " does not contain Service: " << service_path;
176 return false;
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;
191 return false;
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,
200 profile_path_value);
201 } else if (service_profile_path != profile_path) {
202 LOG(ERROR) << "Service has non matching profile path: "
203 << service_profile_path;
204 return false;
207 profile->entries.SetWithoutPathExpansion(service_path,
208 service_properties->DeepCopy());
209 return true;
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) {
223 properties->Clear();
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)) {
230 continue;
232 *profile_path = iter->first;
233 properties->MergeDictionary(entry);
234 return true;
236 return false;
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");
250 return NULL;
253 return found->second;
256 } // namespace chromeos