Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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/ui/login/login_prompt.h" |
| 6 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 7 | #include "base/strings/string16.h" |
| 8 | #include "base/strings/utf_string_conversions.h" |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 9 | #include "chrome/browser/ui/views/constrained_window_views.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | #include "chrome/browser/ui/views/login_view.h" |
Torne (Richard Coles) | 6e8cce6 | 2014-08-19 13:00:08 +0100 | [diff] [blame^] | 11 | #include "chrome/grit/generated_resources.h" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 12 | #include "components/password_manager/core/browser/password_manager.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 16 | #include "net/url_request/url_request.h" |
| 17 | #include "ui/base/l10n/l10n_util.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 18 | #include "ui/views/widget/widget.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 19 | #include "ui/views/window/dialog_delegate.h" |
| 20 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 21 | // ---------------------------------------------------------------------------- |
| 22 | // LoginHandlerViews |
| 23 | |
| 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 |
| 27 | // have been called. |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 28 | class LoginHandlerViews : public LoginHandler, public views::DialogDelegate { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 29 | public: |
| 30 | LoginHandlerViews(net::AuthChallengeInfo* auth_info, net::URLRequest* request) |
| 31 | : LoginHandler(auth_info, request), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 32 | login_view_(NULL), |
| 33 | dialog_(NULL) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 36 | // LoginModelObserver: |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 37 | virtual void OnAutofillDataAvailable( |
| 38 | const base::string16& username, |
| 39 | const base::string16& password) OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 40 | // Nothing to do here since LoginView takes care of autofill for win. |
| 41 | } |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 42 | virtual void OnLoginModelDestroying() OVERRIDE {} |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 43 | |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 44 | // views::DialogDelegate: |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 45 | virtual base::string16 GetDialogButtonLabel( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 46 | ui::DialogButton button) const OVERRIDE { |
| 47 | if (button == ui::DIALOG_BUTTON_OK) |
| 48 | return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL); |
| 49 | return DialogDelegate::GetDialogButtonLabel(button); |
| 50 | } |
| 51 | |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 52 | virtual base::string16 GetWindowTitle() const OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 53 | return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE); |
| 54 | } |
| 55 | |
| 56 | virtual void WindowClosing() OVERRIDE { |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 57 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 58 | content::WebContents* web_contents = GetWebContentsForLogin(); |
| 59 | if (web_contents) |
| 60 | web_contents->GetRenderViewHost()->SetIgnoreInputEvents(false); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 61 | |
| 62 | // Reference is no longer valid. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 63 | dialog_ = NULL; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 64 | CancelAuth(); |
| 65 | } |
| 66 | |
| 67 | virtual void DeleteDelegate() OVERRIDE { |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 68 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 69 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 70 | // The widget is going to delete itself; clear our pointer. |
| 71 | dialog_ = NULL; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 72 | SetModel(NULL); |
| 73 | |
| 74 | ReleaseSoon(); |
| 75 | } |
| 76 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 77 | virtual ui::ModalType GetModalType() const OVERRIDE { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 78 | return ui::MODAL_TYPE_CHILD; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | virtual bool Cancel() OVERRIDE { |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 82 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 83 | CancelAuth(); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | virtual bool Accept() OVERRIDE { |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 88 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 89 | SetAuth(login_view_->GetUsername(), login_view_->GetPassword()); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | virtual views::View* GetInitiallyFocusedView() OVERRIDE { |
| 94 | return login_view_->GetInitiallyFocusedView(); |
| 95 | } |
| 96 | |
| 97 | virtual views::View* GetContentsView() OVERRIDE { |
| 98 | return login_view_; |
| 99 | } |
| 100 | virtual views::Widget* GetWidget() OVERRIDE { |
| 101 | return login_view_->GetWidget(); |
| 102 | } |
| 103 | virtual const views::Widget* GetWidget() const OVERRIDE { |
| 104 | return login_view_->GetWidget(); |
| 105 | } |
| 106 | |
| 107 | // LoginHandler: |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 108 | virtual void BuildViewForPasswordManager( |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 109 | password_manager::PasswordManager* manager, |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 110 | const base::string16& explanation) OVERRIDE { |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 111 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 112 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 113 | // Create a new LoginView and set the model for it. The model (password |
| 114 | // manager) is owned by the WebContents, but the view is parented to the |
| 115 | // browser window, so the view may be destroyed after the password |
| 116 | // manager. The view listens for model destruction and unobserves |
| 117 | // accordingly. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 118 | login_view_ = new LoginView(explanation, manager); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 119 | |
| 120 | // Scary thread safety note: This can potentially be called *after* SetAuth |
| 121 | // or CancelAuth (say, if the request was cancelled before the UI thread got |
| 122 | // control). However, that's OK since any UI interaction in those functions |
| 123 | // will occur via an InvokeLater on the UI thread, which is guaranteed |
| 124 | // to happen after this is called (since this was InvokeLater'd first). |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 125 | dialog_ = ShowWebModalDialogViews(this, GetWebContentsForLogin()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 126 | NotifyAuthNeeded(); |
| 127 | } |
| 128 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 129 | virtual void CloseDialog() OVERRIDE { |
| 130 | // The hosting widget may have been freed. |
| 131 | if (dialog_) |
| 132 | dialog_->Close(); |
| 133 | } |
| 134 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 135 | private: |
| 136 | friend class base::RefCountedThreadSafe<LoginHandlerViews>; |
| 137 | friend class LoginPrompt; |
| 138 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 139 | virtual ~LoginHandlerViews() {} |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 140 | |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 141 | // The LoginView that contains the user's login information. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 142 | LoginView* login_view_; |
| 143 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 144 | views::Widget* dialog_; |
| 145 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 146 | DISALLOW_COPY_AND_ASSIGN(LoginHandlerViews); |
| 147 | }; |
| 148 | |
| 149 | // static |
| 150 | LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info, |
| 151 | net::URLRequest* request) { |
| 152 | return new LoginHandlerViews(auth_info, request); |
| 153 | } |