Index: chrome/browser/extensions/api/networking_private/networking_private_api_nonchromeos.cc |
diff --git a/chrome/browser/extensions/api/networking_private/networking_private_api_nonchromeos.cc b/chrome/browser/extensions/api/networking_private/networking_private_api_nonchromeos.cc |
index 467b9b746b316541b1816a2d4583b2f7542c73a7..8d761e94330b8822bda7196f133a8ea563ad8972 100644 |
--- a/chrome/browser/extensions/api/networking_private/networking_private_api_nonchromeos.cc |
+++ b/chrome/browser/extensions/api/networking_private/networking_private_api_nonchromeos.cc |
@@ -9,30 +9,22 @@ |
#include "base/callback.h" |
#include "base/command_line.h" |
#include "base/json/json_reader.h" |
+#include "base/strings/stringprintf.h" |
#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/extensions/api/networking_private/networking_private_process_client.h" |
#include "chrome/browser/extensions/event_router.h" |
#include "chrome/browser/extensions/extension_function_registry.h" |
#include "chrome/browser/extensions/extension_system.h" |
-#include "chrome/browser/profiles/profile.h" |
#include "chrome/common/chrome_switches.h" |
#include "chrome/common/extensions/api/networking_private.h" |
+#include "components/onc/onc_constants.h" |
-using extensions::EventRouter; |
-using extensions::ExtensionSystem; |
+using extensions::NetworkingPrivateProcessClient; |
namespace api = extensions::api::networking_private; |
//////////////////////////////////////////////////////////////////////////////// |
// NetworkingPrivateGetPropertiesFunction |
- |
-const char kNetworkingPrivateProperties[] = "NetworkingPrivateProperties"; |
- |
-struct NetworkingPrivatePropertiesData : base::SupportsUserData::Data { |
- explicit NetworkingPrivatePropertiesData(const base::DictionaryValue* prop) : |
- properties_(prop->DeepCopy()) { } |
- scoped_ptr<base::DictionaryValue> properties_; |
-}; |
- |
NetworkingPrivateGetPropertiesFunction:: |
~NetworkingPrivateGetPropertiesFunction() { |
} |
@@ -42,38 +34,32 @@ bool NetworkingPrivateGetPropertiesFunction::RunImpl() { |
api::GetProperties::Params::Create(*args_); |
EXTENSION_FUNCTION_VALIDATE(params); |
- // If there are properties set by SetProperties function, use those. |
- NetworkingPrivatePropertiesData* stored_properties = |
- static_cast<NetworkingPrivatePropertiesData*> ( |
- profile()->GetUserData(kNetworkingPrivateProperties)); |
- if (stored_properties != NULL) { |
- SetResult(stored_properties->properties_.release()); |
- SendResponse(true); |
- return true; |
- } |
+ scoped_refptr<NetworkingPrivateProcessClient> process_client( |
+ NetworkingPrivateProcessClient::GetForProfile(profile_)); |
- const std::string network_properties = |
- "{\"ConnectionState\":\"NotConnected\"," |
- "\"GUID\":\"stub_wifi2\"," |
- "\"Name\":\"wifi2_PSK\"," |
- "\"Type\":\"WiFi\"," |
- "\"WiFi\":{" |
- "\"Frequency\":5000," |
- "\"FrequencyList\":[2400,5000]," |
- "\"SSID\":\"wifi2_PSK\"," |
- "\"Security\":\"WPA-PSK\"," |
- "\"SignalStrength\":80}}"; |
- |
- if (params->network_guid == "nonexistent_path") { |
- error_ = "Error.DBusFailed"; |
- SendResponse(false); |
- } else { |
- SetResult(base::JSONReader::Read(network_properties)); |
- SendResponse(true); |
- } |
+ process_client->GetProperties( |
+ params->network_guid, |
+ base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess, |
+ this), |
+ base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed, |
+ this)); |
return true; |
} |
+void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess( |
+ const std::string& network_guid, |
+ const base::DictionaryValue& dictionary) { |
+ SetResult(dictionary.DeepCopy()); |
+ SendResponse(true); |
+} |
+ |
+void NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed( |
+ const std::string& error_name, |
+ scoped_ptr<base::DictionaryValue> error_data) { |
+ error_ = error_name; |
+ SendResponse(false); |
+} |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// NetworkingPrivateGetManagedPropertiesFunction |
@@ -184,13 +170,28 @@ bool NetworkingPrivateSetPropertiesFunction::RunImpl() { |
scoped_ptr<base::DictionaryValue> properties_dict( |
params->properties.ToValue()); |
- // Store properties_dict in profile to return from GetProperties. |
- profile()->SetUserData(kNetworkingPrivateProperties, |
- new NetworkingPrivatePropertiesData(properties_dict.get())); |
- SendResponse(true); |
+ scoped_refptr<NetworkingPrivateProcessClient> process_client( |
+ NetworkingPrivateProcessClient::GetForProfile(profile_)); |
+ |
+ process_client->SetProperties( |
+ params->network_guid, |
+ *properties_dict, |
+ base::Bind(&NetworkingPrivateSetPropertiesFunction::ResultCallback, this), |
+ base::Bind(&NetworkingPrivateSetPropertiesFunction::ErrorCallback, this)); |
return true; |
} |
+void NetworkingPrivateSetPropertiesFunction::ResultCallback() { |
+ SendResponse(true); |
+} |
+ |
+void NetworkingPrivateSetPropertiesFunction::ErrorCallback( |
+ const std::string& error_name, |
+ const scoped_ptr<base::DictionaryValue> error_data) { |
+ error_ = error_name; |
+ SendResponse(false); |
+} |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// NetworkingPrivateCreateNetworkFunction |
@@ -203,14 +204,6 @@ bool NetworkingPrivateCreateNetworkFunction::RunImpl() { |
api::CreateNetwork::Params::Create(*args_); |
EXTENSION_FUNCTION_VALIDATE(params); |
- // Store properties_dict in profile to return from GetProperties. |
- scoped_ptr<base::DictionaryValue> properties_dict( |
- params->properties.ToValue()); |
- properties_dict->SetString("GUID", "fake_guid"); |
- profile()->SetUserData( |
- kNetworkingPrivateProperties, |
- new NetworkingPrivatePropertiesData(properties_dict.get())); |
- |
results_ = api::CreateNetwork::Results::Create("fake_guid"); |
SendResponse(true); |
return true; |
@@ -227,66 +220,46 @@ bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() { |
scoped_ptr<api::GetVisibleNetworks::Params> params = |
api::GetVisibleNetworks::Params::Create(*args_); |
EXTENSION_FUNCTION_VALIDATE(params); |
- const std::string networks_json = |
- "[{" |
- " \"ConnectionState\": \"Connected\"," |
- " \"GUID\": \"stub_ethernet\"," |
- " \"Name\": \"eth0\"," |
- " \"Type\": \"Ethernet\"," |
- " \"Ethernet\": {" |
- " \"Authentication\": \"None\"" |
- " }" |
- " }," |
- " {" |
- " \"ConnectionState\": \"Connected\"," |
- " \"GUID\": \"stub_wifi1\"," |
- " \"Name\": \"wifi1\"," |
- " \"Type\": \"WiFi\"," |
- " \"WiFi\": {" |
- " \"Security\": \"WEP-PSK\"," |
- " \"SignalStrength\": 0" |
- " }" |
- " }," |
- " {" |
- " \"ConnectionState\": \"Connected\"," |
- " \"GUID\": \"stub_vpn1\"," |
- " \"Name\": \"vpn1\"," |
- " \"Type\": \"VPN\"" |
- " }," |
- " {" |
- " \"ConnectionState\": \"NotConnected\"," |
- " \"GUID\": \"stub_wifi2\"," |
- " \"Name\": \"wifi2_PSK\"," |
- " \"Type\": \"WiFi\"," |
- " \"WiFi\": {" |
- " \"Security\": \"WPA-PSK\"," |
- " \"SignalStrength\": 80" |
- " }" |
- " }," |
- " {" |
- " \"Cellular\": {" |
- " \"ActivateOverNonCellularNetwork\": false," |
- " \"ActivationState\": \"not-activated\"," |
- " \"NetworkTechnology\": \"GSM\"," |
- " \"RoamingState\": \"home\"" |
- " }," |
- " \"ConnectionState\": \"NotConnected\"," |
- " \"GUID\": \"stub_cellular1\"," |
- " \"Name\": \"cellular1\"," |
- " \"Type\": \"Cellular\"" |
- " }]"; |
- ListValue* visible_networks = |
- static_cast<ListValue*>(base::JSONReader::Read(networks_json)); |
- // If caller only needs WiFi networks, then remove all other networks. |
- if (params->type == api::GetVisibleNetworks::Params::TYPE_WIFI) { |
- visible_networks->Remove(4, NULL); |
- visible_networks->Remove(2, NULL); |
- visible_networks->Remove(0, NULL); |
+ |
+ scoped_refptr<NetworkingPrivateProcessClient> process_client( |
+ NetworkingPrivateProcessClient::GetForProfile(profile_)); |
+ |
+ process_client->GetVisibleNetworks(base::Bind( |
+ &NetworkingPrivateGetVisibleNetworksFunction::ResultCallback, this)); |
+ |
+ return true; |
+} |
+ |
+void NetworkingPrivateGetVisibleNetworksFunction::ResultCallback( |
+ const base::ListValue& network_list) { |
+ scoped_ptr<api::GetVisibleNetworks::Params> params = |
+ api::GetVisibleNetworks::Params::Create(*args_); |
+ ListValue* result = new ListValue(); |
+ std::string params_type = |
+ api::GetVisibleNetworks::Params::ToString(params->type); |
+ bool request_all = params->type == api::GetVisibleNetworks::Params::TYPE_ALL; |
+ |
+ // Copy networks of requested type; |
+ for (base::ListValue::const_iterator it = network_list.begin(); |
+ it != network_list.end(); |
+ ++it) { |
+ const base::Value* network_value = *it; |
+ const DictionaryValue* network_dict = NULL; |
+ if (!network_value->GetAsDictionary(&network_dict)) { |
+ LOG(ERROR) << "Value is not a dictionary"; |
+ continue; |
+ } |
+ if (!request_all) { |
+ std::string network_type; |
+ network_dict->GetString(onc::network_config::kType, &network_type); |
+ if (network_type != params_type) |
+ continue; |
+ } |
+ result->Append(network_value->DeepCopy()); |
} |
- SetResult(visible_networks); |
+ SetResult(result); |
SendResponse(true); |
- return true; |
} |
//////////////////////////////////////////////////////////////////////////////// |
@@ -343,20 +316,9 @@ NetworkingPrivateRequestNetworkScanFunction:: |
} |
bool NetworkingPrivateRequestNetworkScanFunction::RunImpl() { |
- // Generate onNetworkListChanged event. |
- std::vector<std::string> changes; |
- changes.push_back("stub_ethernet"); |
- changes.push_back("stub_wifi1"); |
- changes.push_back("stub_vpn1"); |
- changes.push_back("stub_wifi2"); |
- changes.push_back("stub_cellular1"); |
- |
- EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router(); |
- scoped_ptr<base::ListValue> args(api::OnNetworkListChanged::Create(changes)); |
- scoped_ptr<extensions::Event> extension_event(new extensions::Event( |
- api::OnNetworkListChanged::kEventName, args.Pass())); |
- event_router->BroadcastEvent(extension_event.Pass()); |
- |
+ scoped_refptr<NetworkingPrivateProcessClient> process_client( |
+ NetworkingPrivateProcessClient::GetForProfile(profile_)); |
+ process_client->RequestNetworkScan(); |
return true; |
} |
@@ -371,52 +333,28 @@ bool NetworkingPrivateStartConnectFunction::RunImpl() { |
scoped_ptr<api::StartConnect::Params> params = |
api::StartConnect::Params::Create(*args_); |
EXTENSION_FUNCTION_VALIDATE(params); |
- if (params->network_guid == "nonexistent_path") { |
- error_ = "configure-failed"; |
- SendResponse(false); |
- } else { |
- SendResponse(true); |
- // Set Properties to reflect connected state |
- const std::string network_properties = |
- "{\"ConnectionState\":\"Connected\"," |
- "\"GUID\":\"stub_wifi2\"," |
- "\"Name\":\"wifi2_PSK\"," |
- "\"Type\":\"WiFi\"," |
- "\"WiFi\":{" |
- "\"SSID\":\"stub_wifi2\"," |
- "\"Security\":\"WPA-PSK\"," |
- "\"SignalStrength\":80}}"; |
- |
- // Store network_properties in profile to return from GetProperties. |
- profile()->SetUserData(kNetworkingPrivateProperties, |
- new NetworkingPrivatePropertiesData( |
- static_cast<DictionaryValue*>( |
- base::JSONReader::Read(network_properties)))); |
- |
- // Broadcast NetworksChanged Event that network is connected |
- EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router(); |
- scoped_ptr<base::ListValue> args(api::OnNetworksChanged::Create( |
- std::vector<std::string>(1, params->network_guid))); |
- scoped_ptr<extensions::Event> netchanged_event( |
- new extensions::Event(api::OnNetworksChanged::kEventName, args.Pass())); |
- event_router->BroadcastEvent(netchanged_event.Pass()); |
- |
- // Generate NetworkListChanged event. |
- std::vector<std::string> list; |
- list.push_back("stub_ethernet"); |
- list.push_back("stub_wifi2"); |
- list.push_back("stub_vpn1"); |
- list.push_back("stub_wifi1"); |
- list.push_back("stub_cellular1"); |
- |
- scoped_ptr<base::ListValue> arg2(api::OnNetworkListChanged::Create(list)); |
- scoped_ptr<extensions::Event> netlist_event(new extensions::Event( |
- api::OnNetworkListChanged::kEventName, arg2.Pass())); |
- event_router->BroadcastEvent(netlist_event.Pass()); |
- } |
+ scoped_refptr<NetworkingPrivateProcessClient> process_client( |
+ NetworkingPrivateProcessClient::GetForProfile(profile_)); |
+ process_client->StartConnect( |
+ params->network_guid, |
+ base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartSuccess, |
+ this), |
+ base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartFailed, |
+ this)); |
return true; |
} |
+void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() { |
+ SendResponse(true); |
+} |
+ |
+void NetworkingPrivateStartConnectFunction::ConnectionStartFailed( |
+ const std::string& error_name, |
+ const scoped_ptr<base::DictionaryValue> error_data) { |
+ error_ = error_name; |
+ SendResponse(false); |
+} |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// NetworkingPrivateStartDisconnectFunction |
@@ -428,39 +366,64 @@ bool NetworkingPrivateStartDisconnectFunction::RunImpl() { |
scoped_ptr<api::StartDisconnect::Params> params = |
api::StartDisconnect::Params::Create(*args_); |
EXTENSION_FUNCTION_VALIDATE(params); |
- if (params->network_guid == "nonexistent_path") { |
- error_ = "not-found"; |
- SendResponse(false); |
- } else { |
- SendResponse(true); |
- |
- // Send Event that network is disconnected. Listener will use GetProperties. |
- EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router(); |
- scoped_ptr<base::ListValue> args(api::OnNetworksChanged::Create( |
- std::vector<std::string>(1, params->network_guid))); |
- scoped_ptr<extensions::Event> extension_event( |
- new extensions::Event(api::OnNetworksChanged::kEventName, args.Pass())); |
- event_router->BroadcastEvent(extension_event.Pass()); |
- } |
+ scoped_refptr<NetworkingPrivateProcessClient> process_client( |
+ NetworkingPrivateProcessClient::GetForProfile(profile_)); |
+ process_client->StartDisconnect( |
+ params->network_guid, |
+ base::Bind( |
+ &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess, |
+ this), |
+ base::Bind( |
+ &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed, |
+ this)); |
return true; |
} |
+void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() { |
+ SendResponse(true); |
+} |
+ |
+void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed( |
+ const std::string& error_name, |
+ const scoped_ptr<base::DictionaryValue> error_data) { |
+ error_ = error_name; |
+ SendResponse(false); |
+} |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// NetworkingPrivateVerifyDestinationFunction |
NetworkingPrivateVerifyDestinationFunction:: |
- ~NetworkingPrivateVerifyDestinationFunction() { |
-} |
+ ~NetworkingPrivateVerifyDestinationFunction() {} |
bool NetworkingPrivateVerifyDestinationFunction::RunImpl() { |
scoped_ptr<api::VerifyDestination::Params> params = |
api::VerifyDestination::Params::Create(*args_); |
EXTENSION_FUNCTION_VALIDATE(params); |
- SetResult(new base::FundamentalValue(true)); |
- SendResponse(true); |
+ scoped_refptr<NetworkingPrivateProcessClient> process_client( |
+ NetworkingPrivateProcessClient::GetForProfile(profile_)); |
+ process_client->VerifyDestination( |
+ args_.Pass(), |
+ base::Bind( |
+ &NetworkingPrivateVerifyDestinationFunction::ResultCallback, |
+ this), |
+ base::Bind( |
+ &NetworkingPrivateVerifyDestinationFunction::ErrorCallback, |
+ this)); |
return true; |
} |
+void NetworkingPrivateVerifyDestinationFunction::ResultCallback(bool result) { |
+ SetResult(new base::FundamentalValue(result)); |
+ SendResponse(true); |
+} |
+ |
+void NetworkingPrivateVerifyDestinationFunction::ErrorCallback( |
+ const std::string& error_name, const std::string& error) { |
+ error_ = error_name; |
+ SendResponse(false); |
+} |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// NetworkingPrivateVerifyAndEncryptCredentialsFunction |
@@ -488,7 +451,27 @@ bool NetworkingPrivateVerifyAndEncryptDataFunction::RunImpl() { |
scoped_ptr<api::VerifyAndEncryptData::Params> params = |
api::VerifyAndEncryptData::Params::Create(*args_); |
EXTENSION_FUNCTION_VALIDATE(params); |
- SetResult(new base::StringValue("encrypted_data")); |
- SendResponse(true); |
+ scoped_refptr<NetworkingPrivateProcessClient> process_client( |
+ NetworkingPrivateProcessClient::GetForProfile(profile_)); |
+ process_client->VerifyAndEncryptData( |
+ args_.Pass(), |
+ base::Bind( |
+ &NetworkingPrivateVerifyAndEncryptDataFunction::ResultCallback, |
+ this), |
+ base::Bind( |
+ &NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback, |
+ this)); |
return true; |
} |
+ |
+void NetworkingPrivateVerifyAndEncryptDataFunction::ResultCallback( |
+ const std::string& result) { |
+ SetResult(new base::StringValue(result)); |
+ SendResponse(true); |
+} |
+ |
+void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback( |
+ const std::string& error_name, const std::string& error) { |
+ error_ = error_name; |
+ SendResponse(false); |
+} |