1 // Copyright 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 package org.chromium.chrome.browser;
7 import android.app.Activity;
9 import org.chromium.base.CalledByNative;
10 import org.chromium.ui.base.WindowAndroid;
13 * Represents an HTTP authentication request to be handled by the UI.
14 * The request can be fulfilled or canceled using setAuth() or cancelAuth().
15 * This class also provides strings for building a login form.
17 * Note: this class supercedes android.webkit.HttpAuthHandler, but does not
18 * extend HttpAuthHandler due to the private access of HttpAuthHandler's
21 public class ChromeHttpAuthHandler {
22 private final long mNativeChromeHttpAuthHandler;
23 private AutofillObserver mAutofillObserver;
24 private String mAutofillUsername;
25 private String mAutofillPassword;
27 private ChromeHttpAuthHandler(long nativeChromeHttpAuthHandler) {
28 assert nativeChromeHttpAuthHandler != 0;
29 mNativeChromeHttpAuthHandler = nativeChromeHttpAuthHandler;
33 private static ChromeHttpAuthHandler create(long nativeChromeHttpAuthHandler) {
34 return new ChromeHttpAuthHandler(nativeChromeHttpAuthHandler);
37 // ---------------------------------------------
38 // HttpAuthHandler methods
39 // ---------------------------------------------
41 // Note on legacy useHttpAuthUsernamePassword() method:
42 // For reference, the existing WebView (when using the chromium stack) returns true here
43 // iff this is the first auth challenge attempt for this connection.
44 // (see WebUrlLoaderClient::authRequired call to didReceiveAuthenticationChallenge)
45 // In ChromeHttpAuthHandler this mechanism is superseded by the
46 // AutofillObserver.onAutofillDataAvailable mechanism below, however the legacy WebView
47 // implementation will need to handle the API mismatch between the legacy
48 // WebView.getHttpAuthUsernamePassword synchronous call and the credentials arriving
49 // asynchronously in onAutofillDataAvailable.
52 * Cancel the authorization request.
54 public void cancel() {
55 nativeCancelAuth(mNativeChromeHttpAuthHandler);
59 * Proceed with the authorization with the given credentials.
61 public void proceed(String username, String password) {
62 nativeSetAuth(mNativeChromeHttpAuthHandler, username, password);
65 public String getMessageTitle() {
66 return nativeGetMessageTitle(mNativeChromeHttpAuthHandler);
69 public String getMessageBody() {
70 return nativeGetMessageBody(mNativeChromeHttpAuthHandler);
73 public String getUsernameLabelText() {
74 return nativeGetUsernameLabelText(mNativeChromeHttpAuthHandler);
77 public String getPasswordLabelText() {
78 return nativeGetPasswordLabelText(mNativeChromeHttpAuthHandler);
81 public String getOkButtonText() {
82 return nativeGetOkButtonText(mNativeChromeHttpAuthHandler);
85 public String getCancelButtonText() {
86 return nativeGetCancelButtonText(mNativeChromeHttpAuthHandler);
90 private void showDialog(WindowAndroid windowAndroid) {
91 if (windowAndroid == null) {
94 Activity activity = windowAndroid.getActivity().get();
95 if (activity == null) {
98 LoginPrompt authDialog = new LoginPrompt(activity, this);
99 setAutofillObserver(authDialog);
103 // ---------------------------------------------
105 // ---------------------------------------------
108 * This is a public interface that will act as a hook for providing login data using
109 * autofill. When the observer is set, {@link ChromeHttpAuthhandler}'s
110 * onAutofillDataAvailable callback can be used by the observer to fill out necessary
113 public static interface AutofillObserver {
114 public void onAutofillDataAvailable(String username, String password);
118 * Register for onAutofillDataAvailable callbacks. |observer| can be null,
119 * in which case no callback is made.
121 private void setAutofillObserver(AutofillObserver observer) {
122 mAutofillObserver = observer;
123 // In case the autofill data arrives before the observer is set.
124 if (mAutofillUsername != null && mAutofillPassword != null) {
125 mAutofillObserver.onAutofillDataAvailable(mAutofillUsername, mAutofillPassword);
130 private void onAutofillDataAvailable(String username, String password) {
131 mAutofillUsername = username;
132 mAutofillPassword = password;
133 if (mAutofillObserver != null) {
134 mAutofillObserver.onAutofillDataAvailable(username, password);
138 // ---------------------------------------------
140 // ---------------------------------------------
142 private native void nativeSetAuth(long nativeChromeHttpAuthHandler,
143 String username, String password);
144 private native void nativeCancelAuth(long nativeChromeHttpAuthHandler);
145 private native String nativeGetCancelButtonText(long nativeChromeHttpAuthHandler);
146 private native String nativeGetMessageTitle(long nativeChromeHttpAuthHandler);
147 private native String nativeGetMessageBody(long nativeChromeHttpAuthHandler);
148 private native String nativeGetPasswordLabelText(long nativeChromeHttpAuthHandler);
149 private native String nativeGetOkButtonText(long nativeChromeHttpAuthHandler);
150 private native String nativeGetUsernameLabelText(long nativeChromeHttpAuthHandler);