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