OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 #ifndef CHROME_BROWSER_LOGIN_PROMPT_H_ | |
6 #define CHROME_BROWSER_LOGIN_PROMPT_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/lock.h" | |
13 #include "base/ref_counted.h" | |
14 #include "chrome/browser/password_manager/password_manager.h" | |
15 #include "chrome/common/notification_observer.h" | |
16 #include "chrome/common/notification_registrar.h" | |
17 | |
18 namespace net { | |
19 class AuthChallengeInfo; | |
20 class URLRequest; | |
21 } // namespace net | |
22 | |
23 class ConstrainedWindow; | |
24 class GURL; | |
25 | |
26 // This is the base implementation for the OS-specific classes that route | |
27 // authentication info to the net::URLRequest that needs it. These functions | |
28 // must be implemented in a thread safe manner. | |
29 class LoginHandler : public base::RefCountedThreadSafe<LoginHandler>, | |
30 public LoginModelObserver, | |
31 public NotificationObserver { | |
32 public: | |
33 LoginHandler(net::AuthChallengeInfo* auth_info, net::URLRequest* request); | |
34 virtual ~LoginHandler(); | |
35 | |
36 // Builds the platform specific LoginHandler. Used from within | |
37 // CreateLoginPrompt() which creates tasks. | |
38 static LoginHandler* Create(net::AuthChallengeInfo* auth_info, | |
39 net::URLRequest* request); | |
40 | |
41 // Initializes the underlying platform specific view. | |
42 virtual void BuildViewForPasswordManager(PasswordManager* manager, | |
43 std::wstring explanation) = 0; | |
44 | |
45 // Sets information about the authentication type (|form|) and the | |
46 // |password_manager| for this profile. | |
47 void SetPasswordForm(const webkit_glue::PasswordForm& form); | |
48 void SetPasswordManager(PasswordManager* password_manager); | |
49 | |
50 // Returns the TabContents that needs authentication. | |
51 TabContents* GetTabContentsForLogin() const; | |
52 | |
53 // Resend the request with authentication credentials. | |
54 // This function can be called from either thread. | |
55 void SetAuth(const std::wstring& username, const std::wstring& password); | |
56 | |
57 // Display the error page without asking for credentials again. | |
58 // This function can be called from either thread. | |
59 void CancelAuth(); | |
60 | |
61 // Notify the handler that the request was cancelled. | |
62 // This function can only be called from the IO thread. | |
63 void OnRequestCancelled(); | |
64 | |
65 // Implements the NotificationObserver interface. | |
66 // Listens for AUTH_SUPPLIED and AUTH_CANCELLED notifications from other | |
67 // LoginHandlers so that this LoginHandler has the chance to dismiss itself | |
68 // if it was waiting for the same authentication. | |
69 virtual void Observe(NotificationType type, | |
70 const NotificationSource& source, | |
71 const NotificationDetails& details); | |
72 | |
73 protected: | |
74 void SetModel(LoginModel* model); | |
75 | |
76 void SetDialog(ConstrainedWindow* dialog); | |
77 | |
78 // Notify observers that authentication is needed. | |
79 void NotifyAuthNeeded(); | |
80 | |
81 // Performs necessary cleanup before deletion. | |
82 void ReleaseSoon(); | |
83 | |
84 // Who/where/what asked for the authentication. | |
85 net::AuthChallengeInfo* auth_info() const { return auth_info_.get(); } | |
86 | |
87 private: | |
88 // Starts observing notifications from other LoginHandlers. | |
89 void AddObservers(); | |
90 | |
91 // Stops observing notifications from other LoginHandlers. | |
92 void RemoveObservers(); | |
93 | |
94 // Notify observers that authentication is supplied. | |
95 void NotifyAuthSupplied(const std::wstring& username, | |
96 const std::wstring& password); | |
97 | |
98 // Notify observers that authentication is cancelled. | |
99 void NotifyAuthCancelled(); | |
100 | |
101 // Returns whether authentication had been handled (SetAuth or CancelAuth). | |
102 // If |set_handled| is true, it will mark authentication as handled. | |
103 bool WasAuthHandled(bool set_handled); | |
104 | |
105 // Calls SetAuth from the IO loop. | |
106 void SetAuthDeferred(const std::wstring& username, | |
107 const std::wstring& password); | |
108 | |
109 // Calls CancelAuth from the IO loop. | |
110 void CancelAuthDeferred(); | |
111 | |
112 // Closes the view_contents from the UI loop. | |
113 void CloseContentsDeferred(); | |
114 | |
115 // True if we've handled auth (SetAuth or CancelAuth has been called). | |
116 bool handled_auth_; | |
117 Lock handled_auth_lock_; | |
118 | |
119 // The ConstrainedWindow that is hosting our LoginView. | |
120 // This should only be accessed on the UI loop. | |
121 ConstrainedWindow* dialog_; | |
122 | |
123 // Who/where/what asked for the authentication. | |
124 scoped_refptr<net::AuthChallengeInfo> auth_info_; | |
125 | |
126 // The request that wants login data. | |
127 // This should only be accessed on the IO loop. | |
128 net::URLRequest* request_; | |
129 | |
130 // The PasswordForm sent to the PasswordManager. This is so we can refer to it | |
131 // when later notifying the password manager if the credentials were accepted | |
132 // or rejected. | |
133 // This should only be accessed on the UI loop. | |
134 webkit_glue::PasswordForm password_form_; | |
135 | |
136 // Points to the password manager owned by the TabContents requesting auth. | |
137 // Can be null if the TabContents is not a TabContents. | |
138 // This should only be accessed on the UI loop. | |
139 PasswordManager* password_manager_; | |
140 | |
141 // Cached from the net::URLRequest, in case it goes NULL on us. | |
142 int render_process_host_id_; | |
143 int tab_contents_id_; | |
144 | |
145 // If not null, points to a model we need to notify of our own destruction | |
146 // so it doesn't try and access this when its too late. | |
147 LoginModel* login_model_; | |
148 | |
149 // Observes other login handlers so this login handler can respond. | |
150 NotificationRegistrar registrar_; | |
151 }; | |
152 | |
153 // Details to provide the NotificationObserver. Used by the automation proxy | |
154 // for testing. | |
155 class LoginNotificationDetails { | |
156 public: | |
157 explicit LoginNotificationDetails(LoginHandler* handler) | |
158 : handler_(handler) {} | |
159 LoginHandler* handler() const { return handler_; } | |
160 | |
161 private: | |
162 LoginNotificationDetails() {} | |
163 | |
164 LoginHandler* handler_; // Where to send the response. | |
165 | |
166 DISALLOW_COPY_AND_ASSIGN(LoginNotificationDetails); | |
167 }; | |
168 | |
169 // Details to provide the NotificationObserver. Used by the automation proxy | |
170 // for testing and by other LoginHandlers to dismiss themselves when an | |
171 // identical auth is supplied. | |
172 class AuthSuppliedLoginNotificationDetails : public LoginNotificationDetails { | |
173 public: | |
174 AuthSuppliedLoginNotificationDetails(LoginHandler* handler, | |
175 const std::wstring& username, | |
176 const std::wstring& password) | |
177 : LoginNotificationDetails(handler), | |
178 username_(username), | |
179 password_(password) {} | |
180 const std::wstring& username() const { return username_; } | |
181 const std::wstring& password() const { return password_; } | |
182 | |
183 private: | |
184 // The username that was used for the authentication. | |
185 const std::wstring username_; | |
186 | |
187 // The password that was used for the authentication. | |
188 const std::wstring password_; | |
189 | |
190 DISALLOW_COPY_AND_ASSIGN(AuthSuppliedLoginNotificationDetails); | |
191 }; | |
192 | |
193 // Prompts the user for their username and password. This is designed to | |
194 // be called on the background (I/O) thread, in response to | |
195 // net::URLRequest::Delegate::OnAuthRequired. The prompt will be created | |
196 // on the main UI thread via a call to UI loop's InvokeLater, and will send the | |
197 // credentials back to the net::URLRequest on the calling thread. | |
198 // A LoginHandler object (which lives on the calling thread) is returned, | |
199 // which can be used to set or cancel authentication programmatically. The | |
200 // caller must invoke OnRequestCancelled() on this LoginHandler before | |
201 // destroying the net::URLRequest. | |
202 LoginHandler* CreateLoginPrompt(net::AuthChallengeInfo* auth_info, | |
203 net::URLRequest* request); | |
204 | |
205 // Helper to remove the ref from an net::URLRequest to the LoginHandler. | |
206 // Should only be called from the IO thread, since it accesses an | |
207 // net::URLRequest. | |
208 void ResetLoginHandlerForRequest(net::URLRequest* request); | |
209 | |
210 // Get the signon_realm under which the identity should be saved. | |
211 std::string GetSignonRealm(const GURL& url, | |
212 const net::AuthChallengeInfo& auth_info); | |
213 | |
214 #endif // CHROME_BROWSER_LOGIN_PROMPT_H_ | |
OLD | NEW |