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