blob: 9a0f648ebbfaf34ecd927a22a87e52fc965704c4 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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)868fa2f2013-06-11 10:57:03 +01007#include "base/strings/string16.h"
8#include "base/strings/utf_string_conversions.h"
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +01009#include "chrome/browser/ui/views/constrained_window_views.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010#include "chrome/browser/ui/views/login_view.h"
Torne (Richard Coles)6e8cce62014-08-19 13:00:08 +010011#include "chrome/grit/generated_resources.h"
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000012#include "components/password_manager/core/browser/password_manager.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#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)58218062012-11-14 11:43:16 +000016#include "net/url_request/url_request.h"
17#include "ui/base/l10n/l10n_util.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000018#include "ui/views/widget/widget.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019#include "ui/views/window/dialog_delegate.h"
20
Torne (Richard Coles)58218062012-11-14 11:43:16 +000021// ----------------------------------------------------------------------------
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)46d4c2b2014-06-09 12:00:27 +010028class LoginHandlerViews : public LoginHandler, public views::DialogDelegate {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029 public:
30 LoginHandlerViews(net::AuthChallengeInfo* auth_info, net::URLRequest* request)
31 : LoginHandler(auth_info, request),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000032 login_view_(NULL),
33 dialog_(NULL) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000034 }
35
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010036 // LoginModelObserver:
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +000037 virtual void OnAutofillDataAvailable(
38 const base::string16& username,
39 const base::string16& password) OVERRIDE {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000040 // Nothing to do here since LoginView takes care of autofill for win.
41 }
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +010042 virtual void OnLoginModelDestroying() OVERRIDE {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000043
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010044 // views::DialogDelegate:
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +000045 virtual base::string16 GetDialogButtonLabel(
Torne (Richard Coles)58218062012-11-14 11:43:16 +000046 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)a3f6a492013-12-18 16:25:09 +000052 virtual base::string16 GetWindowTitle() const OVERRIDE {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000053 return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE);
54 }
55
56 virtual void WindowClosing() OVERRIDE {
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010057 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)58218062012-11-14 11:43:16 +000061
62 // Reference is no longer valid.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000063 dialog_ = NULL;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000064 CancelAuth();
65 }
66
67 virtual void DeleteDelegate() OVERRIDE {
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010068 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000069
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000070 // The widget is going to delete itself; clear our pointer.
71 dialog_ = NULL;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000072 SetModel(NULL);
73
74 ReleaseSoon();
75 }
76
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000077 virtual ui::ModalType GetModalType() const OVERRIDE {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000078 return ui::MODAL_TYPE_CHILD;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000079 }
80
81 virtual bool Cancel() OVERRIDE {
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010082 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000083 CancelAuth();
84 return true;
85 }
86
87 virtual bool Accept() OVERRIDE {
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010088 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000089 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)58218062012-11-14 11:43:16 +0000108 virtual void BuildViewForPasswordManager(
Ben Murdochc5cede92014-04-10 11:22:14 +0100109 password_manager::PasswordManager* manager,
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +0000110 const base::string16& explanation) OVERRIDE {
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000112
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100113 // 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)2a99a7e2013-03-28 15:31:22 +0000118 login_view_ = new LoginView(explanation, manager);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000119
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)46d4c2b2014-06-09 12:00:27 +0100125 dialog_ = ShowWebModalDialogViews(this, GetWebContentsForLogin());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000126 NotifyAuthNeeded();
127 }
128
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000129 virtual void CloseDialog() OVERRIDE {
130 // The hosting widget may have been freed.
131 if (dialog_)
132 dialog_->Close();
133 }
134
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000135 private:
136 friend class base::RefCountedThreadSafe<LoginHandlerViews>;
137 friend class LoginPrompt;
138
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000139 virtual ~LoginHandlerViews() {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000140
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100141 // The LoginView that contains the user's login information.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000142 LoginView* login_view_;
143
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000144 views::Widget* dialog_;
145
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000146 DISALLOW_COPY_AND_ASSIGN(LoginHandlerViews);
147};
148
149// static
150LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info,
151 net::URLRequest* request) {
152 return new LoginHandlerViews(auth_info, request);
153}