OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/chromeos/login/captive_portal_window_proxy.h" | |
6 | |
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" | |
14 | |
15 using web_modal::WebContentsModalDialogManager; | |
16 using web_modal::WebContentsModalDialogManagerDelegate; | |
17 | |
18 namespace chromeos { | |
19 | |
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); | |
28 } | |
29 | |
30 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() { | |
31 if (!widget_) | |
32 return; | |
33 DCHECK(GetState() == STATE_DISPLAYED); | |
34 widget_->RemoveObserver(this); | |
35 widget_->Close(); | |
36 } | |
37 | |
38 void CaptivePortalWindowProxy::ShowIfRedirected() { | |
39 if (GetState() != STATE_IDLE) | |
40 return; | |
41 InitCaptivePortalView(); | |
42 DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION); | |
43 } | |
44 | |
45 void CaptivePortalWindowProxy::Show() { | |
46 if (ProxySettingsDialog::IsShown()) { | |
47 // ProxySettingsDialog is being shown, don't cover it. | |
48 Close(); | |
49 return; | |
50 } | |
51 | |
52 if (GetState() == STATE_DISPLAYED) // Dialog is already shown, do nothing. | |
53 return; | |
54 | |
55 InitCaptivePortalView(); | |
56 | |
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(); | |
68 | |
69 widget_->AddObserver(this); | |
70 web_contents_modal_dialog_manager->ShowModalDialog( | |
71 widget_->GetNativeView()); | |
72 DCHECK(GetState() == STATE_DISPLAYED); | |
73 } | |
74 | |
75 void CaptivePortalWindowProxy::Close() { | |
76 if (GetState() == STATE_DISPLAYED) | |
77 widget_->Close(); | |
78 captive_portal_view_.reset(); | |
79 captive_portal_view_for_testing_ = NULL; | |
80 } | |
81 | |
82 void CaptivePortalWindowProxy::OnRedirected() { | |
83 if (GetState() == STATE_WAITING_FOR_REDIRECTION) | |
84 Show(); | |
85 delegate_->OnPortalDetected(); | |
86 } | |
87 | |
88 void CaptivePortalWindowProxy::OnOriginalURLLoaded() { | |
89 Close(); | |
90 } | |
91 | |
92 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) { | |
93 DCHECK(GetState() == STATE_DISPLAYED); | |
94 DCHECK(widget == widget_); | |
95 | |
96 DetachFromWidget(widget); | |
97 | |
98 DCHECK(GetState() == STATE_IDLE); | |
99 } | |
100 | |
101 void CaptivePortalWindowProxy::OnWidgetDestroying(views::Widget* widget) { | |
102 DetachFromWidget(widget); | |
103 } | |
104 | |
105 void CaptivePortalWindowProxy::OnWidgetDestroyed(views::Widget* widget) { | |
106 DetachFromWidget(widget); | |
107 } | |
108 | |
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(); | |
116 } | |
117 captive_portal_view_->StartLoad(); | |
118 } | |
119 | |
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(); | |
131 } | |
132 return STATE_UNKNOWN; | |
133 } | |
134 | |
135 void CaptivePortalWindowProxy::DetachFromWidget(views::Widget* widget) { | |
136 if (!widget_ || widget_ != widget) | |
137 return; | |
138 widget_->RemoveObserver(this); | |
139 widget_ = NULL; | |
140 } | |
141 | |
142 } // namespace chromeos | |
OLD | NEW |