Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: chrome/browser/chromeos/net/network_portal_notification_controller.cc

Issue 750153002: ChromeOS: bypass proxy for captive portal authorization. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
30 #include "ui/message_center/notification.h" 33 #include "ui/message_center/notification.h"
31 #include "ui/message_center/notification_types.h" 34 #include "ui/message_center/notification_types.h"
32 #include "ui/message_center/notifier_settings.h" 35 #include "ui/message_center/notifier_settings.h"
36 #include "ui/views/widget/widget.h"
33 37
34 using message_center::Notification; 38 using message_center::Notification;
35 39
36 namespace chromeos { 40 namespace chromeos {
37 41
38 namespace { 42 namespace {
39 43
40 bool IsPortalNotificationEnabled() { 44 bool IsPortalNotificationEnabled() {
41 return !CommandLine::ForCurrentProcess()->HasSwitch( 45 return !CommandLine::ForCurrentProcess()->HasSwitch(
42 switches::kDisableNetworkPortalNotification); 46 switches::kDisableNetworkPortalNotification);
43 } 47 }
44 48
45 49
46 void CloseNotification() { 50 void CloseNotification() {
47 message_center::MessageCenter::Get()->RemoveNotification( 51 message_center::MessageCenter::Get()->RemoveNotification(
48 NetworkPortalNotificationController::kNotificationId, false); 52 NetworkPortalNotificationController::kNotificationId, false);
49 } 53 }
50 54
51 class NetworkPortalNotificationControllerDelegate 55 class NetworkPortalNotificationControllerDelegate
52 : public message_center::NotificationDelegate { 56 : public message_center::NotificationDelegate {
53 public: 57 public:
54 NetworkPortalNotificationControllerDelegate(): clicked_(false) {} 58 explicit NetworkPortalNotificationControllerDelegate(
59 base::WeakPtr<NetworkPortalNotificationController> controller)
60 : clicked_(false), controller_(controller) {}
55 61
56 // Overridden from message_center::NotificationDelegate: 62 // Overridden from message_center::NotificationDelegate:
57 virtual void Display() override; 63 virtual void Display() override;
58 virtual void Close(bool by_user) override; 64 virtual void Close(bool by_user) override;
59 virtual void Click() override; 65 virtual void Click() override;
60 66
61 private: 67 private:
62 virtual ~NetworkPortalNotificationControllerDelegate() {} 68 virtual ~NetworkPortalNotificationControllerDelegate() {}
63 69
64 bool clicked_; 70 bool clicked_;
65 71
72 base::WeakPtr<NetworkPortalNotificationController> controller_;
73
66 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerDelegate); 74 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerDelegate);
67 }; 75 };
68 76
69 void NetworkPortalNotificationControllerDelegate::Display() { 77 void NetworkPortalNotificationControllerDelegate::Display() {
70 UMA_HISTOGRAM_ENUMERATION( 78 UMA_HISTOGRAM_ENUMERATION(
71 NetworkPortalNotificationController::kNotificationMetric, 79 NetworkPortalNotificationController::kNotificationMetric,
72 NetworkPortalNotificationController::NOTIFICATION_METRIC_DISPLAYED, 80 NetworkPortalNotificationController::NOTIFICATION_METRIC_DISPLAYED,
73 NetworkPortalNotificationController::NOTIFICATION_METRIC_COUNT); 81 NetworkPortalNotificationController::NOTIFICATION_METRIC_COUNT);
74 } 82 }
75 83
(...skipping 10 matching lines...) Expand all
86 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT); 94 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT);
87 } 95 }
88 96
89 void NetworkPortalNotificationControllerDelegate::Click() { 97 void NetworkPortalNotificationControllerDelegate::Click() {
90 clicked_ = true; 98 clicked_ = true;
91 UMA_HISTOGRAM_ENUMERATION( 99 UMA_HISTOGRAM_ENUMERATION(
92 NetworkPortalNotificationController::kUserActionMetric, 100 NetworkPortalNotificationController::kUserActionMetric,
93 NetworkPortalNotificationController::USER_ACTION_METRIC_CLICKED, 101 NetworkPortalNotificationController::USER_ACTION_METRIC_CLICKED,
94 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT); 102 NetworkPortalNotificationController::USER_ACTION_METRIC_COUNT);
95 103
96 Profile* profile = ProfileManager::GetActiveUserProfile(); 104 if (CommandLine::ForCurrentProcess()->HasSwitch(
97 if (!profile) 105 chromeos::switches::kEnableCaptivePortalBypassProxy)) {
98 return; 106 if (controller_)
99 chrome::ScopedTabbedBrowserDisplayer displayer(profile, 107 controller_->ShowDialog();
100 chrome::HOST_DESKTOP_TYPE_ASH); 108 } else {
101 GURL url(captive_portal::CaptivePortalDetector::kDefaultURL); 109 Profile* profile = ProfileManager::GetActiveUserProfile();
102 chrome::ShowSingletonTab(displayer.browser(), url); 110 if (!profile)
103 111 return;
112 chrome::ScopedTabbedBrowserDisplayer displayer(
113 profile, chrome::HOST_DESKTOP_TYPE_ASH);
114 GURL url(captive_portal::CaptivePortalDetector::kDefaultURL);
115 chrome::ShowSingletonTab(displayer.browser(), url);
116 }
104 CloseNotification(); 117 CloseNotification();
105 } 118 }
106 119
107 } // namespace 120 } // namespace
108 121
109 // static 122 // static
110 const char NetworkPortalNotificationController::kNotificationId[] = 123 const char NetworkPortalNotificationController::kNotificationId[] =
111 "chrome://net/network_portal_detector"; 124 "chrome://net/network_portal_detector";
112 125
113 // static 126 // static
114 const char NetworkPortalNotificationController::kNotificationMetric[] = 127 const char NetworkPortalNotificationController::kNotificationMetric[] =
115 "CaptivePortal.Notification.Status"; 128 "CaptivePortal.Notification.Status";
116 129
117 // static 130 // static
118 const char NetworkPortalNotificationController::kUserActionMetric[] = 131 const char NetworkPortalNotificationController::kUserActionMetric[] =
119 "CaptivePortal.Notification.UserAction"; 132 "CaptivePortal.Notification.UserAction";
120 133
121 NetworkPortalNotificationController::NetworkPortalNotificationController() {} 134 NetworkPortalNotificationController::NetworkPortalNotificationController()
135 : dialog_(nullptr) {
136 }
122 137
123 NetworkPortalNotificationController::~NetworkPortalNotificationController() {} 138 NetworkPortalNotificationController::~NetworkPortalNotificationController() {}
124 139
125 void NetworkPortalNotificationController::OnPortalDetectionCompleted( 140 void NetworkPortalNotificationController::OnPortalDetectionCompleted(
126 const NetworkState* network, 141 const NetworkState* network,
127 const NetworkPortalDetector::CaptivePortalState& state) { 142 const NetworkPortalDetector::CaptivePortalState& state) {
128 if (!IsPortalNotificationEnabled()) 143 if (!IsPortalNotificationEnabled())
129 return; 144 return;
130 145
131 if (!network || 146 if (!network ||
132 state.status != NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL) { 147 state.status != NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL) {
133 last_network_path_.clear(); 148 last_network_path_.clear();
149
150 if (dialog_)
151 dialog_->Close();
152
134 CloseNotification(); 153 CloseNotification();
135 return; 154 return;
136 } 155 }
137 156
138 // Don't do anything if we're currently activating the device. 157 // Don't do anything if we're currently activating the device.
139 if (MobileActivator::GetInstance()->RunningActivation()) 158 if (MobileActivator::GetInstance()->RunningActivation())
140 return; 159 return;
141 160
142 // Don't do anything if notification for |network| already was 161 // Don't do anything if notification for |network| already was
143 // displayed. 162 // displayed.
144 if (network->path() == last_network_path_) 163 if (network->path() == last_network_path_)
145 return; 164 return;
146 last_network_path_ = network->path(); 165 last_network_path_ = network->path();
147 166
148 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); 167 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
149 gfx::Image& icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_ALERT); 168 gfx::Image& icon = bundle.GetImageNamed(IDR_PORTAL_DETECTION_ALERT);
150 message_center::NotifierId notifier_id( 169 message_center::NotifierId notifier_id(
151 message_center::NotifierId::SYSTEM_COMPONENT, 170 message_center::NotifierId::SYSTEM_COMPONENT,
152 ash::system_notifier::kNotifierNetworkPortalDetector); 171 ash::system_notifier::kNotifierNetworkPortalDetector);
153 172
154 scoped_ptr<Notification> notification(new Notification( 173 scoped_ptr<Notification> notification(new Notification(
155 message_center::NOTIFICATION_TYPE_SIMPLE, 174 message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId,
156 kNotificationId,
157 l10n_util::GetStringUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_TITLE), 175 l10n_util::GetStringUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_TITLE),
158 l10n_util::GetStringFUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_MESSAGE, 176 l10n_util::GetStringFUTF16(IDS_PORTAL_DETECTION_NOTIFICATION_MESSAGE,
159 base::UTF8ToUTF16(network->name())), 177 base::UTF8ToUTF16(network->name())),
160 icon, 178 icon, base::string16() /* display_source */, notifier_id,
161 base::string16() /* display_source */,
162 notifier_id,
163 message_center::RichNotificationData(), 179 message_center::RichNotificationData(),
164 new NetworkPortalNotificationControllerDelegate())); 180 new NetworkPortalNotificationControllerDelegate(AsWeakPtr())));
165 notification->SetSystemPriority(); 181 notification->SetSystemPriority();
166 182
167 if (ash::Shell::HasInstance()) { 183 if (ash::Shell::HasInstance()) {
168 ash::Shell::GetInstance() 184 ash::Shell::GetInstance()
169 ->system_tray_notifier() 185 ->system_tray_notifier()
170 ->NotifyOnCaptivePortalDetected(network->path()); 186 ->NotifyOnCaptivePortalDetected(network->path());
171 } 187 }
172 188
173 message_center::MessageCenter::Get()->AddNotification(notification.Pass()); 189 message_center::MessageCenter::Get()->AddNotification(notification.Pass());
174 } 190 }
175 191
192 void NetworkPortalNotificationController::ShowDialog() {
193 if (dialog_)
194 return;
195
196 Profile* signin_profile = ProfileHelper::GetSigninProfile();
197 dialog_ = new NetworkPortalWebDialog(AsWeakPtr());
198 dialog_->SetWidget(views::Widget::GetWidgetForNativeWindow(
199 chrome::ShowWebDialog(nullptr, signin_profile, dialog_)));
200 }
201
202 void NetworkPortalNotificationController::OnDialogDestroyed(
203 const NetworkPortalWebDialog* dialog) {
204 if (dialog == dialog_) {
205 dialog_ = nullptr;
206 ProfileHelper::Get()->ClearSigninProfile(base::Closure());
207 }
208 }
209
176 } // namespace chromeos 210 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698