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. |
4 | |
5 | package org.chromium.chrome.browser; |
6 | |
7 | import org.chromium.base.CalledByNative; |
8 | |
9 | /** |
10 | * Represents an HTTP authentication request to be handled by the UI. |
11 | * The request can be fulfilled or canceled using setAuth() or cancelAuth(). |
12 | * This class also provides strings for building a login form. |
13 | * |
14 | * Note: this class supercedes android.webkit.HttpAuthHandler, but does not |
15 | * extend HttpAuthHandler due to the private access of HttpAuthHandler's |
16 | * constructor. |
17 | */ |
18 | public class ChromeHttpAuthHandler { |
19 | private final int mNativeChromeHttpAuthHandler; |
20 | private AutofillObserver mAutofillObserver; |
21 | private String mAutofillUsername; |
22 | private String mAutofillPassword; |
23 | |
24 | private ChromeHttpAuthHandler(int nativeChromeHttpAuthHandler) { |
25 | assert nativeChromeHttpAuthHandler != 0; |
26 | mNativeChromeHttpAuthHandler = nativeChromeHttpAuthHandler; |
27 | } |
28 | |
29 | @CalledByNative |
30 | private static ChromeHttpAuthHandler create(int nativeChromeHttpAuthHandler) { |
31 | return new ChromeHttpAuthHandler(nativeChromeHttpAuthHandler); |
32 | } |
33 | |
34 | // --------------------------------------------- |
35 | // HttpAuthHandler methods |
36 | // --------------------------------------------- |
37 | |
38 | // Note on legacy useHttpAuthUsernamePassword() method: |
39 | // For reference, the existing WebView (when using the chromium stack) returns true here |
40 | // iff this is the first auth challenge attempt for this connection. |
41 | // (see WebUrlLoaderClient::authRequired call to didReceiveAuthenticationChallenge) |
42 | // In ChromeHttpAuthHandler this mechanism is superseded by the |
43 | // AutofillObserver.onAutofillDataAvailable mechanism below, however the legacy WebView |
44 | // implementation will need to handle the API mismatch between the legacy |
45 | // WebView.getHttpAuthUsernamePassword synchronous call and the credentials arriving |
46 | // asynchronously in onAutofillDataAvailable. |
47 | |
48 | /** |
49 | * Cancel the authorization request. |
50 | */ |
51 | public void cancel() { |
52 | nativeCancelAuth(mNativeChromeHttpAuthHandler); |
53 | } |
54 | |
55 | /** |
56 | * Proceed with the authorization with the given credentials. |
57 | */ |
58 | public void proceed(String username, String password) { |
59 | nativeSetAuth(mNativeChromeHttpAuthHandler, username, password); |
60 | } |
61 | |
62 | public String getMessageTitle() { |
63 | return nativeGetMessageTitle(mNativeChromeHttpAuthHandler); |
64 | } |
65 | |
66 | public String getMessageBody() { |
67 | return nativeGetMessageBody(mNativeChromeHttpAuthHandler); |
68 | } |
69 | |
70 | public String getUsernameLabelText() { |
71 | return nativeGetUsernameLabelText(mNativeChromeHttpAuthHandler); |
72 | } |
73 | |
74 | public String getPasswordLabelText() { |
75 | return nativeGetPasswordLabelText(mNativeChromeHttpAuthHandler); |
76 | } |
77 | |
78 | public String getOkButtonText() { |
79 | return nativeGetOkButtonText(mNativeChromeHttpAuthHandler); |
80 | } |
81 | |
82 | public String getCancelButtonText() { |
83 | return nativeGetCancelButtonText(mNativeChromeHttpAuthHandler); |
84 | } |
85 | |
86 | // --------------------------------------------- |
87 | // Autofill-related |
88 | // --------------------------------------------- |
89 | |
90 | /** |
91 | * This is a public interface that will act as a hook for providing login data using |
92 | * autofill. When the observer is set, {@link ChromeHttpAuthhandler}'s |
93 | * onAutofillDataAvailable callback can be used by the observer to fill out necessary |
94 | * login information. |
95 | */ |
96 | public static interface AutofillObserver { |
97 | public void onAutofillDataAvailable(String username, String password); |
98 | } |
99 | |
100 | /** |
101 | * Register for onAutofillDataAvailable callbacks. |observer| can be null, |
102 | * in which case no callback is made. |
103 | */ |
104 | public void setAutofillObserver(AutofillObserver observer) { |
105 | mAutofillObserver = observer; |
106 | // In case the autofill data arrives before the observer is set. |
107 | if (mAutofillUsername != null && mAutofillPassword != null) { |
108 | mAutofillObserver.onAutofillDataAvailable(mAutofillUsername, mAutofillPassword); |
109 | } |
110 | } |
111 | |
112 | @CalledByNative |
113 | private void onAutofillDataAvailable(String username, String password) { |
114 | mAutofillUsername = username; |
115 | mAutofillPassword = password; |
116 | if (mAutofillObserver != null) { |
117 | mAutofillObserver.onAutofillDataAvailable(username, password); |
118 | } |
119 | } |
120 | |
121 | // --------------------------------------------- |
122 | // Native side calls |
123 | // --------------------------------------------- |
124 | |
125 | private native void nativeSetAuth(int nativeChromeHttpAuthHandler, |
126 | String username, String password); |
127 | private native void nativeCancelAuth(int nativeChromeHttpAuthHandler); |
128 | private native String nativeGetCancelButtonText(int nativeChromeHttpAuthHandler); |
129 | private native String nativeGetMessageTitle(int nativeChromeHttpAuthHandler); |
130 | private native String nativeGetMessageBody(int nativeChromeHttpAuthHandler); |
131 | private native String nativeGetPasswordLabelText(int nativeChromeHttpAuthHandler); |
132 | private native String nativeGetOkButtonText(int nativeChromeHttpAuthHandler); |
133 | private native String nativeGetUsernameLabelText(int nativeChromeHttpAuthHandler); |
134 | } |