| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/login/captive_portal_window_proxy.h" | 5 #include "chrome/browser/chromeos/login/captive_portal_window_proxy.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chromeos/login/captive_portal_view.h" | 7 #include "chrome/browser/chromeos/login/captive_portal_view.h" |
| 8 #include "chrome/browser/chromeos/login/helper.h" | 8 #include "chrome/browser/chromeos/login/helper.h" |
| 9 #include "chrome/browser/chromeos/login/proxy_settings_dialog.h" | 9 #include "chrome/browser/chromeos/login/proxy_settings_dialog.h" |
| 10 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 10 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 11 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 int kMargin = 50; | 15 int kMargin = 50; |
| 16 | 16 |
| 17 } // namespace | 17 } // namespace |
| 18 | 18 |
| 19 namespace chromeos { | 19 namespace chromeos { |
| 20 | 20 |
| 21 CaptivePortalWindowProxy::CaptivePortalWindowProxy(Delegate* delegate, | 21 CaptivePortalWindowProxy::CaptivePortalWindowProxy(Delegate* delegate, |
| 22 gfx::NativeWindow parent) | 22 gfx::NativeWindow parent) |
| 23 : delegate_(delegate), | 23 : delegate_(delegate), |
| 24 widget_(NULL), | 24 widget_(NULL), |
| 25 parent_(parent) { | 25 parent_(parent) { |
| 26 } | 26 } |
| 27 | 27 |
| 28 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() { | 28 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() { |
| 29 if (widget_) { | 29 if (IsShown()) { |
| 30 widget_->RemoveObserver(this); | 30 widget_->RemoveObserver(this); |
| 31 widget_->Close(); | 31 widget_->Close(); |
| 32 } | 32 } |
| 33 } | 33 } |
| 34 | 34 |
| 35 void CaptivePortalWindowProxy::ShowIfRedirected() { | 35 void CaptivePortalWindowProxy::ShowIfRedirected() { |
| 36 if (widget_) { | 36 if (IsShown()) { |
| 37 // Invalid state as when widget is created (Show()) | 37 // Invalid state as when widget is created (Show()) |
| 38 // CaptivePortalView ownership is transferred to it. | 38 // CaptivePortalView ownership is transferred to it. |
| 39 if (captive_portal_view_.get()) { | 39 DCHECK(!captive_portal_view_.get()); |
| 40 NOTREACHED(); | |
| 41 } | |
| 42 // Dialog is already shown, no need to reload. | 40 // Dialog is already shown, no need to reload. |
| 43 return; | 41 return; |
| 44 } | 42 } |
| 45 | 43 InitCaptivePortalView(); |
| 46 // Dialog is not initialized yet. | |
| 47 if (!captive_portal_view_.get()) { | |
| 48 captive_portal_view_.reset( | |
| 49 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this)); | |
| 50 } | |
| 51 | |
| 52 // If dialog has been created (but not shown) previously, force reload. | |
| 53 captive_portal_view_->StartLoad(); | |
| 54 } | 44 } |
| 55 | 45 |
| 56 void CaptivePortalWindowProxy::Show() { | 46 void CaptivePortalWindowProxy::Show() { |
| 57 if (ProxySettingsDialog::IsShown()) { | 47 if (ProxySettingsDialog::IsShown()) { |
| 58 // ProxySettingsDialog is being shown, don't cover it. | 48 // ProxySettingsDialog is being shown, don't cover it. |
| 59 Close(); | 49 Close(); |
| 60 return; | 50 return; |
| 61 } | 51 } |
| 62 | 52 |
| 63 if (!captive_portal_view_.get() || widget_) { | 53 if (IsShown()) // Dialog is already shown, do nothing. |
| 64 // Dialog is already shown, do nothing. | |
| 65 return; | 54 return; |
| 66 } | 55 |
| 56 InitCaptivePortalView(); |
| 67 | 57 |
| 68 CaptivePortalView* captive_portal_view = captive_portal_view_.release(); | 58 CaptivePortalView* captive_portal_view = captive_portal_view_.release(); |
| 69 widget_ = views::Widget::CreateWindowWithParent( | 59 widget_ = views::Widget::CreateWindowWithParent( |
| 70 captive_portal_view, | 60 captive_portal_view, |
| 71 parent_); | 61 parent_); |
| 72 captive_portal_view->Init(); | 62 captive_portal_view->Init(); |
| 73 | 63 |
| 74 gfx::Rect bounds(CalculateScreenBounds(gfx::Size())); | 64 gfx::Rect bounds(CalculateScreenBounds(gfx::Size())); |
| 75 bounds.Inset(kMargin, kMargin); | 65 bounds.Inset(kMargin, kMargin); |
| 76 widget_->SetBounds(bounds); | 66 widget_->SetBounds(bounds); |
| 77 | 67 |
| 78 widget_->AddObserver(this); | 68 widget_->AddObserver(this); |
| 79 widget_->Show(); | 69 widget_->Show(); |
| 80 } | 70 } |
| 81 | 71 |
| 82 void CaptivePortalWindowProxy::Close() { | 72 void CaptivePortalWindowProxy::Close() { |
| 83 if (widget_) { | 73 if (IsShown()) |
| 84 widget_->Close(); | 74 widget_->Close(); |
| 85 } else { | 75 else |
| 86 captive_portal_view_.reset(); | 76 captive_portal_view_.reset(); |
| 87 } | |
| 88 } | 77 } |
| 89 | 78 |
| 90 void CaptivePortalWindowProxy::OnRedirected() { | 79 void CaptivePortalWindowProxy::OnRedirected() { |
| 91 Show(); | 80 Show(); |
| 92 delegate_->OnPortalDetected(); | 81 delegate_->OnPortalDetected(); |
| 93 } | 82 } |
| 94 | 83 |
| 95 void CaptivePortalWindowProxy::OnOriginalURLLoaded() { | 84 void CaptivePortalWindowProxy::OnOriginalURLLoaded() { |
| 96 Close(); | 85 Close(); |
| 97 } | 86 } |
| 98 | 87 |
| 99 void CaptivePortalWindowProxy::OnWidgetDestroying(views::Widget* widget) { | 88 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) { |
| 100 DCHECK(widget == widget_); | 89 DCHECK(widget == widget_); |
| 101 DCHECK(captive_portal_view_.get() == NULL); | 90 DCHECK(captive_portal_view_.get() == NULL); |
| 102 widget->RemoveObserver(this); | 91 widget->RemoveObserver(this); |
| 103 widget_ = NULL; | 92 widget_ = NULL; |
| 104 } | 93 } |
| 105 | 94 |
| 95 void CaptivePortalWindowProxy::InitCaptivePortalView() { |
| 96 if (!captive_portal_view_.get()) { |
| 97 captive_portal_view_.reset( |
| 98 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this)); |
| 99 } |
| 100 captive_portal_view_->StartLoad(); |
| 101 } |
| 102 |
| 103 bool CaptivePortalWindowProxy::IsShown() const { |
| 104 return widget_ != NULL; |
| 105 } |
| 106 |
| 106 } // namespace chromeos | 107 } // namespace chromeos |
| OLD | NEW |