OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/net/network_portal_notification_controller.h" | 5 #include "chrome/browser/chromeos/net/network_portal_notification_controller.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/system/system_notifier.h" | 8 #include "ash/system/system_notifier.h" |
9 #include "ash/system/tray/system_tray_notifier.h" | 9 #include "ash/system/tray/system_tray_notifier.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "chrome/browser/chromeos/mobile/mobile_activator.h" | 18 #include "chrome/browser/chromeos/mobile/mobile_activator.h" |
19 #include "chrome/browser/chromeos/net/network_portal_web_dialog.h" | |
20 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
19 #include "chrome/browser/profiles/profile_manager.h" | 21 #include "chrome/browser/profiles/profile_manager.h" |
22 #include "chrome/browser/ui/browser_dialogs.h" | |
20 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 23 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
21 #include "chrome/browser/ui/singleton_tabs.h" | 24 #include "chrome/browser/ui/singleton_tabs.h" |
22 #include "chrome/grit/generated_resources.h" | 25 #include "chrome/grit/generated_resources.h" |
23 #include "chrome/grit/theme_resources.h" | 26 #include "chrome/grit/theme_resources.h" |
24 #include "chromeos/chromeos_switches.h" | 27 #include "chromeos/chromeos_switches.h" |
25 #include "chromeos/network/network_state.h" | 28 #include "chromeos/network/network_state.h" |
26 #include "components/captive_portal/captive_portal_detector.h" | 29 #include "components/captive_portal/captive_portal_detector.h" |
27 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
28 #include "ui/base/resource/resource_bundle.h" | 31 #include "ui/base/resource/resource_bundle.h" |
29 #include "ui/message_center/message_center.h" | 32 #include "ui/message_center/message_center.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
44 | 47 |
45 | 48 |
46 void CloseNotification() { | 49 void CloseNotification() { |
47 message_center::MessageCenter::Get()->RemoveNotification( | 50 message_center::MessageCenter::Get()->RemoveNotification( |
48 NetworkPortalNotificationController::kNotificationId, false); | 51 NetworkPortalNotificationController::kNotificationId, false); |
49 } | 52 } |
50 | 53 |
51 class NetworkPortalNotificationControllerDelegate | 54 class NetworkPortalNotificationControllerDelegate |
52 : public message_center::NotificationDelegate { | 55 : public message_center::NotificationDelegate { |
53 public: | 56 public: |
54 NetworkPortalNotificationControllerDelegate(): clicked_(false) {} | 57 NetworkPortalNotificationControllerDelegate( |
58 NetworkPortalNotificationController* controller) | |
59 : clicked_(false), controller_(controller->AsWeakPtr()) {} | |
55 | 60 |
56 // Overridden from message_center::NotificationDelegate: | 61 // Overridden from message_center::NotificationDelegate: |
57 virtual void Display() override; | 62 virtual void Display() override; |
58 virtual void Close(bool by_user) override; | 63 virtual void Close(bool by_user) override; |
59 virtual void Click() override; | 64 virtual void Click() override; |
60 | 65 |
61 private: | 66 private: |
62 virtual ~NetworkPortalNotificationControllerDelegate() {} | 67 bool clicked_; |
63 | 68 |
64 bool clicked_; | 69 base::WeakPtr<NetworkPortalNotificationController> controller_; |
65 | 70 |
66 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerDelegate); | 71 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerDelegate); |
67 }; | 72 }; |
68 | 73 |
69 void NetworkPortalNotificationControllerDelegate::Display() { | 74 void NetworkPortalNotificationControllerDelegate::Display() { |
70 UMA_HISTOGRAM_ENUMERATION( | 75 UMA_HISTOGRAM_ENUMERATION( |
71 NetworkPortalNotificationController::kNotificationMetric, | 76 NetworkPortalNotificationController::kNotificationMetric, |
72 NetworkPortalNotificationController::NOTIFICATION_METRIC_DISPLAYED, | 77 NetworkPortalNotificationController::NOTIFICATION_METRIC_DISPLAYED, |
73 NetworkPortalNotificationController::NOTIFICATION_METRIC_COUNT); | 78 NetworkPortalNotificationController::NOTIFICATION_METRIC_COUNT); |
74 } | 79 } |
(...skipping 11 matching lines...) Expand all Loading... | |
86 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT); | 91 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT); |
87 } | 92 } |
88 | 93 |
89 void NetworkPortalNotificationControllerDelegate::Click() { | 94 void NetworkPortalNotificationControllerDelegate::Click() { |
90 clicked_ = true; | 95 clicked_ = true; |
91 UMA_HISTOGRAM_ENUMERATION( | 96 UMA_HISTOGRAM_ENUMERATION( |
92 NetworkPortalNotificationController::kUserActionMetric, | 97 NetworkPortalNotificationController::kUserActionMetric, |
93 NetworkPortalNotificationController::USER_ACTION_METRIC_CLICKED, | 98 NetworkPortalNotificationController::USER_ACTION_METRIC_CLICKED, |
94 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT); | 99 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT); |
95 | 100 |
96 Profile* profile = ProfileManager::GetActiveUserProfile(); | 101 if (CommandLine::ForCurrentProcess()->HasSwitch( |
97 if (!profile) | 102 chromeos::switches::kEnableCaptivePortalBypassProxy)) { |
98 return; | 103 if (controller_) |
99 chrome::ScopedTabbedBrowserDisplayer displayer(profile, | 104 controller_->ShowDialog(); |
ygorshenin1
2014/11/24 15:02:07
I'm proposing to add a return statement at the end
Alexander Alekseev
2014/11/26 00:26:28
Done.
ygorshenin1
2014/11/26 16:33:50
Sorry, I missed CloseNotification() call. Could yo
| |
100 chrome::HOST_DESKTOP_TYPE_ASH); | 105 } else { |
101 GURL url(captive_portal::CaptivePortalDetector::kDefaultURL); | 106 Profile* profile = ProfileManager::GetActiveUserProfile(); |
102 chrome::ShowSingletonTab(displayer.browser(), url); | 107 if (!profile) |
108 return; | |
109 chrome::ScopedTabbedBrowserDisplayer displayer( | |
110 profile, chrome::HOST_DESKTOP_TYPE_ASH); | |
111 GURL url(captive_portal::CaptivePortalDetector::kDefaultURL); | |
112 chrome::ShowSingletonTab(displayer.browser(), url); | |
113 } | |
103 | 114 |
104 CloseNotification(); | 115 CloseNotification(); |
105 } | 116 } |
106 | 117 |
107 } // namespace | 118 } // namespace |
108 | 119 |
109 // static | 120 // static |
110 const char NetworkPortalNotificationController::kNotificationId[] = | 121 const char NetworkPortalNotificationController::kNotificationId[] = |
111 "chrome://net/network_portal_detector"; | 122 "chrome://net/network_portal_detector"; |
112 | 123 |
113 // static | 124 // static |
114 const char NetworkPortalNotificationController::kNotificationMetric[] = | 125 const char NetworkPortalNotificationController::kNotificationMetric[] = |
115 "CaptivePortal.Notification.Status"; | 126 "CaptivePortal.Notification.Status"; |
116 | 127 |
117 // static | 128 // static |
118 const char NetworkPortalNotificationController::kUserActionMetric[] = | 129 const char NetworkPortalNotificationController::kUserActionMetric[] = |
119 "CaptivePortal.Notification.UserAction"; | 130 "CaptivePortal.Notification.UserAction"; |
120 | 131 |
121 NetworkPortalNotificationController::NetworkPortalNotificationController() {} | 132 NetworkPortalNotificationController::NetworkPortalNotificationController() |
133 : dialog_(NULL) { | |
ygorshenin1
2014/11/24 15:02:07
nit: s/NULL/nullptr/g
Alexander Alekseev
2014/11/26 00:26:28
Done.
| |
134 } | |
122 | 135 |
123 NetworkPortalNotificationController::~NetworkPortalNotificationController() {} | 136 NetworkPortalNotificationController::~NetworkPortalNotificationController() {} |
124 | 137 |
125 void NetworkPortalNotificationController::OnPortalDetectionCompleted( | 138 void NetworkPortalNotificationController::OnPortalDetectionCompleted( |
126 const NetworkState* network, | 139 const NetworkState* network, |
127 const NetworkPortalDetector::CaptivePortalState& state) { | 140 const NetworkPortalDetector::CaptivePortalState& state) { |
128 if (!IsPortalNotificationEnabled()) | 141 if (!IsPortalNotificationEnabled()) |
129 return; | 142 return; |
130 | 143 |
131 if (!network || | 144 if (!network || |
132 state.status != NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL) { | 145 state.status != NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL) { |
133 last_network_path_.clear(); | 146 last_network_path_.clear(); |
147 | |
148 if (dialog_) | |
149 dialog_->Close(); | |
150 | |
134 CloseNotification(); | 151 CloseNotification(); |
135 return; | 152 return; |
136 } | 153 } |
137 | 154 |
138 // Don't do anything if we're currently activating the device. | 155 // Don't do anything if we're currently activating the device. |
139 if (MobileActivator::GetInstance()->RunningActivation()) | 156 if (MobileActivator::GetInstance()->RunningActivation()) |
140 return; | 157 return; |
141 | 158 |
142 // Don't do anything if notification for |network| already was | 159 // Don't do anything if notification for |network| already was |
143 // displayed. | 160 // displayed. |
144 if (network->path() == last_network_path_) | 161 if (network->path() == last_network_path_) |
145 return; | 162 return; |
146 last_network_path_ = network->path(); | 163 last_network_path_ = network->path(); |
147 | 164 |
148 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 165 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
149 gfx::Image& icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_ALERT); | 166 gfx::Image& icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_ALERT); |
150 message_center::NotifierId notifier_id( | 167 message_center::NotifierId notifier_id( |
151 message_center::NotifierId::SYSTEM_COMPONENT, | 168 message_center::NotifierId::SYSTEM_COMPONENT, |
152 ash::system_notifier::kNotifierNetworkPortalDetector); | 169 ash::system_notifier::kNotifierNetworkPortalDetector); |
153 | 170 |
154 scoped_ptr<Notification> notification(new Notification( | 171 scoped_ptr<Notification> notification(new Notification( |
155 message_center::NOTIFICATION_TYPE_SIMPLE, | 172 message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId, |
156 kNotificationId, | |
157 l10n_util::GetStringUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_TITLE), | 173 l10n_util::GetStringUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_TITLE), |
158 l10n_util::GetStringFUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_MESSAGE, | 174 l10n_util::GetStringFUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_MESSAGE, |
159 base::UTF8ToUTF16(network->name())), | 175 base::UTF8ToUTF16(network->name())), |
160 icon, | 176 icon, base::string16() /* display_source */, notifier_id, |
161 base::string16() /* display_source */, | |
162 notifier_id, | |
163 message_center::RichNotificationData(), | 177 message_center::RichNotificationData(), |
164 new NetworkPortalNotificationControllerDelegate())); | 178 new NetworkPortalNotificationControllerDelegate(this))); |
165 notification->SetSystemPriority(); | 179 notification->SetSystemPriority(); |
166 | 180 |
167 if (ash::Shell::HasInstance()) { | 181 if (ash::Shell::HasInstance()) { |
168 ash::Shell::GetInstance() | 182 ash::Shell::GetInstance() |
169 ->system_tray_notifier() | 183 ->system_tray_notifier() |
170 ->NotifyOnCaptivePortalDetected(network->path()); | 184 ->NotifyOnCaptivePortalDetected(network->path()); |
171 } | 185 } |
172 | 186 |
173 message_center::MessageCenter::Get()->AddNotification(notification.Pass()); | 187 message_center::MessageCenter::Get()->AddNotification(notification.Pass()); |
174 } | 188 } |
175 | 189 |
190 void NetworkPortalNotificationController::ShowDialog() { | |
191 if (!dialog_) { | |
ygorshenin1
2014/11/24 15:02:07
nit: I'm proposing to change the code in the follo
Alexander Alekseev
2014/11/26 00:26:28
Done.
| |
192 Profile* signin_profile = ProfileHelper::GetSigninProfile(); | |
193 dialog_ = new NetworkPortalWebDialog(this); | |
194 dialog_->SetWidget(views::Widget::GetWidgetForNativeWindow( | |
195 chrome::ShowWebDialog(NULL, signin_profile, dialog_))); | |
196 } | |
197 } | |
198 | |
199 void NetworkPortalNotificationController::OnDialogDestroyed( | |
200 const NetworkPortalWebDialog* dialog) { | |
201 if (dialog == dialog_) { | |
ygorshenin1
2014/11/24 15:02:08
When dialog_ is not equal to dialog? Could you ple
Alexander Alekseev
2014/11/26 00:26:28
Dialog close and destroy are asynchronous. So it i
| |
202 dialog_ = NULL; | |
203 ProfileHelper::Get()->ClearSigninProfile(base::Closure()); | |
204 } | |
205 } | |
206 | |
176 } // namespace chromeos | 207 } // namespace chromeos |
OLD | NEW |