Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / chromeos / login / ui / captive_portal_window_proxy.cc
blob9acc39021941f8b9f5a1887e4f015941050f257b
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 "base/metrics/histogram_macros.h"
8 #include "chrome/browser/chromeos/login/ui/captive_portal_view.h"
9 #include "chrome/browser/chromeos/login/ui/proxy_settings_dialog.h"
10 #include "chrome/browser/chromeos/profiles/profile_helper.h"
11 #include "components/web_modal/web_contents_modal_dialog_host.h"
12 #include "components/web_modal/web_contents_modal_dialog_manager.h"
13 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
14 #include "ui/views/widget/widget.h"
16 namespace {
18 // The captive portal dialog is system-modal, but uses the web-content-modal
19 // dialog manager (odd) and requires this atypical dialog widget initialization.
20 views::Widget* CreateWindowAsFramelessChild(views::WidgetDelegate* delegate,
21 gfx::NativeView parent) {
22 views::Widget* widget = new views::Widget;
24 views::Widget::InitParams params;
25 params.delegate = delegate;
26 params.child = true;
27 params.parent = parent;
28 params.remove_standard_frame = true;
29 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
31 widget->Init(params);
32 return widget;
35 } // namespace
37 namespace chromeos {
39 CaptivePortalWindowProxy::CaptivePortalWindowProxy(
40 Delegate* delegate,
41 content::WebContents* web_contents)
42 : delegate_(delegate),
43 widget_(NULL),
44 web_contents_(web_contents),
45 captive_portal_view_for_testing_(NULL) {
46 DCHECK(GetState() == STATE_IDLE);
49 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
50 if (!widget_)
51 return;
52 DCHECK(GetState() == STATE_DISPLAYED);
53 widget_->RemoveObserver(this);
54 widget_->Close();
57 void CaptivePortalWindowProxy::ShowIfRedirected() {
58 if (GetState() != STATE_IDLE)
59 return;
60 InitCaptivePortalView();
61 DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION);
64 void CaptivePortalWindowProxy::Show() {
65 if (ProxySettingsDialog::IsShown()) {
66 // ProxySettingsDialog is being shown, don't cover it.
67 Close();
68 return;
71 if (GetState() == STATE_DISPLAYED) // Dialog is already shown, do nothing.
72 return;
74 InitCaptivePortalView();
76 CaptivePortalView* portal = captive_portal_view_.release();
77 auto manager =
78 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents_);
79 widget_ = CreateWindowAsFramelessChild(
80 portal,
81 manager->delegate()->GetWebContentsModalDialogHost()->GetHostView());
82 portal->Init();
83 widget_->AddObserver(this);
84 manager->ShowModalDialog(widget_->GetNativeView());
87 void CaptivePortalWindowProxy::Close() {
88 if (GetState() == STATE_DISPLAYED)
89 widget_->Close();
90 captive_portal_view_.reset();
91 captive_portal_view_for_testing_ = NULL;
94 void CaptivePortalWindowProxy::OnRedirected() {
95 if (GetState() == STATE_WAITING_FOR_REDIRECTION) {
96 if (!started_loading_at_.is_null()) {
97 UMA_HISTOGRAM_TIMES("CaptivePortal.RedirectTime",
98 base::Time::Now() - started_loading_at_);
99 started_loading_at_ = base::Time();
101 Show();
103 delegate_->OnPortalDetected();
106 void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
107 Close();
110 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) {
111 DCHECK(GetState() == STATE_DISPLAYED);
112 DCHECK(widget == widget_);
114 DetachFromWidget(widget);
116 DCHECK(GetState() == STATE_IDLE);
119 void CaptivePortalWindowProxy::OnWidgetDestroying(views::Widget* widget) {
120 DetachFromWidget(widget);
123 void CaptivePortalWindowProxy::OnWidgetDestroyed(views::Widget* widget) {
124 DetachFromWidget(widget);
127 void CaptivePortalWindowProxy::InitCaptivePortalView() {
128 DCHECK(GetState() == STATE_IDLE ||
129 GetState() == STATE_WAITING_FOR_REDIRECTION);
130 if (!captive_portal_view_.get()) {
131 captive_portal_view_.reset(
132 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
133 captive_portal_view_for_testing_ = captive_portal_view_.get();
136 started_loading_at_ = base::Time::Now();
137 captive_portal_view_->StartLoad();
140 CaptivePortalWindowProxy::State CaptivePortalWindowProxy::GetState() const {
141 if (widget_ == NULL) {
142 if (captive_portal_view_.get() == NULL)
143 return STATE_IDLE;
144 else
145 return STATE_WAITING_FOR_REDIRECTION;
146 } else {
147 if (captive_portal_view_.get() == NULL)
148 return STATE_DISPLAYED;
149 else
150 NOTREACHED();
152 return STATE_UNKNOWN;
155 void CaptivePortalWindowProxy::DetachFromWidget(views::Widget* widget) {
156 if (!widget_ || widget_ != widget)
157 return;
158 widget_->RemoveObserver(this);
159 widget_ = NULL;
162 } // namespace chromeos