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 DetachFromWidget(widget);
95 DCHECK(GetState() == STATE_IDLE);
98 void CaptivePortalWindowProxy::OnWidgetDestroying(views::Widget* widget) {
99 DetachFromWidget(widget);
102 void CaptivePortalWindowProxy::OnWidgetDestroyed(views::Widget* widget) {
103 DetachFromWidget(widget);
106 void CaptivePortalWindowProxy::InitCaptivePortalView() {
107 DCHECK(GetState() == STATE_IDLE ||
108 GetState() == STATE_WAITING_FOR_REDIRECTION);
109 if (!captive_portal_view_.get()) {
110 captive_portal_view_.reset(
111 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
113 captive_portal_view_->StartLoad();
116 CaptivePortalWindowProxy::State CaptivePortalWindowProxy::GetState() const {
117 if (widget_ == NULL) {
118 if (captive_portal_view_.get() == NULL)
121 return STATE_WAITING_FOR_REDIRECTION;
123 if (captive_portal_view_.get() == NULL)
124 return STATE_DISPLAYED;
128 return STATE_UNKNOWN;
131 void CaptivePortalWindowProxy::DetachFromWidget(views::Widget* widget) {
132 if (!widget_ || widget_ != widget)
134 widget_->RemoveObserver(this);
138 } // namespace chromeos