1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/net/network_portal_notification_controller.h"
8 #include "ash/system/system_notifier.h"
9 #include "ash/system/tray/system_tray_notifier.h"
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/metrics/histogram.h"
16 #include "base/strings/string16.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
20 #include "chrome/browser/ui/singleton_tabs.h"
21 #include "chromeos/chromeos_switches.h"
22 #include "chromeos/network/network_state.h"
23 #include "components/captive_portal/captive_portal_detector.h"
24 #include "grit/generated_resources.h"
25 #include "grit/theme_resources.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/message_center/message_center.h"
29 #include "ui/message_center/notification.h"
30 #include "ui/message_center/notification_types.h"
31 #include "ui/message_center/notifier_settings.h"
33 using message_center::Notification
;
39 bool IsPortalNotificationEnabled() {
40 return !CommandLine::ForCurrentProcess()->HasSwitch(
41 switches::kDisableNetworkPortalNotification
);
45 void CloseNotification() {
46 message_center::MessageCenter::Get()->RemoveNotification(
47 NetworkPortalNotificationController::kNotificationId
, false);
50 class NetworkPortalNotificationControllerDelegate
51 : public message_center::NotificationDelegate
{
53 NetworkPortalNotificationControllerDelegate(): clicked_(false) {}
55 // Overridden from message_center::NotificationDelegate:
56 virtual void Display() OVERRIDE
;
57 virtual void Error() OVERRIDE
;
58 virtual void Close(bool by_user
) OVERRIDE
;
59 virtual void Click() OVERRIDE
;
62 virtual ~NetworkPortalNotificationControllerDelegate() {}
66 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerDelegate
);
69 void NetworkPortalNotificationControllerDelegate::Display() {
70 UMA_HISTOGRAM_ENUMERATION(
71 NetworkPortalNotificationController::kNotificationMetric
,
72 NetworkPortalNotificationController::NOTIFICATION_METRIC_DISPLAYED
,
73 NetworkPortalNotificationController::NOTIFICATION_METRIC_COUNT
);
76 void NetworkPortalNotificationControllerDelegate::Error() {
77 UMA_HISTOGRAM_ENUMERATION(
78 NetworkPortalNotificationController::kNotificationMetric
,
79 NetworkPortalNotificationController::NOTIFICATION_METRIC_ERROR
,
80 NetworkPortalNotificationController::NOTIFICATION_METRIC_COUNT
);
83 void NetworkPortalNotificationControllerDelegate::Close(bool by_user
) {
86 NetworkPortalNotificationController::UserActionMetric metric
=
88 ? NetworkPortalNotificationController::USER_ACTION_METRIC_CLOSED
89 : NetworkPortalNotificationController::USER_ACTION_METRIC_IGNORED
;
90 UMA_HISTOGRAM_ENUMERATION(
91 NetworkPortalNotificationController::kUserActionMetric
,
93 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT
);
96 void NetworkPortalNotificationControllerDelegate::Click() {
98 UMA_HISTOGRAM_ENUMERATION(
99 NetworkPortalNotificationController::kUserActionMetric
,
100 NetworkPortalNotificationController::USER_ACTION_METRIC_CLICKED
,
101 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT
);
103 Profile
* profile
= ProfileManager::GetActiveUserProfile();
106 chrome::ScopedTabbedBrowserDisplayer
displayer(profile
,
107 chrome::HOST_DESKTOP_TYPE_ASH
);
108 GURL
url(captive_portal::CaptivePortalDetector::kDefaultURL
);
109 chrome::ShowSingletonTab(displayer
.browser(), url
);
117 const char NetworkPortalNotificationController::kNotificationId
[] =
118 "chrome://net/network_portal_detector";
121 const char NetworkPortalNotificationController::kNotificationMetric
[] =
122 "CaptivePortal.Notification.Status";
125 const char NetworkPortalNotificationController::kUserActionMetric
[] =
126 "CaptivePortal.Notification.UserAction";
128 NetworkPortalNotificationController::NetworkPortalNotificationController() {}
130 NetworkPortalNotificationController::~NetworkPortalNotificationController() {}
132 void NetworkPortalNotificationController::OnPortalDetectionCompleted(
133 const NetworkState
* network
,
134 const NetworkPortalDetector::CaptivePortalState
& state
) {
135 if (!IsPortalNotificationEnabled())
139 state
.status
!= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
) {
140 last_network_path_
.clear();
145 // Don't do anything if notification for |network| already was
147 if (network
->path() == last_network_path_
)
149 last_network_path_
= network
->path();
151 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
152 gfx::Image
& icon
= bundle
.GetImageNamed(IDR_PORTAL_DETECTION_ALERT
);
153 message_center::NotifierId
notifier_id(
154 message_center::NotifierId::SYSTEM_COMPONENT
,
155 ash::system_notifier::kNotifierNetworkPortalDetector
);
157 scoped_ptr
<Notification
> notification(new Notification(
158 message_center::NOTIFICATION_TYPE_SIMPLE
,
160 l10n_util::GetStringUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_TITLE
),
161 l10n_util::GetStringFUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_MESSAGE
,
162 base::UTF8ToUTF16(network
->name())),
164 base::string16() /* display_source */,
166 message_center::RichNotificationData(),
167 new NetworkPortalNotificationControllerDelegate()));
168 notification
->SetSystemPriority();
170 if (ash::Shell::HasInstance()) {
171 ash::Shell::GetInstance()
172 ->system_tray_notifier()
173 ->NotifyOnCaptivePortalDetected(network
->path());
176 message_center::MessageCenter::Get()->AddNotification(notification
.Pass());
179 } // namespace chromeos