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

Side by Side Diff: chrome/browser/extensions/api/networking_private/networking_private_chromeos.cc

Issue 870163002: Move networking_private to src/extensions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix GN 2 Created 5 years, 11 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
(Empty)
1 // Copyright 2014 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 "chrome/browser/extensions/api/networking_private/networking_private_ch romeos.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "chrome/browser/chromeos/profiles/profile_helper.h"
11 #include "chrome/browser/extensions/api/networking_private/networking_private_ap i.h"
12 #include "chrome/common/extensions/api/networking_private.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/shill_manager_client.h"
15 #include "chromeos/login/login_state.h"
16 #include "chromeos/network/managed_network_configuration_handler.h"
17 #include "chromeos/network/network_connection_handler.h"
18 #include "chromeos/network/network_device_handler.h"
19 #include "chromeos/network/network_event_log.h"
20 #include "chromeos/network/network_state.h"
21 #include "chromeos/network/network_state_handler.h"
22 #include "chromeos/network/network_util.h"
23 #include "chromeos/network/onc/onc_signature.h"
24 #include "chromeos/network/onc/onc_translator.h"
25 #include "chromeos/network/onc/onc_utils.h"
26 #include "chromeos/network/portal_detector/network_portal_detector.h"
27 #include "components/onc/onc_constants.h"
28 #include "content/public/browser/browser_context.h"
29
30 using chromeos::NetworkHandler;
31 using chromeos::NetworkTypePattern;
32 using chromeos::ShillManagerClient;
33 using extensions::NetworkingPrivateDelegate;
34
35 namespace {
36
37 chromeos::NetworkStateHandler* GetStateHandler() {
38 return NetworkHandler::Get()->network_state_handler();
39 }
40
41 chromeos::ManagedNetworkConfigurationHandler* GetManagedConfigurationHandler() {
42 return NetworkHandler::Get()->managed_network_configuration_handler();
43 }
44
45 bool GetServicePathFromGuid(const std::string& guid,
46 std::string* service_path,
47 std::string* error) {
48 const chromeos::NetworkState* network =
49 GetStateHandler()->GetNetworkStateFromGuid(guid);
50 if (!network) {
51 *error = extensions::networking_private::kErrorInvalidNetworkGuid;
52 return false;
53 }
54 *service_path = network->path();
55 return true;
56 }
57
58 bool GetUserIdHash(content::BrowserContext* browser_context,
59 std::string* user_hash,
60 std::string* error) {
61 std::string profile_user_hash =
62 chromeos::ProfileHelper::GetUserIdHashFromProfile(
63 static_cast<Profile*>(browser_context));
64
65 // Currently Chrome OS only configures networks for the primary user.
66 // Configuration attempts from other browser contexts should fail.
67 // TODO(stevenjb): use an ExtensionsBrowserClient method to access
68 // ProfileHelper when moving this to src/extensions.
69 if (profile_user_hash != chromeos::LoginState::Get()->primary_user_hash()) {
70 // Disallow class requiring a user id hash from a non-primary user context
71 // to avoid complexities with the policy code.
72 LOG(ERROR) << "networkingPrivate API call from non primary user: "
73 << profile_user_hash;
74 *error = "Error.NonPrimaryUser";
75 return false;
76 }
77 *user_hash = profile_user_hash;
78 return true;
79 }
80
81 void NetworkHandlerDictionaryCallback(
82 const NetworkingPrivateDelegate::DictionaryCallback& callback,
83 const std::string& service_path,
84 const base::DictionaryValue& dictionary) {
85 scoped_ptr<base::DictionaryValue> dictionary_copy(dictionary.DeepCopy());
86 callback.Run(dictionary_copy.Pass());
87 }
88
89 void NetworkHandlerFailureCallback(
90 const NetworkingPrivateDelegate::FailureCallback& callback,
91 const std::string& error_name,
92 scoped_ptr<base::DictionaryValue> error_data) {
93 callback.Run(error_name);
94 }
95
96 } // namespace
97
98 ////////////////////////////////////////////////////////////////////////////////
99
100 namespace extensions {
101
102 NetworkingPrivateChromeOS::NetworkingPrivateChromeOS(
103 content::BrowserContext* browser_context,
104 scoped_ptr<VerifyDelegate> verify_delegate)
105 : NetworkingPrivateDelegate(verify_delegate.Pass()),
106 browser_context_(browser_context) {
107 }
108
109 NetworkingPrivateChromeOS::~NetworkingPrivateChromeOS() {}
110
111 void NetworkingPrivateChromeOS::GetProperties(
112 const std::string& guid,
113 const DictionaryCallback& success_callback,
114 const FailureCallback& failure_callback) {
115 std::string service_path, error;
116 if (!GetServicePathFromGuid(guid, &service_path, &error)) {
117 failure_callback.Run(error);
118 return;
119 }
120
121 GetManagedConfigurationHandler()->GetProperties(
122 service_path,
123 base::Bind(&NetworkHandlerDictionaryCallback, success_callback),
124 base::Bind(&NetworkHandlerFailureCallback, failure_callback));
125 }
126
127 void NetworkingPrivateChromeOS::GetManagedProperties(
128 const std::string& guid,
129 const DictionaryCallback& success_callback,
130 const FailureCallback& failure_callback) {
131 std::string service_path, error;
132 if (!GetServicePathFromGuid(guid, &service_path, &error)) {
133 failure_callback.Run(error);
134 return;
135 }
136
137 std::string user_id_hash;
138 if (!GetUserIdHash(browser_context_, &user_id_hash, &error)) {
139 failure_callback.Run(error);
140 return;
141 }
142
143 GetManagedConfigurationHandler()->GetManagedProperties(
144 user_id_hash,
145 service_path,
146 base::Bind(&NetworkHandlerDictionaryCallback, success_callback),
147 base::Bind(&NetworkHandlerFailureCallback, failure_callback));
148 }
149
150 void NetworkingPrivateChromeOS::GetState(
151 const std::string& guid,
152 const DictionaryCallback& success_callback,
153 const FailureCallback& failure_callback) {
154 std::string service_path, error;
155 if (!GetServicePathFromGuid(guid, &service_path, &error)) {
156 failure_callback.Run(error);
157 return;
158 }
159
160 const chromeos::NetworkState* network_state =
161 GetStateHandler()->GetNetworkStateFromServicePath(
162 service_path, false /* configured_only */);
163 if (!network_state) {
164 failure_callback.Run(networking_private::kErrorNetworkUnavailable);
165 return;
166 }
167
168 scoped_ptr<base::DictionaryValue> network_properties =
169 chromeos::network_util::TranslateNetworkStateToONC(network_state);
170
171 success_callback.Run(network_properties.Pass());
172 }
173
174 void NetworkingPrivateChromeOS::SetProperties(
175 const std::string& guid,
176 scoped_ptr<base::DictionaryValue> properties,
177 const VoidCallback& success_callback,
178 const FailureCallback& failure_callback) {
179 std::string service_path, error;
180 if (!GetServicePathFromGuid(guid, &service_path, &error)) {
181 failure_callback.Run(error);
182 return;
183 }
184
185 GetManagedConfigurationHandler()->SetProperties(
186 service_path,
187 *properties,
188 success_callback,
189 base::Bind(&NetworkHandlerFailureCallback, failure_callback));
190 }
191
192 void NetworkingPrivateChromeOS::CreateNetwork(
193 bool shared,
194 scoped_ptr<base::DictionaryValue> properties,
195 const StringCallback& success_callback,
196 const FailureCallback& failure_callback) {
197 std::string user_id_hash, error;
198 // Do not allow configuring a non-shared network from a non-primary user.
199 if (!shared && !GetUserIdHash(browser_context_, &user_id_hash, &error)) {
200 failure_callback.Run(error);
201 return;
202 }
203
204 GetManagedConfigurationHandler()->CreateConfiguration(
205 user_id_hash,
206 *properties,
207 success_callback,
208 base::Bind(&NetworkHandlerFailureCallback, failure_callback));
209 }
210
211 void NetworkingPrivateChromeOS::GetNetworks(
212 const std::string& network_type,
213 bool configured_only,
214 bool visible_only,
215 int limit,
216 const NetworkListCallback& success_callback,
217 const FailureCallback& failure_callback) {
218 NetworkTypePattern pattern =
219 chromeos::onc::NetworkTypePatternFromOncType(network_type);
220 scoped_ptr<base::ListValue> network_properties_list =
221 chromeos::network_util::TranslateNetworkListToONC(
222 pattern, configured_only, visible_only, limit, false /* debugging */);
223 success_callback.Run(network_properties_list.Pass());
224 }
225
226 void NetworkingPrivateChromeOS::StartConnect(
227 const std::string& guid,
228 const VoidCallback& success_callback,
229 const FailureCallback& failure_callback) {
230 std::string service_path, error;
231 if (!GetServicePathFromGuid(guid, &service_path, &error)) {
232 failure_callback.Run(error);
233 return;
234 }
235
236 const bool check_error_state = false;
237 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork(
238 service_path,
239 success_callback,
240 base::Bind(&NetworkHandlerFailureCallback, failure_callback),
241 check_error_state);
242 }
243
244 void NetworkingPrivateChromeOS::StartDisconnect(
245 const std::string& guid,
246 const VoidCallback& success_callback,
247 const FailureCallback& failure_callback) {
248 std::string service_path, error;
249 if (!GetServicePathFromGuid(guid, &service_path, &error)) {
250 failure_callback.Run(error);
251 return;
252 }
253
254 NetworkHandler::Get()->network_connection_handler()->DisconnectNetwork(
255 service_path,
256 success_callback,
257 base::Bind(&NetworkHandlerFailureCallback, failure_callback));
258 }
259
260 void NetworkingPrivateChromeOS::SetWifiTDLSEnabledState(
261 const std::string& ip_or_mac_address,
262 bool enabled,
263 const StringCallback& success_callback,
264 const FailureCallback& failure_callback) {
265 NetworkHandler::Get()->network_device_handler()->SetWifiTDLSEnabled(
266 ip_or_mac_address,
267 enabled,
268 success_callback,
269 base::Bind(&NetworkHandlerFailureCallback, failure_callback));
270 }
271
272 void NetworkingPrivateChromeOS::GetWifiTDLSStatus(
273 const std::string& ip_or_mac_address,
274 const StringCallback& success_callback,
275 const FailureCallback& failure_callback) {
276 NetworkHandler::Get()->network_device_handler()->GetWifiTDLSStatus(
277 ip_or_mac_address,
278 success_callback,
279 base::Bind(&NetworkHandlerFailureCallback, failure_callback));
280 }
281
282 void NetworkingPrivateChromeOS::GetCaptivePortalStatus(
283 const std::string& guid,
284 const StringCallback& success_callback,
285 const FailureCallback& failure_callback) {
286 if (!chromeos::NetworkPortalDetector::IsInitialized()) {
287 failure_callback.Run(networking_private::kErrorNotReady);
288 return;
289 }
290
291 chromeos::NetworkPortalDetector::CaptivePortalState state =
292 chromeos::NetworkPortalDetector::Get()->GetCaptivePortalState(guid);
293 success_callback.Run(
294 chromeos::NetworkPortalDetector::CaptivePortalStatusString(state.status));
295 }
296
297 scoped_ptr<base::ListValue>
298 NetworkingPrivateChromeOS::GetEnabledNetworkTypes() {
299 chromeos::NetworkStateHandler* state_handler = GetStateHandler();
300
301 scoped_ptr<base::ListValue> network_list(new base::ListValue);
302
303 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::Ethernet()))
304 network_list->AppendString(::onc::network_type::kEthernet);
305 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::WiFi()))
306 network_list->AppendString(::onc::network_type::kWiFi);
307 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::Wimax()))
308 network_list->AppendString(::onc::network_type::kWimax);
309 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::Cellular()))
310 network_list->AppendString(::onc::network_type::kCellular);
311
312 return network_list.Pass();
313 }
314
315 bool NetworkingPrivateChromeOS::EnableNetworkType(const std::string& type) {
316 NetworkTypePattern pattern =
317 chromeos::onc::NetworkTypePatternFromOncType(type);
318
319 GetStateHandler()->SetTechnologyEnabled(
320 pattern, true, chromeos::network_handler::ErrorCallback());
321
322 return true;
323 }
324
325 bool NetworkingPrivateChromeOS::DisableNetworkType(const std::string& type) {
326 NetworkTypePattern pattern =
327 chromeos::onc::NetworkTypePatternFromOncType(type);
328
329 GetStateHandler()->SetTechnologyEnabled(
330 pattern, false, chromeos::network_handler::ErrorCallback());
331
332 return true;
333 }
334
335 bool NetworkingPrivateChromeOS::RequestScan() {
336 GetStateHandler()->RequestScan();
337 return true;
338 }
339
340 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698