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 gfx::NativeWindow parent,
23 content::WebContents* web_contents)
24 : delegate_(delegate),
27 web_contents_(web_contents) {
28 DCHECK(GetState() == STATE_IDLE);
31 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
34 DCHECK(GetState() == STATE_DISPLAYED);
35 widget_->RemoveObserver(this);
39 void CaptivePortalWindowProxy::ShowIfRedirected() {
40 if (GetState() != STATE_IDLE)
42 InitCaptivePortalView();
43 DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION);
46 void CaptivePortalWindowProxy::Show() {
47 if (ProxySettingsDialog::IsShown()) {
48 // ProxySettingsDialog is being shown, don't cover it.
53 if (GetState() == STATE_DISPLAYED) // Dialog is already shown, do nothing.
56 InitCaptivePortalView();
58 CaptivePortalView* captive_portal_view = captive_portal_view_.release();
59 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
60 WebContentsModalDialogManager::FromWebContents(web_contents_);
61 DCHECK(web_contents_modal_dialog_manager);
62 WebContentsModalDialogManagerDelegate* delegate =
63 web_contents_modal_dialog_manager->delegate();
66 widget_ = views::Widget::CreateWindowAsFramelessChild(
69 delegate->GetWebContentsModalDialogHost()->GetHostView());
70 captive_portal_view->Init();
72 widget_->AddObserver(this);
73 web_contents_modal_dialog_manager->ShowDialog(widget_->GetNativeView());
74 DCHECK(GetState() == STATE_DISPLAYED);
77 void CaptivePortalWindowProxy::Close() {
78 if (GetState() == STATE_DISPLAYED)
80 captive_portal_view_.reset();
83 void CaptivePortalWindowProxy::OnRedirected() {
84 if (GetState() == STATE_WAITING_FOR_REDIRECTION)
86 delegate_->OnPortalDetected();
89 void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
93 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) {
94 DCHECK(GetState() == STATE_DISPLAYED);
95 DCHECK(widget == widget_);
97 widget->RemoveObserver(this);
100 DCHECK(GetState() == STATE_IDLE);
103 void CaptivePortalWindowProxy::InitCaptivePortalView() {
104 DCHECK(GetState() == STATE_IDLE ||
105 GetState() == STATE_WAITING_FOR_REDIRECTION);
106 if (!captive_portal_view_.get()) {
107 captive_portal_view_.reset(
108 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
110 captive_portal_view_->StartLoad();
113 CaptivePortalWindowProxy::State CaptivePortalWindowProxy::GetState() const {
114 if (widget_ == NULL) {
115 if (captive_portal_view_.get() == NULL)
118 return STATE_WAITING_FOR_REDIRECTION;
120 if (captive_portal_view_.get() == NULL)
121 return STATE_DISPLAYED;
125 return STATE_UNKNOWN;
128 } // namespace chromeos