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

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

Issue 13957012: Adding a NetworkProfileHandler used by ManagedNetworkConfigurationHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 7 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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/shill_profile_client_stub.h" 5 #include "chromeos/dbus/shill_profile_client_stub.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/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chromeos/dbus/dbus_thread_manager.h" 12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/shill_manager_client.h"
13 #include "chromeos/dbus/shill_property_changed_observer.h" 14 #include "chromeos/dbus/shill_property_changed_observer.h"
14 #include "chromeos/dbus/shill_service_client.h" 15 #include "chromeos/dbus/shill_service_client.h"
15 #include "dbus/bus.h" 16 #include "dbus/bus.h"
16 #include "dbus/message.h" 17 #include "dbus/message.h"
17 #include "dbus/object_path.h" 18 #include "dbus/object_path.h"
18 #include "dbus/values_util.h" 19 #include "dbus/values_util.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h" 20 #include "third_party/cros_system_api/dbus/service_constants.h"
20 21
21 namespace chromeos { 22 namespace chromeos {
22 23
24 struct ShillProfileClientStub::ProfileProperties {
25 base::DictionaryValue entries;
26 base::DictionaryValue properties;
27 };
28
23 namespace { 29 namespace {
24 30
25 const char kSharedProfilePath[] = "/profile/default"; 31 const char kSharedProfilePath[] = "/profile/default";
26 32
27 void PassEmptyDictionary( 33 void PassEmptyDictionary(
28 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback) { 34 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback) {
29 base::DictionaryValue dictionary; 35 base::DictionaryValue dictionary;
30 if (callback.is_null()) 36 if (callback.is_null())
31 return; 37 return;
32 callback.Run(dictionary); 38 callback.Run(dictionary);
33 } 39 }
34 40
35 void PassDictionary( 41 void PassDictionary(
36 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback, 42 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback,
37 const base::DictionaryValue* dictionary) { 43 const base::DictionaryValue* dictionary) {
38 if (callback.is_null()) 44 if (callback.is_null())
39 return; 45 return;
40 callback.Run(*dictionary); 46 callback.Run(*dictionary);
41 } 47 }
42 48
43 base::DictionaryValue* GetOrCreateDictionary(const std::string& key,
44 base::DictionaryValue* dict) {
45 base::DictionaryValue* nested_dict = NULL;
46 dict->GetDictionaryWithoutPathExpansion(key, &nested_dict);
47 if (!nested_dict) {
48 nested_dict = new base::DictionaryValue;
49 dict->SetWithoutPathExpansion(key, nested_dict);
50 }
51 return nested_dict;
52 }
53
54 } // namespace 49 } // namespace
55 50
56 ShillProfileClientStub::ShillProfileClientStub() { 51 ShillProfileClientStub::ShillProfileClientStub(
57 AddProfile(kSharedProfilePath); 52 ShillManagerClient::TestInterface* manager_test)
53 : manager_test_(manager_test) {
54 AddProfile(kSharedProfilePath, std::string());
58 } 55 }
59 56
60 ShillProfileClientStub::~ShillProfileClientStub() { 57 ShillProfileClientStub::~ShillProfileClientStub() {
58 STLDeleteValues(&profiles_);
61 } 59 }
62 60
63 void ShillProfileClientStub::AddPropertyChangedObserver( 61 void ShillProfileClientStub::AddPropertyChangedObserver(
64 const dbus::ObjectPath& profile_path, 62 const dbus::ObjectPath& profile_path,
65 ShillPropertyChangedObserver* observer) { 63 ShillPropertyChangedObserver* observer) {
66 } 64 }
67 65
68 void ShillProfileClientStub::RemovePropertyChangedObserver( 66 void ShillProfileClientStub::RemovePropertyChangedObserver(
69 const dbus::ObjectPath& profile_path, 67 const dbus::ObjectPath& profile_path,
70 ShillPropertyChangedObserver* observer) { 68 ShillPropertyChangedObserver* observer) {
71 } 69 }
72 70
73 void ShillProfileClientStub::GetProperties( 71 void ShillProfileClientStub::GetProperties(
74 const dbus::ObjectPath& profile_path, 72 const dbus::ObjectPath& profile_path,
75 const DictionaryValueCallbackWithoutStatus& callback, 73 const DictionaryValueCallbackWithoutStatus& callback,
76 const ErrorCallback& error_callback) { 74 const ErrorCallback& error_callback) {
77 base::DictionaryValue* profile = GetProfile(profile_path, error_callback); 75 ProfileProperties* profile = GetProfile(profile_path, error_callback);
78 if (!profile) 76 if (!profile)
79 return; 77 return;
80 78
81 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue); 79 scoped_ptr<base::DictionaryValue> properties(profile->properties.DeepCopy());
82 base::ListValue* entry_paths = new base::ListValue; 80 base::ListValue* entry_paths = new base::ListValue;
83 properties->SetWithoutPathExpansion(flimflam::kEntriesProperty, entry_paths); 81 properties->SetWithoutPathExpansion(flimflam::kEntriesProperty, entry_paths);
84 for (base::DictionaryValue::Iterator it(*profile); !it.IsAtEnd(); 82 for (base::DictionaryValue::Iterator it(profile->entries); !it.IsAtEnd();
85 it.Advance()) { 83 it.Advance()) {
86 entry_paths->AppendString(it.key()); 84 entry_paths->AppendString(it.key());
87 } 85 }
88 86
89 MessageLoop::current()->PostTask( 87 MessageLoop::current()->PostTask(
90 FROM_HERE, 88 FROM_HERE,
91 base::Bind(&PassDictionary, callback, base::Owned(properties.release()))); 89 base::Bind(&PassDictionary, callback, base::Owned(properties.release())));
92 } 90 }
93 91
94 void ShillProfileClientStub::GetEntry( 92 void ShillProfileClientStub::GetEntry(
95 const dbus::ObjectPath& profile_path, 93 const dbus::ObjectPath& profile_path,
96 const std::string& entry_path, 94 const std::string& entry_path,
97 const DictionaryValueCallbackWithoutStatus& callback, 95 const DictionaryValueCallbackWithoutStatus& callback,
98 const ErrorCallback& error_callback) { 96 const ErrorCallback& error_callback) {
99 base::DictionaryValue* profile = GetProfile(profile_path, error_callback); 97 ProfileProperties* profile = GetProfile(profile_path, error_callback);
100 if (!profile) 98 if (!profile)
101 return; 99 return;
102 100
103 base::DictionaryValue* entry = NULL; 101 base::DictionaryValue* entry = NULL;
104 profile->GetDictionaryWithoutPathExpansion(entry_path, &entry); 102 profile->entries.GetDictionaryWithoutPathExpansion(entry_path, &entry);
105 if (!entry) { 103 if (!entry) {
106 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); 104 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry");
107 return; 105 return;
108 } 106 }
109 107
110 MessageLoop::current()->PostTask( 108 MessageLoop::current()->PostTask(
111 FROM_HERE, 109 FROM_HERE,
112 base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy()))); 110 base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy())));
113 } 111 }
114 112
115 void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath& profile_path, 113 void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath& profile_path,
116 const std::string& entry_path, 114 const std::string& entry_path,
117 const base::Closure& callback, 115 const base::Closure& callback,
118 const ErrorCallback& error_callback) { 116 const ErrorCallback& error_callback) {
119 base::DictionaryValue* profile = GetProfile(profile_path, error_callback); 117 ProfileProperties* profile = GetProfile(profile_path, error_callback);
120 if (!profile) 118 if (!profile)
121 return; 119 return;
122 120
123 if (!profile->RemoveWithoutPathExpansion(entry_path, NULL)) { 121 if (!profile->entries.RemoveWithoutPathExpansion(entry_path, NULL)) {
124 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); 122 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry");
125 return; 123 return;
126 } 124 }
127 125
128 MessageLoop::current()->PostTask(FROM_HERE, callback); 126 MessageLoop::current()->PostTask(FROM_HERE, callback);
129 } 127 }
130 128
131 ShillProfileClient::TestInterface* ShillProfileClientStub::GetTestInterface() { 129 ShillProfileClient::TestInterface* ShillProfileClientStub::GetTestInterface() {
132 return this; 130 return this;
133 } 131 }
134 132
135 void ShillProfileClientStub::AddProfile(const std::string& profile_path) { 133 void ShillProfileClientStub::AddProfile(const std::string& profile_path,
136 profile_entries_.SetWithoutPathExpansion(profile_path, 134 const std::string& userhash) {
137 new base::DictionaryValue); 135 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback()))
136 return;
137
138 ProfileProperties* profile = new ProfileProperties;
139 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty,
140 userhash);
141 profiles_[profile_path] = profile;
142 if (manager_test_)
143 manager_test_->AddProfile(profile_path);
138 } 144 }
139 145
140 void ShillProfileClientStub::AddEntry(const std::string& profile_path, 146 void ShillProfileClientStub::AddEntry(const std::string& profile_path,
141 const std::string& entry_path, 147 const std::string& entry_path,
142 const base::DictionaryValue& properties) { 148 const base::DictionaryValue& properties) {
143 base::DictionaryValue* profile = GetOrCreateDictionary(profile_path, 149 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path),
144 &profile_entries_); 150 ErrorCallback());
145 profile->SetWithoutPathExpansion(entry_path, 151 DCHECK(profile);
146 properties.DeepCopy()); 152 profile->entries.SetWithoutPathExpansion(entry_path,
153 properties.DeepCopy());
147 } 154 }
148 155
149 bool ShillProfileClientStub::AddService(const std::string& service_path) { 156 bool ShillProfileClientStub::AddService(const std::string& service_path) {
150 ShillServiceClient::TestInterface* service_test = 157 ShillServiceClient::TestInterface* service_test =
151 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 158 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
152 const base::DictionaryValue* properties = 159 const base::DictionaryValue* properties =
153 service_test->GetServiceProperties(service_path); 160 service_test->GetServiceProperties(service_path);
154 std::string profile_path; 161 std::string profile_path;
155 if (!properties) 162 if (!properties)
156 return false; 163 return false;
157 164
158 properties->GetStringWithoutPathExpansion(flimflam::kProfileProperty, 165 properties->GetStringWithoutPathExpansion(flimflam::kProfileProperty,
159 &profile_path); 166 &profile_path);
160 if (profile_path.empty()) 167 if (profile_path.empty())
161 return false; 168 return false;
162 169
163 AddEntry(profile_path, service_path, *properties); 170 AddEntry(profile_path, service_path, *properties);
164 return true; 171 return true;
165 } 172 }
166 173
167 base::DictionaryValue* ShillProfileClientStub::GetProfile( 174 ShillProfileClientStub::ProfileProperties* ShillProfileClientStub::GetProfile(
168 const dbus::ObjectPath& profile_path, 175 const dbus::ObjectPath& profile_path,
169 const ErrorCallback& error_callback) { 176 const ErrorCallback& error_callback) {
170 base::DictionaryValue* profile = NULL; 177 ProfileMap::const_iterator found = profiles_.find(profile_path.value());
171 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(), 178 if (found == profiles_.end()) {
172 &profile); 179 if (!error_callback.is_null())
173 if (!profile) 180 error_callback.Run("Error.InvalidProfile", "Invalid profile");
174 error_callback.Run("Error.InvalidProfile", "Invalid profile"); 181 return NULL;
175 return profile; 182 }
183
184 return found->second;
176 } 185 }
177 186
178 } // namespace chromeos 187 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698