Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / networking_private / networking_private_api_nonchromeos.cc
1 // Copyright 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 "chrome/browser/extensions/api/networking_private/networking_private_api.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/json/json_reader.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/api/networking_private/networking_private_service_client.h"
14 #include "chrome/browser/extensions/api/networking_private/networking_private_service_client_factory.h"
15 #include "chrome/browser/extensions/extension_function_registry.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/extensions/api/networking_private.h"
18 #include "components/onc/onc_constants.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/browser/extension_system.h"
21
22 using extensions::NetworkingPrivateServiceClient;
23 using extensions::NetworkingPrivateServiceClientFactory;
24 namespace api = extensions::api::networking_private;
25
26 ////////////////////////////////////////////////////////////////////////////////
27 // NetworkingPrivateGetPropertiesFunction
28
29 NetworkingPrivateGetPropertiesFunction::
30   ~NetworkingPrivateGetPropertiesFunction() {
31 }
32
33 bool NetworkingPrivateGetPropertiesFunction::RunImpl() {
34   scoped_ptr<api::GetProperties::Params> params =
35       api::GetProperties::Params::Create(*args_);
36   EXTENSION_FUNCTION_VALIDATE(params);
37
38   NetworkingPrivateServiceClient* service_client =
39       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
40
41   service_client->GetProperties(
42       params->network_guid,
43       base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess,
44                  this),
45       base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed,
46                  this));
47   return true;
48 }
49
50 void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess(
51     const std::string& network_guid,
52     const base::DictionaryValue& dictionary) {
53   SetResult(dictionary.DeepCopy());
54   SendResponse(true);
55 }
56
57 void NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed(
58     const std::string& error_name,
59     scoped_ptr<base::DictionaryValue> error_data) {
60   error_ = error_name;
61   SendResponse(false);
62 }
63
64 ////////////////////////////////////////////////////////////////////////////////
65 // NetworkingPrivateGetManagedPropertiesFunction
66
67 NetworkingPrivateGetManagedPropertiesFunction::
68   ~NetworkingPrivateGetManagedPropertiesFunction() {
69 }
70
71 bool NetworkingPrivateGetManagedPropertiesFunction::RunImpl() {
72   scoped_ptr<api::GetManagedProperties::Params> params =
73       api::GetManagedProperties::Params::Create(*args_);
74   EXTENSION_FUNCTION_VALIDATE(params);
75   NetworkingPrivateServiceClient* service_client =
76       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
77
78   service_client->GetManagedProperties(
79       params->network_guid,
80       base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Success,
81                  this),
82       base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Failure,
83                  this));
84   return true;
85 }
86
87 void NetworkingPrivateGetManagedPropertiesFunction::Success(
88     const std::string& network_guid,
89     const base::DictionaryValue& dictionary) {
90   scoped_ptr<base::DictionaryValue> network_properties(dictionary.DeepCopy());
91   network_properties->SetStringWithoutPathExpansion(onc::network_config::kGUID,
92                                                     network_guid);
93   SetResult(network_properties.release());
94   SendResponse(true);
95 }
96
97 void NetworkingPrivateGetManagedPropertiesFunction::Failure(
98     const std::string& error_name,
99     scoped_ptr<base::DictionaryValue> error_data) {
100   error_ = error_name;
101   SendResponse(false);
102 }
103
104 ////////////////////////////////////////////////////////////////////////////////
105 // NetworkingPrivateGetStateFunction
106
107 NetworkingPrivateGetStateFunction::
108   ~NetworkingPrivateGetStateFunction() {
109 }
110
111 bool NetworkingPrivateGetStateFunction::RunImpl() {
112   scoped_ptr<api::GetState::Params> params =
113       api::GetState::Params::Create(*args_);
114   EXTENSION_FUNCTION_VALIDATE(params);
115   NetworkingPrivateServiceClient* service_client =
116       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
117
118   service_client->GetState(
119       params->network_guid,
120       base::Bind(&NetworkingPrivateGetStateFunction::Success,
121                  this),
122       base::Bind(&NetworkingPrivateGetStateFunction::Failure,
123                  this));
124   return true;
125 }
126
127 void NetworkingPrivateGetStateFunction::Success(
128     const std::string& network_guid,
129     const base::DictionaryValue& dictionary) {
130   SetResult(dictionary.DeepCopy());
131   SendResponse(true);
132 }
133
134 void NetworkingPrivateGetStateFunction::Failure(
135     const std::string& error_name,
136     scoped_ptr<base::DictionaryValue> error_data) {
137   error_ = error_name;
138   SendResponse(false);
139 }
140
141 ////////////////////////////////////////////////////////////////////////////////
142 // NetworkingPrivateSetPropertiesFunction
143
144 NetworkingPrivateSetPropertiesFunction::
145   ~NetworkingPrivateSetPropertiesFunction() {
146 }
147
148 bool NetworkingPrivateSetPropertiesFunction::RunImpl() {
149   scoped_ptr<api::SetProperties::Params> params =
150       api::SetProperties::Params::Create(*args_);
151   EXTENSION_FUNCTION_VALIDATE(params);
152   scoped_ptr<base::DictionaryValue> properties_dict(
153       params->properties.ToValue());
154
155   NetworkingPrivateServiceClient* service_client =
156       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
157
158   service_client->SetProperties(
159       params->network_guid,
160       *properties_dict,
161       base::Bind(&NetworkingPrivateSetPropertiesFunction::ResultCallback, this),
162       base::Bind(&NetworkingPrivateSetPropertiesFunction::ErrorCallback, this));
163   return true;
164 }
165
166 void NetworkingPrivateSetPropertiesFunction::ResultCallback() {
167   SendResponse(true);
168 }
169
170 void NetworkingPrivateSetPropertiesFunction::ErrorCallback(
171     const std::string& error_name,
172     const scoped_ptr<base::DictionaryValue> error_data) {
173   error_ = error_name;
174   SendResponse(false);
175 }
176
177 ////////////////////////////////////////////////////////////////////////////////
178 // NetworkingPrivateCreateNetworkFunction
179
180 NetworkingPrivateCreateNetworkFunction::
181 ~NetworkingPrivateCreateNetworkFunction() {
182 }
183
184 bool NetworkingPrivateCreateNetworkFunction::RunImpl() {
185   scoped_ptr<api::CreateNetwork::Params> params =
186       api::CreateNetwork::Params::Create(*args_);
187   EXTENSION_FUNCTION_VALIDATE(params);
188   scoped_ptr<base::DictionaryValue> properties_dict(
189       params->properties.ToValue());
190
191   NetworkingPrivateServiceClient* service_client =
192       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
193
194   service_client->CreateNetwork(
195       params->shared,
196       *properties_dict,
197       base::Bind(&NetworkingPrivateCreateNetworkFunction::ResultCallback, this),
198       base::Bind(&NetworkingPrivateCreateNetworkFunction::ErrorCallback, this));
199   return true;
200 }
201
202 void NetworkingPrivateCreateNetworkFunction::ErrorCallback(
203     const std::string& error_name,
204     const scoped_ptr<base::DictionaryValue> error_data) {
205   error_ = error_name;
206   SendResponse(false);
207 }
208
209 void NetworkingPrivateCreateNetworkFunction::ResultCallback(
210     const std::string& guid) {
211   results_ = api::CreateNetwork::Results::Create(guid);
212   SendResponse(true);
213 }
214
215 ////////////////////////////////////////////////////////////////////////////////
216 // NetworkingPrivateGetVisibleNetworksFunction
217
218 NetworkingPrivateGetVisibleNetworksFunction::
219   ~NetworkingPrivateGetVisibleNetworksFunction() {
220 }
221
222 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() {
223   scoped_ptr<api::GetVisibleNetworks::Params> params =
224       api::GetVisibleNetworks::Params::Create(*args_);
225   EXTENSION_FUNCTION_VALIDATE(params);
226
227   NetworkingPrivateServiceClient* service_client =
228       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
229
230   service_client->GetVisibleNetworks(
231       api::GetVisibleNetworks::Params::ToString(params->type),
232       base::Bind(&NetworkingPrivateGetVisibleNetworksFunction::ResultCallback,
233                  this));
234
235   return true;
236 }
237
238 void NetworkingPrivateGetVisibleNetworksFunction::ResultCallback(
239     const base::ListValue& network_list) {
240   SetResult(network_list.DeepCopy());
241   SendResponse(true);
242 }
243
244 ////////////////////////////////////////////////////////////////////////////////
245 // NetworkingPrivateGetEnabledNetworkTypesFunction
246
247 NetworkingPrivateGetEnabledNetworkTypesFunction::
248 ~NetworkingPrivateGetEnabledNetworkTypesFunction() {
249 }
250
251 bool NetworkingPrivateGetEnabledNetworkTypesFunction::RunImpl() {
252   base::ListValue* network_list = new base::ListValue;
253   network_list->Append(new base::StringValue(onc::network_type::kWiFi));
254   SetResult(network_list);
255   return true;
256 }
257
258 ////////////////////////////////////////////////////////////////////////////////
259 // NetworkingPrivateEnableNetworkTypeFunction
260
261 NetworkingPrivateEnableNetworkTypeFunction::
262 ~NetworkingPrivateEnableNetworkTypeFunction() {
263 }
264
265 bool NetworkingPrivateEnableNetworkTypeFunction::RunImpl() {
266   scoped_ptr<api::EnableNetworkType::Params> params =
267       api::EnableNetworkType::Params::Create(*args_);
268   EXTENSION_FUNCTION_VALIDATE(params);
269   return true;
270 }
271
272 ////////////////////////////////////////////////////////////////////////////////
273 // NetworkingPrivateDisableNetworkTypeFunction
274
275 NetworkingPrivateDisableNetworkTypeFunction::
276 ~NetworkingPrivateDisableNetworkTypeFunction() {
277 }
278
279 bool NetworkingPrivateDisableNetworkTypeFunction::RunImpl() {
280   scoped_ptr<api::DisableNetworkType::Params> params =
281       api::DisableNetworkType::Params::Create(*args_);
282   EXTENSION_FUNCTION_VALIDATE(params);
283   return true;
284 }
285
286 ////////////////////////////////////////////////////////////////////////////////
287 // NetworkingPrivateRequestNetworkScanFunction
288
289 NetworkingPrivateRequestNetworkScanFunction::
290   ~NetworkingPrivateRequestNetworkScanFunction() {
291 }
292
293 bool NetworkingPrivateRequestNetworkScanFunction::RunImpl() {
294   NetworkingPrivateServiceClient* service_client =
295       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
296   service_client->RequestNetworkScan();
297   return true;
298 }
299
300 ////////////////////////////////////////////////////////////////////////////////
301 // NetworkingPrivateStartConnectFunction
302
303 NetworkingPrivateStartConnectFunction::
304   ~NetworkingPrivateStartConnectFunction() {
305 }
306
307 bool NetworkingPrivateStartConnectFunction::RunImpl() {
308   scoped_ptr<api::StartConnect::Params> params =
309       api::StartConnect::Params::Create(*args_);
310   EXTENSION_FUNCTION_VALIDATE(params);
311   NetworkingPrivateServiceClient* service_client =
312       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
313   service_client->StartConnect(
314       params->network_guid,
315       base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartSuccess,
316                  this),
317       base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartFailed,
318                  this));
319   return true;
320 }
321
322 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() {
323   SendResponse(true);
324 }
325
326 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed(
327     const std::string& error_name,
328     const scoped_ptr<base::DictionaryValue> error_data) {
329   error_ = error_name;
330   SendResponse(false);
331 }
332
333 ////////////////////////////////////////////////////////////////////////////////
334 // NetworkingPrivateStartDisconnectFunction
335
336 NetworkingPrivateStartDisconnectFunction::
337   ~NetworkingPrivateStartDisconnectFunction() {
338 }
339
340 bool NetworkingPrivateStartDisconnectFunction::RunImpl() {
341   scoped_ptr<api::StartDisconnect::Params> params =
342       api::StartDisconnect::Params::Create(*args_);
343   EXTENSION_FUNCTION_VALIDATE(params);
344   NetworkingPrivateServiceClient* service_client =
345       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
346   service_client->StartDisconnect(
347       params->network_guid,
348       base::Bind(
349           &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess,
350           this),
351       base::Bind(
352           &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed,
353           this));
354   return true;
355 }
356
357 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() {
358   SendResponse(true);
359 }
360
361 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed(
362     const std::string& error_name,
363     const scoped_ptr<base::DictionaryValue> error_data) {
364   error_ = error_name;
365   SendResponse(false);
366 }
367
368 ////////////////////////////////////////////////////////////////////////////////
369 // NetworkingPrivateVerifyDestinationFunction
370
371 NetworkingPrivateVerifyDestinationFunction::
372     ~NetworkingPrivateVerifyDestinationFunction() {}
373
374 bool NetworkingPrivateVerifyDestinationFunction::RunImpl() {
375   scoped_ptr<api::VerifyDestination::Params> params =
376       api::VerifyDestination::Params::Create(*args_);
377   EXTENSION_FUNCTION_VALIDATE(params);
378   NetworkingPrivateServiceClient* service_client =
379       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
380   service_client->VerifyDestination(
381       args_.Pass(),
382       base::Bind(
383           &NetworkingPrivateVerifyDestinationFunction::ResultCallback,
384           this),
385       base::Bind(
386           &NetworkingPrivateVerifyDestinationFunction::ErrorCallback,
387           this));
388   return true;
389 }
390
391 void NetworkingPrivateVerifyDestinationFunction::ResultCallback(bool result) {
392   SetResult(new base::FundamentalValue(result));
393   SendResponse(true);
394 }
395
396 void NetworkingPrivateVerifyDestinationFunction::ErrorCallback(
397     const std::string& error_name, const std::string& error) {
398   error_ = error_name;
399   SendResponse(false);
400 }
401
402 ////////////////////////////////////////////////////////////////////////////////
403 // NetworkingPrivateVerifyAndEncryptCredentialsFunction
404
405 NetworkingPrivateVerifyAndEncryptCredentialsFunction::
406   ~NetworkingPrivateVerifyAndEncryptCredentialsFunction() {
407 }
408
409 bool NetworkingPrivateVerifyAndEncryptCredentialsFunction::RunImpl() {
410   scoped_ptr<api::VerifyAndEncryptCredentials::Params> params =
411       api::VerifyAndEncryptCredentials::Params::Create(*args_);
412   EXTENSION_FUNCTION_VALIDATE(params);
413   SetResult(new base::StringValue("encrypted_credentials"));
414   SendResponse(true);
415   return true;
416 }
417
418 ////////////////////////////////////////////////////////////////////////////////
419 // NetworkingPrivateVerifyAndEncryptDataFunction
420
421 NetworkingPrivateVerifyAndEncryptDataFunction::
422   ~NetworkingPrivateVerifyAndEncryptDataFunction() {
423 }
424
425 bool NetworkingPrivateVerifyAndEncryptDataFunction::RunImpl() {
426   scoped_ptr<api::VerifyAndEncryptData::Params> params =
427       api::VerifyAndEncryptData::Params::Create(*args_);
428   EXTENSION_FUNCTION_VALIDATE(params);
429   NetworkingPrivateServiceClient* service_client =
430       NetworkingPrivateServiceClientFactory::GetForProfile(GetProfile());
431   service_client->VerifyAndEncryptData(
432       args_.Pass(),
433       base::Bind(
434           &NetworkingPrivateVerifyAndEncryptDataFunction::ResultCallback,
435           this),
436       base::Bind(
437           &NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback,
438           this));
439   return true;
440 }
441
442 void NetworkingPrivateVerifyAndEncryptDataFunction::ResultCallback(
443     const std::string& result) {
444   SetResult(new base::StringValue(result));
445   SendResponse(true);
446 }
447
448 void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback(
449     const std::string& error_name, const std::string& error) {
450   error_ = error_name;
451   SendResponse(false);
452 }
453
454 ////////////////////////////////////////////////////////////////////////////////
455 // NetworkingPrivateSetWifiTDLSEnabledStateFunction
456
457 NetworkingPrivateSetWifiTDLSEnabledStateFunction::
458   ~NetworkingPrivateSetWifiTDLSEnabledStateFunction() {
459 }
460
461 bool NetworkingPrivateSetWifiTDLSEnabledStateFunction::RunImpl() {
462   scoped_ptr<api::SetWifiTDLSEnabledState::Params> params =
463       api::SetWifiTDLSEnabledState::Params::Create(*args_);
464   EXTENSION_FUNCTION_VALIDATE(params);
465   SetError("not-implemented");
466   return false;
467 }
468
469 ////////////////////////////////////////////////////////////////////////////////
470 // NetworkingPrivateGetWifiTDLSStatusFunction
471
472 NetworkingPrivateGetWifiTDLSStatusFunction::
473   ~NetworkingPrivateGetWifiTDLSStatusFunction() {
474 }
475
476 bool NetworkingPrivateGetWifiTDLSStatusFunction::RunImpl() {
477   scoped_ptr<api::GetWifiTDLSStatus::Params> params =
478       api::GetWifiTDLSStatus::Params::Create(*args_);
479   EXTENSION_FUNCTION_VALIDATE(params);
480   SetError("not-implemented");
481   return false;
482 }