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 DCHECK(GetState() == STATE_IDLE
);
29 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
32 DCHECK(GetState() == STATE_DISPLAYED
);
33 widget_
->RemoveObserver(this);
37 void CaptivePortalWindowProxy::ShowIfRedirected() {
38 if (GetState() != STATE_IDLE
)
40 InitCaptivePortalView();
41 DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION
);
44 void CaptivePortalWindowProxy::Show() {
45 if (ProxySettingsDialog::IsShown()) {
46 // ProxySettingsDialog is being shown, don't cover it.
51 if (GetState() == STATE_DISPLAYED
) // Dialog is already shown, do nothing.
54 InitCaptivePortalView();
56 CaptivePortalView
* captive_portal_view
= captive_portal_view_
.release();
57 WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
58 WebContentsModalDialogManager::FromWebContents(web_contents_
);
59 DCHECK(web_contents_modal_dialog_manager
);
60 WebContentsModalDialogManagerDelegate
* delegate
=
61 web_contents_modal_dialog_manager
->delegate();
63 widget_
= views::Widget::CreateWindowAsFramelessChild(
65 delegate
->GetWebContentsModalDialogHost()->GetHostView());
66 captive_portal_view
->Init();
68 widget_
->AddObserver(this);
69 web_contents_modal_dialog_manager
->ShowDialog(widget_
->GetNativeView());
70 DCHECK(GetState() == STATE_DISPLAYED
);
73 void CaptivePortalWindowProxy::Close() {
74 if (GetState() == STATE_DISPLAYED
)
76 captive_portal_view_
.reset();
79 void CaptivePortalWindowProxy::OnRedirected() {
80 if (GetState() == STATE_WAITING_FOR_REDIRECTION
)
82 delegate_
->OnPortalDetected();
85 void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
89 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget
* widget
) {
90 DCHECK(GetState() == STATE_DISPLAYED
);
91 DCHECK(widget
== widget_
);
93 widget
->RemoveObserver(this);
96 DCHECK(GetState() == STATE_IDLE
);
99 void CaptivePortalWindowProxy::InitCaptivePortalView() {
100 DCHECK(GetState() == STATE_IDLE
||
101 GetState() == STATE_WAITING_FOR_REDIRECTION
);
102 if (!captive_portal_view_
.get()) {
103 captive_portal_view_
.reset(
104 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
106 captive_portal_view_
->StartLoad();
109 CaptivePortalWindowProxy::State
CaptivePortalWindowProxy::GetState() const {
110 if (widget_
== NULL
) {
111 if (captive_portal_view_
.get() == NULL
)
114 return STATE_WAITING_FOR_REDIRECTION
;
116 if (captive_portal_view_
.get() == NULL
)
117 return STATE_DISPLAYED
;
121 return STATE_UNKNOWN
;
124 } // namespace chromeos