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.
5 #include "chrome/browser/ui/login/login_prompt.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/tab_contents/tab_util.h"
10 #include "chrome/browser/ui/views/login_view.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "components/password_manager/core/browser/password_manager.h"
13 #include "components/web_modal/web_contents_modal_dialog_host.h"
14 #include "components/web_modal/web_contents_modal_dialog_manager.h"
15 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/render_view_host.h"
18 #include "content/public/browser/web_contents.h"
19 #include "grit/generated_resources.h"
20 #include "net/url_request/url_request.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/views/widget/widget.h"
23 #include "ui/views/window/dialog_delegate.h"
25 using autofill::PasswordForm
;
26 using content::BrowserThread
;
27 using content::WebContents
;
28 using web_modal::WebContentsModalDialogManager
;
29 using web_modal::WebContentsModalDialogManagerDelegate
;
31 // ----------------------------------------------------------------------------
34 // This class simply forwards the authentication from the LoginView (on
35 // the UI thread) to the net::URLRequest (on the I/O thread).
36 // This class uses ref counting to ensure that it lives until all InvokeLaters
38 class LoginHandlerViews
: public LoginHandler
,
39 public views::DialogDelegate
{
41 LoginHandlerViews(net::AuthChallengeInfo
* auth_info
, net::URLRequest
* request
)
42 : LoginHandler(auth_info
, request
),
47 // LoginModelObserver implementation.
48 virtual void OnAutofillDataAvailable(
49 const base::string16
& username
,
50 const base::string16
& password
) OVERRIDE
{
51 // Nothing to do here since LoginView takes care of autofill for win.
53 virtual void OnLoginModelDestroying() OVERRIDE
{}
55 // views::DialogDelegate methods:
56 virtual base::string16
GetDialogButtonLabel(
57 ui::DialogButton button
) const OVERRIDE
{
58 if (button
== ui::DIALOG_BUTTON_OK
)
59 return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL
);
60 return DialogDelegate::GetDialogButtonLabel(button
);
63 virtual base::string16
GetWindowTitle() const OVERRIDE
{
64 return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE
);
67 virtual void WindowClosing() OVERRIDE
{
68 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
70 WebContents
* tab
= GetWebContentsForLogin();
72 tab
->GetRenderViewHost()->SetIgnoreInputEvents(false);
74 // Reference is no longer valid.
80 virtual void DeleteDelegate() OVERRIDE
{
81 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
83 // The widget is going to delete itself; clear our pointer.
90 virtual ui::ModalType
GetModalType() const OVERRIDE
{
92 return ui::MODAL_TYPE_CHILD
;
94 return views::WidgetDelegate::GetModalType();
98 virtual bool Cancel() OVERRIDE
{
99 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
105 virtual bool Accept() OVERRIDE
{
106 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
108 SetAuth(login_view_
->GetUsername(), login_view_
->GetPassword());
112 virtual views::View
* GetInitiallyFocusedView() OVERRIDE
{
113 return login_view_
->GetInitiallyFocusedView();
116 virtual views::View
* GetContentsView() OVERRIDE
{
119 virtual views::Widget
* GetWidget() OVERRIDE
{
120 return login_view_
->GetWidget();
122 virtual const views::Widget
* GetWidget() const OVERRIDE
{
123 return login_view_
->GetWidget();
128 virtual void BuildViewForPasswordManager(
129 password_manager::PasswordManager
* manager
,
130 const base::string16
& explanation
) OVERRIDE
{
131 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
133 // Create a new LoginView and set the model for it. The model (password
134 // manager) is owned by the WebContents, but the view is parented to the
135 // browser window, so the view may be destroyed after the password
136 // manager. The view listens for model destruction and unobserves
138 login_view_
= new LoginView(explanation
, manager
);
140 // Scary thread safety note: This can potentially be called *after* SetAuth
141 // or CancelAuth (say, if the request was cancelled before the UI thread got
142 // control). However, that's OK since any UI interaction in those functions
143 // will occur via an InvokeLater on the UI thread, which is guaranteed
144 // to happen after this is called (since this was InvokeLater'd first).
145 WebContents
* requesting_contents
= GetWebContentsForLogin();
146 WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
147 WebContentsModalDialogManager::FromWebContents(requesting_contents
);
148 WebContentsModalDialogManagerDelegate
* modal_delegate
=
149 web_contents_modal_dialog_manager
->delegate();
150 CHECK(modal_delegate
);
151 dialog_
= views::Widget::CreateWindowAsFramelessChild(
152 this, modal_delegate
->GetWebContentsModalDialogHost()->GetHostView());
153 web_contents_modal_dialog_manager
->ShowModalDialog(
154 dialog_
->GetNativeView());
158 virtual void CloseDialog() OVERRIDE
{
159 // The hosting widget may have been freed.
165 friend class base::RefCountedThreadSafe
<LoginHandlerViews
>;
166 friend class LoginPrompt
;
168 virtual ~LoginHandlerViews() {}
170 // The LoginView that contains the user's login information
171 LoginView
* login_view_
;
173 views::Widget
* dialog_
;
175 DISALLOW_COPY_AND_ASSIGN(LoginHandlerViews
);
179 LoginHandler
* LoginHandler::Create(net::AuthChallengeInfo
* auth_info
,
180 net::URLRequest
* request
) {
181 return new LoginHandlerViews(auth_info
, request
);