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 #ifndef CHROME_BROWSER_UI_LOGIN_LOGIN_PROMPT_H_
6 #define CHROME_BROWSER_UI_LOGIN_LOGIN_PROMPT_H_
10 #include "base/basictypes.h"
11 #include "base/synchronization/lock.h"
12 #include "components/password_manager/core/browser/password_manager.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/resource_dispatcher_host_login_delegate.h"
19 class RenderViewHostDelegate
;
20 class NotificationRegistrar
;
22 } // namespace content
25 class AuthChallengeInfo
;
26 class HttpNetworkSession
;
30 namespace password_manager
{
31 class ContentPasswordManagerDriver
;
32 } // namespace password_manager
34 // This is the base implementation for the OS-specific classes that route
35 // authentication info to the net::URLRequest that needs it. These functions
36 // must be implemented in a thread safe manner.
37 class LoginHandler
: public content::ResourceDispatcherHostLoginDelegate
,
38 public password_manager::LoginModelObserver
,
39 public content::NotificationObserver
{
41 LoginHandler(net::AuthChallengeInfo
* auth_info
, net::URLRequest
* request
);
43 // Builds the platform specific LoginHandler. Used from within
44 // CreateLoginPrompt() which creates tasks.
45 static LoginHandler
* Create(net::AuthChallengeInfo
* auth_info
,
46 net::URLRequest
* request
);
48 // ResourceDispatcherHostLoginDelegate implementation:
49 void OnRequestCancelled() override
;
51 // Initializes the underlying platform specific view.
52 virtual void BuildViewForPasswordManager(
53 password_manager::PasswordManager
* manager
,
54 const base::string16
& explanation
) = 0;
56 // Sets information about the authentication type (|form|) and the
57 // |password_manager| for this profile.
58 void SetPasswordForm(const autofill::PasswordForm
& form
);
59 void SetPasswordManager(password_manager::PasswordManager
* password_manager
);
61 // Returns the WebContents that needs authentication.
62 content::WebContents
* GetWebContentsForLogin() const;
64 // Returns the PasswordManager for the render frame that needs login.
65 password_manager::ContentPasswordManagerDriver
*
66 GetPasswordManagerDriverForLogin();
68 // Resend the request with authentication credentials.
69 // This function can be called from either thread.
70 void SetAuth(const base::string16
& username
, const base::string16
& password
);
72 // Display the error page without asking for credentials again.
73 // This function can be called from either thread.
76 // Implements the content::NotificationObserver interface.
77 // Listens for AUTH_SUPPLIED and AUTH_CANCELLED notifications from other
78 // LoginHandlers so that this LoginHandler has the chance to dismiss itself
79 // if it was waiting for the same authentication.
80 void Observe(int type
,
81 const content::NotificationSource
& source
,
82 const content::NotificationDetails
& details
) override
;
84 // Who/where/what asked for the authentication.
85 const net::AuthChallengeInfo
* auth_info() const { return auth_info_
.get(); }
87 // Returns whether authentication had been handled (SetAuth or CancelAuth).
88 bool WasAuthHandled() const;
91 ~LoginHandler() override
;
93 void SetModel(password_manager::LoginModel
* model
);
95 // Notify observers that authentication is needed.
96 void NotifyAuthNeeded();
98 // Performs necessary cleanup before deletion.
101 // Closes the native dialog.
102 virtual void CloseDialog() = 0;
105 // Starts observing notifications from other LoginHandlers.
108 // Stops observing notifications from other LoginHandlers.
109 void RemoveObservers();
111 // Notify observers that authentication is supplied.
112 void NotifyAuthSupplied(const base::string16
& username
,
113 const base::string16
& password
);
115 // Notify observers that authentication is cancelled.
116 void NotifyAuthCancelled();
118 // Marks authentication as handled and returns the previous handled
120 bool TestAndSetAuthHandled();
122 // Calls SetAuth from the IO loop.
123 void SetAuthDeferred(const base::string16
& username
,
124 const base::string16
& password
);
126 // Calls CancelAuth from the IO loop.
127 void CancelAuthDeferred();
129 // Closes the view_contents from the UI loop.
130 void CloseContentsDeferred();
132 // True if we've handled auth (SetAuth or CancelAuth has been called).
134 mutable base::Lock handled_auth_lock_
;
136 // Who/where/what asked for the authentication.
137 scoped_refptr
<net::AuthChallengeInfo
> auth_info_
;
139 // The request that wants login data.
140 // This should only be accessed on the IO loop.
141 net::URLRequest
* request_
;
143 // The HttpNetworkSession |request_| is associated with.
144 const net::HttpNetworkSession
* http_network_session_
;
146 // The PasswordForm sent to the PasswordManager. This is so we can refer to it
147 // when later notifying the password manager if the credentials were accepted
149 // This should only be accessed on the UI loop.
150 autofill::PasswordForm password_form_
;
152 // Points to the password manager owned by the WebContents requesting auth.
153 // This should only be accessed on the UI loop.
154 password_manager::PasswordManager
* password_manager_
;
156 // Cached from the net::URLRequest, in case it goes NULL on us.
157 int render_process_host_id_
;
158 int render_frame_id_
;
160 // If not null, points to a model we need to notify of our own destruction
161 // so it doesn't try and access this when its too late.
162 password_manager::LoginModel
* login_model_
;
164 // Observes other login handlers so this login handler can respond.
165 // This is only accessed on the UI thread.
166 scoped_ptr
<content::NotificationRegistrar
> registrar_
;
169 // Details to provide the content::NotificationObserver. Used by the automation
170 // proxy for testing.
171 class LoginNotificationDetails
{
173 explicit LoginNotificationDetails(LoginHandler
* handler
)
174 : handler_(handler
) {}
175 LoginHandler
* handler() const { return handler_
; }
178 LoginNotificationDetails() {}
180 LoginHandler
* handler_
; // Where to send the response.
182 DISALLOW_COPY_AND_ASSIGN(LoginNotificationDetails
);
185 // Details to provide the NotificationObserver. Used by the automation proxy
186 // for testing and by other LoginHandlers to dismiss themselves when an
187 // identical auth is supplied.
188 class AuthSuppliedLoginNotificationDetails
: public LoginNotificationDetails
{
190 AuthSuppliedLoginNotificationDetails(LoginHandler
* handler
,
191 const base::string16
& username
,
192 const base::string16
& password
)
193 : LoginNotificationDetails(handler
),
195 password_(password
) {}
196 const base::string16
& username() const { return username_
; }
197 const base::string16
& password() const { return password_
; }
200 // The username that was used for the authentication.
201 const base::string16 username_
;
203 // The password that was used for the authentication.
204 const base::string16 password_
;
206 DISALLOW_COPY_AND_ASSIGN(AuthSuppliedLoginNotificationDetails
);
209 // Prompts the user for their username and password. This is designed to
210 // be called on the background (I/O) thread, in response to
211 // net::URLRequest::Delegate::OnAuthRequired. The prompt will be created
212 // on the main UI thread via a call to UI loop's InvokeLater, and will send the
213 // credentials back to the net::URLRequest on the calling thread.
214 // A LoginHandler object (which lives on the calling thread) is returned,
215 // which can be used to set or cancel authentication programmatically. The
216 // caller must invoke OnRequestCancelled() on this LoginHandler before
217 // destroying the net::URLRequest.
218 LoginHandler
* CreateLoginPrompt(net::AuthChallengeInfo
* auth_info
,
219 net::URLRequest
* request
);
221 // Helper to remove the ref from an net::URLRequest to the LoginHandler.
222 // Should only be called from the IO thread, since it accesses an
224 void ResetLoginHandlerForRequest(net::URLRequest
* request
);
226 // Get the signon_realm under which the identity should be saved.
227 std::string
GetSignonRealm(const GURL
& url
,
228 const net::AuthChallengeInfo
& auth_info
);
230 #endif // CHROME_BROWSER_UI_LOGIN_LOGIN_PROMPT_H_