Added event to networkingPrivate extension that portal detection is completed.

BUG=354946
TEST=ExtensionNetworkingPrivateApiTest.*
R=yoz@chromium.org

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=262736

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@262855 0039d316-1c4b-4281-b951-d872f2087c98
parent 94f6294e
...@@ -702,9 +702,6 @@ bool NetworkingPrivateGetCaptivePortalStatusFunction::RunImpl() { ...@@ -702,9 +702,6 @@ bool NetworkingPrivateGetCaptivePortalStatusFunction::RunImpl() {
api::GetCaptivePortalStatus::Params::Create(*args_); api::GetCaptivePortalStatus::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params); EXTENSION_FUNCTION_VALIDATE(params);
// The |network_guid| parameter is storing the service path.
const std::string& service_path = params->network_guid;
NetworkPortalDetector* detector = NetworkPortalDetector::Get(); NetworkPortalDetector* detector = NetworkPortalDetector::Get();
if (!detector) { if (!detector) {
error_ = "Error.NotReady"; error_ = "Error.NotReady";
...@@ -712,7 +709,7 @@ bool NetworkingPrivateGetCaptivePortalStatusFunction::RunImpl() { ...@@ -712,7 +709,7 @@ bool NetworkingPrivateGetCaptivePortalStatusFunction::RunImpl() {
} }
NetworkPortalDetector::CaptivePortalState state = NetworkPortalDetector::CaptivePortalState state =
detector->GetCaptivePortalState(service_path); detector->GetCaptivePortalState(params->network_path);
SetResult(new base::StringValue( SetResult(new base::StringValue(
NetworkPortalDetector::CaptivePortalStatusString(state.status))); NetworkPortalDetector::CaptivePortalStatusString(state.status)));
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/macros.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/login/user.h" #include "chrome/browser/chromeos/login/user.h"
#include "chrome/browser/chromeos/login/user_manager.h" #include "chrome/browser/chromeos/login/user_manager.h"
...@@ -14,6 +16,7 @@ ...@@ -14,6 +16,7 @@
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/net/network_portal_detector.h" #include "chrome/browser/chromeos/net/network_portal_detector.h"
#include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h" #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
#include "chromeos/chromeos_switches.h" #include "chromeos/chromeos_switches.h"
...@@ -30,6 +33,10 @@ ...@@ -30,6 +33,10 @@
#include "components/policy/core/common/mock_configuration_policy_provider.h" #include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h" #include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h" #include "components/policy/core/common/policy_types.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "policy/policy_constants.h" #include "policy/policy_constants.h"
#include "third_party/cros_system_api/dbus/service_constants.h" #include "third_party/cros_system_api/dbus/service_constants.h"
#else // !defined(OS_CHROMEOS) #else // !defined(OS_CHROMEOS)
...@@ -61,6 +68,32 @@ namespace { ...@@ -61,6 +68,32 @@ namespace {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
const char kUser1ProfilePath[] = "/profile/user1/shill"; const char kUser1ProfilePath[] = "/profile/user1/shill";
class TestListener : public content::NotificationObserver {
public:
TestListener(const std::string& message, const base::Closure& callback)
: message_(message), callback_(callback) {
registrar_.Add(this,
chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE,
content::NotificationService::AllSources());
}
virtual void Observe(int type,
const content::NotificationSource& /* source */,
const content::NotificationDetails& details) OVERRIDE {
const std::string& message = *content::Details<std::string>(details).ptr();
if (message == message_)
callback_.Run();
}
private:
std::string message_;
base::Closure callback_;
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(TestListener);
};
#else // !defined(OS_CHROMEOS) #else // !defined(OS_CHROMEOS)
// Stub Verify* methods implementation to satisfy expectations of // Stub Verify* methods implementation to satisfy expectations of
...@@ -91,10 +124,17 @@ class CryptoVerifyStub ...@@ -91,10 +124,17 @@ class CryptoVerifyStub
}; };
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
class ExtensionNetworkingPrivateApiTest : class ExtensionNetworkingPrivateApiTest
public ExtensionApiTest, : public ExtensionApiTest,
public testing::WithParamInterface<bool> { public testing::WithParamInterface<bool> {
public: public:
ExtensionNetworkingPrivateApiTest()
#if defined(OS_CHROMEOS)
: detector_(NULL)
#endif
{
}
bool RunNetworkingSubtest(const std::string& subtest) { bool RunNetworkingSubtest(const std::string& subtest) {
return RunExtensionSubtest( return RunExtensionSubtest(
"networking", "main.html?" + subtest, "networking", "main.html?" + subtest,
...@@ -151,6 +191,9 @@ class ExtensionNetworkingPrivateApiTest : ...@@ -151,6 +191,9 @@ class ExtensionNetworkingPrivateApiTest :
} }
virtual void SetUpOnMainThread() OVERRIDE { virtual void SetUpOnMainThread() OVERRIDE {
detector_ = new NetworkPortalDetectorTestImpl();
NetworkPortalDetector::InitializeForTesting(detector_);
ExtensionApiTest::SetUpOnMainThread(); ExtensionApiTest::SetUpOnMainThread();
content::RunAllPendingInMessageLoop(); content::RunAllPendingInMessageLoop();
...@@ -290,6 +333,9 @@ class ExtensionNetworkingPrivateApiTest : ...@@ -290,6 +333,9 @@ class ExtensionNetworkingPrivateApiTest :
protected: protected:
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
NetworkPortalDetectorTestImpl* detector() { return detector_; }
NetworkPortalDetectorTestImpl* detector_;
policy::MockConfigurationPolicyProvider provider_; policy::MockConfigurationPolicyProvider provider_;
std::string userhash_; std::string userhash_;
#endif #endif
...@@ -463,24 +509,36 @@ IN_PROC_BROWSER_TEST_P(ExtensionNetworkingPrivateApiTest, ...@@ -463,24 +509,36 @@ IN_PROC_BROWSER_TEST_P(ExtensionNetworkingPrivateApiTest,
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_P(ExtensionNetworkingPrivateApiTest, IN_PROC_BROWSER_TEST_P(ExtensionNetworkingPrivateApiTest,
GetCaptivePortalStatus) { GetCaptivePortalStatus) {
NetworkPortalDetectorTestImpl* detector = new NetworkPortalDetectorTestImpl();
NetworkPortalDetector::InitializeForTesting(detector);
NetworkPortalDetector::CaptivePortalState state; NetworkPortalDetector::CaptivePortalState state;
state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE; state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
detector->SetDetectionResultsForTesting("stub_ethernet", state); detector()->SetDetectionResultsForTesting("stub_ethernet", state);
state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE; state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
detector->SetDetectionResultsForTesting("stub_wifi1", state); detector()->SetDetectionResultsForTesting("stub_wifi1", state);
state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL; state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
detector->SetDetectionResultsForTesting("stub_wifi2", state); detector()->SetDetectionResultsForTesting("stub_wifi2", state);
state.status = state.status =
NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED;
detector->SetDetectionResultsForTesting("stub_cellular1", state); detector()->SetDetectionResultsForTesting("stub_cellular1", state);
EXPECT_TRUE(RunNetworkingSubtest("getCaptivePortalStatus")) << message_; EXPECT_TRUE(RunNetworkingSubtest("getCaptivePortalStatus")) << message_;
} }
IN_PROC_BROWSER_TEST_P(ExtensionNetworkingPrivateApiTest,
CaptivePortalNotification) {
detector()->SetDefaultNetworkPathForTesting("wifi");
NetworkPortalDetector::CaptivePortalState state;
state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
detector()->SetDetectionResultsForTesting("wifi", state);
TestListener listener(
"notifyPortalDetectorObservers",
base::Bind(&NetworkPortalDetectorTestImpl::NotifyObserversForTesting,
base::Unretained(detector())));
EXPECT_TRUE(RunNetworkingSubtest("captivePortalNotification")) << message_;
}
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
INSTANTIATE_TEST_CASE_P(ExtensionNetworkingPrivateApiTestInstantiation, INSTANTIATE_TEST_CASE_P(ExtensionNetworkingPrivateApiTestInstantiation,
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/net/network_portal_detector.h"
#include "chrome/browser/extensions/api/networking_private/networking_private_api.h" #include "chrome/browser/extensions/api/networking_private/networking_private_api.h"
#include "chrome/browser/extensions/event_router_forwarder.h" #include "chrome/browser/extensions/event_router_forwarder.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
...@@ -22,6 +23,7 @@ ...@@ -22,6 +23,7 @@
#include "third_party/cros_system_api/dbus/service_constants.h" #include "third_party/cros_system_api/dbus/service_constants.h"
using chromeos::NetworkHandler; using chromeos::NetworkHandler;
using chromeos::NetworkPortalDetector;
using chromeos::NetworkState; using chromeos::NetworkState;
using chromeos::NetworkStateHandler; using chromeos::NetworkStateHandler;
...@@ -29,7 +31,8 @@ namespace extensions { ...@@ -29,7 +31,8 @@ namespace extensions {
class NetworkingPrivateEventRouterImpl class NetworkingPrivateEventRouterImpl
: public NetworkingPrivateEventRouter, : public NetworkingPrivateEventRouter,
public chromeos::NetworkStateHandlerObserver { public chromeos::NetworkStateHandlerObserver,
public NetworkPortalDetector::Observer {
public: public:
explicit NetworkingPrivateEventRouterImpl(Profile* profile); explicit NetworkingPrivateEventRouterImpl(Profile* profile);
virtual ~NetworkingPrivateEventRouterImpl(); virtual ~NetworkingPrivateEventRouterImpl();
...@@ -46,6 +49,11 @@ class NetworkingPrivateEventRouterImpl ...@@ -46,6 +49,11 @@ class NetworkingPrivateEventRouterImpl
virtual void NetworkListChanged() OVERRIDE; virtual void NetworkListChanged() OVERRIDE;
virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE; virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE;
// NetworkPortalDetector::Observer overrides:
virtual void OnPortalDetectionCompleted(
const NetworkState* network,
const NetworkPortalDetector::CaptivePortalState& state) OVERRIDE;
private: private:
// Decide if we should listen for network changes or not. If there are any // Decide if we should listen for network changes or not. If there are any
// JavaScript listeners registered for the onNetworkChanged event, then we // JavaScript listeners registered for the onNetworkChanged event, then we
...@@ -72,6 +80,8 @@ NetworkingPrivateEventRouterImpl::NetworkingPrivateEventRouterImpl( ...@@ -72,6 +80,8 @@ NetworkingPrivateEventRouterImpl::NetworkingPrivateEventRouterImpl(
this, api::networking_private::OnNetworksChanged::kEventName); this, api::networking_private::OnNetworksChanged::kEventName);
event_router->RegisterObserver( event_router->RegisterObserver(
this, api::networking_private::OnNetworkListChanged::kEventName); this, api::networking_private::OnNetworkListChanged::kEventName);
event_router->RegisterObserver(
this, api::networking_private::OnPortalDetectionCompleted::kEventName);
StartOrStopListeningForNetworkChanges(); StartOrStopListeningForNetworkChanges();
} }
} }
...@@ -114,14 +124,18 @@ void NetworkingPrivateEventRouterImpl::StartOrStopListeningForNetworkChanges() { ...@@ -114,14 +124,18 @@ void NetworkingPrivateEventRouterImpl::StartOrStopListeningForNetworkChanges() {
event_router->HasEventListener( event_router->HasEventListener(
api::networking_private::OnNetworksChanged::kEventName) || api::networking_private::OnNetworksChanged::kEventName) ||
event_router->HasEventListener( event_router->HasEventListener(
api::networking_private::OnNetworkListChanged::kEventName); api::networking_private::OnNetworkListChanged::kEventName) ||
event_router->HasEventListener(
api::networking_private::OnPortalDetectionCompleted::kEventName);
if (should_listen && !listening_) { if (should_listen && !listening_) {
NetworkHandler::Get()->network_state_handler()->AddObserver( NetworkHandler::Get()->network_state_handler()->AddObserver(
this, FROM_HERE); this, FROM_HERE);
NetworkPortalDetector::Get()->AddObserver(this);
} else if (!should_listen && listening_) { } else if (!should_listen && listening_) {
NetworkHandler::Get()->network_state_handler()->RemoveObserver( NetworkHandler::Get()->network_state_handler()->RemoveObserver(
this, FROM_HERE); this, FROM_HERE);
NetworkPortalDetector::Get()->RemoveObserver(this);
} }
listening_ = should_listen; listening_ = should_listen;
} }
...@@ -177,6 +191,53 @@ void NetworkingPrivateEventRouterImpl::NetworkPropertiesUpdated( ...@@ -177,6 +191,53 @@ void NetworkingPrivateEventRouterImpl::NetworkPropertiesUpdated(
event_router->BroadcastEvent(extension_event.Pass()); event_router->BroadcastEvent(extension_event.Pass());
} }
void NetworkingPrivateEventRouterImpl::OnPortalDetectionCompleted(
const NetworkState* network,
const NetworkPortalDetector::CaptivePortalState& state) {
const std::string path = network ? network->path() : std::string();
EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router();
if (!event_router->HasEventListener(
api::networking_private::OnPortalDetectionCompleted::kEventName)) {
NET_LOG_EVENT("NetworkingPrivate.OnPortalDetectionCompleted: No Listeners",
path);
return;
}
NET_LOG_EVENT("NetworkingPrivate.OnPortalDetectionCompleted", path);
api::networking_private::CaptivePortalStatus status =
api::networking_private::CAPTIVE_PORTAL_STATUS_UNKNOWN;
switch (state.status) {
case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN:
status = api::networking_private::CAPTIVE_PORTAL_STATUS_UNKNOWN;
break;
case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE:
status = api::networking_private::CAPTIVE_PORTAL_STATUS_OFFLINE;
break;
case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE:
status = api::networking_private::CAPTIVE_PORTAL_STATUS_ONLINE;
break;
case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL:
status = api::networking_private::CAPTIVE_PORTAL_STATUS_PORTAL;
break;
case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED:
status =
api::networking_private::CAPTIVE_PORTAL_STATUS_PROXYAUTHREQUIRED;
break;
default:
NOTREACHED();
break;
}
scoped_ptr<base::ListValue> args(
api::networking_private::OnPortalDetectionCompleted::Create(
path, status));
scoped_ptr<Event> extension_event(
new Event(api::networking_private::OnPortalDetectionCompleted::kEventName,
args.Pass()));
event_router->BroadcastEvent(extension_event.Pass());
}
NetworkingPrivateEventRouter* NetworkingPrivateEventRouter::Create( NetworkingPrivateEventRouter* NetworkingPrivateEventRouter::Create(
Profile* profile) { Profile* profile) {
return new NetworkingPrivateEventRouterImpl(profile); return new NetworkingPrivateEventRouterImpl(profile);
......
...@@ -413,9 +413,9 @@ ...@@ -413,9 +413,9 @@
"description": "Returns captive portal status for the network with networkGuid.", "description": "Returns captive portal status for the network with networkGuid.",
"parameters": [ "parameters": [
{ {
"name": "networkGuid", "name": "networkPath",
"type": "string", "type": "string",
"description": "The unique identifier of the network to get captive portal status." "description": "The path of the network to get captive portal status."
}, },
{ {
"name": "callback", "name": "callback",
...@@ -455,6 +455,21 @@ ...@@ -455,6 +455,21 @@
"items": { "type": "string" } "items": { "type": "string" }
} }
] ]
},
{
"name": "onPortalDetectionCompleted",
"type": "function",
"description": "Fired when a portal detection for a network completes. Sends a name of the network and corresponding captive portal status.",
"parameters": [
{
"name": "networkPath",
"type": "string"
},
{
"name": "status",
"$ref": "CaptivePortalStatus"
}
]
} }
] ]
} }
......
...@@ -55,6 +55,20 @@ var privateHelpers = { ...@@ -55,6 +55,20 @@ var privateHelpers = {
self.listenForChanges); self.listenForChanges);
done(); done();
}; };
},
watchForCaptivePortalState: function(expectedNetworkPath,
expectedState,
done) {
var self = this;
this.onPortalDetectionCompleted = function(networkPath, state) {
assertEq(expectedNetworkPath, networkPath);
assertEq(expectedState, state);
chrome.networkingPrivate.onPortalDetectionCompleted.removeListener(
self.onPortalDetectionCompleted);
done();
};
chrome.networkingPrivate.onPortalDetectionCompleted.addListener(
self.onPortalDetectionCompleted);
} }
}; };
...@@ -397,7 +411,13 @@ var availableTests = [ ...@@ -397,7 +411,13 @@ var availableTests = [
assertEq(expectedStatus, status); assertEq(expectedStatus, status);
})); }));
}); });
} },
function captivePortalNotification() {
var done = chrome.test.callbackAdded();
var listener =
new privateHelpers.watchForCaptivePortalState('wifi', 'Online', done);
chrome.test.sendMessage('notifyPortalDetectorObservers');
},
]; ];
var testToRun = window.location.search.substring(1); var testToRun = window.location.search.substring(1);
......
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