Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(713)

Side by Side Diff: chromeos/dbus/fake_shill_profile_client.cc

Issue 2754903002: Prevent networkingPrivate.forgetNetwork from removing shared configs (Closed)
Patch Set: . Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/dbus/fake_shill_profile_client.h" 5 #include "chromeos/dbus/fake_shill_profile_client.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chromeos/dbus/dbus_thread_manager.h" 14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/shill_property_changed_observer.h" 15 #include "chromeos/dbus/shill_property_changed_observer.h"
16 #include "chromeos/dbus/shill_service_client.h" 16 #include "chromeos/dbus/shill_service_client.h"
17 #include "dbus/bus.h" 17 #include "dbus/bus.h"
18 #include "dbus/message.h" 18 #include "dbus/message.h"
19 #include "dbus/object_path.h" 19 #include "dbus/object_path.h"
20 #include "dbus/values_util.h" 20 #include "dbus/values_util.h"
21 #include "third_party/cros_system_api/dbus/service_constants.h" 21 #include "third_party/cros_system_api/dbus/service_constants.h"
22 22
23 namespace chromeos { 23 namespace chromeos {
24 24
25 struct FakeShillProfileClient::ProfileProperties { 25 struct FakeShillProfileClient::ProfileProperties {
26 std::string path; // Profile path
26 base::DictionaryValue entries; // Dictionary of Service Dictionaries 27 base::DictionaryValue entries; // Dictionary of Service Dictionaries
27 base::DictionaryValue properties; // Dictionary of Profile properties 28 base::DictionaryValue properties; // Dictionary of Profile properties
28 }; 29 };
29 30
30 namespace { 31 namespace {
31 32
32 void PassDictionary( 33 void PassDictionary(
33 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback, 34 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback,
34 const base::DictionaryValue* dictionary) { 35 const base::DictionaryValue* dictionary) {
35 callback.Run(*dictionary); 36 callback.Run(*dictionary);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 121
121 ShillProfileClient::TestInterface* FakeShillProfileClient::GetTestInterface() { 122 ShillProfileClient::TestInterface* FakeShillProfileClient::GetTestInterface() {
122 return this; 123 return this;
123 } 124 }
124 125
125 void FakeShillProfileClient::AddProfile(const std::string& profile_path, 126 void FakeShillProfileClient::AddProfile(const std::string& profile_path,
126 const std::string& userhash) { 127 const std::string& userhash) {
127 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback())) 128 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback()))
128 return; 129 return;
129 130
130 std::unique_ptr<ProfileProperties> profile = 131 // If adding a shared profile, make sure there are no user profiles currently
131 base::MakeUnique<ProfileProperties>(); 132 // on the stack - note that it's enough to check the top of the stack only.
133 if (profile_path == GetSharedProfilePath()) {
134 if (!(profiles_.empty() ||
135 profiles_.back()->path == GetSharedProfilePath())) {
136 NOTREACHED() << "Adding shared profile when a user profile exists.";
137 return;
138 }
139 }
stevenjb 2017/03/30 17:33:13 nit: We shouldn't need to support multiple shared
tbarzic 2017/03/30 18:00:05 Done.
140
141 auto profile = base::MakeUnique<ProfileProperties>();
132 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty, 142 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty,
133 userhash); 143 userhash);
134 profiles_[profile_path] = std::move(profile); 144 profile->path = profile_path;
145 profiles_.emplace_back(std::move(profile));
146
135 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> 147 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
136 AddProfile(profile_path); 148 AddProfile(profile_path);
137 } 149 }
138 150
139 void FakeShillProfileClient::AddEntry(const std::string& profile_path, 151 void FakeShillProfileClient::AddEntry(const std::string& profile_path,
140 const std::string& entry_path, 152 const std::string& entry_path,
141 const base::DictionaryValue& properties) { 153 const base::DictionaryValue& properties) {
142 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path), 154 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path),
143 ErrorCallback()); 155 ErrorCallback());
144 DCHECK(profile); 156 DCHECK(profile);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 return false; 216 return false;
205 } 217 }
206 218
207 profile->entries.SetWithoutPathExpansion(service_path, 219 profile->entries.SetWithoutPathExpansion(service_path,
208 service_properties->DeepCopy()); 220 service_properties->DeepCopy());
209 return true; 221 return true;
210 } 222 }
211 223
212 void FakeShillProfileClient::GetProfilePaths( 224 void FakeShillProfileClient::GetProfilePaths(
213 std::vector<std::string>* profiles) { 225 std::vector<std::string>* profiles) {
214 for (auto iter = profiles_.begin(); iter != profiles_.end(); ++iter) 226 for (const auto& profile : profiles_)
215 profiles->push_back(iter->first); 227 profiles->push_back(profile->path);
228 }
229
230 void FakeShillProfileClient::GetProfilePathsContainingService(
231 const std::string& service_path,
232 std::vector<std::string>* profiles) {
233 for (const auto& profile : profiles_) {
234 if (GetServiceDataFromProfile(profile.get(), service_path, nullptr))
235 profiles->push_back(profile->path);
236 }
216 } 237 }
217 238
218 bool FakeShillProfileClient::GetService(const std::string& service_path, 239 bool FakeShillProfileClient::GetService(const std::string& service_path,
219 std::string* profile_path, 240 std::string* profile_path,
220 base::DictionaryValue* properties) { 241 base::DictionaryValue* properties) {
221 properties->Clear(); 242 properties->Clear();
222 for (auto iter = profiles_.begin(); iter != profiles_.end(); ++iter) { 243
223 const ProfileProperties* profile = iter->second.get(); 244 bool found_profile = false;
224 const base::DictionaryValue* entry; 245 for (const auto& profile : profiles_) {
225 if (!profile->entries.GetDictionaryWithoutPathExpansion( 246 if (GetServiceDataFromProfile(profile.get(), service_path, properties)) {
226 service_path, &entry)) { 247 found_profile = true;
227 continue; 248 *profile_path = profile->path;
228 } 249 }
229 *profile_path = iter->first;
230 properties->MergeDictionary(entry);
231 return true;
232 } 250 }
233 return false; 251 return found_profile;
234 } 252 }
235 253
236 void FakeShillProfileClient::ClearProfiles() { 254 void FakeShillProfileClient::ClearProfiles() {
237 profiles_.clear(); 255 profiles_.clear();
238 } 256 }
239 257
240 FakeShillProfileClient::ProfileProperties* FakeShillProfileClient::GetProfile( 258 FakeShillProfileClient::ProfileProperties* FakeShillProfileClient::GetProfile(
241 const dbus::ObjectPath& profile_path, 259 const dbus::ObjectPath& profile_path,
242 const ErrorCallback& error_callback) { 260 const ErrorCallback& error_callback) {
243 auto found = profiles_.find(profile_path.value()); 261 for (auto& profile : profiles_) {
244 if (found == profiles_.end()) { 262 if (profile->path == profile_path.value())
245 if (!error_callback.is_null()) 263 return profile.get();
246 error_callback.Run("Error.InvalidProfile", "Invalid profile"); 264 }
247 return nullptr; 265 if (!error_callback.is_null())
266 error_callback.Run("Error.InvalidProfile", "Invalid profile");
267 return nullptr;
268 }
269
270 bool FakeShillProfileClient::GetServiceDataFromProfile(
271 const FakeShillProfileClient::ProfileProperties* profile,
272 const std::string& service_path,
273 base::DictionaryValue* properties) {
274 const base::DictionaryValue* entry;
275 if (!profile->entries.GetDictionaryWithoutPathExpansion(service_path,
276 &entry)) {
277 return false;
248 } 278 }
249 279
250 return found->second.get(); 280 if (properties)
281 properties->MergeDictionary(entry);
282
283 return true;
251 } 284 }
252 285
253 } // namespace chromeos 286 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698