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.
4
5package org.chromium.chrome.browser;
6
7import android.app.Activity;
8
9import org.chromium.base.CalledByNative;
10import org.chromium.ui.base.WindowAndroid;
11
12/**
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.
16 *
17 * Note: this class supercedes android.webkit.HttpAuthHandler, but does not
18 * extend HttpAuthHandler due to the private access of HttpAuthHandler's
19 * constructor.
20 */
21public class ChromeHttpAuthHandler {
22    private final long mNativeChromeHttpAuthHandler;
23    private AutofillObserver mAutofillObserver;
24    private String mAutofillUsername;
25    private String mAutofillPassword;
26
27    private ChromeHttpAuthHandler(long nativeChromeHttpAuthHandler) {
28        assert nativeChromeHttpAuthHandler != 0;
29        mNativeChromeHttpAuthHandler = nativeChromeHttpAuthHandler;
30    }
31
32    @CalledByNative
33    private static ChromeHttpAuthHandler create(long nativeChromeHttpAuthHandler) {
34        return new ChromeHttpAuthHandler(nativeChromeHttpAuthHandler);
35    }
36
37    // ---------------------------------------------
38    // HttpAuthHandler methods
39    // ---------------------------------------------
40
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.
50
51    /**
52     * Cancel the authorization request.
53     */
54    public void cancel() {
55        nativeCancelAuth(mNativeChromeHttpAuthHandler);
56    }
57
58    /**
59     * Proceed with the authorization with the given credentials.
60     */
61    public void proceed(String username, String password) {
62        nativeSetAuth(mNativeChromeHttpAuthHandler, username, password);
63    }
64
65    public String getMessageTitle() {
66        return nativeGetMessageTitle(mNativeChromeHttpAuthHandler);
67    }
68
69    public String getMessageBody() {
70        return nativeGetMessageBody(mNativeChromeHttpAuthHandler);
71    }
72
73    public String getUsernameLabelText() {
74        return nativeGetUsernameLabelText(mNativeChromeHttpAuthHandler);
75    }
76
77    public String getPasswordLabelText() {
78        return nativeGetPasswordLabelText(mNativeChromeHttpAuthHandler);
79    }
80
81    public String getOkButtonText() {
82        return nativeGetOkButtonText(mNativeChromeHttpAuthHandler);
83    }
84
85    public String getCancelButtonText() {
86        return nativeGetCancelButtonText(mNativeChromeHttpAuthHandler);
87    }
88
89    @CalledByNative
90    private void showDialog(WindowAndroid windowAndroid) {
91        if (windowAndroid == null) {
92            cancel();
93        }
94        Activity activity = windowAndroid.getActivity().get();
95        if (activity == null) {
96            cancel();
97        }
98        LoginPrompt authDialog = new LoginPrompt(activity, this);
99        setAutofillObserver(authDialog);
100        authDialog.show();
101    }
102
103    // ---------------------------------------------
104    // Autofill-related
105    // ---------------------------------------------
106
107    /**
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
111     * login information.
112     */
113    public static interface AutofillObserver {
114        public void onAutofillDataAvailable(String username, String password);
115    }
116
117    /**
118     * Register for onAutofillDataAvailable callbacks.  |observer| can be null,
119     * in which case no callback is made.
120     */
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);
126        }
127    }
128
129    @CalledByNative
130    private void onAutofillDataAvailable(String username, String password) {
131        mAutofillUsername = username;
132        mAutofillPassword = password;
133        if (mAutofillObserver != null) {
134            mAutofillObserver.onAutofillDataAvailable(username, password);
135        }
136    }
137
138    // ---------------------------------------------
139    // Native side calls
140    // ---------------------------------------------
141
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);
151}
152