Skip to content

Commit

Permalink
Added in-session captive portal notification.
Browse files Browse the repository at this point in the history
BUG=240237
TEST=unit_tests:NetworkPortalDetectorNotification*

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@252547 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
ygorshenin@chromium.org committed Feb 21, 2014
1 parent bd03790 commit 81f89e9
Show file tree
Hide file tree
Showing 13 changed files with 418 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ash/system/system_notifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const char* kAshSystemNotifiers[] = {
kNotifierMultiProfileFirstRun,
kNotifierNetwork,
kNotifierNetworkError,
kNotifierNetworkPortalDetector,
kNotifierScreenshot,
kNotifierScreenCapture,
kNotifierScreenShare,
Expand Down Expand Up @@ -59,6 +60,7 @@ const char kNotifierLocallyManagedUser[] = "ash.locally-managed-user";
const char kNotifierMultiProfileFirstRun[] = "ash.multi-profile.first-run";
const char kNotifierNetwork[] = "ash.network";
const char kNotifierNetworkError[] = "ash.network.error";
const char kNotifierNetworkPortalDetector[] = "ash.network.portal-detector";
const char kNotifierScreenshot[] = "ash.screenshot";
const char kNotifierScreenCapture[] = "ash.screen-capture";
const char kNotifierScreenShare[] = "ash.screen-share";
Expand Down
1 change: 1 addition & 0 deletions ash/system/system_notifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ASH_EXPORT extern const char kNotifierLocallyManagedUser[];
ASH_EXPORT extern const char kNotifierMultiProfileFirstRun[];
ASH_EXPORT extern const char kNotifierNetwork[];
ASH_EXPORT extern const char kNotifierNetworkError[];
ASH_EXPORT extern const char kNotifierNetworkPortalDetector[];
ASH_EXPORT extern const char kNotifierScreenshot[];
ASH_EXPORT extern const char kNotifierScreenCapture[];
ASH_EXPORT extern const char kNotifierScreenShare[];
Expand Down
16 changes: 16 additions & 0 deletions chrome/app/chromeos_strings.grdp
Original file line number Diff line number Diff line change
Expand Up @@ -4967,4 +4967,20 @@ All users must sign out to continue.
Check charger
</message>

<!-- Network portal notification -->
<message name="IDS_FLAGS_NETWORK_PORTAL_NOTIFICATION_NAME" desc="Title for the flag to enable/disable notifications about captive portals.">
Notifications about captive portals
</message>
<message name="IDS_FLAGS_NETWORK_PORTAL_NOTIFICATION_DESCRIPTION" desc="Description for the flag to enable/disable notifications about captive portals.">
If enabled, notification is displayed when device is connected to a network behind captive portal.
</message>
<message name="IDS_PORTAL_DETECTION_NOTIFICATION_TITLE" desc="Title for the system notification that current network is behind captive portal">
Action required to connect
</message>
<message name="IDS_PORTAL_DETECTION_NOTIFICATION_MESSAGE" desc="Body of the system notification that current network is behind captive portal">
To use <ph name="NETWORK_ID">$1<ex>Public Wifi</ex></ph> you need to visit the network's login page.
</message>
<message name="IDS_PORTAL_DETECTION_NOTIFICATION_SIGNIN_BUTTON" desc="Label on the button on the system notification that current network is behind captive portal. When clicked, captive portal login page is opened.">
Go to login page
</message>
</grit-part>
9 changes: 9 additions & 0 deletions chrome/browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,15 @@ const Experiment kExperiments[] = {
ash::switches::kAshEnableImmersiveFullscreenForAllWindows,
ash::switches::kAshEnableImmersiveFullscreenForBrowserOnly)
},
{
"network-portal-notification",
IDS_FLAGS_NETWORK_PORTAL_NOTIFICATION_NAME,
IDS_FLAGS_NETWORK_PORTAL_NOTIFICATION_DESCRIPTION,
kOsCrOS,
ENABLE_DISABLE_VALUE_TYPE(
chromeos::switches::kEnableNetworkPortalNotification,
chromeos::switches::kDisableNetworkPortalNotification)
},
#endif
{
"enable-download-resumption",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ void NetworkPortalDetectorImpl::NotifyPortalDetectionCompleted(
const CaptivePortalState& state) {
FOR_EACH_OBSERVER(Observer, observers_,
OnPortalDetectionCompleted(network, state));
notification_controller_.OnPortalDetectionCompleted(network, state);
}

base::TimeTicks NetworkPortalDetectorImpl::GetCurrentTimeTicks() const {
Expand Down
4 changes: 4 additions & 0 deletions chrome/browser/chromeos/net/network_portal_detector_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "base/time/time.h"
#include "chrome/browser/captive_portal/captive_portal_detector.h"
#include "chrome/browser/chromeos/net/network_portal_detector.h"
#include "chrome/browser/chromeos/net/network_portal_notification_controller.h"
#include "chromeos/network/network_state_handler_observer.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
Expand Down Expand Up @@ -259,6 +260,9 @@ class NetworkPortalDetectorImpl
// True if |request_timeout_for_testing_| is initialized.
bool request_timeout_for_testing_initialized_;

// UI notification controller about captive portal state.
NetworkPortalNotificationController notification_controller_;

content::NotificationRegistrar registrar_;

DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl);
Expand Down
136 changes: 136 additions & 0 deletions chrome/browser/chromeos/net/network_portal_notification_controller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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.

#include "chrome/browser/chromeos/net/network_portal_notification_controller.h"

#include "ash/system/system_notifier.h"
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/captive_portal/captive_portal_detector.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/browser/ui/singleton_tabs.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/network/network_state.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification.h"
#include "ui/message_center/notification_types.h"
#include "ui/message_center/notifier_settings.h"

using message_center::Notification;

namespace chromeos {

namespace {

bool IsPortalNotificationEnabled() {
return CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableNetworkPortalNotification);
}


void CloseNotification() {
message_center::MessageCenter::Get()->RemoveNotification(
NetworkPortalNotificationController::kNotificationId, false);
}

class NetworkPortalNotificationControllerDelegate
: public message_center::NotificationDelegate {
public:
NetworkPortalNotificationControllerDelegate() {}

// Overridden from message_center::NotificationDelegate:
virtual void Display() OVERRIDE {}
virtual void Error() OVERRIDE {}
virtual void Close(bool /* by_user */) OVERRIDE {}
virtual void Click() OVERRIDE {}
virtual void ButtonClick(int button_index) OVERRIDE;

private:
virtual ~NetworkPortalNotificationControllerDelegate() {}

DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerDelegate);
};

void NetworkPortalNotificationControllerDelegate::ButtonClick(
int button_index) {
if (!button_index)
return;
Profile* profile = ProfileManager::GetActiveUserProfile();
if (!profile)
return;
chrome::ScopedTabbedBrowserDisplayer displayer(profile,
chrome::HOST_DESKTOP_TYPE_ASH);
GURL url(captive_portal::CaptivePortalDetector::kDefaultURL);
chrome::ShowSingletonTab(displayer.browser(), url);

CloseNotification();
}

} // namespace

const char NetworkPortalNotificationController::kNotificationId[] =
"chrome://net/network_portal_detector";

NetworkPortalNotificationController::NetworkPortalNotificationController() {}

NetworkPortalNotificationController::~NetworkPortalNotificationController() {}

void NetworkPortalNotificationController::OnPortalDetectionCompleted(
const NetworkState* network,
const NetworkPortalDetector::CaptivePortalState& state) {
if (!IsPortalNotificationEnabled())
return;

if (!network ||
state.status != NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL) {
last_network_path_.clear();
CloseNotification();
return;
}

// Don't do anything if notification for |network| already was
// displayed.
if (network->path() == last_network_path_)
return;
last_network_path_ = network->path();

ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
gfx::Image& icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_ALERT);
message_center::NotifierId notifier_id(
message_center::NotifierId::SYSTEM_COMPONENT,
ash::system_notifier::kNotifierNetworkPortalDetector);

message_center::ButtonInfo signin_button(l10n_util::GetStringUTF16(
IDS_PORTAL_DETECTION_NOTIFICATION_SIGNIN_BUTTON));
signin_button.icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_GLOBE);
message_center::RichNotificationData data;
data.buttons.push_back(signin_button);

scoped_ptr<Notification> notification(new Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
kNotificationId,
l10n_util::GetStringUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_TITLE),
l10n_util::GetStringFUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_MESSAGE,
base::UTF8ToUTF16(network->name())),
icon,
base::string16() /* display_source */,
notifier_id,
data,
new NetworkPortalNotificationControllerDelegate()));
notification->SetSystemPriority();

message_center::MessageCenter::Get()->AddNotification(notification.Pass());
}

} // namespace chromeos
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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.

#ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_NOTIFICATION_CONTROLLER_H_
#define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_NOTIFICATION_CONTROLLER_H_

#include <string>

#include "base/macros.h"
#include "chrome/browser/chromeos/net/network_portal_detector.h"

namespace chromeos {

class NetworkState;

class NetworkPortalNotificationController {
public:
static const char kNotificationId[];

NetworkPortalNotificationController();
virtual ~NetworkPortalNotificationController();

void OnPortalDetectionCompleted(
const NetworkState* network,
const NetworkPortalDetector::CaptivePortalState& state);

private:
// Last network path for which notification was displayed.
std::string last_network_path_;

DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationController);
};

} // namespace chromeos

#endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_NOTIFICATION_CONTROLLER_H_
Loading

0 comments on commit 81f89e9

Please sign in to comment.