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