blob: bdd7a090021372da0829e765e0fedeac53fdf73d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
Nate Fischer5ab00b32017-07-13 15:28:21 -070019import android.annotation.IntDef;
Nate Fischer3442c742017-09-08 17:02:00 -070020import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.graphics.Bitmap;
22import android.net.http.SslError;
23import android.os.Message;
Michael Wright899d7052014-04-23 17:23:39 -070024import android.view.InputEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.view.KeyEvent;
John Reckd6b10982012-04-19 18:01:35 -070026import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Nate Fischer5ab00b32017-07-13 15:28:21 -070028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031public class WebViewClient {
32
33 /**
Paul Millerf2043b42018-07-25 18:11:26 -070034 * Give the host application a chance to take control when a URL is about to be loaded in the
35 * current WebView. If a WebViewClient is not provided, by default WebView will ask Activity
36 * Manager to choose the proper handler for the URL. If a WebViewClient is provided, returning
37 * {@code true} causes the current WebView to abort loading the URL, while returning
38 * {@code false} causes the WebView to continue loading the URL as usual.
39 *
40 * <p class="note"><b>Note:</b> Do not call {@link WebView#loadUrl(String)} with the same
41 * URL and then return {@code true}. This unnecessarily cancels the current load and starts a
42 * new load with the same URL. The correct way to continue loading a given URL is to simply
43 * return {@code false}, without calling {@link WebView#loadUrl(String)}.
44 *
45 * <p class="note"><b>Note:</b> This method is not called for POST requests.
46 *
47 * <p class="note"><b>Note:</b> This method may be called for subframes and with non-HTTP(S)
48 * schemes; calling {@link WebView#loadUrl(String)} with such a URL will fail.
Brian Carlstroma1477592011-02-11 13:39:56 -080049 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 * @param view The WebView that is initiating the callback.
Paul Millerf2043b42018-07-25 18:11:26 -070051 * @param url The URL to be loaded.
52 * @return {@code true} to cancel the current load, otherwise return {@code false}.
Mikhail Naganovb8519292015-10-26 13:27:33 -070053 * @deprecated Use {@link #shouldOverrideUrlLoading(WebView, WebResourceRequest)
54 * shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 */
Mikhail Naganovb8519292015-10-26 13:27:33 -070056 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 public boolean shouldOverrideUrlLoading(WebView view, String url) {
58 return false;
59 }
60
61 /**
Paul Millerf2043b42018-07-25 18:11:26 -070062 * Give the host application a chance to take control when a URL is about to be loaded in the
63 * current WebView. If a WebViewClient is not provided, by default WebView will ask Activity
64 * Manager to choose the proper handler for the URL. If a WebViewClient is provided, returning
65 * {@code true} causes the current WebView to abort loading the URL, while returning
66 * {@code false} causes the WebView to continue loading the URL as usual.
Mikhail Naganovb8519292015-10-26 13:27:33 -070067 *
Paul Millerf2043b42018-07-25 18:11:26 -070068 * <p class="note"><b>Note:</b> Do not call {@link WebView#loadUrl(String)} with the request's
69 * URL and then return {@code true}. This unnecessarily cancels the current load and starts a
70 * new load with the same URL. The correct way to continue loading a given URL is to simply
71 * return {@code false}, without calling {@link WebView#loadUrl(String)}.
72 *
73 * <p class="note"><b>Note:</b> This method is not called for POST requests.
74 *
75 * <p class="note"><b>Note:</b> This method may be called for subframes and with non-HTTP(S)
76 * schemes; calling {@link WebView#loadUrl(String)} with such a URL will fail.
Mikhail Naganovb8519292015-10-26 13:27:33 -070077 *
78 * @param view The WebView that is initiating the callback.
79 * @param request Object containing the details of the request.
Paul Millerf2043b42018-07-25 18:11:26 -070080 * @return {@code true} to cancel the current load, otherwise return {@code false}.
Mikhail Naganovb8519292015-10-26 13:27:33 -070081 */
82 public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
83 return shouldOverrideUrlLoading(view, request.getUrl().toString());
84 }
85
86 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 * Notify the host application that a page has started loading. This method
88 * is called once for each main frame load so a page with iframes or
89 * framesets will call onPageStarted one time for the main frame. This also
90 * means that onPageStarted will not be called when the contents of an
Marcin Kosiba16687cc2015-01-27 17:35:49 +000091 * embedded frame changes, i.e. clicking a link whose target is an iframe,
92 * it will also not be called for fragment navigations (navigations to
93 * #fragment_id).
Brian Carlstroma1477592011-02-11 13:39:56 -080094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * @param view The WebView that is initiating the callback.
96 * @param url The url to be loaded.
97 * @param favicon The favicon for this page if it already exists in the
98 * database.
99 */
100 public void onPageStarted(WebView view, String url, Bitmap favicon) {
101 }
102
103 /**
104 * Notify the host application that a page has finished loading. This method
105 * is called only for main frame. When onPageFinished() is called, the
106 * rendering picture may not be updated yet. To get the notification for the
107 * new Picture, use {@link WebView.PictureListener#onNewPicture}.
Brian Carlstroma1477592011-02-11 13:39:56 -0800108 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * @param view The WebView that is initiating the callback.
110 * @param url The url of the page.
111 */
112 public void onPageFinished(WebView view, String url) {
113 }
114
115 /**
116 * Notify the host application that the WebView will load the resource
117 * specified by the given url.
Brian Carlstroma1477592011-02-11 13:39:56 -0800118 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 * @param view The WebView that is initiating the callback.
120 * @param url The url of the resource the WebView will load.
121 */
122 public void onLoadResource(WebView view, String url) {
123 }
124
125 /**
Tobias Sargeantb3656042015-05-13 11:23:57 +0100126 * Notify the host application that {@link android.webkit.WebView} content left over from
127 * previous page navigations will no longer be drawn.
Tobias Sargeanta8352f42015-03-03 18:07:40 +0000128 *
Tobias Sargeantb3656042015-05-13 11:23:57 +0100129 * <p>This callback can be used to determine the point at which it is safe to make a recycled
130 * {@link android.webkit.WebView} visible, ensuring that no stale content is shown. It is called
131 * at the earliest point at which it can be guaranteed that {@link WebView#onDraw} will no
132 * longer draw any content from previous navigations. The next draw will display either the
133 * {@link WebView#setBackgroundColor background color} of the {@link WebView}, or some of the
134 * contents of the newly loaded page.
Tobias Sargeanta8352f42015-03-03 18:07:40 +0000135 *
Tobias Sargeantb3656042015-05-13 11:23:57 +0100136 * <p>This method is called when the body of the HTTP response has started loading, is reflected
137 * in the DOM, and will be visible in subsequent draws. This callback occurs early in the
138 * document loading process, and as such you should expect that linked resources (for example,
Nate Fischer73fd92a2017-10-31 13:49:09 -0700139 * CSS and images) may not be available.
Tobias Sargeanta8352f42015-03-03 18:07:40 +0000140 *
Tobias Sargeantb3656042015-05-13 11:23:57 +0100141 * <p>For more fine-grained notification of visual state updates, see {@link
Nate Fischerf02f8462017-09-11 15:16:33 -0700142 * WebView#postVisualStateCallback}.
Tobias Sargeanta8352f42015-03-03 18:07:40 +0000143 *
Tobias Sargeantb3656042015-05-13 11:23:57 +0100144 * <p>Please note that all the conditions and recommendations applicable to
Nate Fischerf02f8462017-09-11 15:16:33 -0700145 * {@link WebView#postVisualStateCallback} also apply to this API.
Tobias Sargeantb3656042015-05-13 11:23:57 +0100146 *
Nate Fischerf02f8462017-09-11 15:16:33 -0700147 * <p>This callback is only called for main frame navigations.
Tobias Sargeantb3656042015-05-13 11:23:57 +0100148 *
149 * @param view The {@link android.webkit.WebView} for which the navigation occurred.
150 * @param url The URL corresponding to the page navigation that triggered this callback.
Tobias Sargeanta8352f42015-03-03 18:07:40 +0000151 */
152 public void onPageCommitVisible(WebView view, String url) {
153 }
154
155 /**
Patrick Scottc12544a2010-11-11 13:16:44 -0500156 * Notify the host application of a resource request and allow the
Nate Fischer0a6140d2017-09-05 12:37:49 -0700157 * application to return the data. If the return value is {@code null}, the WebView
Patrick Scottc12544a2010-11-11 13:16:44 -0500158 * will continue to load the resource as usual. Otherwise, the return
Nate Fischer7051dd12017-10-26 14:51:25 -0700159 * response and data will be used.
160 *
Nate Fischerdb010232018-03-26 18:46:31 -0700161 * <p>This callback is invoked for a variety of URL schemes (e.g., {@code http(s):}, {@code
162 * data:}, {@code file:}, etc.), not only those schemes which send requests over the network.
163 * This is not called for {@code javascript:} URLs, {@code blob:} URLs, or for assets accessed
164 * via {@code file:///android_asset/} or {@code file:///android_res/} URLs.
165 *
166 * <p>In the case of redirects, this is only called for the initial resource URL, not any
167 * subsequent redirect URLs.
168 *
Nate Fischer7051dd12017-10-26 14:51:25 -0700169 * <p class="note"><b>Note:</b> This method is called on a thread
Martin Kosiba15cb8252012-11-13 14:40:41 +0000170 * other than the UI thread so clients should exercise caution
171 * when accessing private data or the view system.
Patrick Scottc12544a2010-11-11 13:16:44 -0500172 *
Nate Fischer7051dd12017-10-26 14:51:25 -0700173 * <p class="note"><b>Note:</b> When Safe Browsing is enabled, these URLs still undergo Safe
174 * Browsing checks. If this is undesired, whitelist the URL with {@link
175 * WebView#setSafeBrowsingWhitelist} or ignore the warning with {@link #onSafeBrowsingHit}.
Nate Fischerb5a9bf42017-09-11 15:42:28 -0700176 *
Patrick Scottc12544a2010-11-11 13:16:44 -0500177 * @param view The {@link android.webkit.WebView} that is requesting the
178 * resource.
179 * @param url The raw url of the resource.
180 * @return A {@link android.webkit.WebResourceResponse} containing the
Nate Fischer0a6140d2017-09-05 12:37:49 -0700181 * response information or {@code null} if the WebView should load the
Patrick Scottc12544a2010-11-11 13:16:44 -0500182 * resource itself.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100183 * @deprecated Use {@link #shouldInterceptRequest(WebView, WebResourceRequest)
184 * shouldInterceptRequest(WebView, WebResourceRequest)} instead.
Patrick Scottc12544a2010-11-11 13:16:44 -0500185 */
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100186 @Deprecated
Nate Fischer3442c742017-09-08 17:02:00 -0700187 @Nullable
Patrick Scottc12544a2010-11-11 13:16:44 -0500188 public WebResourceResponse shouldInterceptRequest(WebView view,
189 String url) {
190 return null;
191 }
192
193 /**
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100194 * Notify the host application of a resource request and allow the
Nate Fischer0a6140d2017-09-05 12:37:49 -0700195 * application to return the data. If the return value is {@code null}, the WebView
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100196 * will continue to load the resource as usual. Otherwise, the return
Nate Fischer7051dd12017-10-26 14:51:25 -0700197 * response and data will be used.
198 *
Nate Fischerdb010232018-03-26 18:46:31 -0700199 * <p>This callback is invoked for a variety of URL schemes (e.g., {@code http(s):}, {@code
200 * data:}, {@code file:}, etc.), not only those schemes which send requests over the network.
201 * This is not called for {@code javascript:} URLs, {@code blob:} URLs, or for assets accessed
202 * via {@code file:///android_asset/} or {@code file:///android_res/} URLs.
203 *
204 * <p>In the case of redirects, this is only called for the initial resource URL, not any
205 * subsequent redirect URLs.
206 *
Nate Fischer7051dd12017-10-26 14:51:25 -0700207 * <p class="note"><b>Note:</b> This method is called on a thread
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100208 * other than the UI thread so clients should exercise caution
209 * when accessing private data or the view system.
210 *
Nate Fischer7051dd12017-10-26 14:51:25 -0700211 * <p class="note"><b>Note:</b> When Safe Browsing is enabled, these URLs still undergo Safe
212 * Browsing checks. If this is undesired, whitelist the URL with {@link
213 * WebView#setSafeBrowsingWhitelist} or ignore the warning with {@link #onSafeBrowsingHit}.
Nate Fischerb5a9bf42017-09-11 15:42:28 -0700214 *
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100215 * @param view The {@link android.webkit.WebView} that is requesting the
216 * resource.
217 * @param request Object containing the details of the request.
218 * @return A {@link android.webkit.WebResourceResponse} containing the
Nate Fischer0a6140d2017-09-05 12:37:49 -0700219 * response information or {@code null} if the WebView should load the
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100220 * resource itself.
221 */
Nate Fischer3442c742017-09-08 17:02:00 -0700222 @Nullable
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100223 public WebResourceResponse shouldInterceptRequest(WebView view,
224 WebResourceRequest request) {
225 return shouldInterceptRequest(view, request.getUrl().toString());
226 }
227
228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * Notify the host application that there have been an excessive number of
230 * HTTP redirects. As the host application if it would like to continue
231 * trying to load the resource. The default behavior is to send the cancel
232 * message.
Brian Carlstroma1477592011-02-11 13:39:56 -0800233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * @param view The WebView that is initiating the callback.
235 * @param cancelMsg The message to send if the host wants to cancel
236 * @param continueMsg The message to send if the host wants to continue
Patrick Scott6a5b0ec2010-01-08 09:55:33 -0500237 * @deprecated This method is no longer called. When the WebView encounters
238 * a redirect loop, it will cancel the load.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
Kristian Monsenb5503c12010-08-31 14:02:38 +0100240 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 public void onTooManyRedirects(WebView view, Message cancelMsg,
242 Message continueMsg) {
243 cancelMsg.sendToTarget();
244 }
245
Patrick Scott05c9ed92009-08-25 13:51:08 -0400246 // These ints must match up to the hidden values in EventHandler.
247 /** Generic error */
248 public static final int ERROR_UNKNOWN = -1;
249 /** Server or proxy hostname lookup failed */
250 public static final int ERROR_HOST_LOOKUP = -2;
251 /** Unsupported authentication scheme (not basic or digest) */
252 public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3;
253 /** User authentication failed on server */
254 public static final int ERROR_AUTHENTICATION = -4;
255 /** User authentication failed on proxy */
256 public static final int ERROR_PROXY_AUTHENTICATION = -5;
257 /** Failed to connect to the server */
258 public static final int ERROR_CONNECT = -6;
259 /** Failed to read or write to the server */
260 public static final int ERROR_IO = -7;
261 /** Connection timed out */
262 public static final int ERROR_TIMEOUT = -8;
263 /** Too many redirects */
264 public static final int ERROR_REDIRECT_LOOP = -9;
265 /** Unsupported URI scheme */
266 public static final int ERROR_UNSUPPORTED_SCHEME = -10;
267 /** Failed to perform SSL handshake */
268 public static final int ERROR_FAILED_SSL_HANDSHAKE = -11;
269 /** Malformed URL */
270 public static final int ERROR_BAD_URL = -12;
271 /** Generic file error */
272 public static final int ERROR_FILE = -13;
273 /** File not found */
274 public static final int ERROR_FILE_NOT_FOUND = -14;
275 /** Too many requests during this load */
276 public static final int ERROR_TOO_MANY_REQUESTS = -15;
Nate Fischer73fd92a2017-10-31 13:49:09 -0700277 /** Resource load was canceled by Safe Browsing */
Nate Fischer3c7c9372017-03-06 17:11:40 -0800278 public static final int ERROR_UNSAFE_RESOURCE = -16;
Patrick Scott05c9ed92009-08-25 13:51:08 -0400279
Nate Fischer5ab00b32017-07-13 15:28:21 -0700280 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700281 @IntDef(prefix = { "SAFE_BROWSING_THREAT_" }, value = {
282 SAFE_BROWSING_THREAT_UNKNOWN,
283 SAFE_BROWSING_THREAT_MALWARE,
284 SAFE_BROWSING_THREAT_PHISHING,
285 SAFE_BROWSING_THREAT_UNWANTED_SOFTWARE
Nate Fischer5ab00b32017-07-13 15:28:21 -0700286 })
287 @Retention(RetentionPolicy.SOURCE)
288 public @interface SafeBrowsingThreat {}
289
Nate Fischer0c304362017-06-16 15:32:50 -0700290 /** The resource was blocked for an unknown reason */
291 public static final int SAFE_BROWSING_THREAT_UNKNOWN = 0;
292 /** The resource was blocked because it contains malware */
293 public static final int SAFE_BROWSING_THREAT_MALWARE = 1;
294 /** The resource was blocked because it contains deceptive content */
295 public static final int SAFE_BROWSING_THREAT_PHISHING = 2;
296 /** The resource was blocked because it contains unwanted software */
297 public static final int SAFE_BROWSING_THREAT_UNWANTED_SOFTWARE = 3;
298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
Patrick Scott05c9ed92009-08-25 13:51:08 -0400300 * Report an error to the host application. These errors are unrecoverable
Nate Fischer73fd92a2017-10-31 13:49:09 -0700301 * (i.e. the main resource is unavailable). The {@code errorCode} parameter
302 * corresponds to one of the {@code ERROR_*} constants.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 * @param view The WebView that is initiating the callback.
Patrick Scott447811c2009-09-01 09:57:20 -0400304 * @param errorCode The error code corresponding to an ERROR_* value.
Patrick Scott05c9ed92009-08-25 13:51:08 -0400305 * @param description A String describing the error.
306 * @param failingUrl The url that failed to load.
Mikhail Naganov25e89542015-02-09 17:01:40 +0000307 * @deprecated Use {@link #onReceivedError(WebView, WebResourceRequest, WebResourceError)
308 * onReceivedError(WebView, WebResourceRequest, WebResourceError)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 */
Mikhail Naganov25e89542015-02-09 17:01:40 +0000310 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 public void onReceivedError(WebView view, int errorCode,
312 String description, String failingUrl) {
313 }
314
315 /**
Mikhail Naganov25e89542015-02-09 17:01:40 +0000316 * Report web resource loading error to the host application. These errors usually indicate
317 * inability to connect to the server. Note that unlike the deprecated version of the callback,
Nate Fischer73fd92a2017-10-31 13:49:09 -0700318 * the new version will be called for any resource (iframe, image, etc.), not just for the main
Mikhail Naganov25e89542015-02-09 17:01:40 +0000319 * page. Thus, it is recommended to perform minimum required work in this callback.
320 * @param view The WebView that is initiating the callback.
321 * @param request The originating request.
Nate Fischer73fd92a2017-10-31 13:49:09 -0700322 * @param error Information about the error occurred.
Mikhail Naganov25e89542015-02-09 17:01:40 +0000323 */
324 public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
325 if (request.isForMainFrame()) {
326 onReceivedError(view,
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700327 error.getErrorCode(), error.getDescription().toString(),
328 request.getUrl().toString());
Mikhail Naganov25e89542015-02-09 17:01:40 +0000329 }
330 }
331
332 /**
333 * Notify the host application that an HTTP error has been received from the server while
334 * loading a resource. HTTP errors have status codes &gt;= 400. This callback will be called
Nate Fischer73fd92a2017-10-31 13:49:09 -0700335 * for any resource (iframe, image, etc.), not just for the main page. Thus, it is recommended
336 * to perform minimum required work in this callback. Note that the content of the server
337 * response may not be provided within the {@code errorResponse} parameter.
Mikhail Naganov25e89542015-02-09 17:01:40 +0000338 * @param view The WebView that is initiating the callback.
339 * @param request The originating request.
Nate Fischer73fd92a2017-10-31 13:49:09 -0700340 * @param errorResponse Information about the error occurred.
Mikhail Naganov25e89542015-02-09 17:01:40 +0000341 */
342 public void onReceivedHttpError(
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700343 WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
Mikhail Naganov25e89542015-02-09 17:01:40 +0000344 }
345
346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 * As the host application if the browser should resend data as the
348 * requested page was a result of a POST. The default is to not resend the
349 * data.
Brian Carlstroma1477592011-02-11 13:39:56 -0800350 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 * @param view The WebView that is initiating the callback.
352 * @param dontResend The message to send if the browser should not resend
353 * @param resend The message to send if the browser should resend data
354 */
355 public void onFormResubmission(WebView view, Message dontResend,
356 Message resend) {
357 dontResend.sendToTarget();
358 }
359
360 /**
361 * Notify the host application to update its visited links database.
Brian Carlstroma1477592011-02-11 13:39:56 -0800362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 * @param view The WebView that is initiating the callback.
364 * @param url The url being visited.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700365 * @param isReload {@code true} if this url is being reloaded.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 */
367 public void doUpdateVisitedHistory(WebView view, String url,
368 boolean isReload) {
369 }
370
371 /**
Steve Blockfe33a752011-10-04 19:09:13 +0100372 * Notify the host application that an SSL error occurred while loading a
373 * resource. The host application must call either handler.cancel() or
374 * handler.proceed(). Note that the decision may be retained for use in
375 * response to future SSL errors. The default behavior is to cancel the
376 * load.
Torne (Richard Coles)97bf0be2018-05-16 10:51:56 -0400377 * <p>
378 * Applications are advised not to prompt the user about SSL errors, as
379 * the user is unlikely to be able to make an informed security decision
380 * and WebView does not provide any UI for showing the details of the
381 * error in a meaningful way.
382 * <p>
383 * Application overrides of this method may display custom error pages or
384 * silently log issues, but it is strongly recommended to always call
385 * handler.cancel() and never allow proceeding past errors.
Brian Carlstroma1477592011-02-11 13:39:56 -0800386 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * @param view The WebView that is initiating the callback.
388 * @param handler An SslErrorHandler object that will handle the user's
389 * response.
390 * @param error The SSL error object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 */
392 public void onReceivedSslError(WebView view, SslErrorHandler handler,
393 SslError error) {
394 handler.cancel();
395 }
396
397 /**
Nate Fischerda0f1b42017-11-29 12:18:54 -0800398 * Notify the host application to handle a SSL client certificate request. The host application
399 * is responsible for showing the UI if desired and providing the keys. There are three ways to
400 * respond: {@link ClientCertRequest#proceed}, {@link ClientCertRequest#cancel}, or {@link
401 * ClientCertRequest#ignore}. Webview stores the response in memory (for the life of the
402 * application) if {@link ClientCertRequest#proceed} or {@link ClientCertRequest#cancel} is
403 * called and does not call {@code onReceivedClientCertRequest()} again for the same host and
404 * port pair. Webview does not store the response if {@link ClientCertRequest#ignore}
Selim Gurun977d9432016-02-16 12:30:08 -0800405 * is called. Note that, multiple layers in chromium network stack might be
406 * caching the responses, so the behavior for ignore is only a best case
407 * effort.
Selim Gurunb6aa97e2014-03-26 14:10:28 -0700408 *
409 * This method is called on the UI thread. During the callback, the
410 * connection is suspended.
411 *
Selim Guruna679ac52015-06-19 11:27:38 -0700412 * For most use cases, the application program should implement the
413 * {@link android.security.KeyChainAliasCallback} interface and pass it to
414 * {@link android.security.KeyChain#choosePrivateKeyAlias} to start an
415 * activity for the user to choose the proper alias. The keychain activity will
416 * provide the alias through the callback method in the implemented interface. Next
417 * the application should create an async task to call
418 * {@link android.security.KeyChain#getPrivateKey} to receive the key.
419 *
420 * An example implementation of client certificates can be seen at
421 * <A href="https://android.googlesource.com/platform/packages/apps/Browser/+/android-5.1.1_r1/src/com/android/browser/Tab.java">
422 * AOSP Browser</a>
423 *
Selim Gurunb6aa97e2014-03-26 14:10:28 -0700424 * The default behavior is to cancel, returning no client certificate.
425 *
426 * @param view The WebView that is initiating the callback
427 * @param request An instance of a {@link ClientCertRequest}
428 *
Selim Gurunb6aa97e2014-03-26 14:10:28 -0700429 */
430 public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
431 request.cancel();
432 }
433
434 /**
Steve Block46ce1db2012-07-17 16:43:00 +0100435 * Notifies the host application that the WebView received an HTTP
436 * authentication request. The host application can use the supplied
437 * {@link HttpAuthHandler} to set the WebView's response to the request.
438 * The default behavior is to cancel the request.
Brian Carlstroma1477592011-02-11 13:39:56 -0800439 *
Steve Block46ce1db2012-07-17 16:43:00 +0100440 * @param view the WebView that is initiating the callback
441 * @param handler the HttpAuthHandler used to set the WebView's response
442 * @param host the host requiring authentication
443 * @param realm the realm for which authentication is required
Jonathan Dixon47aaba32012-11-30 16:32:17 -0800444 * @see WebView#getHttpAuthUsernamePassword
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 */
446 public void onReceivedHttpAuthRequest(WebView view,
447 HttpAuthHandler handler, String host, String realm) {
448 handler.cancel();
449 }
450
451 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 * Give the host application a chance to handle the key event synchronously.
453 * e.g. menu shortcut key events need to be filtered this way. If return
Nate Fischer0a6140d2017-09-05 12:37:49 -0700454 * true, WebView will not handle the key event. If return {@code false}, WebView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * will always handle the key event, so none of the super in the view chain
Nate Fischer0a6140d2017-09-05 12:37:49 -0700456 * will see the key event. The default behavior returns {@code false}.
Brian Carlstroma1477592011-02-11 13:39:56 -0800457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * @param view The WebView that is initiating the callback.
459 * @param event The key event.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700460 * @return {@code true} if the host application wants to handle the key event
461 * itself, otherwise return {@code false}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 */
463 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
464 return false;
465 }
466
467 /**
468 * Notify the host application that a key was not handled by the WebView.
469 * Except system keys, WebView always consumes the keys in the normal flow
Nate Fischer73fd92a2017-10-31 13:49:09 -0700470 * or if {@link #shouldOverrideKeyEvent} returns {@code true}. This is called asynchronously
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900471 * from where the key is dispatched. It gives the host application a chance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 * to handle the unhandled key events.
Brian Carlstroma1477592011-02-11 13:39:56 -0800473 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 * @param view The WebView that is initiating the callback.
475 * @param event The key event.
476 */
477 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Wright899d7052014-04-23 17:23:39 -0700478 onUnhandledInputEventInternal(view, event);
479 }
480
481 /**
482 * Notify the host application that a input event was not handled by the WebView.
483 * Except system keys, WebView always consumes input events in the normal flow
Nate Fischer73fd92a2017-10-31 13:49:09 -0700484 * or if {@link #shouldOverrideKeyEvent} returns {@code true}. This is called asynchronously
Michael Wright899d7052014-04-23 17:23:39 -0700485 * from where the event is dispatched. It gives the host application a chance
486 * to handle the unhandled input events.
487 *
Michael Wright1feb11f2014-05-01 15:41:33 -0700488 * Note that if the event is a {@link android.view.MotionEvent}, then it's lifetime is only
489 * that of the function call. If the WebViewClient wishes to use the event beyond that, then it
490 * <i>must</i> create a copy of the event.
Michael Wright899d7052014-04-23 17:23:39 -0700491 *
Michael Wright1feb11f2014-05-01 15:41:33 -0700492 * It is the responsibility of overriders of this method to call
493 * {@link #onUnhandledKeyEvent(WebView, KeyEvent)}
Michael Wright899d7052014-04-23 17:23:39 -0700494 * when appropriate if they wish to continue receiving events through it.
495 *
496 * @param view The WebView that is initiating the callback.
497 * @param event The input event.
Michael Wright1ad39552016-04-27 13:07:18 -0400498 * @removed
Michael Wright899d7052014-04-23 17:23:39 -0700499 */
500 public void onUnhandledInputEvent(WebView view, InputEvent event) {
501 if (event instanceof KeyEvent) {
502 onUnhandledKeyEvent(view, (KeyEvent) event);
503 return;
504 }
505 onUnhandledInputEventInternal(view, event);
506 }
507
508 private void onUnhandledInputEventInternal(WebView view, InputEvent event) {
John Reckd6b10982012-04-19 18:01:35 -0700509 ViewRootImpl root = view.getViewRootImpl();
510 if (root != null) {
Michael Wright899d7052014-04-23 17:23:39 -0700511 root.dispatchUnhandledInputEvent(event);
John Reckd6b10982012-04-19 18:01:35 -0700512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
515 /**
516 * Notify the host application that the scale applied to the WebView has
517 * changed.
Brian Carlstroma1477592011-02-11 13:39:56 -0800518 *
Philipp Hasper802aa0f2015-05-12 09:09:25 +0200519 * @param view The WebView that is initiating the callback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 * @param oldScale The old scale factor
521 * @param newScale The new scale factor
522 */
523 public void onScaleChanged(WebView view, float oldScale, float newScale) {
524 }
Patrick Scott85a50ff2011-01-25 14:42:12 -0500525
526 /**
527 * Notify the host application that a request to automatically log in the
528 * user has been processed.
529 * @param view The WebView requesting the login.
530 * @param realm The account realm used to look up accounts.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700531 * @param account An optional account. If not {@code null}, the account should be
Patrick Scott85a50ff2011-01-25 14:42:12 -0500532 * checked against accounts on the device. If it is a valid
533 * account, it should be used to log in the user.
534 * @param args Authenticator specific arguments used to log in the user.
535 */
536 public void onReceivedLoginRequest(WebView view, String realm,
Nate Fischer3442c742017-09-08 17:02:00 -0700537 @Nullable String account, String args) {
Patrick Scott85a50ff2011-01-25 14:42:12 -0500538 }
Tao Baic53fae12016-11-16 15:21:40 -0800539
540 /**
Nate Fischer73fd92a2017-10-31 13:49:09 -0700541 * Notify host application that the given WebView's render process has exited.
Tao Baic53fae12016-11-16 15:21:40 -0800542 *
543 * Multiple WebView instances may be associated with a single render process;
544 * onRenderProcessGone will be called for each WebView that was affected.
545 * The application's implementation of this callback should only attempt to
546 * clean up the specific WebView given as a parameter, and should not assume
547 * that other WebView instances are affected.
548 *
549 * The given WebView can't be used, and should be removed from the view hierarchy,
550 * all references to it should be cleaned up, e.g any references in the Activity
Nate Fischer73fd92a2017-10-31 13:49:09 -0700551 * or other classes saved using {@link android.view.View#findViewById} and similar calls, etc.
Tao Baic53fae12016-11-16 15:21:40 -0800552 *
553 * To cause an render process crash for test purpose, the application can
Nate Fischer73fd92a2017-10-31 13:49:09 -0700554 * call {@code loadUrl("chrome://crash")} on the WebView. Note that multiple WebView
Tao Baic53fae12016-11-16 15:21:40 -0800555 * instances may be affected if they share a render process, not just the
556 * specific WebView which loaded chrome://crash.
557 *
558 * @param view The WebView which needs to be cleaned up.
559 * @param detail the reason why it exited.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700560 * @return {@code true} if the host application handled the situation that process has
Tao Baidd264f62017-01-30 16:54:28 -0800561 * exited, otherwise, application will crash if render process crashed,
562 * or be killed if render process was killed by the system.
Tao Baic53fae12016-11-16 15:21:40 -0800563 */
564 public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
565 return false;
566 }
Nate Fischer0c304362017-06-16 15:32:50 -0700567
568 /**
569 * Notify the host application that a loading URL has been flagged by Safe Browsing.
570 *
571 * The application must invoke the callback to indicate the preferred response. The default
Nate Fischer2f7af062017-07-06 11:32:31 -0700572 * behavior is to show an interstitial to the user, with the reporting checkbox visible.
Nate Fischer0c304362017-06-16 15:32:50 -0700573 *
574 * If the application needs to show its own custom interstitial UI, the callback can be invoked
Nate Fischer73fd92a2017-10-31 13:49:09 -0700575 * asynchronously with {@link SafeBrowsingResponse#backToSafety} or {@link
576 * SafeBrowsingResponse#proceed}, depending on user response.
Nate Fischer0c304362017-06-16 15:32:50 -0700577 *
578 * @param view The WebView that hit the malicious resource.
579 * @param request Object containing the details of the request.
580 * @param threatType The reason the resource was caught by Safe Browsing, corresponding to a
Nate Fischer73fd92a2017-10-31 13:49:09 -0700581 * {@code SAFE_BROWSING_THREAT_*} value.
Nate Fischer2f7af062017-07-06 11:32:31 -0700582 * @param callback Applications must invoke one of the callback methods.
Nate Fischer0c304362017-06-16 15:32:50 -0700583 */
Nate Fischer5ab00b32017-07-13 15:28:21 -0700584 public void onSafeBrowsingHit(WebView view, WebResourceRequest request,
585 @SafeBrowsingThreat int threatType, SafeBrowsingResponse callback) {
Nate Fischer2f7af062017-07-06 11:32:31 -0700586 callback.showInterstitial(/* allowReporting */ true);
Nate Fischer0c304362017-06-16 15:32:50 -0700587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588}