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

Unified Diff: chrome/browser/extensions/api/networking_private/networking_private_api_nonchromeos.cc

Issue 22295002: Base infrastructure for Networking Private API on Windows and Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reset DHCP after WiFi Connect to speed up the connection after factory reset. Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
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 d7a4884164351078cb40dbcff87166c2a730b8fa..c2db662bdd875fd97a85ce4a5a941201f77ae99e 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
@@ -4,16 +4,19 @@
#include "chrome/browser/extensions/api/networking_private/networking_private_api.h"
+#include "base/base64.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#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_crypto.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"
@@ -21,18 +24,26 @@ using extensions::EventRouter;
using extensions::ExtensionSystem;
namespace api = extensions::api::networking_private;
+// Network Configuration
+namespace network_config {
+const char kCellular[] = "Cellular";
+const char kEthernet[] = "Ethernet";
+const char kGUID[] = "GUID";
+const char kIPConfigs[] = "IPConfigs";
+const char kName[] = "Name";
+const char kNameServers[] = "NameServers";
+const char kProxySettings[] = "ProxySettings";
+const char kSearchDomains[] = "SearchDomains";
+const char kServicePath[] = "ServicePath";
+const char kConnectionState[] = "ConnectionState";
+const char kType[] = "Type";
+const char kVPN[] = "VPN";
+const char kWiFi[] = "WiFi";
+} // namespace network_config
+
////////////////////////////////////////////////////////////////////////////////
// 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 +53,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::GetProcessClientForProfile(profile_));
- const std::string network_properties =
- "{\"ConnectionState\":\"NotConnected\","
- "\"GUID\":\"stub_wifi2\","
- "\"Name\":\"wifi2_PSK\","
- "\"Type\":\"WiFi\","
- "\"WiFi\":{"
- "\"Frequency\":5000,"
- "\"FrequencyList\":[2400,5000],"
- "\"SSID\":\"stub_wifi2\","
- "\"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, // service path
+ base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess,
+ this),
+ base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed,
+ this));
return true;
}
+void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess(
+ const std::string& service_path,
+ 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
@@ -185,13 +190,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::GetProcessClientForProfile(profile_));
+
+ process_client->SetProperties(
+ params->network_guid, // service path
+ *properties_dict,
+ base::Bind(&NetworkingPrivateSetPropertiesFunction::ResultCallback, this),
+ base::Bind(&NetworkingPrivateSetPropertiesFunction::ErrorCallback, this));
return true;
}
+void NetworkingPrivateSetPropertiesFunction::ErrorCallback(
+ const std::string& error_name,
+ const scoped_ptr<base::DictionaryValue> error_data) {
+ error_ = error_name;
+ SendResponse(false);
+}
+
+void NetworkingPrivateSetPropertiesFunction::ResultCallback() {
+ SendResponse(true);
+}
+
////////////////////////////////////////////////////////////////////////////////
// NetworkingPrivateCreateNetworkFunction
@@ -204,14 +224,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;
@@ -228,68 +240,47 @@ 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\""
- " },"
- " {"
- " \"ConnectionState\": \"Connected\","
- " \"GUID\": \"stub_wifi1\","
- " \"Name\": \"wifi1\","
- " \"Type\": \"WiFi\","
- " \"WiFi\": {"
- " \"AutoConnect\": false,"
- " \"Security\": \"WEP-PSK\","
- " \"SignalStrength\": 0"
- " }"
- " },"
- " {"
- " \"ConnectionState\": \"Connected\","
- " \"GUID\": \"stub_vpn1\","
- " \"Name\": \"vpn1\","
- " \"Type\": \"VPN\","
- " \"VPN\": {"
- " \"AutoConnect\": false"
- " }"
- " },"
- " {"
- " \"ConnectionState\": \"NotConnected\","
- " \"GUID\": \"stub_wifi2\","
- " \"Name\": \"wifi2_PSK\","
- " \"Type\": \"WiFi\","
- " \"WiFi\": {"
- " \"AutoConnect\": false,"
- " \"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::GetProcessClientForProfile(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(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;
}
////////////////////////////////////////////////////////////////////////////////
@@ -300,20 +291,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::GetProcessClientForProfile(profile_));
+ process_client->RequestNetworkScan();
return true;
}
@@ -324,53 +304,29 @@ NetworkingPrivateStartConnectFunction::
~NetworkingPrivateStartConnectFunction() {
}
+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);
+}
+
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::GetProcessClientForProfile(profile_));
+ process_client->StartConnect(
+ params->network_guid, // service path
+ base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartSuccess,
+ this),
+ base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartFailed,
+ this));
return true;
}
@@ -381,39 +337,76 @@ NetworkingPrivateStartDisconnectFunction::
~NetworkingPrivateStartDisconnectFunction() {
}
+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);
+}
+
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::GetProcessClientForProfile(profile_));
+ process_client->StartDisconnect(
+ params->network_guid, // service path
+ base::Bind(
+ &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess,
+ this),
+ base::Bind(
+ &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed,
+ this));
return true;
}
////////////////////////////////////////////////////////////////////////////////
// NetworkingPrivateVerifyDestinationFunction
-NetworkingPrivateVerifyDestinationFunction::
- ~NetworkingPrivateVerifyDestinationFunction() {
+namespace {
+bool VerifyDestionationProperties(
+ const api::VerificationProperties& properties) {
+ std::vector<std::string> data_parts;
+ data_parts.push_back(properties.device_ssid);
+ data_parts.push_back(properties.device_serial);
+ data_parts.push_back(properties.device_bssid);
+ data_parts.push_back(properties.public_key);
+ data_parts.push_back(properties.nonce);
+ std::string unsigned_data = JoinString(data_parts, ",");
+ std::string signed_data;
+ if (!base::Base64Decode(properties.signed_data, &signed_data))
+ return false;
+ NetworkingPrivateCrypto crypto;
+ return crypto.VerifyCredentials(properties.certificate,
+ signed_data,
+ unsigned_data,
+ properties.device_bssid);
}
+}; // namespace
+
+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));
+ // Don't do actual verification during browser test.
+ scoped_refptr<NetworkingPrivateProcessClient> process_client(
+ NetworkingPrivateProcessClient::GetProcessClientForProfile(profile_));
+ if (process_client->IsUsingWiFiServiceMock()) {
+ SetResult(new base::FundamentalValue(true));
+ SendResponse(true);
+ return true;
+ }
+
+ bool verified = VerifyDestionationProperties(params->properties);
+ SetResult(new base::FundamentalValue(verified));
SendResponse(true);
return true;
}
@@ -445,7 +438,29 @@ bool NetworkingPrivateVerifyAndEncryptDataFunction::RunImpl() {
scoped_ptr<api::VerifyAndEncryptData::Params> params =
api::VerifyAndEncryptData::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params);
- SetResult(new base::StringValue("encrypted_data"));
+ // Don't do actual verification during browser test.
+ scoped_refptr<NetworkingPrivateProcessClient> process_client(
+ NetworkingPrivateProcessClient::GetProcessClientForProfile(profile_));
+ if (process_client->IsUsingWiFiServiceMock()) {
+ SetResult(new base::StringValue("encrypted_data"));
+ SendResponse(true);
+ return true;
+ }
+
+ if (VerifyDestionationProperties(params->properties)) {
+ std::string public_key;
+ if (base::Base64Decode(params->properties.public_key, &public_key)) {
+ NetworkingPrivateCrypto crypto;
+ std::string encrypted_data;
+ if (crypto.EncryptByteString(public_key, params->data, &encrypted_data)) {
+ std::string encoded_data;
+ if (base::Base64Encode(encrypted_data, &encoded_data)) {
+ SetResult(new base::StringValue(encoded_data));
+ }
+ }
+ }
+ }
+
SendResponse(true);
return true;
}

Powered by Google App Engine
This is Rietveld 408576698