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

Side by Side Diff: chromeos/network/network_profile_handler.cc

Issue 13957012: Adding a NetworkProfileHandler used by ManagedNetworkConfigurationHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Initial patch. Created 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chromeos/network/network_profile_handler.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/string_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_profile_client.h"
15 #include "chromeos/network/network_profile_observer.h"
16 #include "dbus/object_path.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
18
19 // TODO(pneubeck): remove once cros_system_api DEP is rolled!!!!!!!
20 // !!!!!!!!!!!!
21 namespace shill {
22 const char kUserHashProperty[] = "UserHash";
23 }
24
25 namespace chromeos {
26
27 namespace {
28
29 NetworkProfileHandler* g_profile_handler_instance = NULL;
stevenjb 2013/04/26 21:00:49 nit: for debugging ease I prefer not to put the gl
30
31 bool ConvertListValueToStringVector(const base::ListValue& string_list,
32 std::vector<std::string>* result) {
33 for (size_t i = 0; i < string_list.GetSize(); ++i) {
34 std::string str;
35 if (!string_list.GetString(i, &str))
36 return false;
37 result->push_back(str);
38 }
39 return true;
40 }
41
42 void GetProfilePropertiesError(const std::string& profile_path,
43 const std::string& error_name,
44 const std::string& error_message) {
stevenjb 2013/04/26 21:00:49 s/Get/Log
pneubeck (no reviews) 2013/04/29 18:05:51 Done.
45 LOG(ERROR) << "Error when requesting properties for profile "
46 << profile_path << ": " << error_message;
47 }
48
49 NetworkProfileHandler::ProfileList::const_iterator FindByPath(
50 const NetworkProfileHandler::ProfileList& profiles,
stevenjb 2013/04/26 21:00:49 There's at least one case below where you really w
pneubeck (no reviews) 2013/04/29 18:05:51 Done.
51 const std::string& path) {
52 for (NetworkProfileHandler::ProfileList::const_iterator it = profiles.begin();
53 it != profiles.end(); ++it) {
54 if (it->path == path)
55 return it;
56 }
57 return profiles.end();
58 }
59
60 } // namespace
61
62 // static
63 NetworkProfileHandler* NetworkProfileHandler::Initialize() {
64 CHECK(!g_profile_handler_instance);
65 g_profile_handler_instance = new NetworkProfileHandler();
66 DBusThreadManager::Get()->GetShillManagerClient()->
67 AddPropertyChangedObserver(g_profile_handler_instance);
68 g_profile_handler_instance->RequestInitialProfileList();
stevenjb 2013/04/26 21:00:49 Why not do these in the constructor?
pneubeck (no reviews) 2013/04/29 18:05:51 because of the derived Mock and Stub. As many peop
stevenjb 2013/04/30 17:42:47 I see. Typically we would use an Impl class and ca
69 return g_profile_handler_instance;
70 }
71
72 // static
73 bool NetworkProfileHandler::IsInitialized() {
74 return g_profile_handler_instance;
75 }
76
77 // static
78 void NetworkProfileHandler::Shutdown() {
79 CHECK(g_profile_handler_instance);
80 DBusThreadManager::Get()->GetShillManagerClient()->
81 RemovePropertyChangedObserver(g_profile_handler_instance);
stevenjb 2013/04/26 21:00:49 In destructor?
82 delete g_profile_handler_instance;
83 g_profile_handler_instance = NULL;
84 }
85
86 // static
87 NetworkProfileHandler* NetworkProfileHandler::Get() {
88 CHECK(g_profile_handler_instance);
89 return g_profile_handler_instance;
90 }
91
92 void NetworkProfileHandler::AddObserver(NetworkProfileObserver* observer) {
93 observers_.AddObserver(observer);
94 }
95
96 void NetworkProfileHandler::RemoveObserver(NetworkProfileObserver* observer) {
97 observers_.RemoveObserver(observer);
98 }
99
100 void NetworkProfileHandler::RequestInitialProfileList() {
101 // Request the initial profile list.
102 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties(
103 base::Bind(&NetworkProfileHandler::GetManagerPropertiesCallback,
104 weak_ptr_factory_.GetWeakPtr()));
105 }
106
107 void NetworkProfileHandler::GetManagerPropertiesCallback(
108 DBusMethodCallStatus call_status,
109 const base::DictionaryValue& properties) {
110 if (DBUS_METHOD_CALL_FAILURE) {
111 LOG(ERROR) << "Error when requesting manager properties.";
112 return;
113 }
114
115 const base::Value* profiles = NULL;
116 properties.GetWithoutPathExpansion(flimflam::kProfilesProperty, &profiles);
117 DCHECK(profiles);
stevenjb 2013/04/26 21:00:49 I'd rather not crash chrome if Shill fails to prov
pneubeck (no reviews) 2013/04/29 18:05:51 Done.
118 OnPropertyChanged(flimflam::kProfilesProperty, *profiles);
119 }
120
121 void NetworkProfileHandler::OnPropertyChanged(const std::string& name,
122 const base::Value& value) {
123 if (name != flimflam::kProfilesProperty)
124 return;
125
126 const base::ListValue* profiles_value = NULL;
127 value.GetAsList(&profiles_value);
128 DCHECK(profiles_value);
129
130 std::vector<std::string> new_profile_paths;
131 bool result = ConvertListValueToStringVector(*profiles_value,
132 &new_profile_paths);
133 DCHECK(result);
134
135 // Search for removed profiles.
136 std::vector<std::string> removed_profile_paths;
137 for (ProfileList::const_iterator it = profiles_.begin();
138 it != profiles_.end(); ++it) {
139 LOG(ERROR) << " " << it->path;
stevenjb 2013/04/26 21:00:49 remove
pneubeck (no reviews) 2013/04/29 18:05:51 Done.
140 if (std::find(new_profile_paths.begin(),
141 new_profile_paths.end(),
142 it->path) == new_profile_paths.end()) {
143 removed_profile_paths.push_back(it->path);
144 }
145 }
146
147 for (std::vector<std::string>::const_iterator it =
148 removed_profile_paths.begin();
149 it != removed_profile_paths.end(); ++it) {
150 RemoveProfile(*it);
151 }
152
153 for (std::vector<std::string>::const_iterator it = new_profile_paths.begin();
154 it != new_profile_paths.end(); ++it) {
155 // Skip known profiles. The associated userhash should never change.
156 if (GetProfileForPath(*it))
157 continue;
158
159 VLOG(2) << "Requesting properties of profile path " << *it << ".";
160 DBusThreadManager::Get()->GetShillProfileClient()->GetProperties(
161 dbus::ObjectPath(*it),
162 base::Bind(&NetworkProfileHandler::GetProfilePropertiesCallback,
163 weak_ptr_factory_.GetWeakPtr(),
164 *it),
165 base::Bind(&GetProfilePropertiesError, *it));
166 }
167 }
168
169 void NetworkProfileHandler::GetProfilePropertiesCallback(
170 const std::string& profile_path,
171 const base::DictionaryValue& properties) {
172 std::string userhash;
173 properties.GetStringWithoutPathExpansion(shill::kUserHashProperty,
174 &userhash);
175
176 AddProfile(NetworkProfile(profile_path, userhash));
177 }
178
179 void NetworkProfileHandler::AddProfile(const NetworkProfile& profile) {
180 VLOG(2) << "Adding profile " << ProfileToString(&profile) << ".";
181 profiles_.push_back(profile);
182 FOR_EACH_OBSERVER(NetworkProfileObserver, observers_,
183 OnProfileAdded(profiles_.back()));
184 }
185
186 void NetworkProfileHandler::RemoveProfile(const std::string& profile_path) {
187 VLOG(2) << "Removing profile for path " << profile_path << ".";
188 ProfileList::const_iterator found = FindByPath(profiles_, profile_path);
189 if (found == profiles_.end())
190 return;
191 NetworkProfile profile = *found;
192 // Workaround the deficiency of std::vector<>::erase(), which doesn't accept
193 // const_iterator.
194 profiles_.erase(profiles_.begin() + (found - profiles_.begin()));
stevenjb 2013/04/26 21:00:49 Ew. See suggestion above for adding a comparator t
pneubeck (no reviews) 2013/04/29 18:05:51 Done.
195 FOR_EACH_OBSERVER(NetworkProfileObserver, observers_,
196 OnProfileRemoved(profile));
197 }
198
199 const NetworkProfile* NetworkProfileHandler::GetProfileForPath(
200 const std::string& profile_path) const {
201 ProfileList::const_iterator found = FindByPath(profiles_, profile_path);
202 if (found == profiles_.end())
203 return NULL;
204 return &*found;
205 }
206
207 const NetworkProfile* NetworkProfileHandler::GetProfileForUserhash(
208 const std::string& userhash) const {
stevenjb 2013/04/26 21:00:49 This could also use std::find with a CompareByUser
pneubeck (no reviews) 2013/04/29 18:05:51 IMO, not worth the overhead of the comparator defi
209 for (NetworkProfileHandler::ProfileList::const_iterator it =
210 profiles_.begin();
211 it != profiles_.end(); ++it) {
212 if (it->userhash == userhash)
213 return &*it;
214 }
215 return NULL;
216 }
217
218 NetworkProfileHandler::NetworkProfileHandler()
219 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
220
221 }
222
223 NetworkProfileHandler::~NetworkProfileHandler() {
224
225 }
226
227 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698