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