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