- add sources.
[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/event_router.h"
14 #include "chrome/browser/extensions/extension_function_registry.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/extensions/api/networking_private.h"
19
20 using extensions::EventRouter;
21 using extensions::ExtensionSystem;
22 namespace api = extensions::api::networking_private;
23
24 ////////////////////////////////////////////////////////////////////////////////
25 // NetworkingPrivateGetPropertiesFunction
26
27
28 const char kNetworkingPrivateProperties[] = "NetworkingPrivateProperties";
29
30 struct NetworkingPrivatePropertiesData : base::SupportsUserData::Data {
31   explicit NetworkingPrivatePropertiesData(const base::DictionaryValue* prop) :
32     properties_(prop->DeepCopy()) { }
33   scoped_ptr<base::DictionaryValue> properties_;
34 };
35
36 NetworkingPrivateGetPropertiesFunction::
37   ~NetworkingPrivateGetPropertiesFunction() {
38 }
39
40 bool NetworkingPrivateGetPropertiesFunction::RunImpl() {
41   scoped_ptr<api::GetProperties::Params> params =
42       api::GetProperties::Params::Create(*args_);
43   EXTENSION_FUNCTION_VALIDATE(params);
44
45   // If there are properties set by SetProperties function, use those.
46   NetworkingPrivatePropertiesData* stored_properties =
47       static_cast<NetworkingPrivatePropertiesData*>(
48           GetProfile()->GetUserData(kNetworkingPrivateProperties));
49   if (stored_properties != NULL) {
50     SetResult(stored_properties->properties_.release());
51     SendResponse(true);
52     return true;
53   }
54
55   const std::string network_properties =
56     "{\"ConnectionState\":\"NotConnected\","
57      "\"GUID\":\"stub_wifi2\","
58      "\"Name\":\"wifi2_PSK\","
59      "\"Type\":\"WiFi\","
60      "\"WiFi\":{"
61        "\"Frequency\":5000,"
62        "\"FrequencyList\":[2400,5000],"
63        "\"SSID\":\"wifi2_PSK\","
64        "\"Security\":\"WPA-PSK\","
65        "\"SignalStrength\":80}}";
66
67   if (params->network_guid == "nonexistent_path") {
68     error_ = "Error.DBusFailed";
69     SendResponse(false);
70   } else {
71     SetResult(base::JSONReader::Read(network_properties));
72     SendResponse(true);
73   }
74   return true;
75 }
76
77 ////////////////////////////////////////////////////////////////////////////////
78 // NetworkingPrivateGetManagedPropertiesFunction
79
80 NetworkingPrivateGetManagedPropertiesFunction::
81   ~NetworkingPrivateGetManagedPropertiesFunction() {
82 }
83
84 bool NetworkingPrivateGetManagedPropertiesFunction::RunImpl() {
85   scoped_ptr<api::GetManagedProperties::Params> params =
86       api::GetManagedProperties::Params::Create(*args_);
87   EXTENSION_FUNCTION_VALIDATE(params);
88   const std::string network_properties =
89       "{"
90       "  \"ConnectionState\": {"
91       "    \"Active\": \"NotConnected\","
92       "    \"Effective\": \"Unmanaged\""
93       "  },"
94       "  \"GUID\": \"stub_wifi2\","
95       "  \"Name\": {"
96       "    \"Active\": \"wifi2_PSK\","
97       "    \"Effective\": \"UserPolicy\","
98       "    \"UserPolicy\": \"My WiFi Network\""
99       "  },"
100       "  \"Type\": {"
101       "    \"Active\": \"WiFi\","
102       "    \"Effective\": \"UserPolicy\","
103       "    \"UserPolicy\": \"WiFi\""
104       "  },"
105       "  \"WiFi\": {"
106       "    \"AutoConnect\": {"
107       "      \"Active\": false,"
108       "      \"UserEditable\": true"
109       "    },"
110       "    \"Frequency\" : {"
111       "      \"Active\": 5000,"
112       "      \"Effective\": \"Unmanaged\""
113       "    },"
114       "    \"FrequencyList\" : {"
115       "      \"Active\": [2400, 5000],"
116       "      \"Effective\": \"Unmanaged\""
117       "    },"
118       "    \"Passphrase\": {"
119       "      \"Effective\": \"UserSetting\","
120       "      \"UserEditable\": true,"
121       "      \"UserSetting\": \"FAKE_CREDENTIAL_VPaJDV9x\""
122       "    },"
123       "    \"SSID\": {"
124       "      \"Active\": \"wifi2_PSK\","
125       "      \"Effective\": \"UserPolicy\","
126       "      \"UserPolicy\": \"wifi2_PSK\""
127       "    },"
128       "    \"Security\": {"
129       "      \"Active\": \"WPA-PSK\","
130       "      \"Effective\": \"UserPolicy\","
131       "      \"UserPolicy\": \"WPA-PSK\""
132       "    },"
133       "    \"SignalStrength\": {"
134       "      \"Active\": 80,"
135       "      \"Effective\": \"Unmanaged\""
136       "    }"
137       "  }"
138       "}";
139
140   SetResult(base::JSONReader::Read(network_properties));
141   SendResponse(true);
142   return true;
143 }
144
145 ////////////////////////////////////////////////////////////////////////////////
146 // NetworkingPrivateGetStateFunction
147
148 NetworkingPrivateGetStateFunction::
149   ~NetworkingPrivateGetStateFunction() {
150 }
151
152 bool NetworkingPrivateGetStateFunction::RunImpl() {
153   scoped_ptr<api::GetState::Params> params =
154       api::GetState::Params::Create(*args_);
155   EXTENSION_FUNCTION_VALIDATE(params);
156   const std::string network_state =
157       "{"
158       "  \"ConnectionState\": \"NotConnected\","
159       "  \"GUID\": \"stub_wifi2\","
160       "  \"Name\": \"wifi2_PSK\","
161       "  \"Type\": \"WiFi\","
162       "  \"WiFi\": {"
163       "    \"Security\": \"WPA-PSK\","
164       "    \"SignalStrength\": 80"
165       "  }"
166       "}";
167   SetResult(base::JSONReader::Read(network_state));
168   SendResponse(true);
169   return true;
170 }
171
172
173 ////////////////////////////////////////////////////////////////////////////////
174 // NetworkingPrivateSetPropertiesFunction
175
176 NetworkingPrivateSetPropertiesFunction::
177   ~NetworkingPrivateSetPropertiesFunction() {
178 }
179
180 bool NetworkingPrivateSetPropertiesFunction::RunImpl() {
181   scoped_ptr<api::SetProperties::Params> params =
182       api::SetProperties::Params::Create(*args_);
183   EXTENSION_FUNCTION_VALIDATE(params);
184   scoped_ptr<base::DictionaryValue> properties_dict(
185       params->properties.ToValue());
186
187   // Store properties_dict in profile to return from GetProperties.
188   GetProfile()->SetUserData(
189       kNetworkingPrivateProperties,
190       new NetworkingPrivatePropertiesData(properties_dict.get()));
191   SendResponse(true);
192   return true;
193 }
194
195 ////////////////////////////////////////////////////////////////////////////////
196 // NetworkingPrivateCreateNetworkFunction
197
198 NetworkingPrivateCreateNetworkFunction::
199 ~NetworkingPrivateCreateNetworkFunction() {
200 }
201
202 bool NetworkingPrivateCreateNetworkFunction::RunImpl() {
203   scoped_ptr<api::CreateNetwork::Params> params =
204       api::CreateNetwork::Params::Create(*args_);
205   EXTENSION_FUNCTION_VALIDATE(params);
206
207   // Store properties_dict in profile to return from GetProperties.
208   scoped_ptr<base::DictionaryValue> properties_dict(
209       params->properties.ToValue());
210   properties_dict->SetString("GUID", "fake_guid");
211   GetProfile()->SetUserData(
212       kNetworkingPrivateProperties,
213       new NetworkingPrivatePropertiesData(properties_dict.get()));
214
215   results_ = api::CreateNetwork::Results::Create("fake_guid");
216   SendResponse(true);
217   return true;
218 }
219
220 ////////////////////////////////////////////////////////////////////////////////
221 // NetworkingPrivateGetVisibleNetworksFunction
222
223 NetworkingPrivateGetVisibleNetworksFunction::
224   ~NetworkingPrivateGetVisibleNetworksFunction() {
225 }
226
227 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() {
228   scoped_ptr<api::GetVisibleNetworks::Params> params =
229       api::GetVisibleNetworks::Params::Create(*args_);
230   EXTENSION_FUNCTION_VALIDATE(params);
231   const std::string networks_json =
232       "[{"
233       "    \"ConnectionState\": \"Connected\","
234       "    \"GUID\": \"stub_ethernet\","
235       "    \"Name\": \"eth0\","
236       "    \"Type\": \"Ethernet\","
237       "    \"Ethernet\": {"
238       "      \"Authentication\": \"None\""
239       "    }"
240       "  },"
241       "  {"
242       "    \"ConnectionState\": \"Connected\","
243       "    \"GUID\": \"stub_wifi1\","
244       "    \"Name\": \"wifi1\","
245       "    \"Type\": \"WiFi\","
246       "    \"WiFi\": {"
247       "      \"Security\": \"WEP-PSK\","
248       "      \"SignalStrength\": 0"
249       "    }"
250       "  },"
251       "  {"
252       "    \"ConnectionState\": \"Connected\","
253       "    \"GUID\": \"stub_vpn1\","
254       "    \"Name\": \"vpn1\","
255       "    \"Type\": \"VPN\""
256       "  },"
257       "  {"
258       "    \"ConnectionState\": \"NotConnected\","
259       "    \"GUID\": \"stub_wifi2\","
260       "    \"Name\": \"wifi2_PSK\","
261       "    \"Type\": \"WiFi\","
262       "    \"WiFi\": {"
263       "      \"Security\": \"WPA-PSK\","
264       "      \"SignalStrength\": 80"
265       "    }"
266       "  },"
267       "  {"
268       "    \"Cellular\": {"
269       "      \"ActivateOverNonCellularNetwork\": false,"
270       "      \"ActivationState\": \"not-activated\","
271       "      \"NetworkTechnology\": \"GSM\","
272       "      \"RoamingState\": \"home\""
273       "    },"
274       "    \"ConnectionState\": \"NotConnected\","
275       "    \"GUID\": \"stub_cellular1\","
276       "    \"Name\": \"cellular1\","
277       "    \"Type\": \"Cellular\""
278       "  }]";
279   ListValue* visible_networks =
280       static_cast<ListValue*>(base::JSONReader::Read(networks_json));
281   // If caller only needs WiFi networks, then remove all other networks.
282   if (params->type == api::GetVisibleNetworks::Params::TYPE_WIFI) {
283     visible_networks->Remove(4, NULL);
284     visible_networks->Remove(2, NULL);
285     visible_networks->Remove(0, NULL);
286   }
287
288   SetResult(visible_networks);
289   SendResponse(true);
290   return true;
291 }
292
293 ////////////////////////////////////////////////////////////////////////////////
294 // NetworkingPrivateGetEnabledNetworkTypesFunction
295
296 NetworkingPrivateGetEnabledNetworkTypesFunction::
297 ~NetworkingPrivateGetEnabledNetworkTypesFunction() {
298 }
299
300 bool NetworkingPrivateGetEnabledNetworkTypesFunction::RunImpl() {
301   base::ListValue* network_list = new base::ListValue;
302
303   network_list->Append(new base::StringValue("Ethernet"));
304   network_list->Append(new base::StringValue("WiFi"));
305   network_list->Append(new base::StringValue("Cellular"));
306
307   SetResult(network_list);
308   return true;
309 }
310
311 ////////////////////////////////////////////////////////////////////////////////
312 // NetworkingPrivateEnableNetworkTypeFunction
313
314 NetworkingPrivateEnableNetworkTypeFunction::
315 ~NetworkingPrivateEnableNetworkTypeFunction() {
316 }
317
318 bool NetworkingPrivateEnableNetworkTypeFunction::RunImpl() {
319   scoped_ptr<api::EnableNetworkType::Params> params =
320       api::EnableNetworkType::Params::Create(*args_);
321   EXTENSION_FUNCTION_VALIDATE(params);
322   return true;
323 }
324
325 ////////////////////////////////////////////////////////////////////////////////
326 // NetworkingPrivateDisableNetworkTypeFunction
327
328 NetworkingPrivateDisableNetworkTypeFunction::
329 ~NetworkingPrivateDisableNetworkTypeFunction() {
330 }
331
332 bool NetworkingPrivateDisableNetworkTypeFunction::RunImpl() {
333   scoped_ptr<api::DisableNetworkType::Params> params =
334       api::DisableNetworkType::Params::Create(*args_);
335   EXTENSION_FUNCTION_VALIDATE(params);
336   return true;
337 }
338
339 ////////////////////////////////////////////////////////////////////////////////
340 // NetworkingPrivateRequestNetworkScanFunction
341
342 NetworkingPrivateRequestNetworkScanFunction::
343   ~NetworkingPrivateRequestNetworkScanFunction() {
344 }
345
346 bool NetworkingPrivateRequestNetworkScanFunction::RunImpl() {
347   // Generate onNetworkListChanged event.
348   std::vector<std::string> changes;
349   changes.push_back("stub_ethernet");
350   changes.push_back("stub_wifi1");
351   changes.push_back("stub_vpn1");
352   changes.push_back("stub_wifi2");
353   changes.push_back("stub_cellular1");
354
355   EventRouter* event_router =
356       ExtensionSystem::Get(GetProfile())->event_router();
357   scoped_ptr<base::ListValue> args(api::OnNetworkListChanged::Create(changes));
358   scoped_ptr<extensions::Event> extension_event(new extensions::Event(
359       api::OnNetworkListChanged::kEventName, args.Pass()));
360   event_router->BroadcastEvent(extension_event.Pass());
361
362   return true;
363 }
364
365 ////////////////////////////////////////////////////////////////////////////////
366 // NetworkingPrivateStartConnectFunction
367
368 NetworkingPrivateStartConnectFunction::
369   ~NetworkingPrivateStartConnectFunction() {
370 }
371
372 bool NetworkingPrivateStartConnectFunction::RunImpl() {
373   scoped_ptr<api::StartConnect::Params> params =
374       api::StartConnect::Params::Create(*args_);
375   EXTENSION_FUNCTION_VALIDATE(params);
376   if (params->network_guid == "nonexistent_path") {
377     error_ = "configure-failed";
378     SendResponse(false);
379   } else {
380     SendResponse(true);
381     // Set Properties to reflect connected state
382     const std::string network_properties =
383       "{\"ConnectionState\":\"Connected\","
384        "\"GUID\":\"stub_wifi2\","
385        "\"Name\":\"wifi2_PSK\","
386        "\"Type\":\"WiFi\","
387        "\"WiFi\":{"
388          "\"SSID\":\"stub_wifi2\","
389          "\"Security\":\"WPA-PSK\","
390          "\"SignalStrength\":80}}";
391
392     // Store network_properties in profile to return from GetProperties.
393     scoped_ptr<Value> network_properties_value(
394         base::JSONReader::Read(network_properties));
395     GetProfile()->SetUserData(
396         kNetworkingPrivateProperties,
397         new NetworkingPrivatePropertiesData(
398             static_cast<DictionaryValue*>(network_properties_value.get())));
399
400     // Broadcast NetworksChanged Event that network is connected
401     EventRouter* event_router =
402         ExtensionSystem::Get(GetProfile())->event_router();
403     scoped_ptr<base::ListValue> args(api::OnNetworksChanged::Create(
404         std::vector<std::string>(1, params->network_guid)));
405     scoped_ptr<extensions::Event> netchanged_event(
406         new extensions::Event(api::OnNetworksChanged::kEventName, args.Pass()));
407     event_router->BroadcastEvent(netchanged_event.Pass());
408
409     // Generate NetworkListChanged event.
410     std::vector<std::string> list;
411     list.push_back("stub_ethernet");
412     list.push_back("stub_wifi2");
413     list.push_back("stub_vpn1");
414     list.push_back("stub_wifi1");
415     list.push_back("stub_cellular1");
416
417     scoped_ptr<base::ListValue> arg2(api::OnNetworkListChanged::Create(list));
418     scoped_ptr<extensions::Event> netlist_event(new extensions::Event(
419         api::OnNetworkListChanged::kEventName, arg2.Pass()));
420     event_router->BroadcastEvent(netlist_event.Pass());
421   }
422   return true;
423 }
424
425 ////////////////////////////////////////////////////////////////////////////////
426 // NetworkingPrivateStartDisconnectFunction
427
428 NetworkingPrivateStartDisconnectFunction::
429   ~NetworkingPrivateStartDisconnectFunction() {
430 }
431
432 bool NetworkingPrivateStartDisconnectFunction::RunImpl() {
433   scoped_ptr<api::StartDisconnect::Params> params =
434       api::StartDisconnect::Params::Create(*args_);
435   EXTENSION_FUNCTION_VALIDATE(params);
436   if (params->network_guid == "nonexistent_path") {
437     error_ = "not-found";
438     SendResponse(false);
439   } else {
440     SendResponse(true);
441
442     // Send Event that network is disconnected. Listener will use GetProperties.
443     EventRouter* event_router =
444         ExtensionSystem::Get(GetProfile())->event_router();
445     scoped_ptr<base::ListValue> args(api::OnNetworksChanged::Create(
446         std::vector<std::string>(1, params->network_guid)));
447     scoped_ptr<extensions::Event> extension_event(
448         new extensions::Event(api::OnNetworksChanged::kEventName, args.Pass()));
449     event_router->BroadcastEvent(extension_event.Pass());
450   }
451   return true;
452 }
453
454 ////////////////////////////////////////////////////////////////////////////////
455 // NetworkingPrivateVerifyDestinationFunction
456
457 NetworkingPrivateVerifyDestinationFunction::
458   ~NetworkingPrivateVerifyDestinationFunction() {
459 }
460
461 bool NetworkingPrivateVerifyDestinationFunction::RunImpl() {
462   scoped_ptr<api::VerifyDestination::Params> params =
463       api::VerifyDestination::Params::Create(*args_);
464   EXTENSION_FUNCTION_VALIDATE(params);
465   SetResult(new base::FundamentalValue(true));
466   SendResponse(true);
467   return true;
468 }
469
470 ////////////////////////////////////////////////////////////////////////////////
471 // NetworkingPrivateVerifyAndEncryptCredentialsFunction
472
473 NetworkingPrivateVerifyAndEncryptCredentialsFunction::
474   ~NetworkingPrivateVerifyAndEncryptCredentialsFunction() {
475 }
476
477 bool NetworkingPrivateVerifyAndEncryptCredentialsFunction::RunImpl() {
478   scoped_ptr<api::VerifyAndEncryptCredentials::Params> params =
479       api::VerifyAndEncryptCredentials::Params::Create(*args_);
480   EXTENSION_FUNCTION_VALIDATE(params);
481   SetResult(new base::StringValue("encrypted_credentials"));
482   SendResponse(true);
483   return true;
484 }
485
486 ////////////////////////////////////////////////////////////////////////////////
487 // NetworkingPrivateVerifyAndEncryptDataFunction
488
489 NetworkingPrivateVerifyAndEncryptDataFunction::
490   ~NetworkingPrivateVerifyAndEncryptDataFunction() {
491 }
492
493 bool NetworkingPrivateVerifyAndEncryptDataFunction::RunImpl() {
494   scoped_ptr<api::VerifyAndEncryptData::Params> params =
495       api::VerifyAndEncryptData::Params::Create(*args_);
496   EXTENSION_FUNCTION_VALIDATE(params);
497   SetResult(new base::StringValue("encrypted_data"));
498   SendResponse(true);
499   return true;
500 }