| 1 | /* |
| 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 | |
| 17 | package android.webkit; |
| 18 | |
| 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.graphics.Bitmap; |
| 22 | import android.net.http.SslError; |
| 23 | import android.os.Message; |
| 24 | import android.view.InputEvent; |
| 25 | import android.view.KeyEvent; |
| 26 | import android.view.ViewRootImpl; |
| 27 | |
| 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
| 30 | |
| 31 | public class WebViewClient { |
| 32 | |
| 33 | /** |
| 34 | * 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. |
| 49 | * |
| 50 | * @param view The WebView that is initiating the callback. |
| 51 | * @param url The URL to be loaded. |
| 52 | * @return {@code true} to cancel the current load, otherwise return {@code false}. |
| 53 | * @deprecated Use {@link #shouldOverrideUrlLoading(WebView, WebResourceRequest) |
| 54 | * shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead. |
| 55 | */ |
| 56 | @Deprecated |
| 57 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * 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. |
| 67 | * |
| 68 | * <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. |
| 77 | * |
| 78 | * @param view The WebView that is initiating the callback. |
| 79 | * @param request Object containing the details of the request. |
| 80 | * @return {@code true} to cancel the current load, otherwise return {@code false}. |
| 81 | */ |
| 82 | public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { |
| 83 | return shouldOverrideUrlLoading(view, request.getUrl().toString()); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * 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 |
| 91 | * 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). |
| 94 | * |
| 95 | * @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. Receiving an {@code onPageFinished()} callback does not |
| 106 | * guarantee that the next frame drawn by WebView will reflect the state of the DOM at this |
| 107 | * point. In order to be notified that the current DOM state is ready to be rendered, request a |
| 108 | * visual state callback with {@link WebView#postVisualStateCallback} and wait for the supplied |
| 109 | * callback to be triggered. |
| 110 | * |
| 111 | * @param view The WebView that is initiating the callback. |
| 112 | * @param url The url of the page. |
| 113 | */ |
| 114 | public void onPageFinished(WebView view, String url) { |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Notify the host application that the WebView will load the resource |
| 119 | * specified by the given url. |
| 120 | * |
| 121 | * @param view The WebView that is initiating the callback. |
| 122 | * @param url The url of the resource the WebView will load. |
| 123 | */ |
| 124 | public void onLoadResource(WebView view, String url) { |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Notify the host application that {@link android.webkit.WebView} content left over from |
| 129 | * previous page navigations will no longer be drawn. |
| 130 | * |
| 131 | * <p>This callback can be used to determine the point at which it is safe to make a recycled |
| 132 | * {@link android.webkit.WebView} visible, ensuring that no stale content is shown. It is called |
| 133 | * at the earliest point at which it can be guaranteed that {@link WebView#onDraw} will no |
| 134 | * longer draw any content from previous navigations. The next draw will display either the |
| 135 | * {@link WebView#setBackgroundColor background color} of the {@link WebView}, or some of the |
| 136 | * contents of the newly loaded page. |
| 137 | * |
| 138 | * <p>This method is called when the body of the HTTP response has started loading, is reflected |
| 139 | * in the DOM, and will be visible in subsequent draws. This callback occurs early in the |
| 140 | * document loading process, and as such you should expect that linked resources (for example, |
| 141 | * CSS and images) may not be available. |
| 142 | * |
| 143 | * <p>For more fine-grained notification of visual state updates, see {@link |
| 144 | * WebView#postVisualStateCallback}. |
| 145 | * |
| 146 | * <p>Please note that all the conditions and recommendations applicable to |
| 147 | * {@link WebView#postVisualStateCallback} also apply to this API. |
| 148 | * |
| 149 | * <p>This callback is only called for main frame navigations. |
| 150 | * |
| 151 | * @param view The {@link android.webkit.WebView} for which the navigation occurred. |
| 152 | * @param url The URL corresponding to the page navigation that triggered this callback. |
| 153 | */ |
| 154 | public void onPageCommitVisible(WebView view, String url) { |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Notify the host application of a resource request and allow the |
| 159 | * application to return the data. If the return value is {@code null}, the WebView |
| 160 | * will continue to load the resource as usual. Otherwise, the return |
| 161 | * response and data will be used. |
| 162 | * |
| 163 | * <p>This callback is invoked for a variety of URL schemes (e.g., {@code http(s):}, {@code |
| 164 | * data:}, {@code file:}, etc.), not only those schemes which send requests over the network. |
| 165 | * This is not called for {@code javascript:} URLs, {@code blob:} URLs, or for assets accessed |
| 166 | * via {@code file:///android_asset/} or {@code file:///android_res/} URLs. |
| 167 | * |
| 168 | * <p>In the case of redirects, this is only called for the initial resource URL, not any |
| 169 | * subsequent redirect URLs. |
| 170 | * |
| 171 | * <p class="note"><b>Note:</b> This method is called on a thread |
| 172 | * other than the UI thread so clients should exercise caution |
| 173 | * when accessing private data or the view system. |
| 174 | * |
| 175 | * <p class="note"><b>Note:</b> When Safe Browsing is enabled, these URLs still undergo Safe |
| 176 | * Browsing checks. If this is undesired, you can use {@link WebView#setSafeBrowsingWhitelist} |
| 177 | * to skip Safe Browsing checks for that host or dismiss the warning in {@link |
| 178 | * #onSafeBrowsingHit} by calling {@link SafeBrowsingResponse#proceed}. |
| 179 | * |
| 180 | * @param view The {@link android.webkit.WebView} that is requesting the |
| 181 | * resource. |
| 182 | * @param url The raw url of the resource. |
| 183 | * @return A {@link android.webkit.WebResourceResponse} containing the |
| 184 | * response information or {@code null} if the WebView should load the |
| 185 | * resource itself. |
| 186 | * @deprecated Use {@link #shouldInterceptRequest(WebView, WebResourceRequest) |
| 187 | * shouldInterceptRequest(WebView, WebResourceRequest)} instead. |
| 188 | */ |
| 189 | @Deprecated |
| 190 | @Nullable |
| 191 | public WebResourceResponse shouldInterceptRequest(WebView view, |
| 192 | String url) { |
| 193 | return null; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Notify the host application of a resource request and allow the |
| 198 | * application to return the data. If the return value is {@code null}, the WebView |
| 199 | * will continue to load the resource as usual. Otherwise, the return |
| 200 | * response and data will be used. |
| 201 | * |
| 202 | * <p>This callback is invoked for a variety of URL schemes (e.g., {@code http(s):}, {@code |
| 203 | * data:}, {@code file:}, etc.), not only those schemes which send requests over the network. |
| 204 | * This is not called for {@code javascript:} URLs, {@code blob:} URLs, or for assets accessed |
| 205 | * via {@code file:///android_asset/} or {@code file:///android_res/} URLs. |
| 206 | * |
| 207 | * <p>In the case of redirects, this is only called for the initial resource URL, not any |
| 208 | * subsequent redirect URLs. |
| 209 | * |
| 210 | * <p class="note"><b>Note:</b> This method is called on a thread |
| 211 | * other than the UI thread so clients should exercise caution |
| 212 | * when accessing private data or the view system. |
| 213 | * |
| 214 | * <p class="note"><b>Note:</b> When Safe Browsing is enabled, these URLs still undergo Safe |
| 215 | * Browsing checks. If this is undesired, you can use {@link WebView#setSafeBrowsingWhitelist} |
| 216 | * to skip Safe Browsing checks for that host or dismiss the warning in {@link |
| 217 | * #onSafeBrowsingHit} by calling {@link SafeBrowsingResponse#proceed}. |
| 218 | * |
| 219 | * @param view The {@link android.webkit.WebView} that is requesting the |
| 220 | * resource. |
| 221 | * @param request Object containing the details of the request. |
| 222 | * @return A {@link android.webkit.WebResourceResponse} containing the |
| 223 | * response information or {@code null} if the WebView should load the |
| 224 | * resource itself. |
| 225 | */ |
| 226 | @Nullable |
| 227 | public WebResourceResponse shouldInterceptRequest(WebView view, |
| 228 | WebResourceRequest request) { |
| 229 | return shouldInterceptRequest(view, request.getUrl().toString()); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Notify the host application that there have been an excessive number of |
| 234 | * HTTP redirects. As the host application if it would like to continue |
| 235 | * trying to load the resource. The default behavior is to send the cancel |
| 236 | * message. |
| 237 | * |
| 238 | * @param view The WebView that is initiating the callback. |
| 239 | * @param cancelMsg The message to send if the host wants to cancel |
| 240 | * @param continueMsg The message to send if the host wants to continue |
| 241 | * @deprecated This method is no longer called. When the WebView encounters |
| 242 | * a redirect loop, it will cancel the load. |
| 243 | */ |
| 244 | @Deprecated |
| 245 | public void onTooManyRedirects(WebView view, Message cancelMsg, |
| 246 | Message continueMsg) { |
| 247 | cancelMsg.sendToTarget(); |
| 248 | } |
| 249 | |
| 250 | // These ints must match up to the hidden values in EventHandler. |
| 251 | /** Generic error */ |
| 252 | public static final int ERROR_UNKNOWN = -1; |
| 253 | /** Server or proxy hostname lookup failed */ |
| 254 | public static final int ERROR_HOST_LOOKUP = -2; |
| 255 | /** Unsupported authentication scheme (not basic or digest) */ |
| 256 | public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3; |
| 257 | /** User authentication failed on server */ |
| 258 | public static final int ERROR_AUTHENTICATION = -4; |
| 259 | /** User authentication failed on proxy */ |
| 260 | public static final int ERROR_PROXY_AUTHENTICATION = -5; |
| 261 | /** Failed to connect to the server */ |
| 262 | public static final int ERROR_CONNECT = -6; |
| 263 | /** Failed to read or write to the server */ |
| 264 | public static final int ERROR_IO = -7; |
| 265 | /** Connection timed out */ |
| 266 | public static final int ERROR_TIMEOUT = -8; |
| 267 | /** Too many redirects */ |
| 268 | public static final int ERROR_REDIRECT_LOOP = -9; |
| 269 | /** Unsupported URI scheme */ |
| 270 | public static final int ERROR_UNSUPPORTED_SCHEME = -10; |
| 271 | /** Failed to perform SSL handshake */ |
| 272 | public static final int ERROR_FAILED_SSL_HANDSHAKE = -11; |
| 273 | /** Malformed URL */ |
| 274 | public static final int ERROR_BAD_URL = -12; |
| 275 | /** Generic file error */ |
| 276 | public static final int ERROR_FILE = -13; |
| 277 | /** File not found */ |
| 278 | public static final int ERROR_FILE_NOT_FOUND = -14; |
| 279 | /** Too many requests during this load */ |
| 280 | public static final int ERROR_TOO_MANY_REQUESTS = -15; |
| 281 | /** Resource load was canceled by Safe Browsing */ |
| 282 | public static final int ERROR_UNSAFE_RESOURCE = -16; |
| 283 | |
| 284 | /** @hide */ |
| 285 | @IntDef(prefix = { "SAFE_BROWSING_THREAT_" }, value = { |
| 286 | SAFE_BROWSING_THREAT_UNKNOWN, |
| 287 | SAFE_BROWSING_THREAT_MALWARE, |
| 288 | SAFE_BROWSING_THREAT_PHISHING, |
| 289 | SAFE_BROWSING_THREAT_UNWANTED_SOFTWARE, |
| 290 | SAFE_BROWSING_THREAT_BILLING, |
| 291 | }) |
| 292 | @Retention(RetentionPolicy.SOURCE) |
| 293 | public @interface SafeBrowsingThreat {} |
| 294 | |
| 295 | /** The resource was blocked for an unknown reason. */ |
| 296 | public static final int SAFE_BROWSING_THREAT_UNKNOWN = 0; |
| 297 | /** The resource was blocked because it contains malware. */ |
| 298 | public static final int SAFE_BROWSING_THREAT_MALWARE = 1; |
| 299 | /** The resource was blocked because it contains deceptive content. */ |
| 300 | public static final int SAFE_BROWSING_THREAT_PHISHING = 2; |
| 301 | /** The resource was blocked because it contains unwanted software. */ |
| 302 | public static final int SAFE_BROWSING_THREAT_UNWANTED_SOFTWARE = 3; |
| 303 | /** |
| 304 | * The resource was blocked because it may trick the user into a billing agreement. |
| 305 | * |
| 306 | * <p>This constant is only used when targetSdkVersion is at least {@link |
| 307 | * android.os.Build.VERSION_CODES#Q}. Otherwise, {@link #SAFE_BROWSING_THREAT_UNKNOWN} is used |
| 308 | * instead. |
| 309 | */ |
| 310 | public static final int SAFE_BROWSING_THREAT_BILLING = 4; |
| 311 | |
| 312 | /** |
| 313 | * Report an error to the host application. These errors are unrecoverable |
| 314 | * (i.e. the main resource is unavailable). The {@code errorCode} parameter |
| 315 | * corresponds to one of the {@code ERROR_*} constants. |
| 316 | * @param view The WebView that is initiating the callback. |
| 317 | * @param errorCode The error code corresponding to an ERROR_* value. |
| 318 | * @param description A String describing the error. |
| 319 | * @param failingUrl The url that failed to load. |
| 320 | * @deprecated Use {@link #onReceivedError(WebView, WebResourceRequest, WebResourceError) |
| 321 | * onReceivedError(WebView, WebResourceRequest, WebResourceError)} instead. |
| 322 | */ |
| 323 | @Deprecated |
| 324 | public void onReceivedError(WebView view, int errorCode, |
| 325 | String description, String failingUrl) { |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Report web resource loading error to the host application. These errors usually indicate |
| 330 | * inability to connect to the server. Note that unlike the deprecated version of the callback, |
| 331 | * the new version will be called for any resource (iframe, image, etc.), not just for the main |
| 332 | * page. Thus, it is recommended to perform minimum required work in this callback. |
| 333 | * @param view The WebView that is initiating the callback. |
| 334 | * @param request The originating request. |
| 335 | * @param error Information about the error occurred. |
| 336 | */ |
| 337 | public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { |
| 338 | if (request.isForMainFrame()) { |
| 339 | onReceivedError(view, |
| 340 | error.getErrorCode(), error.getDescription().toString(), |
| 341 | request.getUrl().toString()); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Notify the host application that an HTTP error has been received from the server while |
| 347 | * loading a resource. HTTP errors have status codes >= 400. This callback will be called |
| 348 | * for any resource (iframe, image, etc.), not just for the main page. Thus, it is recommended |
| 349 | * to perform minimum required work in this callback. Note that the content of the server |
| 350 | * response may not be provided within the {@code errorResponse} parameter. |
| 351 | * @param view The WebView that is initiating the callback. |
| 352 | * @param request The originating request. |
| 353 | * @param errorResponse Information about the error occurred. |
| 354 | */ |
| 355 | public void onReceivedHttpError( |
| 356 | WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * As the host application if the browser should resend data as the |
| 361 | * requested page was a result of a POST. The default is to not resend the |
| 362 | * data. |
| 363 | * |
| 364 | * @param view The WebView that is initiating the callback. |
| 365 | * @param dontResend The message to send if the browser should not resend |
| 366 | * @param resend The message to send if the browser should resend data |
| 367 | */ |
| 368 | public void onFormResubmission(WebView view, Message dontResend, |
| 369 | Message resend) { |
| 370 | dontResend.sendToTarget(); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Notify the host application to update its visited links database. |
| 375 | * |
| 376 | * @param view The WebView that is initiating the callback. |
| 377 | * @param url The url being visited. |
| 378 | * @param isReload {@code true} if this url is being reloaded. |
| 379 | */ |
| 380 | public void doUpdateVisitedHistory(WebView view, String url, |
| 381 | boolean isReload) { |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Notify the host application that an SSL error occurred while loading a |
| 386 | * resource. The host application must call either {@link SslErrorHandler#cancel} or |
| 387 | * {@link SslErrorHandler#proceed}. Note that the decision may be retained for use in |
| 388 | * response to future SSL errors. The default behavior is to cancel the |
| 389 | * load. |
| 390 | * <p> |
| 391 | * This API is only called for recoverable SSL certificate errors. In the case of |
| 392 | * non-recoverable errors (such as when the server fails the client), WebView will call {@link |
| 393 | * #onReceivedError(WebView, WebResourceRequest, WebResourceError)} with {@link |
| 394 | * #ERROR_FAILED_SSL_HANDSHAKE}. |
| 395 | * <p> |
| 396 | * Applications are advised not to prompt the user about SSL errors, as |
| 397 | * the user is unlikely to be able to make an informed security decision |
| 398 | * and WebView does not provide any UI for showing the details of the |
| 399 | * error in a meaningful way. |
| 400 | * <p> |
| 401 | * Application overrides of this method may display custom error pages or |
| 402 | * silently log issues, but it is strongly recommended to always call |
| 403 | * {@link SslErrorHandler#cancel} and never allow proceeding past errors. |
| 404 | * |
| 405 | * @param view The WebView that is initiating the callback. |
| 406 | * @param handler An {@link SslErrorHandler} that will handle the user's |
| 407 | * response. |
| 408 | * @param error The SSL error object. |
| 409 | */ |
| 410 | public void onReceivedSslError(WebView view, SslErrorHandler handler, |
| 411 | SslError error) { |
| 412 | handler.cancel(); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Notify the host application to handle a SSL client certificate request. The host application |
| 417 | * is responsible for showing the UI if desired and providing the keys. There are three ways to |
| 418 | * respond: {@link ClientCertRequest#proceed}, {@link ClientCertRequest#cancel}, or {@link |
| 419 | * ClientCertRequest#ignore}. Webview stores the response in memory (for the life of the |
| 420 | * application) if {@link ClientCertRequest#proceed} or {@link ClientCertRequest#cancel} is |
| 421 | * called and does not call {@code onReceivedClientCertRequest()} again for the same host and |
| 422 | * port pair. Webview does not store the response if {@link ClientCertRequest#ignore} |
| 423 | * is called. Note that, multiple layers in chromium network stack might be |
| 424 | * caching the responses, so the behavior for ignore is only a best case |
| 425 | * effort. |
| 426 | * |
| 427 | * This method is called on the UI thread. During the callback, the |
| 428 | * connection is suspended. |
| 429 | * |
| 430 | * For most use cases, the application program should implement the |
| 431 | * {@link android.security.KeyChainAliasCallback} interface and pass it to |
| 432 | * {@link android.security.KeyChain#choosePrivateKeyAlias} to start an |
| 433 | * activity for the user to choose the proper alias. The keychain activity will |
| 434 | * provide the alias through the callback method in the implemented interface. Next |
| 435 | * the application should create an async task to call |
| 436 | * {@link android.security.KeyChain#getPrivateKey} to receive the key. |
| 437 | * |
| 438 | * An example implementation of client certificates can be seen at |
| 439 | * <A href="https://android.googlesource.com/platform/packages/apps/Browser/+/android-5.1.1_r1/src/com/android/browser/Tab.java"> |
| 440 | * AOSP Browser</a> |
| 441 | * |
| 442 | * The default behavior is to cancel, returning no client certificate. |
| 443 | * |
| 444 | * @param view The WebView that is initiating the callback |
| 445 | * @param request An instance of a {@link ClientCertRequest} |
| 446 | * |
| 447 | */ |
| 448 | public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) { |
| 449 | request.cancel(); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Notifies the host application that the WebView received an HTTP |
| 454 | * authentication request. The host application can use the supplied |
| 455 | * {@link HttpAuthHandler} to set the WebView's response to the request. |
| 456 | * The default behavior is to cancel the request. |
| 457 | * |
| 458 | * @param view the WebView that is initiating the callback |
| 459 | * @param handler the HttpAuthHandler used to set the WebView's response |
| 460 | * @param host the host requiring authentication |
| 461 | * @param realm the realm for which authentication is required |
| 462 | * @see WebView#getHttpAuthUsernamePassword |
| 463 | */ |
| 464 | public void onReceivedHttpAuthRequest(WebView view, |
| 465 | HttpAuthHandler handler, String host, String realm) { |
| 466 | handler.cancel(); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Give the host application a chance to handle the key event synchronously. |
| 471 | * e.g. menu shortcut key events need to be filtered this way. If return |
| 472 | * true, WebView will not handle the key event. If return {@code false}, WebView |
| 473 | * will always handle the key event, so none of the super in the view chain |
| 474 | * will see the key event. The default behavior returns {@code false}. |
| 475 | * |
| 476 | * @param view The WebView that is initiating the callback. |
| 477 | * @param event The key event. |
| 478 | * @return {@code true} if the host application wants to handle the key event |
| 479 | * itself, otherwise return {@code false} |
| 480 | */ |
| 481 | public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Notify the host application that a key was not handled by the WebView. |
| 487 | * Except system keys, WebView always consumes the keys in the normal flow |
| 488 | * or if {@link #shouldOverrideKeyEvent} returns {@code true}. This is called asynchronously |
| 489 | * from where the key is dispatched. It gives the host application a chance |
| 490 | * to handle the unhandled key events. |
| 491 | * |
| 492 | * @param view The WebView that is initiating the callback. |
| 493 | * @param event The key event. |
| 494 | */ |
| 495 | public void onUnhandledKeyEvent(WebView view, KeyEvent event) { |
| 496 | onUnhandledInputEventInternal(view, event); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Notify the host application that a input event was not handled by the WebView. |
| 501 | * Except system keys, WebView always consumes input events in the normal flow |
| 502 | * or if {@link #shouldOverrideKeyEvent} returns {@code true}. This is called asynchronously |
| 503 | * from where the event is dispatched. It gives the host application a chance |
| 504 | * to handle the unhandled input events. |
| 505 | * |
| 506 | * Note that if the event is a {@link android.view.MotionEvent}, then it's lifetime is only |
| 507 | * that of the function call. If the WebViewClient wishes to use the event beyond that, then it |
| 508 | * <i>must</i> create a copy of the event. |
| 509 | * |
| 510 | * It is the responsibility of overriders of this method to call |
| 511 | * {@link #onUnhandledKeyEvent(WebView, KeyEvent)} |
| 512 | * when appropriate if they wish to continue receiving events through it. |
| 513 | * |
| 514 | * @param view The WebView that is initiating the callback. |
| 515 | * @param event The input event. |
| 516 | * @removed |
| 517 | */ |
| 518 | public void onUnhandledInputEvent(WebView view, InputEvent event) { |
| 519 | if (event instanceof KeyEvent) { |
| 520 | onUnhandledKeyEvent(view, (KeyEvent) event); |
| 521 | return; |
| 522 | } |
| 523 | onUnhandledInputEventInternal(view, event); |
| 524 | } |
| 525 | |
| 526 | private void onUnhandledInputEventInternal(WebView view, InputEvent event) { |
| 527 | ViewRootImpl root = view.getViewRootImpl(); |
| 528 | if (root != null) { |
| 529 | root.dispatchUnhandledInputEvent(event); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Notify the host application that the scale applied to the WebView has |
| 535 | * changed. |
| 536 | * |
| 537 | * @param view The WebView that is initiating the callback. |
| 538 | * @param oldScale The old scale factor |
| 539 | * @param newScale The new scale factor |
| 540 | */ |
| 541 | public void onScaleChanged(WebView view, float oldScale, float newScale) { |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Notify the host application that a request to automatically log in the |
| 546 | * user has been processed. |
| 547 | * @param view The WebView requesting the login. |
| 548 | * @param realm The account realm used to look up accounts. |
| 549 | * @param account An optional account. If not {@code null}, the account should be |
| 550 | * checked against accounts on the device. If it is a valid |
| 551 | * account, it should be used to log in the user. |
| 552 | * @param args Authenticator specific arguments used to log in the user. |
| 553 | */ |
| 554 | public void onReceivedLoginRequest(WebView view, String realm, |
| 555 | @Nullable String account, String args) { |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Notify host application that the given WebView's render process has exited. |
| 560 | * |
| 561 | * Multiple WebView instances may be associated with a single render process; |
| 562 | * onRenderProcessGone will be called for each WebView that was affected. |
| 563 | * The application's implementation of this callback should only attempt to |
| 564 | * clean up the specific WebView given as a parameter, and should not assume |
| 565 | * that other WebView instances are affected. |
| 566 | * |
| 567 | * The given WebView can't be used, and should be removed from the view hierarchy, |
| 568 | * all references to it should be cleaned up, e.g any references in the Activity |
| 569 | * or other classes saved using {@link android.view.View#findViewById} and similar calls, etc. |
| 570 | * |
| 571 | * To cause an render process crash for test purpose, the application can |
| 572 | * call {@code loadUrl("chrome://crash")} on the WebView. Note that multiple WebView |
| 573 | * instances may be affected if they share a render process, not just the |
| 574 | * specific WebView which loaded chrome://crash. |
| 575 | * |
| 576 | * @param view The WebView which needs to be cleaned up. |
| 577 | * @param detail the reason why it exited. |
| 578 | * @return {@code true} if the host application handled the situation that process has |
| 579 | * exited, otherwise, application will crash if render process crashed, |
| 580 | * or be killed if render process was killed by the system. |
| 581 | */ |
| 582 | public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) { |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Notify the host application that a loading URL has been flagged by Safe Browsing. |
| 588 | * |
| 589 | * The application must invoke the callback to indicate the preferred response. The default |
| 590 | * behavior is to show an interstitial to the user, with the reporting checkbox visible. |
| 591 | * |
| 592 | * If the application needs to show its own custom interstitial UI, the callback can be invoked |
| 593 | * asynchronously with {@link SafeBrowsingResponse#backToSafety} or {@link |
| 594 | * SafeBrowsingResponse#proceed}, depending on user response. |
| 595 | * |
| 596 | * @param view The WebView that hit the malicious resource. |
| 597 | * @param request Object containing the details of the request. |
| 598 | * @param threatType The reason the resource was caught by Safe Browsing, corresponding to a |
| 599 | * {@code SAFE_BROWSING_THREAT_*} value. |
| 600 | * @param callback Applications must invoke one of the callback methods. |
| 601 | */ |
| 602 | public void onSafeBrowsingHit(WebView view, WebResourceRequest request, |
| 603 | @SafeBrowsingThreat int threatType, SafeBrowsingResponse callback) { |
| 604 | callback.showInterstitial(/* allowReporting */ true); |
| 605 | } |
| 606 | } |