Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / captive_portal_window_proxy.cc
blob5e547a2d5fa7669a6e6e4ac2ad508d28490c11e8
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;
18 namespace chromeos {
20 CaptivePortalWindowProxy::CaptivePortalWindowProxy(
21 Delegate* delegate,
22 content::WebContents* web_contents)
23 : delegate_(delegate),
24 widget_(NULL),
25 web_contents_(web_contents) {
26 DCHECK(GetState() == STATE_IDLE);
29 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
30 if (!widget_)
31 return;
32 DCHECK(GetState() == STATE_DISPLAYED);
33 widget_->RemoveObserver(this);
34 widget_->Close();
37 void CaptivePortalWindowProxy::ShowIfRedirected() {
38 if (GetState() != STATE_IDLE)
39 return;
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.
47 Close();
48 return;
51 if (GetState() == STATE_DISPLAYED) // Dialog is already shown, do nothing.
52 return;
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();
62 DCHECK(delegate);
63 widget_ = views::Widget::CreateWindowAsFramelessChild(
64 captive_portal_view,
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)
75 widget_->Close();
76 captive_portal_view_.reset();
79 void CaptivePortalWindowProxy::OnRedirected() {
80 if (GetState() == STATE_WAITING_FOR_REDIRECTION)
81 Show();
82 delegate_->OnPortalDetected();
85 void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
86 Close();
89 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) {
90 DCHECK(GetState() == STATE_DISPLAYED);
91 DCHECK(widget == widget_);
93 widget->RemoveObserver(this);
94 widget_ = NULL;
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)
112 return STATE_IDLE;
113 else
114 return STATE_WAITING_FOR_REDIRECTION;
115 } else {
116 if (captive_portal_view_.get() == NULL)
117 return STATE_DISPLAYED;
118 else
119 NOTREACHED();
121 return STATE_UNKNOWN;
124 } // namespace chromeos