- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / captive_portal_window_proxy.cc
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     gfx::NativeWindow parent,
23     content::WebContents* web_contents)
24     : delegate_(delegate),
25       widget_(NULL),
26       parent_(parent),
27       web_contents_(web_contents) {
28   DCHECK(GetState() == STATE_IDLE);
29 }
30
31 CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
32   if (!widget_)
33     return;
34   DCHECK(GetState() == STATE_DISPLAYED);
35   widget_->RemoveObserver(this);
36   widget_->Close();
37 }
38
39 void CaptivePortalWindowProxy::ShowIfRedirected() {
40   if (GetState() != STATE_IDLE)
41     return;
42   InitCaptivePortalView();
43   DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION);
44 }
45
46 void CaptivePortalWindowProxy::Show() {
47   if (ProxySettingsDialog::IsShown()) {
48     // ProxySettingsDialog is being shown, don't cover it.
49     Close();
50     return;
51   }
52
53   if (GetState() == STATE_DISPLAYED)  // Dialog is already shown, do nothing.
54     return;
55
56   InitCaptivePortalView();
57
58   CaptivePortalView* captive_portal_view = captive_portal_view_.release();
59   WebContentsModalDialogManager* web_contents_modal_dialog_manager =
60       WebContentsModalDialogManager::FromWebContents(web_contents_);
61   DCHECK(web_contents_modal_dialog_manager);
62   WebContentsModalDialogManagerDelegate* delegate =
63       web_contents_modal_dialog_manager->delegate();
64   DCHECK(delegate);
65
66   widget_ = views::Widget::CreateWindowAsFramelessChild(
67       captive_portal_view,
68       parent_,
69       delegate->GetWebContentsModalDialogHost()->GetHostView());
70   captive_portal_view->Init();
71
72   widget_->AddObserver(this);
73   web_contents_modal_dialog_manager->ShowDialog(widget_->GetNativeView());
74   DCHECK(GetState() == STATE_DISPLAYED);
75 }
76
77 void CaptivePortalWindowProxy::Close() {
78   if (GetState() == STATE_DISPLAYED)
79     widget_->Close();
80   captive_portal_view_.reset();
81 }
82
83 void CaptivePortalWindowProxy::OnRedirected() {
84   if (GetState() == STATE_WAITING_FOR_REDIRECTION)
85     Show();
86   delegate_->OnPortalDetected();
87 }
88
89 void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
90   Close();
91 }
92
93 void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) {
94   DCHECK(GetState() == STATE_DISPLAYED);
95   DCHECK(widget == widget_);
96
97   widget->RemoveObserver(this);
98   widget_ = NULL;
99
100   DCHECK(GetState() == STATE_IDLE);
101 }
102
103 void CaptivePortalWindowProxy::InitCaptivePortalView() {
104   DCHECK(GetState() == STATE_IDLE ||
105          GetState() == STATE_WAITING_FOR_REDIRECTION);
106   if (!captive_portal_view_.get()) {
107     captive_portal_view_.reset(
108         new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
109   }
110   captive_portal_view_->StartLoad();
111 }
112
113 CaptivePortalWindowProxy::State CaptivePortalWindowProxy::GetState() const {
114   if (widget_ == NULL) {
115     if (captive_portal_view_.get() == NULL)
116       return STATE_IDLE;
117     else
118       return STATE_WAITING_FOR_REDIRECTION;
119   } else {
120     if (captive_portal_view_.get() == NULL)
121       return STATE_DISPLAYED;
122     else
123       NOTREACHED();
124   }
125   return STATE_UNKNOWN;
126 }
127
128 }  // namespace chromeos