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.
5 #include "chrome/browser/chromeos/login/ui/captive_portal_window_proxy.h"
7 #include "base/metrics/histogram_macros.h"
8 #include "chrome/browser/chromeos/login/ui/captive_portal_view.h"
9 #include "chrome/browser/chromeos/login/ui/proxy_settings_dialog.h"
10 #include "chrome/browser/chromeos/profiles/profile_helper.h"
11 #include "components/web_modal/web_contents_modal_dialog_host.h"
12 #include "components/web_modal/web_contents_modal_dialog_manager.h"
13 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
14 #include "ui/views/widget/widget.h"
18 // The captive portal dialog is system-modal, but uses the web-content-modal
19 // dialog manager (odd) and requires this atypical dialog widget initialization.
20 views::Widget
* CreateWindowAsFramelessChild(views::WidgetDelegate
* delegate
,
21 gfx::NativeView parent
) {
22 views::Widget
* widget
= new views::Widget
;
24 views::Widget::InitParams params
;
25 params
.delegate
= delegate
;
27 params
.parent
= parent
;
28 params
.remove_standard_frame
= true;
29 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
39 CaptivePortalWindowProxy::CaptivePortalWindowProxy(
41 content::WebContents
* web_contents
)
42 : delegate_(delegate
),
44 web_contents_(web_contents
),
45 captive_portal_view_for_testing_(NULL
) {
46 DCHECK(GetState() == STATE_IDLE
);
49 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
52 DCHECK(GetState() == STATE_DISPLAYED
);
53 widget_
->RemoveObserver(this);
57 void CaptivePortalWindowProxy::ShowIfRedirected() {
58 if (GetState() != STATE_IDLE
)
60 InitCaptivePortalView();
61 DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION
);
64 void CaptivePortalWindowProxy::Show() {
65 if (ProxySettingsDialog::IsShown()) {
66 // ProxySettingsDialog is being shown, don't cover it.
71 if (GetState() == STATE_DISPLAYED
) // Dialog is already shown, do nothing.
74 InitCaptivePortalView();
76 CaptivePortalView
* portal
= captive_portal_view_
.release();
78 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents_
);
79 widget_
= CreateWindowAsFramelessChild(
81 manager
->delegate()->GetWebContentsModalDialogHost()->GetHostView());
83 widget_
->AddObserver(this);
84 manager
->ShowModalDialog(widget_
->GetNativeView());
87 void CaptivePortalWindowProxy::Close() {
88 if (GetState() == STATE_DISPLAYED
)
90 captive_portal_view_
.reset();
91 captive_portal_view_for_testing_
= NULL
;
94 void CaptivePortalWindowProxy::OnRedirected() {
95 if (GetState() == STATE_WAITING_FOR_REDIRECTION
) {
96 if (!started_loading_at_
.is_null()) {
97 UMA_HISTOGRAM_TIMES("CaptivePortal.RedirectTime",
98 base::Time::Now() - started_loading_at_
);
99 started_loading_at_
= base::Time();
103 delegate_
->OnPortalDetected();
106 void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
110 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget
* widget
) {
111 DCHECK(GetState() == STATE_DISPLAYED
);
112 DCHECK(widget
== widget_
);
114 DetachFromWidget(widget
);
116 DCHECK(GetState() == STATE_IDLE
);
119 void CaptivePortalWindowProxy::OnWidgetDestroying(views::Widget
* widget
) {
120 DetachFromWidget(widget
);
123 void CaptivePortalWindowProxy::OnWidgetDestroyed(views::Widget
* widget
) {
124 DetachFromWidget(widget
);
127 void CaptivePortalWindowProxy::InitCaptivePortalView() {
128 DCHECK(GetState() == STATE_IDLE
||
129 GetState() == STATE_WAITING_FOR_REDIRECTION
);
130 if (!captive_portal_view_
.get()) {
131 captive_portal_view_
.reset(
132 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
133 captive_portal_view_for_testing_
= captive_portal_view_
.get();
136 started_loading_at_
= base::Time::Now();
137 captive_portal_view_
->StartLoad();
140 CaptivePortalWindowProxy::State
CaptivePortalWindowProxy::GetState() const {
141 if (widget_
== NULL
) {
142 if (captive_portal_view_
.get() == NULL
)
145 return STATE_WAITING_FOR_REDIRECTION
;
147 if (captive_portal_view_
.get() == NULL
)
148 return STATE_DISPLAYED
;
152 return STATE_UNKNOWN
;
155 void CaptivePortalWindowProxy::DetachFromWidget(views::Widget
* widget
) {
156 if (!widget_
|| widget_
!= widget
)
158 widget_
->RemoveObserver(this);
162 } // namespace chromeos