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/helper.h" 9 #include "chrome/browser/chromeos/login/proxy_settings_dialog.h" 10 #include "chrome/browser/chromeos/profiles/profile_helper.h" 11 #include "ui/views/widget/widget.h" 12 13 namespace { 14 15 int kMargin = 50; 16 17 } // namespace 18 19 namespace chromeos { 20 21 CaptivePortalWindowProxy::CaptivePortalWindowProxy(Delegate* delegate, 22 gfx::NativeWindow parent) 23 : delegate_(delegate), 24 widget_(NULL), 25 parent_(parent) { 26 DCHECK(GetState() == STATE_IDLE); 27 } 28 29 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() { 30 if (!widget_) 31 return; 32 DCHECK(GetState() == STATE_DISPLAYED); 33 widget_->RemoveObserver(this); 34 widget_->Close(); 35 } 36 37 void CaptivePortalWindowProxy::ShowIfRedirected() { 38 if (GetState() != STATE_IDLE) 39 return; 40 InitCaptivePortalView(); 41 DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION); 42 } 43 44 void CaptivePortalWindowProxy::Show() { 45 if (ProxySettingsDialog::IsShown()) { 46 // ProxySettingsDialog is being shown, don't cover it. 47 Close(); 48 return; 49 } 50 51 if (GetState() == STATE_DISPLAYED) // Dialog is already shown, do nothing. 52 return; 53 54 InitCaptivePortalView(); 55 56 CaptivePortalView* captive_portal_view = captive_portal_view_.release(); 57 widget_ = views::Widget::CreateWindowWithParent( 58 captive_portal_view, 59 parent_); 60 captive_portal_view->Init(); 61 62 gfx::Rect bounds(CalculateScreenBounds(gfx::Size())); 63 bounds.Inset(kMargin, kMargin); 64 widget_->SetBounds(bounds); 65 66 widget_->AddObserver(this); 67 widget_->Show(); 68 DCHECK(GetState() == STATE_DISPLAYED); 69 } 70 71 void CaptivePortalWindowProxy::Close() { 72 if (GetState() == STATE_DISPLAYED) 73 widget_->Close(); 74 captive_portal_view_.reset(); 75 } 76 77 void CaptivePortalWindowProxy::OnRedirected() { 78 if (GetState() == STATE_WAITING_FOR_REDIRECTION) 79 Show(); 80 delegate_->OnPortalDetected(); 81 } 82 83 void CaptivePortalWindowProxy::OnOriginalURLLoaded() { 84 Close(); 85 } 86 87 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) { 88 DCHECK(GetState() == STATE_DISPLAYED); 89 DCHECK(widget == widget_); 90 91 widget->RemoveObserver(this); 92 widget_ = NULL; 93 94 DCHECK(GetState() == STATE_IDLE); 95 } 96 97 void CaptivePortalWindowProxy::InitCaptivePortalView() { 98 DCHECK(GetState() == STATE_IDLE || 99 GetState() == STATE_WAITING_FOR_REDIRECTION); 100 if (!captive_portal_view_.get()) { 101 captive_portal_view_.reset( 102 new CaptivePortalView(ProfileHelper::GetSigninProfile(), this)); 103 } 104 captive_portal_view_->StartLoad(); 105 } 106 107 CaptivePortalWindowProxy::State CaptivePortalWindowProxy::GetState() const { 108 if (widget_ == NULL) { 109 if (captive_portal_view_.get() == NULL) 110 return STATE_IDLE; 111 else 112 return STATE_WAITING_FOR_REDIRECTION; 113 } else { 114 if (captive_portal_view_.get() == NULL) 115 return STATE_DISPLAYED; 116 else 117 NOTREACHED(); 118 } 119 return STATE_UNKNOWN; 120 } 121 122 } // namespace chromeos 123