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 |
| 42 void CloseNotification() { |
| 43 message_center::MessageCenter::Get()->RemoveNotification( |
| 44 NetworkPortalNotificationController::kNotificationId, false); |
| 45 } |
| 46 |
| 47 class NetworkPortalNotificationControllerDelegate |
| 48 : public message_center::NotificationDelegate { |
| 49 public: |
| 50 NetworkPortalNotificationControllerDelegate() {} |
| 51 |
| 52 // Overridden from message_center::NotificationDelegate: |
| 53 virtual void Display() OVERRIDE {} |
| 54 virtual void Error() OVERRIDE {} |
| 55 virtual void Close(bool /* by_user */) OVERRIDE {} |
| 56 virtual void Click() OVERRIDE {} |
| 57 virtual void ButtonClick(int button_index) OVERRIDE; |
| 58 |
| 59 private: |
| 60 virtual ~NetworkPortalNotificationControllerDelegate() {} |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerDelegate); |
| 63 }; |
| 64 |
| 65 void NetworkPortalNotificationControllerDelegate::ButtonClick( |
| 66 int button_index) { |
| 67 if (!button_index) |
| 68 return; |
| 69 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 70 if (!profile) |
| 71 return; |
| 72 chrome::ScopedTabbedBrowserDisplayer displayer(profile, |
| 73 chrome::HOST_DESKTOP_TYPE_ASH); |
| 74 GURL url(captive_portal::CaptivePortalDetector::kDefaultURL); |
| 75 chrome::ShowSingletonTab(displayer.browser(), url); |
| 76 |
| 77 CloseNotification(); |
| 78 } |
| 79 |
| 80 } // namespace |
| 81 |
| 82 const char NetworkPortalNotificationController::kNotificationId[] = |
| 83 "chrome://net/network_portal_detector"; |
| 84 |
| 85 NetworkPortalNotificationController::NetworkPortalNotificationController() {} |
| 86 |
| 87 NetworkPortalNotificationController::~NetworkPortalNotificationController() {} |
| 88 |
| 89 void NetworkPortalNotificationController::OnPortalDetectionCompleted( |
| 90 const NetworkState* network, |
| 91 const NetworkPortalDetector::CaptivePortalState& state) { |
| 92 if (!IsPortalNotificationEnabled()) |
| 93 return; |
| 94 |
| 95 if (!network || |
| 96 state.status != NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL) { |
| 97 last_network_path_.clear(); |
| 98 CloseNotification(); |
| 99 return; |
| 100 } |
| 101 |
| 102 // Don't do anything if notification for |network| already was |
| 103 // displayed. |
| 104 if (network->path() == last_network_path_) |
| 105 return; |
| 106 last_network_path_ = network->path(); |
| 107 |
| 108 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| 109 gfx::Image& icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_ALERT); |
| 110 message_center::NotifierId notifier_id( |
| 111 message_center::NotifierId::SYSTEM_COMPONENT, |
| 112 ash::system_notifier::kNotifierNetworkPortalDetector); |
| 113 |
| 114 message_center::ButtonInfo signin_button(l10n_util::GetStringUTF16( |
| 115 IDS_PORTAL_DETECTION_NOTIFICATION_SIGNIN_BUTTON)); |
| 116 signin_button.icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_GLOBE); |
| 117 message_center::RichNotificationData data; |
| 118 data.buttons.push_back(signin_button); |
| 119 |
| 120 scoped_ptr<Notification> notification(new Notification( |
| 121 message_center::NOTIFICATION_TYPE_SIMPLE, |
| 122 kNotificationId, |
| 123 l10n_util::GetStringUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_TITLE), |
| 124 l10n_util::GetStringFUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_MESSAGE, |
| 125 base::UTF8ToUTF16(network->name())), |
| 126 icon, |
| 127 base::string16() /* display_source */, |
| 128 notifier_id, |
| 129 data, |
| 130 new NetworkPortalNotificationControllerDelegate())); |
| 131 notification->SetSystemPriority(); |
| 132 |
| 133 message_center::MessageCenter::Get()->AddNotification(notification.Pass()); |
| 134 } |
| 135 |
| 136 } // namespace chromeos |
OLD | NEW |