Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / captive_portal_window_proxy.cc
blob8d5a8e99b4a5e16d0bca5997f5b6fcf26caa54ba
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 captive_portal_view_for_testing_(NULL) {
27 DCHECK(GetState() == STATE_IDLE);
30 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
31 if (!widget_)
32 return;
33 DCHECK(GetState() == STATE_DISPLAYED);
34 widget_->RemoveObserver(this);
35 widget_->Close();
38 void CaptivePortalWindowProxy::ShowIfRedirected() {
39 if (GetState() != STATE_IDLE)
40 return;
41 InitCaptivePortalView();
42 DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION);
45 void CaptivePortalWindowProxy::Show() {
46 if (ProxySettingsDialog::IsShown()) {
47 // ProxySettingsDialog is being shown, don't cover it.
48 Close();
49 return;
52 if (GetState() == STATE_DISPLAYED) // Dialog is already shown, do nothing.
53 return;
55 InitCaptivePortalView();
57 CaptivePortalView* captive_portal_view = captive_portal_view_.release();
58 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
59 WebContentsModalDialogManager::FromWebContents(web_contents_);
60 DCHECK(web_contents_modal_dialog_manager);
61 WebContentsModalDialogManagerDelegate* delegate =
62 web_contents_modal_dialog_manager->delegate();
63 DCHECK(delegate);
64 widget_ = views::Widget::CreateWindowAsFramelessChild(
65 captive_portal_view,
66 delegate->GetWebContentsModalDialogHost()->GetHostView());
67 captive_portal_view->Init();
69 widget_->AddObserver(this);
70 web_contents_modal_dialog_manager->ShowModalDialog(
71 widget_->GetNativeView());
72 DCHECK(GetState() == STATE_DISPLAYED);
75 void CaptivePortalWindowProxy::Close() {
76 if (GetState() == STATE_DISPLAYED)
77 widget_->Close();
78 captive_portal_view_.reset();
79 captive_portal_view_for_testing_ = NULL;
82 void CaptivePortalWindowProxy::OnRedirected() {
83 if (GetState() == STATE_WAITING_FOR_REDIRECTION)
84 Show();
85 delegate_->OnPortalDetected();
88 void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
89 Close();
92 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) {
93 DCHECK(GetState() == STATE_DISPLAYED);
94 DCHECK(widget == widget_);
96 DetachFromWidget(widget);
98 DCHECK(GetState() == STATE_IDLE);
101 void CaptivePortalWindowProxy::OnWidgetDestroying(views::Widget* widget) {
102 DetachFromWidget(widget);
105 void CaptivePortalWindowProxy::OnWidgetDestroyed(views::Widget* widget) {
106 DetachFromWidget(widget);
109 void CaptivePortalWindowProxy::InitCaptivePortalView() {
110 DCHECK(GetState() == STATE_IDLE ||
111 GetState() == STATE_WAITING_FOR_REDIRECTION);
112 if (!captive_portal_view_.get()) {
113 captive_portal_view_.reset(
114 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
115 captive_portal_view_for_testing_ = captive_portal_view_.get();
117 captive_portal_view_->StartLoad();
120 CaptivePortalWindowProxy::State CaptivePortalWindowProxy::GetState() const {
121 if (widget_ == NULL) {
122 if (captive_portal_view_.get() == NULL)
123 return STATE_IDLE;
124 else
125 return STATE_WAITING_FOR_REDIRECTION;
126 } else {
127 if (captive_portal_view_.get() == NULL)
128 return STATE_DISPLAYED;
129 else
130 NOTREACHED();
132 return STATE_UNKNOWN;
135 void CaptivePortalWindowProxy::DetachFromWidget(views::Widget* widget) {
136 if (!widget_ || widget_ != widget)
137 return;
138 widget_->RemoveObserver(this);
139 widget_ = NULL;
142 } // namespace chromeos