Commit 1c524240 authored by noamsml@chromium.org's avatar noamsml@chromium.org

Added function to get list of networks with prefetched passwords.

Added function that allows javascript apps using gcdPrivate to get a list of networks with prefetched passwords.

Fixed reland of https://codereview.chromium.org/434733003/ which was reverted due to test failure.

BUG=383167
TBR=asargent@chromium.org,vitalybuka@chromium.org

Review URL: https://codereview.chromium.org/450533003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@287995 0039d316-1c4b-4281-b951-d872f2087c98
parent f7209be6
...@@ -136,6 +136,8 @@ class GcdPrivateAPIImpl : public EventRouter::Observer, ...@@ -136,6 +136,8 @@ class GcdPrivateAPIImpl : public EventRouter::Observer,
void RemoveSession(int session_id); void RemoveSession(int session_id);
scoped_ptr<base::ListValue> GetPrefetchedSSIDList();
private: private:
typedef std::map<std::string /* id_string */, typedef std::map<std::string /* id_string */,
linked_ptr<api::gcd_private::GCDDevice> > GCDDeviceMap; linked_ptr<api::gcd_private::GCDDevice> > GCDDeviceMap;
...@@ -391,10 +393,7 @@ void GcdPrivateAPIImpl::SendMessage(int session_id, ...@@ -391,10 +393,7 @@ void GcdPrivateAPIImpl::SendMessage(int session_id,
if (api == kPrivatAPISetup) { if (api == kPrivatAPISetup) {
const base::DictionaryValue* wifi = NULL; const base::DictionaryValue* wifi = NULL;
if (input.GetDictionary(kPrivetKeyWifi, &wifi) && if (input.GetDictionary(kPrivetKeyWifi, &wifi)) {
!wifi->HasKey(kPrivetKeyPassphrase)) {
// If the message is a setup message, has a wifi section, try sending the
// passphrase.
std::string ssid; std::string ssid;
if (!wifi->GetString(kPrivetKeySSID, &ssid)) { if (!wifi->GetString(kPrivetKeySSID, &ssid)) {
...@@ -403,6 +402,10 @@ void GcdPrivateAPIImpl::SendMessage(int session_id, ...@@ -403,6 +402,10 @@ void GcdPrivateAPIImpl::SendMessage(int session_id,
return; return;
} }
if (!wifi->HasKey(kPrivetKeyPassphrase)) {
// If the message is a setup message, has a wifi section, try sending
// the passphrase.
PasswordMap::iterator found = wifi_passwords_.find(ssid); PasswordMap::iterator found = wifi_passwords_.find(ssid);
if (found == wifi_passwords_.end()) { if (found == wifi_passwords_.end()) {
callback.Run(gcd_private::STATUS_WIFIPASSWORDERROR, callback.Run(gcd_private::STATUS_WIFIPASSWORDERROR,
...@@ -415,6 +418,7 @@ void GcdPrivateAPIImpl::SendMessage(int session_id, ...@@ -415,6 +418,7 @@ void GcdPrivateAPIImpl::SendMessage(int session_id,
input_actual = input_cloned.get(); input_actual = input_cloned.get();
} }
} }
}
#endif #endif
GCDSessionMap::iterator found = sessions_.find(session_id); GCDSessionMap::iterator found = sessions_.find(session_id);
...@@ -467,6 +471,20 @@ void GcdPrivateAPIImpl::RemoveSession(int session_id) { ...@@ -467,6 +471,20 @@ void GcdPrivateAPIImpl::RemoveSession(int session_id) {
sessions_.erase(session_id); sessions_.erase(session_id);
} }
scoped_ptr<base::ListValue> GcdPrivateAPIImpl::GetPrefetchedSSIDList() {
scoped_ptr<base::ListValue> retval(new base::ListValue);
#if defined(ENABLE_WIFI_BOOTSTRAPPING)
for (PasswordMap::iterator i = wifi_passwords_.begin();
i != wifi_passwords_.end();
i++) {
retval->AppendString(i->first);
}
#endif
return retval.Pass();
}
GcdPrivateRequest::GcdPrivateRequest( GcdPrivateRequest::GcdPrivateRequest(
const std::string& api, const std::string& api,
const base::DictionaryValue& input, const base::DictionaryValue& input,
...@@ -686,9 +704,6 @@ GcdPrivateQueryForNewLocalDevicesFunction:: ...@@ -686,9 +704,6 @@ GcdPrivateQueryForNewLocalDevicesFunction::
bool GcdPrivateQueryForNewLocalDevicesFunction::RunSync() { bool GcdPrivateQueryForNewLocalDevicesFunction::RunSync() {
GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile());
if (!gcd_api)
return false;
if (!gcd_api->QueryForDevices()) { if (!gcd_api->QueryForDevices()) {
error_ = error_ =
"You must first subscribe to onDeviceStateChanged or onDeviceRemoved " "You must first subscribe to onDeviceStateChanged or onDeviceRemoved "
...@@ -716,9 +731,6 @@ bool GcdPrivatePrefetchWifiPasswordFunction::RunAsync() { ...@@ -716,9 +731,6 @@ bool GcdPrivatePrefetchWifiPasswordFunction::RunAsync() {
GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile());
if (!gcd_api)
return false;
gcd_api->RequestWifiPassword( gcd_api->RequestWifiPassword(
params->ssid, params->ssid,
base::Bind(&GcdPrivatePrefetchWifiPasswordFunction::OnResponse, this)); base::Bind(&GcdPrivatePrefetchWifiPasswordFunction::OnResponse, this));
...@@ -748,9 +760,6 @@ bool GcdPrivateEstablishSessionFunction::RunAsync() { ...@@ -748,9 +760,6 @@ bool GcdPrivateEstablishSessionFunction::RunAsync() {
GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile());
if (!gcd_api)
return false;
gcd_api->EstablishSession( gcd_api->EstablishSession(
params->ip_address, params->ip_address,
params->port, params->port,
...@@ -785,9 +794,6 @@ bool GcdPrivateConfirmCodeFunction::RunAsync() { ...@@ -785,9 +794,6 @@ bool GcdPrivateConfirmCodeFunction::RunAsync() {
GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile());
if (!gcd_api)
return false;
gcd_api->ConfirmCode( gcd_api->ConfirmCode(
params->session_id, params->session_id,
base::Bind(&GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback, base::Bind(&GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback,
...@@ -817,8 +823,6 @@ bool GcdPrivateSendMessageFunction::RunAsync() { ...@@ -817,8 +823,6 @@ bool GcdPrivateSendMessageFunction::RunAsync() {
GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile());
if (!gcd_api)
return false;
gcd_api->SendMessage( gcd_api->SendMessage(
params->session_id, params->session_id,
...@@ -854,9 +858,6 @@ bool GcdPrivateTerminateSessionFunction::RunAsync() { ...@@ -854,9 +858,6 @@ bool GcdPrivateTerminateSessionFunction::RunAsync() {
GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile());
if (!gcd_api)
return false;
gcd_api->RemoveSession(params->session_id); gcd_api->RemoveSession(params->session_id);
SendResponse(true); SendResponse(true);
...@@ -871,6 +872,24 @@ GcdPrivateGetCommandDefinitionsFunction:: ...@@ -871,6 +872,24 @@ GcdPrivateGetCommandDefinitionsFunction::
~GcdPrivateGetCommandDefinitionsFunction() { ~GcdPrivateGetCommandDefinitionsFunction() {
} }
GcdPrivateGetPrefetchedWifiNameListFunction::
GcdPrivateGetPrefetchedWifiNameListFunction() {
}
GcdPrivateGetPrefetchedWifiNameListFunction::
~GcdPrivateGetPrefetchedWifiNameListFunction() {
}
bool GcdPrivateGetPrefetchedWifiNameListFunction::RunSync() {
GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile());
scoped_ptr<base::ListValue> ssid_list = gcd_api->GetPrefetchedSSIDList();
SetResult(ssid_list.release());
return true;
}
bool GcdPrivateGetCommandDefinitionsFunction::RunAsync() { bool GcdPrivateGetCommandDefinitionsFunction::RunAsync() {
return false; return false;
} }
......
...@@ -108,6 +108,21 @@ class GcdPrivatePrefetchWifiPasswordFunction ...@@ -108,6 +108,21 @@ class GcdPrivatePrefetchWifiPasswordFunction
void OnResponse(bool response); void OnResponse(bool response);
}; };
class GcdPrivateGetPrefetchedWifiNameListFunction
: public ChromeSyncExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("gcdPrivate.getPrefetchedWifiNameList",
GCDPRIVATE_GETPREFETCHEDWIFINAMELIST);
GcdPrivateGetPrefetchedWifiNameListFunction();
protected:
virtual ~GcdPrivateGetPrefetchedWifiNameListFunction();
// SyncExtensionFunction overrides.
virtual bool RunSync() OVERRIDE;
};
class GcdPrivateEstablishSessionFunction : public ChromeAsyncExtensionFunction { class GcdPrivateEstablishSessionFunction : public ChromeAsyncExtensionFunction {
public: public:
DECLARE_EXTENSION_FUNCTION("gcdPrivate.establishSession", DECLARE_EXTENSION_FUNCTION("gcdPrivate.establishSession",
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/local_discovery/gcd_api_flow.h" #include "chrome/browser/local_discovery/gcd_api_flow.h"
#include "chrome/browser/local_discovery/wifi/mock_wifi_manager.h"
#include "chrome/common/extensions/api/mdns.h" #include "chrome/common/extensions/api/mdns.h"
#include "extensions/common/switches.h" #include "extensions/common/switches.h"
#include "net/url_request/test_url_fetcher_factory.h" #include "net/url_request/test_url_fetcher_factory.h"
...@@ -21,6 +22,8 @@ ...@@ -21,6 +22,8 @@
namespace api = extensions::api; namespace api = extensions::api;
using testing::Invoke;
namespace { namespace {
const char kCloudPrintResponse[] = const char kCloudPrintResponse[] =
...@@ -232,6 +235,29 @@ class GcdPrivateAPITest : public ExtensionApiTest { ...@@ -232,6 +235,29 @@ class GcdPrivateAPITest : public ExtensionApiTest {
"ddchlicdkolnonkihahngkmmmjnjlkkf"); "ddchlicdkolnonkihahngkmmmjnjlkkf");
} }
#if defined(ENABLE_WIFI_BOOTSTRAPPING)
virtual void OnCreateWifiManager() {
wifi_manager_ = wifi_manager_factory_.GetLastCreatedWifiManager();
EXPECT_CALL(*wifi_manager_, Start());
EXPECT_CALL(*wifi_manager_,
RequestNetworkCredentialsInternal("SuccessNetwork"))
.WillOnce(Invoke(this, &GcdPrivateAPITest::RespondToNetwork));
EXPECT_CALL(*wifi_manager_,
RequestNetworkCredentialsInternal("FailureNetwork"))
.WillOnce(Invoke(this, &GcdPrivateAPITest::RespondToNetwork));
}
void RespondToNetwork(const std::string& network) {
bool success = (network == "SuccessNetwork");
wifi_manager_->CallRequestNetworkCredentialsCallback(
success, network, success ? "SuccessPass" : "");
}
#endif
protected: protected:
FakeGCDApiFlowFactory api_flow_factory_; FakeGCDApiFlowFactory api_flow_factory_;
net::URLFetcherImplFactory url_fetcher_impl_factory_; net::URLFetcherImplFactory url_fetcher_impl_factory_;
...@@ -241,6 +267,11 @@ class GcdPrivateAPITest : public ExtensionApiTest { ...@@ -241,6 +267,11 @@ class GcdPrivateAPITest : public ExtensionApiTest {
scoped_refptr<local_discovery::TestServiceDiscoveryClient> scoped_refptr<local_discovery::TestServiceDiscoveryClient>
test_service_discovery_client_; test_service_discovery_client_;
#endif // ENABLE_MDNS #endif // ENABLE_MDNS
#if defined(ENABLE_WIFI_BOOTSTRAPPING)
local_discovery::wifi::MockWifiManagerFactory wifi_manager_factory_;
local_discovery::wifi::MockWifiManager* wifi_manager_;
#endif
}; };
IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest, GetCloudList) { IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest, GetCloudList) {
...@@ -319,4 +350,24 @@ IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest, SendQuery) { ...@@ -319,4 +350,24 @@ IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest, SendQuery) {
#endif // ENABLE_MDNS #endif // ENABLE_MDNS
#if defined(ENABLE_WIFI_BOOTSTRAPPING)
IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest, WifiMessage) {
EXPECT_TRUE(RunExtensionSubtest("gcd_private/api", "wifi_message.html"));
}
IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest, WifiPasswords) {
// TODO(noamsml): Win Dbg has a workaround that makes RunExtensionSubtest
// always return true without actually running the test. Remove when fixed.
// See http://crbug.com/177163 for details.
#if !defined(OS_WIN) || defined(NDEBUG)
EXPECT_CALL(wifi_manager_factory_, WifiManagerCreated())
.WillOnce(Invoke(this, &GcdPrivateAPITest::OnCreateWifiManager));
#endif
EXPECT_TRUE(RunExtensionSubtest("gcd_private/api", "wifi_password.html"));
}
#endif // ENABLE_WIFI_BOOTSTRAPPING
} // namespace } // namespace
...@@ -85,9 +85,15 @@ MockWifiManagerFactory::~MockWifiManagerFactory() { ...@@ -85,9 +85,15 @@ MockWifiManagerFactory::~MockWifiManagerFactory() {
scoped_ptr<WifiManager> MockWifiManagerFactory::CreateWifiManager() { scoped_ptr<WifiManager> MockWifiManagerFactory::CreateWifiManager() {
last_created_manager_ = new MockWifiManager(); last_created_manager_ = new MockWifiManager();
WifiManagerCreated();
return scoped_ptr<WifiManager>(last_created_manager_); return scoped_ptr<WifiManager>(last_created_manager_);
} }
MockWifiManager* MockWifiManagerFactory::GetLastCreatedWifiManager() {
return last_created_manager_;
}
} // namespace wifi } // namespace wifi
} // namespace local_discovery } // namespace local_discovery
...@@ -82,6 +82,8 @@ class MockWifiManagerFactory : public WifiManagerFactory { ...@@ -82,6 +82,8 @@ class MockWifiManagerFactory : public WifiManagerFactory {
MockWifiManager* GetLastCreatedWifiManager(); MockWifiManager* GetLastCreatedWifiManager();
MOCK_METHOD0(WifiManagerCreated, void());
private: private:
MockWifiManager* last_created_manager_; MockWifiManager* last_created_manager_;
}; };
......
...@@ -96,6 +96,9 @@ namespace gcdPrivate { ...@@ -96,6 +96,9 @@ namespace gcdPrivate {
// |success| Denotes whether the password fetch has succeeded or failed. // |success| Denotes whether the password fetch has succeeded or failed.
callback SuccessCallback = void(boolean success); callback SuccessCallback = void(boolean success);
// Called as a response to |getPrefetchedWifiNameList|
// |list| the list of ssids for which wifi passwords were prefetched.
callback SSIDListCallback = void(DOMString[] networks);
interface Functions { interface Functions {
// Returns the list of cloud devices visible locally or available in the // Returns the list of cloud devices visible locally or available in the
...@@ -113,6 +116,9 @@ namespace gcdPrivate { ...@@ -113,6 +116,9 @@ namespace gcdPrivate {
// with true if wifi password was cached and false if it was unavailable. // with true if wifi password was cached and false if it was unavailable.
static void prefetchWifiPassword(DOMString ssid, SuccessCallback callback); static void prefetchWifiPassword(DOMString ssid, SuccessCallback callback);
// Get the list of ssids with prefetched callbacks.
static void getPrefetchedWifiNameList(SSIDListCallback callback);
// Establish the session. // Establish the session.
static void establishSession(DOMString ipAddress, static void establishSession(DOMString ipAddress,
long port, long port,
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
onload = function() {
chrome.test.runTests([
function wifiMessage() {
var messages_needed = 3;
function onConfirmCode(sessionId, status, code, method) {
chrome.test.assertEq("success", status);
chrome.test.assertEq("01234", code);
chrome.gcdPrivate.confirmCode(sessionId,
onSessionEstablished.bind(null,
sessionId));
}
function onSessionEstablished(sessionId, status) {
chrome.test.assertEq("success", status);
chrome.gcdPrivate.sendMessage(sessionId, "/privet/v3/setup/start", {
"wifi" : {
}
}, onMessageSent.bind(null, "setupParseError"));
chrome.gcdPrivate.sendMessage(sessionId, "/privet/v3/setup/start", {
"wifi" : {
"passphrase": "Blah"
}
}, onMessageSent.bind(null, "setupParseError"));
chrome.gcdPrivate.sendMessage(sessionId, "/privet/v3/setup/start", {
"wifi" : {
"ssid": "Blah"
}
}, onMessageSent.bind(null, "wifiPasswordError"));
}
function onMessageSent(expected_status, status, output) {
chrome.test.assertEq(expected_status, status);
messages_needed--;
console.log("Messages needed " + messages_needed);
if (messages_needed == 0) {
chrome.test.notifyPass();
}
}
chrome.gcdPrivate.establishSession("1.2.3.4", 9090, onConfirmCode);
}
]);
};
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
onload = function() {
chrome.test.runTests([
function wifiPassword() {
var succeeded = false;
var failed = false;
function expectResult(expectedResult, result) {
chrome.test.assertEq(expectedResult, result);
if (expectedResult) succeeded = true;
else failed = true;
if (succeeded && failed) {
chrome.gcdPrivate.getPrefetchedWifiNameList(onWifiList);
}
}
function onWifiList(list) {
chrome.test.assertEq(["SuccessNetwork"], list);
chrome.test.notifyPass();
}
chrome.gcdPrivate.prefetchWifiPassword("SuccessNetwork",
expectResult.bind(null, true));
chrome.gcdPrivate.prefetchWifiPassword("FailureNetwork",
expectResult.bind(null, false));
}
]);
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment