1 // Copyright (c) 2012 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/captive_portal_window_proxy.h"
7 #include "chrome/browser/chromeos/login/captive_portal_view.h"
8 #include "chrome/browser/chromeos/login/proxy_settings_dialog.h"
9 #include "chrome/browser/chromeos/profiles/profile_helper.h"
10 #include "components/web_modal/web_contents_modal_dialog_host.h"
11 #include "components/web_modal/web_contents_modal_dialog_manager.h"
12 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
13 #include "ui/views/widget/widget.h"
15 using web_modal::WebContentsModalDialogManager
;
16 using web_modal::WebContentsModalDialogManagerDelegate
;
20 CaptivePortalWindowProxy::CaptivePortalWindowProxy(
22 content::WebContents
* web_contents
)
23 : delegate_(delegate
),
25 web_contents_(web_contents
),
26 captive_portal_view_for_testing_(NULL
) {
27 DCHECK(GetState() == STATE_IDLE
);
30 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
33 DCHECK(GetState() == STATE_DISPLAYED
);
34 widget_
->RemoveObserver(this);
38 void CaptivePortalWindowProxy::ShowIfRedirected() {
39 if (GetState() != STATE_IDLE
)
41 InitCaptivePortalView();
42 DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION
);
45 void CaptivePortalWindowProxy::Show() {
46 if (ProxySettingsDialog::IsShown()) {
47 // ProxySettingsDialog is being shown, don't cover it.
52 if (GetState() == STATE_DISPLAYED
) // Dialog is already shown, do nothing.
55 InitCaptivePortalView();
57 CaptivePortalView
* captive_portal_view
= captive_portal_view_
.release();
58 WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
59 WebContentsModalDialogManager::FromWebContents(web_contents_
);
60 DCHECK(web_contents_modal_dialog_manager
);
61 WebContentsModalDialogManagerDelegate
* delegate
=
62 web_contents_modal_dialog_manager
->delegate();
64 widget_
= views::Widget::CreateWindowAsFramelessChild(
66 delegate
->GetWebContentsModalDialogHost()->GetHostView());
67 captive_portal_view
->Init();
69 widget_
->AddObserver(this);
70 web_contents_modal_dialog_manager
->ShowModalDialog(
71 widget_
->GetNativeView());
72 DCHECK(GetState() == STATE_DISPLAYED
);
75 void CaptivePortalWindowProxy::Close() {
76 if (GetState() == STATE_DISPLAYED
)
78 captive_portal_view_
.reset();
79 captive_portal_view_for_testing_
= NULL
;
82 void CaptivePortalWindowProxy::OnRedirected() {
83 if (GetState() == STATE_WAITING_FOR_REDIRECTION
)
85 delegate_
->OnPortalDetected();
88 void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
92 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget
* widget
) {
93 DCHECK(GetState() == STATE_DISPLAYED
);
94 DCHECK(widget
== widget_
);
96 DetachFromWidget(widget
);
98 DCHECK(GetState() == STATE_IDLE
);
101 void CaptivePortalWindowProxy::OnWidgetDestroying(views::Widget
* widget
) {
102 DetachFromWidget(widget
);
105 void CaptivePortalWindowProxy::OnWidgetDestroyed(views::Widget
* widget
) {
106 DetachFromWidget(widget
);
109 void CaptivePortalWindowProxy::InitCaptivePortalView() {
110 DCHECK(GetState() == STATE_IDLE
||
111 GetState() == STATE_WAITING_FOR_REDIRECTION
);
112 if (!captive_portal_view_
.get()) {
113 captive_portal_view_
.reset(
114 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
115 captive_portal_view_for_testing_
= captive_portal_view_
.get();
117 captive_portal_view_
->StartLoad();
120 CaptivePortalWindowProxy::State
CaptivePortalWindowProxy::GetState() const {
121 if (widget_
== NULL
) {
122 if (captive_portal_view_
.get() == NULL
)
125 return STATE_WAITING_FOR_REDIRECTION
;
127 if (captive_portal_view_
.get() == NULL
)
128 return STATE_DISPLAYED
;
132 return STATE_UNKNOWN
;
135 void CaptivePortalWindowProxy::DetachFromWidget(views::Widget
* widget
) {
136 if (!widget_
|| widget_
!= widget
)
138 widget_
->RemoveObserver(this);
142 } // namespace chromeos