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

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: 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
(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 namespace chromeos {
20
21 namespace {
22
23 bool ConvertListValueToStringVector(const base::ListValue& string_list,
24 std::vector<std::string>* result) {
25 for (size_t i = 0; i < string_list.GetSize(); ++i) {
26 std::string str;
27 if (!string_list.GetString(i, &str))
28 return false;
29 result->push_back(str);
30 }
31 return true;
32 }
33
34 void LogProfileRequestError(const std::string& profile_path,
35 const std::string& error_name,
36 const std::string& error_message) {
37 LOG(ERROR) << "Error when requesting properties for profile "
38 << profile_path << ": " << error_message;
39 }
40
41 class ProfilePathEquals {
42 public:
43 explicit ProfilePathEquals(const std::string& path)
44 : path_(path) {
45 }
46
47 bool operator()(const NetworkProfile& profile) {
48 return profile.path == path_;
49 }
50
51 private:
52 std::string path_;
53 };
54
55 } // namespace
56
57 NetworkProfileHandler* g_profile_handler_instance = NULL;
58
59 // static
60 NetworkProfileHandler* NetworkProfileHandler::Initialize() {
61 CHECK(!g_profile_handler_instance);
62 g_profile_handler_instance = new NetworkProfileHandler();
63 DBusThreadManager::Get()->GetShillManagerClient()->
64 AddPropertyChangedObserver(g_profile_handler_instance);
65 g_profile_handler_instance->RequestInitialProfileList();
66 return g_profile_handler_instance;
67 }
68
69 // static
70 bool NetworkProfileHandler::IsInitialized() {
71 return g_profile_handler_instance;
72 }
73
74 // static
75 void NetworkProfileHandler::Shutdown() {
76 CHECK(g_profile_handler_instance);
77 DBusThreadManager::Get()->GetShillManagerClient()->
78 RemovePropertyChangedObserver(g_profile_handler_instance);
79 delete g_profile_handler_instance;
80 g_profile_handler_instance = NULL;
81 }
82
83 // static
84 NetworkProfileHandler* NetworkProfileHandler::Get() {
85 CHECK(g_profile_handler_instance);
86 return g_profile_handler_instance;
87 }
88
89 void NetworkProfileHandler::AddObserver(NetworkProfileObserver* observer) {
90 observers_.AddObserver(observer);
91 }
92
93 void NetworkProfileHandler::RemoveObserver(NetworkProfileObserver* observer) {
94 observers_.RemoveObserver(observer);
95 }
96
97 void NetworkProfileHandler::RequestInitialProfileList() {
98 // Request the initial profile list.
99 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties(
100 base::Bind(&NetworkProfileHandler::GetManagerPropertiesCallback,
101 weak_ptr_factory_.GetWeakPtr()));
102 }
103
104 void NetworkProfileHandler::GetManagerPropertiesCallback(
105 DBusMethodCallStatus call_status,
106 const base::DictionaryValue& properties) {
107 if (DBUS_METHOD_CALL_FAILURE) {
108 LOG(ERROR) << "Error when requesting manager properties.";
109 return;
110 }
111
112 const base::Value* profiles = NULL;
113 properties.GetWithoutPathExpansion(flimflam::kProfilesProperty, &profiles);
114 if (!profiles) {
115 LOG(ERROR) << "Manager properties returned from Shill don't contain "
116 << "the field " << flimflam::kProfilesProperty;
117 return;
118 }
119 OnPropertyChanged(flimflam::kProfilesProperty, *profiles);
120 }
121
122 void NetworkProfileHandler::OnPropertyChanged(const std::string& name,
123 const base::Value& value) {
124 if (name != flimflam::kProfilesProperty)
125 return;
126
127 const base::ListValue* profiles_value = NULL;
128 value.GetAsList(&profiles_value);
129 DCHECK(profiles_value);
130
131 std::vector<std::string> new_profile_paths;
132 bool result = ConvertListValueToStringVector(*profiles_value,
133 &new_profile_paths);
134 DCHECK(result);
135
136 // Search for removed profiles.
137 std::vector<std::string> removed_profile_paths;
138 for (ProfileList::const_iterator it = profiles_.begin();
139 it != profiles_.end(); ++it) {
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(&LogProfileRequestError, *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 " << profile.ToDebugString() << ".";
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::iterator found = std::find_if(profiles_.begin(), profiles_.end(),
189 ProfilePathEquals(profile_path));
190 if (found == profiles_.end())
191 return;
192 NetworkProfile profile = *found;
193 // Workaround the deficiency of std::vector<>::erase(), which doesn't accept
194 // const_iterator.
195 profiles_.erase(profiles_.begin() + (found - profiles_.begin()));
stevenjb 2013/04/30 17:42:47 Can't this now just be profiles_.erase(found)?
pneubeck (no reviews) 2013/05/03 17:32:55 Uhrg, of course.
196 FOR_EACH_OBSERVER(NetworkProfileObserver, observers_,
197 OnProfileRemoved(profile));
198 }
199
200 const NetworkProfile* NetworkProfileHandler::GetProfileForPath(
201 const std::string& profile_path) const {
202 ProfileList::const_iterator found =
203 std::find_if(profiles_.begin(), profiles_.end(),
204 ProfilePathEquals(profile_path));
205
206 if (found == profiles_.end())
207 return NULL;
208 return &*found;
209 }
210
211 const NetworkProfile* NetworkProfileHandler::GetProfileForUserhash(
212 const std::string& userhash) const {
213 for (NetworkProfileHandler::ProfileList::const_iterator it =
214 profiles_.begin();
215 it != profiles_.end(); ++it) {
216 if (it->userhash == userhash)
217 return &*it;
218 }
219 return NULL;
220 }
221
222 NetworkProfileHandler::NetworkProfileHandler()
223 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
224
225 }
226
227 NetworkProfileHandler::~NetworkProfileHandler() {
228
229 }
230
231 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698