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

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

Issue 378103002: Add NetworkingPrivateDelegate class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 5 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 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_ap i.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "chrome/browser/extensions/api/networking_private/networking_private_de legate.h"
11 #include "chrome/common/extensions/api/networking_private.h"
12 #include "extensions/browser/extension_function_registry.h"
13
14 namespace {
15
16 const int kDefaultNetworkListLimit = 1000;
17
18 extensions::NetworkingPrivateDelegate* GetDelegate(
19 content::BrowserContext* browser_context) {
20 return extensions::NetworkingPrivateDelegate::GetForBrowserContext(
21 browser_context);
22 }
23
24 } // namespace
25
26 namespace private_api = extensions::api::networking_private;
27
28 namespace extensions {
29
30 namespace networking_private {
31
32 // static
33 const char kErrorInvalidNetworkGuid[] = "Error.InvalidNetworkGuid";
34 const char kErrorNetworkUnavailable[] = "Error.NetworkUnavailable";
35 const char kErrorNotReady[] = "Error.NotReady";
36 const char kErrorNotSupported[] = "Error.NotSupported";
37
38 } // namespace networking_private
39
40 ////////////////////////////////////////////////////////////////////////////////
41 // NetworkingPrivateGetPropertiesFunction
42
43 NetworkingPrivateGetPropertiesFunction::
44 ~NetworkingPrivateGetPropertiesFunction() {
45 }
46
47 bool NetworkingPrivateGetPropertiesFunction::RunAsync() {
pneubeck (no reviews) 2014/07/14 21:33:57 Feel free to skip this comment and the next two, b
stevenjb 2014/07/15 00:33:39 I see. I think that should be done separately. At
48 scoped_ptr<private_api::GetProperties::Params> params =
49 private_api::GetProperties::Params::Create(*args_);
50 EXTENSION_FUNCTION_VALIDATE(params);
51
52 GetDelegate(browser_context())->GetProperties(
53 params->network_guid,
54 base::Bind(&NetworkingPrivateGetPropertiesFunction::Success, this),
55 base::Bind(&NetworkingPrivateGetPropertiesFunction::Failure, this));
56 return true;
57 }
58
59 void NetworkingPrivateGetPropertiesFunction::Success(
60 scoped_ptr<base::DictionaryValue> result) {
61 SetResult(result.release());
pneubeck (no reviews) 2014/07/14 21:33:57 the new style of returning results in extension fu
stevenjb 2014/07/15 00:33:39 Acknowledged.
62 SendResponse(true);
63 }
64
65 void NetworkingPrivateGetPropertiesFunction::Failure(const std::string& error) {
66 error_ = error;
pneubeck (no reviews) 2014/07/14 21:33:57 should be Respond(Error(error));
stevenjb 2014/07/15 00:33:39 Acknowledged.
67 SendResponse(false);
68 }
69
70 ////////////////////////////////////////////////////////////////////////////////
71 // NetworkingPrivateGetManagedPropertiesFunction
72
73 NetworkingPrivateGetManagedPropertiesFunction::
74 ~NetworkingPrivateGetManagedPropertiesFunction() {
75 }
76
77 bool NetworkingPrivateGetManagedPropertiesFunction::RunAsync() {
78 scoped_ptr<private_api::GetManagedProperties::Params> params =
79 private_api::GetManagedProperties::Params::Create(*args_);
80 EXTENSION_FUNCTION_VALIDATE(params);
81
82 GetDelegate(browser_context())->GetManagedProperties(
83 params->network_guid,
84 base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Success, this),
85 base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Failure,
86 this));
87 return true;
88 }
89
90 void NetworkingPrivateGetManagedPropertiesFunction::Success(
91 scoped_ptr<base::DictionaryValue> result) {
92 SetResult(result.release());
93 SendResponse(true);
94 }
95
96 void NetworkingPrivateGetManagedPropertiesFunction::Failure(
97 const std::string& error) {
98 error_ = error;
99 SendResponse(false);
100 }
101
102 ////////////////////////////////////////////////////////////////////////////////
103 // NetworkingPrivateGetStateFunction
104
105 NetworkingPrivateGetStateFunction::~NetworkingPrivateGetStateFunction() {
106 }
107
108 bool NetworkingPrivateGetStateFunction::RunAsync() {
109 scoped_ptr<private_api::GetState::Params> params =
110 private_api::GetState::Params::Create(*args_);
111 EXTENSION_FUNCTION_VALIDATE(params);
112
113 GetDelegate(browser_context())->GetState(
114 params->network_guid,
115 base::Bind(&NetworkingPrivateGetStateFunction::Success, this),
116 base::Bind(&NetworkingPrivateGetStateFunction::Failure, this));
117 return true;
118 }
119
120 void NetworkingPrivateGetStateFunction::Success(
121 scoped_ptr<base::DictionaryValue> result) {
122 SetResult(result.release());
123 SendResponse(true);
124 }
125
126 void NetworkingPrivateGetStateFunction::Failure(const std::string& error) {
127 error_ = error;
128 SendResponse(false);
129 }
130
131 ////////////////////////////////////////////////////////////////////////////////
132 // NetworkingPrivateSetPropertiesFunction
133
134 NetworkingPrivateSetPropertiesFunction::
135 ~NetworkingPrivateSetPropertiesFunction() {
136 }
137
138 bool NetworkingPrivateSetPropertiesFunction::RunAsync() {
139 scoped_ptr<private_api::SetProperties::Params> params =
140 private_api::SetProperties::Params::Create(*args_);
141 EXTENSION_FUNCTION_VALIDATE(params);
142
143 scoped_ptr<base::DictionaryValue> properties_dict(
144 params->properties.ToValue());
145
146 GetDelegate(browser_context())->SetProperties(
147 params->network_guid,
148 properties_dict.Pass(),
149 base::Bind(&NetworkingPrivateSetPropertiesFunction::Success, this),
150 base::Bind(&NetworkingPrivateSetPropertiesFunction::Failure, this));
151 return true;
152 }
153
154 void NetworkingPrivateSetPropertiesFunction::Success() {
155 SendResponse(true);
156 }
157
158 void NetworkingPrivateSetPropertiesFunction::Failure(const std::string& error) {
159 error_ = error;
160 SendResponse(false);
161 }
162
163 ////////////////////////////////////////////////////////////////////////////////
164 // NetworkingPrivateCreateNetworkFunction
165
166 NetworkingPrivateCreateNetworkFunction::
167 ~NetworkingPrivateCreateNetworkFunction() {
168 }
169
170 bool NetworkingPrivateCreateNetworkFunction::RunAsync() {
171 scoped_ptr<private_api::CreateNetwork::Params> params =
172 private_api::CreateNetwork::Params::Create(*args_);
173 EXTENSION_FUNCTION_VALIDATE(params);
174
175 scoped_ptr<base::DictionaryValue> properties_dict(
176 params->properties.ToValue());
177
178 GetDelegate(browser_context())->CreateNetwork(
179 params->shared,
180 properties_dict.Pass(),
181 base::Bind(&NetworkingPrivateCreateNetworkFunction::Success, this),
182 base::Bind(&NetworkingPrivateCreateNetworkFunction::Failure, this));
183 return true;
184 }
185
186 void NetworkingPrivateCreateNetworkFunction::Success(const std::string& guid) {
187 results_ = private_api::CreateNetwork::Results::Create(guid);
188 SendResponse(true);
189 }
190
191 void NetworkingPrivateCreateNetworkFunction::Failure(const std::string& error) {
192 error_ = error;
193 SendResponse(false);
194 }
195
196 ////////////////////////////////////////////////////////////////////////////////
197 // NetworkingPrivateGetNetworksFunction
198
199 NetworkingPrivateGetNetworksFunction::~NetworkingPrivateGetNetworksFunction() {
200 }
201
202 bool NetworkingPrivateGetNetworksFunction::RunAsync() {
203 scoped_ptr<private_api::GetNetworks::Params> params =
204 private_api::GetNetworks::Params::Create(*args_);
205 EXTENSION_FUNCTION_VALIDATE(params);
206
207 std::string network_type = private_api::ToString(params->filter.network_type);
208 const bool configured_only =
209 params->filter.configured ? *params->filter.configured : false;
210 const bool visible_only =
211 params->filter.visible ? *params->filter.visible : false;
212 const int limit =
213 params->filter.limit ? *params->filter.limit : kDefaultNetworkListLimit;
214
215 GetDelegate(browser_context())->GetNetworks(
216 network_type,
217 configured_only,
218 visible_only,
219 limit,
220 base::Bind(&NetworkingPrivateGetNetworksFunction::Success, this),
221 base::Bind(&NetworkingPrivateGetNetworksFunction::Failure, this));
222 return true;
223 }
224
225 void NetworkingPrivateGetNetworksFunction::Success(
226 scoped_ptr<base::ListValue> network_list) {
227 SetResult(network_list.release());
228 SendResponse(true);
229 }
230
231 void NetworkingPrivateGetNetworksFunction::Failure(const std::string& error) {
232 error_ = error;
233 SendResponse(false);
234 }
235
236 ////////////////////////////////////////////////////////////////////////////////
237 // NetworkingPrivateGetVisibleNetworksFunction
238
239 NetworkingPrivateGetVisibleNetworksFunction::
240 ~NetworkingPrivateGetVisibleNetworksFunction() {
241 }
242
243 bool NetworkingPrivateGetVisibleNetworksFunction::RunAsync() {
244 scoped_ptr<private_api::GetVisibleNetworks::Params> params =
245 private_api::GetVisibleNetworks::Params::Create(*args_);
246 EXTENSION_FUNCTION_VALIDATE(params);
247
248 std::string network_type = private_api::ToString(params->network_type);
249 const bool configured_only = false;
250 const bool visible_only = true;
251
252 GetDelegate(browser_context())->GetNetworks(
253 network_type,
254 configured_only,
255 visible_only,
256 kDefaultNetworkListLimit,
257 base::Bind(&NetworkingPrivateGetVisibleNetworksFunction::Success, this),
258 base::Bind(&NetworkingPrivateGetVisibleNetworksFunction::Failure, this));
259 return true;
260 }
261
262 void NetworkingPrivateGetVisibleNetworksFunction::Success(
263 scoped_ptr<base::ListValue> network_properties_list) {
264 SetResult(network_properties_list.release());
265 SendResponse(true);
266 }
267
268 void NetworkingPrivateGetVisibleNetworksFunction::Failure(
269 const std::string& error) {
270 error_ = error;
271 SendResponse(false);
272 }
273
274 ////////////////////////////////////////////////////////////////////////////////
275 // NetworkingPrivateGetEnabledNetworkTypesFunction
276
277 NetworkingPrivateGetEnabledNetworkTypesFunction::
278 ~NetworkingPrivateGetEnabledNetworkTypesFunction() {
279 }
280
281 bool NetworkingPrivateGetEnabledNetworkTypesFunction::RunSync() {
282 scoped_ptr<base::ListValue> enabled_networks_list(
283 GetDelegate(browser_context())->GetEnabledNetworkTypes());
284 if (enabled_networks_list) {
285 SetResult(enabled_networks_list.release());
286 return true;
287 }
288 return false;
289 }
290
291 ////////////////////////////////////////////////////////////////////////////////
292 // NetworkingPrivateEnableNetworkTypeFunction
293
294 NetworkingPrivateEnableNetworkTypeFunction::
295 ~NetworkingPrivateEnableNetworkTypeFunction() {
296 }
297
298 bool NetworkingPrivateEnableNetworkTypeFunction::RunSync() {
299 scoped_ptr<private_api::EnableNetworkType::Params> params =
300 private_api::EnableNetworkType::Params::Create(*args_);
301 EXTENSION_FUNCTION_VALIDATE(params);
302
303 return GetDelegate(browser_context())->EnableNetworkType(
304 private_api::ToString(params->network_type));
305 }
306
307 ////////////////////////////////////////////////////////////////////////////////
308 // NetworkingPrivateDisableNetworkTypeFunction
309
310 NetworkingPrivateDisableNetworkTypeFunction::
311 ~NetworkingPrivateDisableNetworkTypeFunction() {
312 }
313
314 bool NetworkingPrivateDisableNetworkTypeFunction::RunSync() {
315 scoped_ptr<private_api::DisableNetworkType::Params> params =
316 private_api::DisableNetworkType::Params::Create(*args_);
317
318 return GetDelegate(browser_context())->DisableNetworkType(
319 private_api::ToString(params->network_type));
320 }
321
322 ////////////////////////////////////////////////////////////////////////////////
323 // NetworkingPrivateRequestNetworkScanFunction
324
325 NetworkingPrivateRequestNetworkScanFunction::
326 ~NetworkingPrivateRequestNetworkScanFunction() {
327 }
328
329 bool NetworkingPrivateRequestNetworkScanFunction::RunSync() {
330 return GetDelegate(browser_context())->RequestScan();
331 }
332
333 ////////////////////////////////////////////////////////////////////////////////
334 // NetworkingPrivateStartConnectFunction
335
336 NetworkingPrivateStartConnectFunction::
337 ~NetworkingPrivateStartConnectFunction() {
338 }
339
340 bool NetworkingPrivateStartConnectFunction::RunAsync() {
341 scoped_ptr<private_api::StartConnect::Params> params =
342 private_api::StartConnect::Params::Create(*args_);
343 EXTENSION_FUNCTION_VALIDATE(params);
344
345 GetDelegate(browser_context())->StartConnect(
346 params->network_guid,
347 base::Bind(&NetworkingPrivateStartConnectFunction::Success, this),
348 base::Bind(&NetworkingPrivateStartConnectFunction::Failure, this));
349 return true;
350 }
351
352 void NetworkingPrivateStartConnectFunction::Success() {
353 SendResponse(true);
354 }
355
356 void NetworkingPrivateStartConnectFunction::Failure(const std::string& error) {
357 error_ = error;
358 SendResponse(false);
359 }
360
361 ////////////////////////////////////////////////////////////////////////////////
362 // NetworkingPrivateStartDisconnectFunction
363
364 NetworkingPrivateStartDisconnectFunction::
365 ~NetworkingPrivateStartDisconnectFunction() {
366 }
367
368 bool NetworkingPrivateStartDisconnectFunction::RunAsync() {
369 scoped_ptr<private_api::StartDisconnect::Params> params =
370 private_api::StartDisconnect::Params::Create(*args_);
371 EXTENSION_FUNCTION_VALIDATE(params);
372
373 GetDelegate(browser_context())->StartDisconnect(
374 params->network_guid,
375 base::Bind(&NetworkingPrivateStartDisconnectFunction::Success, this),
376 base::Bind(&NetworkingPrivateStartDisconnectFunction::Failure, this));
377 return true;
378 }
379
380 void NetworkingPrivateStartDisconnectFunction::Success() {
381 SendResponse(true);
382 }
383
384 void NetworkingPrivateStartDisconnectFunction::Failure(
385 const std::string& error) {
386 error_ = error;
387 SendResponse(false);
388 }
389
390 ////////////////////////////////////////////////////////////////////////////////
391 // NetworkingPrivateVerifyDestinationFunction
392
393 NetworkingPrivateVerifyDestinationFunction::
394 ~NetworkingPrivateVerifyDestinationFunction() {
395 }
396
397 bool NetworkingPrivateVerifyDestinationFunction::RunAsync() {
398 scoped_ptr<private_api::VerifyDestination::Params> params =
399 private_api::VerifyDestination::Params::Create(*args_);
400 EXTENSION_FUNCTION_VALIDATE(params);
401
402 scoped_ptr<base::DictionaryValue> verification_properties =
403 params->properties.ToValue();
404
405 GetDelegate(browser_context())->VerifyDestination(
406 verification_properties.Pass(),
pneubeck (no reviews) 2014/07/14 21:33:57 The _api.cc should translate into the representati
stevenjb 2014/07/15 00:33:39 Yeah, I went back and forth on this. What I think
407 base::Bind(&NetworkingPrivateVerifyDestinationFunction::Success, this),
408 base::Bind(&NetworkingPrivateVerifyDestinationFunction::Failure, this));
409 return true;
410 }
411
412 void NetworkingPrivateVerifyDestinationFunction::Success(bool result) {
413 results_ = private_api::VerifyDestination::Results::Create(result);
414 SendResponse(true);
415 }
416
417 void NetworkingPrivateVerifyDestinationFunction::Failure(
418 const std::string& error) {
419 error_ = error;
420 SendResponse(false);
421 }
422
423 ////////////////////////////////////////////////////////////////////////////////
424 // NetworkingPrivateVerifyAndEncryptCredentialsFunction
425
426 NetworkingPrivateVerifyAndEncryptCredentialsFunction::
427 ~NetworkingPrivateVerifyAndEncryptCredentialsFunction() {
428 }
429
430 bool NetworkingPrivateVerifyAndEncryptCredentialsFunction::RunAsync() {
431 scoped_ptr<private_api::VerifyAndEncryptCredentials::Params> params =
432 private_api::VerifyAndEncryptCredentials::Params::Create(*args_);
433 EXTENSION_FUNCTION_VALIDATE(params);
434
435 scoped_ptr<base::DictionaryValue> verification_properties =
436 params->properties.ToValue();
437
438 GetDelegate(browser_context())->VerifyAndEncryptCredentials(
439 params->network_guid,
440 verification_properties.Pass(),
441 base::Bind(&NetworkingPrivateVerifyAndEncryptCredentialsFunction::Success,
442 this),
443 base::Bind(&NetworkingPrivateVerifyAndEncryptCredentialsFunction::Failure,
444 this));
445 return true;
446 }
447
448 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::Success(
449 const std::string& result) {
450 results_ = private_api::VerifyAndEncryptCredentials::Results::Create(result);
451 SendResponse(true);
452 }
453
454 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::Failure(
455 const std::string& error) {
456 error_ = error;
457 SendResponse(false);
458 }
459
460 ////////////////////////////////////////////////////////////////////////////////
461 // NetworkingPrivateVerifyAndEncryptDataFunction
462
463 NetworkingPrivateVerifyAndEncryptDataFunction::
464 ~NetworkingPrivateVerifyAndEncryptDataFunction() {
465 }
466
467 bool NetworkingPrivateVerifyAndEncryptDataFunction::RunAsync() {
468 scoped_ptr<private_api::VerifyAndEncryptData::Params> params =
469 private_api::VerifyAndEncryptData::Params::Create(*args_);
470 EXTENSION_FUNCTION_VALIDATE(params);
471
472 scoped_ptr<base::DictionaryValue> verification_properties =
473 params->properties.ToValue();
474
475 GetDelegate(browser_context())->VerifyAndEncryptData(
476 verification_properties.Pass(),
477 params->data,
478 base::Bind(&NetworkingPrivateVerifyAndEncryptDataFunction::Success, this),
479 base::Bind(&NetworkingPrivateVerifyAndEncryptDataFunction::Failure,
480 this));
481 return true;
482 }
483
484 void NetworkingPrivateVerifyAndEncryptDataFunction::Success(
485 const std::string& result) {
486 results_ = private_api::VerifyAndEncryptData::Results::Create(result);
487 SendResponse(true);
488 }
489
490 void NetworkingPrivateVerifyAndEncryptDataFunction::Failure(
491 const std::string& error) {
492 error_ = error;
493 SendResponse(false);
494 }
495
496 ////////////////////////////////////////////////////////////////////////////////
497 // NetworkingPrivateSetWifiTDLSEnabledStateFunction
498
499 NetworkingPrivateSetWifiTDLSEnabledStateFunction::
500 ~NetworkingPrivateSetWifiTDLSEnabledStateFunction() {
501 }
502
503 bool NetworkingPrivateSetWifiTDLSEnabledStateFunction::RunAsync() {
504 scoped_ptr<private_api::SetWifiTDLSEnabledState::Params> params =
505 private_api::SetWifiTDLSEnabledState::Params::Create(*args_);
506 EXTENSION_FUNCTION_VALIDATE(params);
507
508 GetDelegate(browser_context())->SetWifiTDLSEnabledState(
509 params->ip_or_mac_address,
510 params->enabled,
511 base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success,
512 this),
513 base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure,
514 this));
515
516 return true;
517 }
518
519 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success(
520 const std::string& result) {
521 results_ = private_api::SetWifiTDLSEnabledState::Results::Create(result);
522 SendResponse(true);
523 }
524
525 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure(
526 const std::string& error) {
527 error_ = error;
528 SendResponse(false);
529 }
530
531 ////////////////////////////////////////////////////////////////////////////////
532 // NetworkingPrivateGetWifiTDLSStatusFunction
533
534 NetworkingPrivateGetWifiTDLSStatusFunction::
535 ~NetworkingPrivateGetWifiTDLSStatusFunction() {
536 }
537
538 bool NetworkingPrivateGetWifiTDLSStatusFunction::RunAsync() {
539 scoped_ptr<private_api::GetWifiTDLSStatus::Params> params =
540 private_api::GetWifiTDLSStatus::Params::Create(*args_);
541 EXTENSION_FUNCTION_VALIDATE(params);
542
543 GetDelegate(browser_context())->GetWifiTDLSStatus(
544 params->ip_or_mac_address,
545 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Success, this),
546 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Failure, this));
547
548 return true;
549 }
550
551 void NetworkingPrivateGetWifiTDLSStatusFunction::Success(
552 const std::string& result) {
553 results_ = private_api::GetWifiTDLSStatus::Results::Create(result);
554 SendResponse(true);
555 }
556
557 void NetworkingPrivateGetWifiTDLSStatusFunction::Failure(
558 const std::string& error) {
559 error_ = error;
560 SendResponse(false);
561 }
562
563 ////////////////////////////////////////////////////////////////////////////////
564 // NetworkingPrivateGetCaptivePortalStatusFunction
565
566 NetworkingPrivateGetCaptivePortalStatusFunction::
567 ~NetworkingPrivateGetCaptivePortalStatusFunction() {
568 }
569
570 bool NetworkingPrivateGetCaptivePortalStatusFunction::RunAsync() {
571 scoped_ptr<private_api::GetCaptivePortalStatus::Params> params =
572 private_api::GetCaptivePortalStatus::Params::Create(*args_);
573 EXTENSION_FUNCTION_VALIDATE(params);
574
575 GetDelegate(browser_context())->GetCaptivePortalStatus(
576 params->network_guid,
577 base::Bind(&NetworkingPrivateGetCaptivePortalStatusFunction::Success,
578 this),
579 base::Bind(&NetworkingPrivateGetCaptivePortalStatusFunction::Failure,
580 this));
581 return true;
582 }
583
584 void NetworkingPrivateGetCaptivePortalStatusFunction::Success(
585 const std::string& result) {
586 results_ = private_api::GetCaptivePortalStatus::Results::Create(
587 private_api::ParseCaptivePortalStatus(result));
588 SendResponse(true);
589 }
590
591 void NetworkingPrivateGetCaptivePortalStatusFunction::Failure(
592 const std::string& error) {
593 error_ = error;
594 SendResponse(false);
595 }
596
597 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698