Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / networking_private / networking_private_api_chromeos.cc
index 3742a09..685290e 100644 (file)
 #include "base/command_line.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/browser_process_platform_part_chromeos.h"
+#include "chrome/browser/chromeos/net/network_portal_detector.h"
 #include "chrome/browser/chromeos/profiles/profile_helper.h"
-#include "chrome/browser/extensions/extension_function_registry.h"
 #include "chrome/common/chrome_switches.h"
 #include "chrome/common/extensions/api/networking_private.h"
 #include "chromeos/dbus/dbus_thread_manager.h"
 #include "chromeos/dbus/shill_manager_client.h"
 #include "chromeos/network/managed_network_configuration_handler.h"
 #include "chromeos/network/network_connection_handler.h"
+#include "chromeos/network/network_device_handler.h"
 #include "chromeos/network/network_state.h"
 #include "chromeos/network/network_state_handler.h"
 #include "chromeos/network/onc/onc_signature.h"
 #include "chromeos/network/onc/onc_translator.h"
+#include "chromeos/network/onc/onc_utils.h"
 #include "chromeos/network/shill_property_util.h"
 #include "components/onc/onc_constants.h"
+#include "extensions/browser/extension_function_registry.h"
 
 namespace api = extensions::api::networking_private;
 
 using chromeos::DBusThreadManager;
 using chromeos::ManagedNetworkConfigurationHandler;
 using chromeos::NetworkHandler;
+using chromeos::NetworkPortalDetector;
 using chromeos::NetworkState;
 using chromeos::NetworkStateHandler;
 using chromeos::NetworkTypePattern;
@@ -76,7 +80,7 @@ NetworkingPrivateGetPropertiesFunction::
   ~NetworkingPrivateGetPropertiesFunction() {
 }
 
-bool NetworkingPrivateGetPropertiesFunction::RunImpl() {
+bool NetworkingPrivateGetPropertiesFunction::RunAsync() {
   scoped_ptr<api::GetProperties::Params> params =
       api::GetProperties::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -114,7 +118,7 @@ NetworkingPrivateGetManagedPropertiesFunction::
   ~NetworkingPrivateGetManagedPropertiesFunction() {
 }
 
-bool NetworkingPrivateGetManagedPropertiesFunction::RunImpl() {
+bool NetworkingPrivateGetManagedPropertiesFunction::RunAsync() {
   scoped_ptr<api::GetManagedProperties::Params> params =
       api::GetManagedProperties::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -156,7 +160,7 @@ NetworkingPrivateGetStateFunction::
   ~NetworkingPrivateGetStateFunction() {
 }
 
-bool NetworkingPrivateGetStateFunction::RunImpl() {
+bool NetworkingPrivateGetStateFunction::RunAsync() {
   scoped_ptr<api::GetState::Params> params =
       api::GetState::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -171,7 +175,7 @@ bool NetworkingPrivateGetStateFunction::RunImpl() {
   }
 
   scoped_ptr<base::DictionaryValue> result_dict(new base::DictionaryValue);
-  state->GetProperties(result_dict.get());
+  state->GetStateProperties(result_dict.get());
   scoped_ptr<base::DictionaryValue> onc_network_part =
       chromeos::onc::TranslateShillServiceToONCPart(*result_dict,
           &chromeos::onc::kNetworkWithStateSignature);
@@ -188,7 +192,7 @@ NetworkingPrivateSetPropertiesFunction::
 ~NetworkingPrivateSetPropertiesFunction() {
 }
 
-bool NetworkingPrivateSetPropertiesFunction::RunImpl() {
+bool NetworkingPrivateSetPropertiesFunction::RunAsync() {
   scoped_ptr<api::SetProperties::Params> params =
       api::SetProperties::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -224,7 +228,7 @@ NetworkingPrivateCreateNetworkFunction::
 ~NetworkingPrivateCreateNetworkFunction() {
 }
 
-bool NetworkingPrivateCreateNetworkFunction::RunImpl() {
+bool NetworkingPrivateCreateNetworkFunction::RunAsync() {
   scoped_ptr<api::CreateNetwork::Params> params =
       api::CreateNetwork::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -267,39 +271,31 @@ NetworkingPrivateGetVisibleNetworksFunction::
 ~NetworkingPrivateGetVisibleNetworksFunction() {
 }
 
-bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() {
+bool NetworkingPrivateGetVisibleNetworksFunction::RunAsync() {
   scoped_ptr<api::GetVisibleNetworks::Params> params =
       api::GetVisibleNetworks::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
-  std::string type_filter =
-      api::GetVisibleNetworks::Params::ToString(params->type);
+  NetworkTypePattern type = chromeos::onc::NetworkTypePatternFromOncType(
+      api::GetVisibleNetworks::Params::ToString(params->type));
 
   NetworkStateHandler::NetworkStateList network_states;
-  NetworkHandler::Get()->network_state_handler()->GetNetworkList(
-      &network_states);
+  NetworkHandler::Get()->network_state_handler()->GetNetworkListByType(
+      type, &network_states);
 
   base::ListValue* network_properties_list = new base::ListValue;
   for (NetworkStateHandler::NetworkStateList::iterator it =
            network_states.begin();
        it != network_states.end(); ++it) {
-    const std::string& service_path = (*it)->path();
     base::DictionaryValue shill_dictionary;
-    (*it)->GetProperties(&shill_dictionary);
+    (*it)->GetStateProperties(&shill_dictionary);
 
     scoped_ptr<base::DictionaryValue> onc_network_part =
-        chromeos::onc::TranslateShillServiceToONCPart(shill_dictionary,
-            &chromeos::onc::kNetworkWithStateSignature);
-
-    std::string onc_type;
-    onc_network_part->GetStringWithoutPathExpansion(onc::network_config::kType,
-                                                    &onc_type);
-    if (type_filter == onc::network_type::kAllTypes ||
-        onc_type == type_filter) {
-      onc_network_part->SetStringWithoutPathExpansion(
-          onc::network_config::kGUID,
-          service_path);
-      network_properties_list->Append(onc_network_part.release());
-    }
+        chromeos::onc::TranslateShillServiceToONCPart(
+            shill_dictionary, &chromeos::onc::kNetworkWithStateSignature);
+    // TODO(stevenjb): Fix this to always use GUID: crbug.com/284827
+    onc_network_part->SetStringWithoutPathExpansion(
+        onc::network_config::kGUID, (*it)->path());
+    network_properties_list->Append(onc_network_part.release());
   }
 
   SetResult(network_properties_list);
@@ -314,7 +310,7 @@ NetworkingPrivateGetEnabledNetworkTypesFunction::
 ~NetworkingPrivateGetEnabledNetworkTypesFunction() {
 }
 
-bool NetworkingPrivateGetEnabledNetworkTypesFunction::RunImpl() {
+bool NetworkingPrivateGetEnabledNetworkTypesFunction::RunSync() {
   NetworkStateHandler* state_handler =
       NetworkHandler::Get()->network_state_handler();
 
@@ -338,7 +334,7 @@ NetworkingPrivateEnableNetworkTypeFunction::
 ~NetworkingPrivateEnableNetworkTypeFunction() {
 }
 
-bool NetworkingPrivateEnableNetworkTypeFunction::RunImpl() {
+bool NetworkingPrivateEnableNetworkTypeFunction::RunSync() {
   scoped_ptr<api::EnableNetworkType::Params> params =
       api::EnableNetworkType::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -377,7 +373,7 @@ NetworkingPrivateDisableNetworkTypeFunction::
 ~NetworkingPrivateDisableNetworkTypeFunction() {
 }
 
-bool NetworkingPrivateDisableNetworkTypeFunction::RunImpl() {
+bool NetworkingPrivateDisableNetworkTypeFunction::RunSync() {
   scoped_ptr<api::DisableNetworkType::Params> params =
       api::DisableNetworkType::Params::Create(*args_);
   NetworkStateHandler* state_handler =
@@ -416,7 +412,7 @@ NetworkingPrivateRequestNetworkScanFunction::
 ~NetworkingPrivateRequestNetworkScanFunction() {
 }
 
-bool NetworkingPrivateRequestNetworkScanFunction::RunImpl() {
+bool NetworkingPrivateRequestNetworkScanFunction::RunSync() {
   NetworkHandler::Get()->network_state_handler()->RequestScan();
   return true;
 }
@@ -439,7 +435,7 @@ void NetworkingPrivateStartConnectFunction::ConnectionStartFailed(
   SendResponse(false);
 }
 
-bool NetworkingPrivateStartConnectFunction::RunImpl() {
+bool NetworkingPrivateStartConnectFunction::RunAsync() {
   scoped_ptr<api::StartConnect::Params> params =
       api::StartConnect::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -475,7 +471,7 @@ void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed(
   SendResponse(false);
 }
 
-bool NetworkingPrivateStartDisconnectFunction::RunImpl() {
+bool NetworkingPrivateStartDisconnectFunction::RunAsync() {
   scoped_ptr<api::StartDisconnect::Params> params =
       api::StartDisconnect::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -498,7 +494,7 @@ NetworkingPrivateVerifyDestinationFunction::
   ~NetworkingPrivateVerifyDestinationFunction() {
 }
 
-bool NetworkingPrivateVerifyDestinationFunction::RunImpl() {
+bool NetworkingPrivateVerifyDestinationFunction::RunAsync() {
   scoped_ptr<api::VerifyDestination::Params> params =
       api::VerifyDestination::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -536,7 +532,7 @@ NetworkingPrivateVerifyAndEncryptCredentialsFunction::
   ~NetworkingPrivateVerifyAndEncryptCredentialsFunction() {
 }
 
-bool NetworkingPrivateVerifyAndEncryptCredentialsFunction::RunImpl() {
+bool NetworkingPrivateVerifyAndEncryptCredentialsFunction::RunAsync() {
   scoped_ptr<api::VerifyAndEncryptCredentials::Params> params =
       api::VerifyAndEncryptCredentials::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -577,7 +573,7 @@ NetworkingPrivateVerifyAndEncryptDataFunction::
   ~NetworkingPrivateVerifyAndEncryptDataFunction() {
 }
 
-bool NetworkingPrivateVerifyAndEncryptDataFunction::RunImpl() {
+bool NetworkingPrivateVerifyAndEncryptDataFunction::RunAsync() {
   scoped_ptr<api::VerifyAndEncryptData::Params> params =
       api::VerifyAndEncryptData::Params::Create(*args_);
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -604,7 +600,112 @@ void NetworkingPrivateVerifyAndEncryptDataFunction::ResultCallback(
 }
 
 void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback(
-    const std::string& error_name, const std::string& error) {
+    const std::string& error_name,
+    const std::string& error) {
   error_ = error_name;
   SendResponse(false);
 }
+
+////////////////////////////////////////////////////////////////////////////////
+// NetworkingPrivateSetWifiTDLSEnabledStateFunction
+
+NetworkingPrivateSetWifiTDLSEnabledStateFunction::
+  ~NetworkingPrivateSetWifiTDLSEnabledStateFunction() {
+}
+
+bool NetworkingPrivateSetWifiTDLSEnabledStateFunction::RunAsync() {
+  scoped_ptr<api::SetWifiTDLSEnabledState::Params> params =
+      api::SetWifiTDLSEnabledState::Params::Create(*args_);
+  EXTENSION_FUNCTION_VALIDATE(params);
+
+  std::string ip_or_mac_address = params->ip_or_mac_address;
+  bool enable = params->enabled;
+
+  NetworkHandler::Get()->network_device_handler()->
+      SetWifiTDLSEnabled(
+          ip_or_mac_address,
+          enable,
+          base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success,
+                     this),
+          base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure,
+                     this));
+
+  return true;
+}
+
+void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success(
+    const std::string& result) {
+  results_ = api::SetWifiTDLSEnabledState::Results::Create(result);
+  SendResponse(true);
+}
+
+void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure(
+    const std::string& error_name,
+    scoped_ptr<base::DictionaryValue> error_data) {
+  error_ = error_name;
+  SendResponse(false);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// NetworkingPrivateGetWifiTDLSStatusFunction
+
+NetworkingPrivateGetWifiTDLSStatusFunction::
+  ~NetworkingPrivateGetWifiTDLSStatusFunction() {
+}
+
+bool NetworkingPrivateGetWifiTDLSStatusFunction::RunAsync() {
+  scoped_ptr<api::GetWifiTDLSStatus::Params> params =
+      api::GetWifiTDLSStatus::Params::Create(*args_);
+  EXTENSION_FUNCTION_VALIDATE(params);
+
+  std::string ip_or_mac_address = params->ip_or_mac_address;
+
+  NetworkHandler::Get()->network_device_handler()->
+      GetWifiTDLSStatus(
+          ip_or_mac_address,
+          base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Success,
+                     this),
+          base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Failure,
+                     this));
+
+  return true;
+}
+
+void NetworkingPrivateGetWifiTDLSStatusFunction::Success(
+    const std::string& result) {
+  results_ = api::GetWifiTDLSStatus::Results::Create(result);
+  SendResponse(true);
+}
+
+void NetworkingPrivateGetWifiTDLSStatusFunction::Failure(
+    const std::string& error_name,
+    scoped_ptr<base::DictionaryValue> error_data) {
+  error_ = error_name;
+  SendResponse(false);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// NetworkingPrivateGetCaptivePortalStatusFunction
+
+NetworkingPrivateGetCaptivePortalStatusFunction::
+    ~NetworkingPrivateGetCaptivePortalStatusFunction() {}
+
+bool NetworkingPrivateGetCaptivePortalStatusFunction::RunAsync() {
+  scoped_ptr<api::GetCaptivePortalStatus::Params> params =
+      api::GetCaptivePortalStatus::Params::Create(*args_);
+  EXTENSION_FUNCTION_VALIDATE(params);
+
+  NetworkPortalDetector* detector = NetworkPortalDetector::Get();
+  if (!detector) {
+    error_ = "Error.NotReady";
+    return false;
+  }
+
+  NetworkPortalDetector::CaptivePortalState state =
+      detector->GetCaptivePortalState(params->network_path);
+
+  SetResult(new base::StringValue(
+      NetworkPortalDetector::CaptivePortalStatusString(state.status)));
+  SendResponse(true);
+  return true;
+}