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/string16.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/password_manager/password_manager.h"
10 #include "chrome/browser/tab_contents/tab_util.h"
11 #include "chrome/browser/ui/views/constrained_window_views.h"
12 #include "chrome/browser/ui/views/login_view.h"
13 #include "chrome/browser/ui/web_contents_modal_dialog_manager.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_view.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 content::BrowserThread
;
26 using content::PasswordForm
;
27 using content::WebContents
;
29 // ----------------------------------------------------------------------------
32 // This class simply forwards the authentication from the LoginView (on
33 // the UI thread) to the net::URLRequest (on the I/O thread).
34 // This class uses ref counting to ensure that it lives until all InvokeLaters
36 class LoginHandlerViews
: public LoginHandler
,
37 public views::DialogDelegate
{
39 LoginHandlerViews(net::AuthChallengeInfo
* auth_info
, net::URLRequest
* request
)
40 : LoginHandler(auth_info
, request
),
45 // LoginModelObserver implementation.
46 virtual void OnAutofillDataAvailable(const string16
& username
,
47 const string16
& password
) OVERRIDE
{
48 // Nothing to do here since LoginView takes care of autofill for win.
51 // views::DialogDelegate methods:
52 virtual string16
GetDialogButtonLabel(
53 ui::DialogButton button
) const OVERRIDE
{
54 if (button
== ui::DIALOG_BUTTON_OK
)
55 return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL
);
56 return DialogDelegate::GetDialogButtonLabel(button
);
59 virtual string16
GetWindowTitle() const OVERRIDE
{
60 return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE
);
63 virtual void WindowClosing() OVERRIDE
{
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
66 WebContents
* tab
= GetWebContentsForLogin();
68 tab
->GetRenderViewHost()->SetIgnoreInputEvents(false);
70 // Reference is no longer valid.
76 virtual void DeleteDelegate() OVERRIDE
{
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
79 // The widget is going to delete itself; clear our pointer.
86 virtual ui::ModalType
GetModalType() const OVERRIDE
{
88 return ui::MODAL_TYPE_CHILD
;
90 return views::WidgetDelegate::GetModalType();
94 virtual bool Cancel() OVERRIDE
{
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
101 virtual bool Accept() OVERRIDE
{
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
104 SetAuth(login_view_
->GetUsername(), login_view_
->GetPassword());
108 // TODO(wittman): Remove this override once we move to the new style frame
109 // view on all dialogs.
110 virtual views::NonClientFrameView
* CreateNonClientFrameView(
111 views::Widget
* widget
) OVERRIDE
{
112 return CreateConstrainedStyleNonClientFrameView(
114 GetWebContentsForLogin()->GetBrowserContext());
117 virtual views::View
* GetInitiallyFocusedView() OVERRIDE
{
118 return login_view_
->GetInitiallyFocusedView();
121 virtual views::View
* GetContentsView() OVERRIDE
{
124 virtual views::Widget
* GetWidget() OVERRIDE
{
125 return login_view_
->GetWidget();
127 virtual const views::Widget
* GetWidget() const OVERRIDE
{
128 return login_view_
->GetWidget();
133 virtual void BuildViewForPasswordManager(
134 PasswordManager
* manager
,
135 const string16
& explanation
) OVERRIDE
{
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
138 // Create a new LoginView and set the model for it. The model
139 // (password manager) is owned by the view's parent WebContents,
140 // so natural destruction order means we don't have to worry about
141 // disassociating the model from the view, because the view will
142 // be deleted before the password manager.
143 login_view_
= new LoginView(explanation
, manager
);
145 // Scary thread safety note: This can potentially be called *after* SetAuth
146 // or CancelAuth (say, if the request was cancelled before the UI thread got
147 // control). However, that's OK since any UI interaction in those functions
148 // will occur via an InvokeLater on the UI thread, which is guaranteed
149 // to happen after this is called (since this was InvokeLater'd first).
150 WebContents
* requesting_contents
= GetWebContentsForLogin();
151 dialog_
= CreateWebContentsModalDialogViews(
153 requesting_contents
->GetView()->GetNativeView());
154 WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
155 WebContentsModalDialogManager::FromWebContents(requesting_contents
);
156 web_contents_modal_dialog_manager
->ShowDialog(dialog_
->GetNativeView());
160 virtual void CloseDialog() OVERRIDE
{
161 // The hosting widget may have been freed.
167 friend class base::RefCountedThreadSafe
<LoginHandlerViews
>;
168 friend class LoginPrompt
;
170 virtual ~LoginHandlerViews() {}
172 // The LoginView that contains the user's login information
173 LoginView
* login_view_
;
175 views::Widget
* dialog_
;
177 DISALLOW_COPY_AND_ASSIGN(LoginHandlerViews
);
181 LoginHandler
* LoginHandler::Create(net::AuthChallengeInfo
* auth_info
,
182 net::URLRequest
* request
) {
183 return new LoginHandlerViews(auth_info
, request
);