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 "chrome/browser/chromeos/login/ui/captive_portal_view.h"
8 #include "chrome/browser/chromeos/login/ui/proxy_settings_dialog.h"
9 #include "chrome/browser/chromeos/profiles/profile_helper.h"
10 #include "components/web_modal/popup_manager.h"
11 #include "ui/views/widget/widget.h"
15 // The captive portal dialog is system-modal, but uses the web-content-modal
16 // dialog manager (odd) and requires this atypical dialog widget initialization.
17 views::Widget* CreateWindowAsFramelessChild(views::WidgetDelegate* delegate,
18 gfx::NativeView parent) {
19 views::Widget* widget = new views::Widget;
21 views::Widget::InitParams params;
22 params.delegate = delegate;
24 params.parent = parent;
25 params.remove_standard_frame = true;
26 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
36 CaptivePortalWindowProxy::CaptivePortalWindowProxy(
38 content::WebContents* web_contents)
39 : delegate_(delegate),
41 web_contents_(web_contents),
42 captive_portal_view_for_testing_(NULL) {
43 DCHECK(GetState() == STATE_IDLE);
46 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
49 DCHECK(GetState() == STATE_DISPLAYED);
50 widget_->RemoveObserver(this);
54 void CaptivePortalWindowProxy::ShowIfRedirected() {
55 if (GetState() != STATE_IDLE)
57 InitCaptivePortalView();
58 DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION);
61 void CaptivePortalWindowProxy::Show() {
62 if (ProxySettingsDialog::IsShown()) {
63 // ProxySettingsDialog is being shown, don't cover it.
68 if (GetState() == STATE_DISPLAYED) // Dialog is already shown, do nothing.
71 InitCaptivePortalView();
73 CaptivePortalView* portal = captive_portal_view_.release();
74 web_modal::PopupManager* popup_manager =
75 web_modal::PopupManager::FromWebContents(web_contents_);
78 CreateWindowAsFramelessChild(portal, popup_manager->GetHostView());
80 widget_->AddObserver(this);
81 popup_manager->ShowModalDialog(widget_->GetNativeView(), web_contents_);
85 void CaptivePortalWindowProxy::Close() {
86 if (GetState() == STATE_DISPLAYED)
88 captive_portal_view_.reset();
89 captive_portal_view_for_testing_ = NULL;
92 void CaptivePortalWindowProxy::OnRedirected() {
93 if (GetState() == STATE_WAITING_FOR_REDIRECTION)
95 delegate_->OnPortalDetected();
98 void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
102 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) {
103 DCHECK(GetState() == STATE_DISPLAYED);
104 DCHECK(widget == widget_);
106 DetachFromWidget(widget);
108 DCHECK(GetState() == STATE_IDLE);
111 void CaptivePortalWindowProxy::OnWidgetDestroying(views::Widget* widget) {
112 DetachFromWidget(widget);
115 void CaptivePortalWindowProxy::OnWidgetDestroyed(views::Widget* widget) {
116 DetachFromWidget(widget);
119 void CaptivePortalWindowProxy::InitCaptivePortalView() {
120 DCHECK(GetState() == STATE_IDLE ||
121 GetState() == STATE_WAITING_FOR_REDIRECTION);
122 if (!captive_portal_view_.get()) {
123 captive_portal_view_.reset(
124 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
125 captive_portal_view_for_testing_ = captive_portal_view_.get();
127 captive_portal_view_->StartLoad();
130 CaptivePortalWindowProxy::State CaptivePortalWindowProxy::GetState() const {
131 if (widget_ == NULL) {
132 if (captive_portal_view_.get() == NULL)
135 return STATE_WAITING_FOR_REDIRECTION;
137 if (captive_portal_view_.get() == NULL)
138 return STATE_DISPLAYED;
142 return STATE_UNKNOWN;
145 void CaptivePortalWindowProxy::DetachFromWidget(views::Widget* widget) {
146 if (!widget_ || widget_ != widget)
148 widget_->RemoveObserver(this);
152 } // namespace chromeos