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

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: Add preliminary userhash retrieval. 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(
Joao da Silva 2013/05/07 14:31:36 This isn't used.
pneubeck (no reviews) 2013/05/07 15:03:19 Done.
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 AddProfile(kSharedProfilePath, std::string());
58 } 53 }
59 54
60 ShillProfileClientStub::~ShillProfileClientStub() { 55 ShillProfileClientStub::~ShillProfileClientStub() {
56 STLDeleteValues(&profiles_);
61 } 57 }
62 58
63 void ShillProfileClientStub::AddPropertyChangedObserver( 59 void ShillProfileClientStub::AddPropertyChangedObserver(
64 const dbus::ObjectPath& profile_path, 60 const dbus::ObjectPath& profile_path,
65 ShillPropertyChangedObserver* observer) { 61 ShillPropertyChangedObserver* observer) {
66 } 62 }
67 63
68 void ShillProfileClientStub::RemovePropertyChangedObserver( 64 void ShillProfileClientStub::RemovePropertyChangedObserver(
69 const dbus::ObjectPath& profile_path, 65 const dbus::ObjectPath& profile_path,
70 ShillPropertyChangedObserver* observer) { 66 ShillPropertyChangedObserver* observer) {
71 } 67 }
72 68
73 void ShillProfileClientStub::GetProperties( 69 void ShillProfileClientStub::GetProperties(
74 const dbus::ObjectPath& profile_path, 70 const dbus::ObjectPath& profile_path,
75 const DictionaryValueCallbackWithoutStatus& callback, 71 const DictionaryValueCallbackWithoutStatus& callback,
76 const ErrorCallback& error_callback) { 72 const ErrorCallback& error_callback) {
77 base::DictionaryValue* profile = GetProfile(profile_path, error_callback); 73 ProfileProperties* profile = GetProfile(profile_path, error_callback);
78 if (!profile) 74 if (!profile)
79 return; 75 return;
80 76
81 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue); 77 scoped_ptr<base::DictionaryValue> properties(profile->properties.DeepCopy());
82 base::ListValue* entry_paths = new base::ListValue; 78 base::ListValue* entry_paths = new base::ListValue;
83 properties->SetWithoutPathExpansion(flimflam::kEntriesProperty, entry_paths); 79 properties->SetWithoutPathExpansion(flimflam::kEntriesProperty, entry_paths);
84 for (base::DictionaryValue::Iterator it(*profile); !it.IsAtEnd(); 80 for (base::DictionaryValue::Iterator it(profile->entries); !it.IsAtEnd();
85 it.Advance()) { 81 it.Advance()) {
86 entry_paths->AppendString(it.key()); 82 entry_paths->AppendString(it.key());
87 } 83 }
88 84
89 MessageLoop::current()->PostTask( 85 MessageLoop::current()->PostTask(
90 FROM_HERE, 86 FROM_HERE,
91 base::Bind(&PassDictionary, callback, base::Owned(properties.release()))); 87 base::Bind(&PassDictionary, callback, base::Owned(properties.release())));
92 } 88 }
93 89
94 void ShillProfileClientStub::GetEntry( 90 void ShillProfileClientStub::GetEntry(
95 const dbus::ObjectPath& profile_path, 91 const dbus::ObjectPath& profile_path,
96 const std::string& entry_path, 92 const std::string& entry_path,
97 const DictionaryValueCallbackWithoutStatus& callback, 93 const DictionaryValueCallbackWithoutStatus& callback,
98 const ErrorCallback& error_callback) { 94 const ErrorCallback& error_callback) {
99 base::DictionaryValue* profile = GetProfile(profile_path, error_callback); 95 ProfileProperties* profile = GetProfile(profile_path, error_callback);
100 if (!profile) 96 if (!profile)
101 return; 97 return;
102 98
103 base::DictionaryValue* entry = NULL; 99 base::DictionaryValue* entry = NULL;
104 profile->GetDictionaryWithoutPathExpansion(entry_path, &entry); 100 profile->entries.GetDictionaryWithoutPathExpansion(entry_path, &entry);
105 if (!entry) { 101 if (!entry) {
106 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); 102 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry");
107 return; 103 return;
108 } 104 }
109 105
110 MessageLoop::current()->PostTask( 106 MessageLoop::current()->PostTask(
111 FROM_HERE, 107 FROM_HERE,
112 base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy()))); 108 base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy())));
113 } 109 }
114 110
115 void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath& profile_path, 111 void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath& profile_path,
116 const std::string& entry_path, 112 const std::string& entry_path,
117 const base::Closure& callback, 113 const base::Closure& callback,
118 const ErrorCallback& error_callback) { 114 const ErrorCallback& error_callback) {
119 base::DictionaryValue* profile = GetProfile(profile_path, error_callback); 115 ProfileProperties* profile = GetProfile(profile_path, error_callback);
120 if (!profile) 116 if (!profile)
121 return; 117 return;
122 118
123 if (!profile->RemoveWithoutPathExpansion(entry_path, NULL)) { 119 if (!profile->entries.RemoveWithoutPathExpansion(entry_path, NULL)) {
124 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); 120 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry");
125 return; 121 return;
126 } 122 }
127 123
128 MessageLoop::current()->PostTask(FROM_HERE, callback); 124 MessageLoop::current()->PostTask(FROM_HERE, callback);
129 } 125 }
130 126
131 ShillProfileClient::TestInterface* ShillProfileClientStub::GetTestInterface() { 127 ShillProfileClient::TestInterface* ShillProfileClientStub::GetTestInterface() {
132 return this; 128 return this;
133 } 129 }
134 130
135 void ShillProfileClientStub::AddProfile(const std::string& profile_path) { 131 void ShillProfileClientStub::AddProfile(const std::string& profile_path,
136 profile_entries_.SetWithoutPathExpansion(profile_path, 132 const std::string& userhash) {
137 new base::DictionaryValue); 133 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback()))
134 return;
135
136 ProfileProperties* profile = new ProfileProperties;
137 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty,
138 userhash);
139 profiles_[profile_path] = profile;
140 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
141 AddProfile(profile_path);
138 } 142 }
139 143
140 void ShillProfileClientStub::AddEntry(const std::string& profile_path, 144 void ShillProfileClientStub::AddEntry(const std::string& profile_path,
141 const std::string& entry_path, 145 const std::string& entry_path,
142 const base::DictionaryValue& properties) { 146 const base::DictionaryValue& properties) {
143 base::DictionaryValue* profile = GetOrCreateDictionary(profile_path, 147 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path),
144 &profile_entries_); 148 ErrorCallback());
145 profile->SetWithoutPathExpansion(entry_path, 149 DCHECK(profile);
146 properties.DeepCopy()); 150 profile->entries.SetWithoutPathExpansion(entry_path,
151 properties.DeepCopy());
147 } 152 }
148 153
149 bool ShillProfileClientStub::AddService(const std::string& service_path) { 154 bool ShillProfileClientStub::AddService(const std::string& service_path) {
150 ShillServiceClient::TestInterface* service_test = 155 ShillServiceClient::TestInterface* service_test =
151 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 156 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
152 const base::DictionaryValue* properties = 157 const base::DictionaryValue* properties =
153 service_test->GetServiceProperties(service_path); 158 service_test->GetServiceProperties(service_path);
154 std::string profile_path; 159 std::string profile_path;
155 if (!properties) 160 if (!properties)
156 return false; 161 return false;
157 162
158 properties->GetStringWithoutPathExpansion(flimflam::kProfileProperty, 163 properties->GetStringWithoutPathExpansion(flimflam::kProfileProperty,
159 &profile_path); 164 &profile_path);
160 if (profile_path.empty()) 165 if (profile_path.empty())
161 return false; 166 return false;
162 167
163 AddEntry(profile_path, service_path, *properties); 168 AddEntry(profile_path, service_path, *properties);
164 return true; 169 return true;
165 } 170 }
166 171
167 base::DictionaryValue* ShillProfileClientStub::GetProfile( 172 ShillProfileClientStub::ProfileProperties* ShillProfileClientStub::GetProfile(
168 const dbus::ObjectPath& profile_path, 173 const dbus::ObjectPath& profile_path,
169 const ErrorCallback& error_callback) { 174 const ErrorCallback& error_callback) {
170 base::DictionaryValue* profile = NULL; 175 ProfileMap::const_iterator found = profiles_.find(profile_path.value());
171 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(), 176 if (found == profiles_.end()) {
172 &profile); 177 if (!error_callback.is_null())
173 if (!profile) 178 error_callback.Run("Error.InvalidProfile", "Invalid profile");
174 error_callback.Run("Error.InvalidProfile", "Invalid profile"); 179 return NULL;
175 return profile; 180 }
181
182 return found->second;
176 } 183 }
177 184
178 } // namespace chromeos 185 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698