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 "chrome/browser/password_manager/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;
21 } // namespace content
24 class AuthChallengeInfo;
25 class HttpNetworkSession;
29 // This is the base implementation for the OS-specific classes that route
30 // authentication info to the net::URLRequest that needs it. These functions
31 // must be implemented in a thread safe manner.
32 class LoginHandler : public content::ResourceDispatcherHostLoginDelegate,
33 public LoginModelObserver,
34 public content::NotificationObserver {
36 LoginHandler(net::AuthChallengeInfo* auth_info, net::URLRequest* request);
38 // Builds the platform specific LoginHandler. Used from within
39 // CreateLoginPrompt() which creates tasks.
40 static LoginHandler* Create(net::AuthChallengeInfo* auth_info,
41 net::URLRequest* request);
43 // ResourceDispatcherHostLoginDelegate implementation:
44 virtual void OnRequestCancelled() OVERRIDE;
46 // Initializes the underlying platform specific view.
47 virtual void BuildViewForPasswordManager(PasswordManager* manager,
48 const string16& explanation) = 0;
50 // Sets information about the authentication type (|form|) and the
51 // |password_manager| for this profile.
52 void SetPasswordForm(const autofill::PasswordForm& form);
53 void SetPasswordManager(PasswordManager* password_manager);
55 // Returns the WebContents that needs authentication.
56 content::WebContents* GetWebContentsForLogin() const;
58 // Resend the request with authentication credentials.
59 // This function can be called from either thread.
60 void SetAuth(const string16& username, const string16& password);
62 // Display the error page without asking for credentials again.
63 // This function can be called from either thread.
66 // Implements the content::NotificationObserver interface.
67 // Listens for AUTH_SUPPLIED and AUTH_CANCELLED notifications from other
68 // LoginHandlers so that this LoginHandler has the chance to dismiss itself
69 // if it was waiting for the same authentication.
70 virtual void Observe(int type,
71 const content::NotificationSource& source,
72 const content::NotificationDetails& details) OVERRIDE;
74 // Who/where/what asked for the authentication.
75 const net::AuthChallengeInfo* auth_info() const { return auth_info_.get(); }
77 // Returns whether authentication had been handled (SetAuth or CancelAuth).
78 bool WasAuthHandled() const;
81 virtual ~LoginHandler();
83 void SetModel(LoginModel* model);
85 // Notify observers that authentication is needed.
86 void NotifyAuthNeeded();
88 // Performs necessary cleanup before deletion.
91 // Closes the native dialog.
92 virtual void CloseDialog() = 0;
95 // Starts observing notifications from other LoginHandlers.
98 // Stops observing notifications from other LoginHandlers.
99 void RemoveObservers();
101 // Notify observers that authentication is supplied.
102 void NotifyAuthSupplied(const string16& username,
103 const string16& password);
105 // Notify observers that authentication is cancelled.
106 void NotifyAuthCancelled();
108 // Marks authentication as handled and returns the previous handled
110 bool TestAndSetAuthHandled();
112 // Calls SetAuth from the IO loop.
113 void SetAuthDeferred(const string16& username,
114 const string16& password);
116 // Calls CancelAuth from the IO loop.
117 void CancelAuthDeferred();
119 // Closes the view_contents from the UI loop.
120 void CloseContentsDeferred();
122 // True if we've handled auth (SetAuth or CancelAuth has been called).
124 mutable base::Lock handled_auth_lock_;
126 // Who/where/what asked for the authentication.
127 scoped_refptr<net::AuthChallengeInfo> auth_info_;
129 // The request that wants login data.
130 // This should only be accessed on the IO loop.
131 net::URLRequest* request_;
133 // The HttpNetworkSession |request_| is associated with.
134 const net::HttpNetworkSession* http_network_session_;
136 // The PasswordForm sent to the PasswordManager. This is so we can refer to it
137 // when later notifying the password manager if the credentials were accepted
139 // This should only be accessed on the UI loop.
140 autofill::PasswordForm password_form_;
142 // Points to the password manager owned by the WebContents requesting auth.
143 // This should only be accessed on the UI loop.
144 PasswordManager* password_manager_;
146 // Cached from the net::URLRequest, in case it goes NULL on us.
147 int render_process_host_id_;
148 int tab_contents_id_;
150 // If not null, points to a model we need to notify of our own destruction
151 // so it doesn't try and access this when its too late.
152 LoginModel* login_model_;
154 // Observes other login handlers so this login handler can respond.
155 // This is only accessed on the UI thread.
156 scoped_ptr<content::NotificationRegistrar> registrar_;
159 // Details to provide the content::NotificationObserver. Used by the automation
160 // proxy for testing.
161 class LoginNotificationDetails {
163 explicit LoginNotificationDetails(LoginHandler* handler)
164 : handler_(handler) {}
165 LoginHandler* handler() const { return handler_; }
168 LoginNotificationDetails() {}
170 LoginHandler* handler_; // Where to send the response.
172 DISALLOW_COPY_AND_ASSIGN(LoginNotificationDetails);
175 // Details to provide the NotificationObserver. Used by the automation proxy
176 // for testing and by other LoginHandlers to dismiss themselves when an
177 // identical auth is supplied.
178 class AuthSuppliedLoginNotificationDetails : public LoginNotificationDetails {
180 AuthSuppliedLoginNotificationDetails(LoginHandler* handler,
181 const string16& username,
182 const string16& password)
183 : LoginNotificationDetails(handler),
185 password_(password) {}
186 const string16& username() const { return username_; }
187 const string16& password() const { return password_; }
190 // The username that was used for the authentication.
191 const string16 username_;
193 // The password that was used for the authentication.
194 const string16 password_;
196 DISALLOW_COPY_AND_ASSIGN(AuthSuppliedLoginNotificationDetails);
199 // Prompts the user for their username and password. This is designed to
200 // be called on the background (I/O) thread, in response to
201 // net::URLRequest::Delegate::OnAuthRequired. The prompt will be created
202 // on the main UI thread via a call to UI loop's InvokeLater, and will send the
203 // credentials back to the net::URLRequest on the calling thread.
204 // A LoginHandler object (which lives on the calling thread) is returned,
205 // which can be used to set or cancel authentication programmatically. The
206 // caller must invoke OnRequestCancelled() on this LoginHandler before
207 // destroying the net::URLRequest.
208 LoginHandler* CreateLoginPrompt(net::AuthChallengeInfo* auth_info,
209 net::URLRequest* request);
211 // Helper to remove the ref from an net::URLRequest to the LoginHandler.
212 // Should only be called from the IO thread, since it accesses an
214 void ResetLoginHandlerForRequest(net::URLRequest* request);
216 // Get the signon_realm under which the identity should be saved.
217 std::string GetSignonRealm(const GURL& url,
218 const net::AuthChallengeInfo& auth_info);
220 #endif // CHROME_BROWSER_UI_LOGIN_LOGIN_PROMPT_H_