Only fsync leveldb's directory when the manifest is being updated.
[chromium-blink-merge.git] / chromeos / dbus / shill_profile_client_stub.cc
blobe023237a11580165275e46f6598ce43749c51e1b
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"
7 #include "base/bind.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"
16 #include "dbus/bus.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"
22 namespace chromeos {
24 struct ShillProfileClientStub::ProfileProperties {
25 base::DictionaryValue entries;
26 base::DictionaryValue properties;
29 namespace {
31 const char kSharedProfilePath[] = "/profile/default";
33 void PassDictionary(
34 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback,
35 const base::DictionaryValue* dictionary) {
36 if (callback.is_null())
37 return;
38 callback.Run(*dictionary);
41 } // namespace
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);
66 if (!profile)
67 return;
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();
73 it.Advance()) {
74 entry_paths->AppendString(it.key());
77 base::MessageLoop::current()->PostTask(
78 FROM_HERE,
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);
88 if (!profile)
89 return;
91 base::DictionaryValue* entry = NULL;
92 profile->entries.GetDictionaryWithoutPathExpansion(entry_path, &entry);
93 if (!entry) {
94 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry");
95 return;
98 base::MessageLoop::current()->PostTask(
99 FROM_HERE,
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);
108 if (!profile)
109 return;
111 if (!profile->entries.RemoveWithoutPathExpansion(entry_path, NULL)) {
112 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry");
113 return;
116 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
119 ShillProfileClient::TestInterface* ShillProfileClientStub::GetTestInterface() {
120 return this;
123 void ShillProfileClientStub::AddProfile(const std::string& profile_path,
124 const std::string& userhash) {
125 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback()))
126 return;
128 ProfileProperties* profile = new ProfileProperties;
129 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty,
130 userhash);
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),
140 ErrorCallback());
141 DCHECK(profile);
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;
152 if (!properties)
153 return false;
155 properties->GetStringWithoutPathExpansion(flimflam::kProfileProperty,
156 &profile_path);
157 if (profile_path.empty())
158 return false;
160 AddEntry(profile_path, service_path, *properties);
161 return true;
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");
171 return NULL;
174 return found->second;
177 } // namespace chromeos