Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/net/network_portal_notification_controller.h" | |
| 6 | |
| 7 #include "ash/system/system_notifier.h" | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "chrome/browser/captive_portal/captive_portal_detector.h" | |
| 16 #include "chrome/browser/profiles/profile_manager.h" | |
| 17 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | |
| 18 #include "chrome/browser/ui/singleton_tabs.h" | |
| 19 #include "chromeos/chromeos_switches.h" | |
| 20 #include "chromeos/network/network_state.h" | |
| 21 #include "grit/generated_resources.h" | |
| 22 #include "grit/theme_resources.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | |
| 24 #include "ui/base/resource/resource_bundle.h" | |
| 25 #include "ui/message_center/message_center.h" | |
| 26 #include "ui/message_center/notification.h" | |
| 27 #include "ui/message_center/notification_types.h" | |
| 28 #include "ui/message_center/notifier_settings.h" | |
| 29 | |
| 30 using message_center::Notification; | |
| 31 | |
| 32 namespace chromeos { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 bool IsPortalNotificationEnabled() { | |
| 37 return CommandLine::ForCurrentProcess()->HasSwitch( | |
| 38 switches::kEnableNetworkPortalNotification); | |
| 39 } | |
| 40 | |
| 41 bool NotificationDisplayed() { | |
| 42 return message_center::MessageCenter::Get()->HasNotification( | |
| 43 NetworkPortalNotificationController::kNotificationId); | |
| 44 } | |
| 45 | |
| 46 void CloseNotification() { | |
| 47 message_center::MessageCenter::Get()->RemoveNotification( | |
| 48 NetworkPortalNotificationController::kNotificationId, false); | |
| 49 } | |
| 50 | |
| 51 class NetworkPortalNotificationControllerDelegate | |
| 52 : public message_center::NotificationDelegate { | |
| 53 public: | |
| 54 NetworkPortalNotificationControllerDelegate() {} | |
| 55 | |
| 56 // Overridden from message_center::NotificationDelegate: | |
| 57 virtual void Display() OVERRIDE {} | |
| 58 virtual void Error() OVERRIDE {} | |
| 59 virtual void Close(bool /* by_user */) OVERRIDE {} | |
| 60 virtual void Click() OVERRIDE {} | |
| 61 virtual void ButtonClick(int button_index) OVERRIDE; | |
| 62 | |
| 63 private: | |
| 64 virtual ~NetworkPortalNotificationControllerDelegate() {} | |
|
Jun Mukai
2014/02/20 18:30:15
Did your compiler or presubmit script raise warnin
ygorshenin1
2014/02/20 19:18:45
clang complains that classes that are ref-counted
| |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerDelegate); | |
| 67 }; | |
| 68 | |
| 69 void NetworkPortalNotificationControllerDelegate::ButtonClick( | |
| 70 int button_index) { | |
| 71 if (button_index != 0) | |
|
oshima
2014/02/20 19:00:54
optional:
!button_index
is more common in chromi
Jun Mukai
2014/02/20 19:04:20
or DCHECK_EQ(0, button_index)
Since this notificat
ygorshenin1
2014/02/20 19:18:45
Done.
| |
| 72 return; | |
| 73 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 74 if (!profile) | |
| 75 return; | |
| 76 chrome::ScopedTabbedBrowserDisplayer displayer(profile, | |
| 77 chrome::HOST_DESKTOP_TYPE_ASH); | |
| 78 GURL url(captive_portal::CaptivePortalDetector::kDefaultURL); | |
| 79 chrome::ShowSingletonTab(displayer.browser(), url); | |
| 80 | |
| 81 CloseNotification(); | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 const char NetworkPortalNotificationController::kNotificationId[] = | |
| 87 "chrome://net/network_portal_detector"; | |
| 88 | |
| 89 NetworkPortalNotificationController::NetworkPortalNotificationController() {} | |
| 90 | |
| 91 NetworkPortalNotificationController::~NetworkPortalNotificationController() {} | |
| 92 | |
| 93 void NetworkPortalNotificationController::OnPortalDetectionCompleted( | |
| 94 const NetworkState* network, | |
| 95 const NetworkPortalDetector::CaptivePortalState& state) { | |
| 96 if (!IsPortalNotificationEnabled()) | |
| 97 return; | |
| 98 | |
| 99 if (!network || | |
| 100 state.status != NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL) { | |
| 101 last_network_path_.clear(); | |
| 102 CloseNotification(); | |
| 103 return; | |
| 104 } | |
| 105 | |
| 106 // Don't do anything if notification for |network| already was | |
| 107 // displayed. | |
| 108 if (network->path() == last_network_path_) | |
| 109 return; | |
| 110 last_network_path_ = network->path(); | |
| 111 | |
| 112 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 113 gfx::Image& icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_ALERT); | |
| 114 message_center::NotifierId notifier_id( | |
| 115 message_center::NotifierId::SYSTEM_COMPONENT, | |
| 116 ash::system_notifier::kNotifierNetworkPortalDetector); | |
| 117 | |
| 118 message_center::ButtonInfo signin_button(l10n_util::GetStringUTF16( | |
| 119 IDS_PORTAL_DETECTION_NOTIFICATION_SIGNIN_BUTTON)); | |
| 120 signin_button.icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_GLOBE); | |
| 121 message_center::RichNotificationData data; | |
| 122 data.buttons.push_back(signin_button); | |
| 123 | |
| 124 scoped_ptr<Notification> notification(new Notification( | |
| 125 message_center::NOTIFICATION_TYPE_SIMPLE, | |
| 126 kNotificationId, | |
| 127 l10n_util::GetStringUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_TITLE), | |
| 128 l10n_util::GetStringFUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_MESSAGE, | |
| 129 base::UTF8ToUTF16(network->name())), | |
| 130 icon, | |
| 131 base::string16() /* display_source */, | |
| 132 notifier_id, | |
| 133 data, | |
| 134 new NetworkPortalNotificationControllerDelegate())); | |
| 135 notification->SetSystemPriority(); | |
| 136 | |
| 137 message_center::MessageCenter::Get()->AddNotification(notification.Pass()); | |
| 138 } | |
| 139 | |
| 140 } // namespace chromeos | |
| OLD | NEW |