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/ui/views/login_view.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "components/constrained_window/constrained_window_views.h"
12 #include "components/password_manager/core/browser/password_manager.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "net/url_request/url_request.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/views/window/dialog_delegate.h"
21 // ----------------------------------------------------------------------------
24 // This class simply forwards the authentication from the LoginView (on
25 // the UI thread) to the net::URLRequest (on the I/O thread).
26 // This class uses ref counting to ensure that it lives until all InvokeLaters
28 class LoginHandlerViews
: public LoginHandler
, public views::DialogDelegate
{
30 LoginHandlerViews(net::AuthChallengeInfo
* auth_info
, net::URLRequest
* request
)
31 : LoginHandler(auth_info
, request
),
36 // LoginModelObserver:
37 void OnAutofillDataAvailable(const base::string16
& username
,
38 const base::string16
& password
) override
{
39 // Nothing to do here since LoginView takes care of autofill for win.
41 void OnLoginModelDestroying() override
{}
43 // views::DialogDelegate:
44 base::string16
GetDialogButtonLabel(ui::DialogButton button
) const override
{
45 if (button
== ui::DIALOG_BUTTON_OK
)
46 return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL
);
47 return DialogDelegate::GetDialogButtonLabel(button
);
50 base::string16
GetWindowTitle() const override
{
51 return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE
);
54 void WindowClosing() override
{
55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
56 content::WebContents
* web_contents
= GetWebContentsForLogin();
58 web_contents
->GetRenderViewHost()->SetIgnoreInputEvents(false);
60 // Reference is no longer valid.
65 void DeleteDelegate() override
{
66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
68 // The widget is going to delete itself; clear our pointer.
75 ui::ModalType
GetModalType() const override
{ return ui::MODAL_TYPE_CHILD
; }
77 bool Cancel() override
{
78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
83 bool Accept() override
{
84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
85 SetAuth(login_view_
->GetUsername(), login_view_
->GetPassword());
89 views::View
* GetInitiallyFocusedView() override
{
90 return login_view_
->GetInitiallyFocusedView();
93 views::View
* GetContentsView() override
{ return login_view_
; }
94 views::Widget
* GetWidget() override
{ return login_view_
->GetWidget(); }
95 const views::Widget
* GetWidget() const override
{
96 return login_view_
->GetWidget();
100 void BuildViewForPasswordManager(password_manager::PasswordManager
* manager
,
101 const base::string16
& explanation
) override
{
102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
104 // Create a new LoginView and set the model for it. The model (password
105 // manager) is owned by the WebContents, but the view is parented to the
106 // browser window, so the view may be destroyed after the password
107 // manager. The view listens for model destruction and unobserves
109 login_view_
= new LoginView(explanation
, manager
);
111 // Scary thread safety note: This can potentially be called *after* SetAuth
112 // or CancelAuth (say, if the request was cancelled before the UI thread got
113 // control). However, that's OK since any UI interaction in those functions
114 // will occur via an InvokeLater on the UI thread, which is guaranteed
115 // to happen after this is called (since this was InvokeLater'd first).
116 dialog_
= constrained_window::ShowWebModalDialogViews(
117 this, GetWebContentsForLogin());
121 void CloseDialog() override
{
122 // The hosting widget may have been freed.
128 friend class base::RefCountedThreadSafe
<LoginHandlerViews
>;
129 friend class LoginPrompt
;
131 ~LoginHandlerViews() override
{}
133 // The LoginView that contains the user's login information.
134 LoginView
* login_view_
;
136 views::Widget
* dialog_
;
138 DISALLOW_COPY_AND_ASSIGN(LoginHandlerViews
);
143 LoginHandler
* CreateLoginHandlerViews(net::AuthChallengeInfo
* auth_info
,
144 net::URLRequest
* request
) {
145 return new LoginHandlerViews(auth_info
, request
);
148 } // namespace chrome