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.graphics.Bitmap;
     20 import android.net.http.SslError;
     21 import android.os.Message;
     22 import android.view.KeyEvent;
     23 
     24 public class WebViewClient {
     25 
     26     /**
     27      * Give the host application a chance to take over the control when a new
     28      * url is about to be loaded in the current WebView. If WebViewClient is not
     29      * provided, by default WebView will ask Activity Manager to choose the
     30      * proper handler for the url. If WebViewClient is provided, return true
     31      * means the host application handles the url, while return false means the
     32      * current WebView handles the url.
     33      *
     34      * @param view The WebView that is initiating the callback.
     35      * @param url The url to be loaded.
     36      * @return True if the host application wants to leave the current WebView
     37      *         and handle the url itself, otherwise return false.
     38      */
     39     public boolean shouldOverrideUrlLoading(WebView view, String url) {
     40         return false;
     41     }
     42 
     43     /**
     44      * Notify the host application that a page has started loading. This method
     45      * is called once for each main frame load so a page with iframes or
     46      * framesets will call onPageStarted one time for the main frame. This also
     47      * means that onPageStarted will not be called when the contents of an
     48      * embedded frame changes, i.e. clicking a link whose target is an iframe.
     49      *
     50      * @param view The WebView that is initiating the callback.
     51      * @param url The url to be loaded.
     52      * @param favicon The favicon for this page if it already exists in the
     53      *            database.
     54      */
     55     public void onPageStarted(WebView view, String url, Bitmap favicon) {
     56     }
     57 
     58     /**
     59      * Notify the host application that a page has finished loading. This method
     60      * is called only for main frame. When onPageFinished() is called, the
     61      * rendering picture may not be updated yet. To get the notification for the
     62      * new Picture, use {@link WebView.PictureListener#onNewPicture}.
     63      *
     64      * @param view The WebView that is initiating the callback.
     65      * @param url The url of the page.
     66      */
     67     public void onPageFinished(WebView view, String url) {
     68     }
     69 
     70     /**
     71      * Notify the host application that the WebView will load the resource
     72      * specified by the given url.
     73      *
     74      * @param view The WebView that is initiating the callback.
     75      * @param url The url of the resource the WebView will load.
     76      */
     77     public void onLoadResource(WebView view, String url) {
     78     }
     79 
     80     /**
     81      * Notify the host application that there have been an excessive number of
     82      * HTTP redirects. As the host application if it would like to continue
     83      * trying to load the resource. The default behavior is to send the cancel
     84      * message.
     85      *
     86      * @param view The WebView that is initiating the callback.
     87      * @param cancelMsg The message to send if the host wants to cancel
     88      * @param continueMsg The message to send if the host wants to continue
     89      * @deprecated This method is no longer called. When the WebView encounters
     90      *             a redirect loop, it will cancel the load.
     91      */
     92     @Deprecated
     93     public void onTooManyRedirects(WebView view, Message cancelMsg,
     94             Message continueMsg) {
     95         cancelMsg.sendToTarget();
     96     }
     97 
     98     // These ints must match up to the hidden values in EventHandler.
     99     /** Generic error */
    100     public static final int ERROR_UNKNOWN = -1;
    101     /** Server or proxy hostname lookup failed */
    102     public static final int ERROR_HOST_LOOKUP = -2;
    103     /** Unsupported authentication scheme (not basic or digest) */
    104     public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3;
    105     /** User authentication failed on server */
    106     public static final int ERROR_AUTHENTICATION = -4;
    107     /** User authentication failed on proxy */
    108     public static final int ERROR_PROXY_AUTHENTICATION = -5;
    109     /** Failed to connect to the server */
    110     public static final int ERROR_CONNECT = -6;
    111     /** Failed to read or write to the server */
    112     public static final int ERROR_IO = -7;
    113     /** Connection timed out */
    114     public static final int ERROR_TIMEOUT = -8;
    115     /** Too many redirects */
    116     public static final int ERROR_REDIRECT_LOOP = -9;
    117     /** Unsupported URI scheme */
    118     public static final int ERROR_UNSUPPORTED_SCHEME = -10;
    119     /** Failed to perform SSL handshake */
    120     public static final int ERROR_FAILED_SSL_HANDSHAKE = -11;
    121     /** Malformed URL */
    122     public static final int ERROR_BAD_URL = -12;
    123     /** Generic file error */
    124     public static final int ERROR_FILE = -13;
    125     /** File not found */
    126     public static final int ERROR_FILE_NOT_FOUND = -14;
    127     /** Too many requests during this load */
    128     public static final int ERROR_TOO_MANY_REQUESTS = -15;
    129 
    130     /**
    131      * Report an error to the host application. These errors are unrecoverable
    132      * (i.e. the main resource is unavailable). The errorCode parameter
    133      * corresponds to one of the ERROR_* constants.
    134      * @param view The WebView that is initiating the callback.
    135      * @param errorCode The error code corresponding to an ERROR_* value.
    136      * @param description A String describing the error.
    137      * @param failingUrl The url that failed to load.
    138      */
    139     public void onReceivedError(WebView view, int errorCode,
    140             String description, String failingUrl) {
    141     }
    142 
    143     /**
    144      * As the host application if the browser should resend data as the
    145      * requested page was a result of a POST. The default is to not resend the
    146      * data.
    147      *
    148      * @param view The WebView that is initiating the callback.
    149      * @param dontResend The message to send if the browser should not resend
    150      * @param resend The message to send if the browser should resend data
    151      */
    152     public void onFormResubmission(WebView view, Message dontResend,
    153             Message resend) {
    154         dontResend.sendToTarget();
    155     }
    156 
    157     /**
    158      * Notify the host application to update its visited links database.
    159      *
    160      * @param view The WebView that is initiating the callback.
    161      * @param url The url being visited.
    162      * @param isReload True if this url is being reloaded.
    163      */
    164     public void doUpdateVisitedHistory(WebView view, String url,
    165             boolean isReload) {
    166     }
    167 
    168     /**
    169      * Notify the host application to handle a ssl certificate error request
    170      * (display the error to the user and ask whether to proceed or not). The
    171      * host application has to call either handler.cancel() or handler.proceed()
    172      * as the connection is suspended and waiting for the response. The default
    173      * behavior is to cancel the load.
    174      *
    175      * @param view The WebView that is initiating the callback.
    176      * @param handler An SslErrorHandler object that will handle the user's
    177      *            response.
    178      * @param error The SSL error object.
    179      */
    180     public void onReceivedSslError(WebView view, SslErrorHandler handler,
    181             SslError error) {
    182         handler.cancel();
    183     }
    184 
    185     /**
    186      * Notify the host application to handle an authentication request. The
    187      * default behavior is to cancel the request.
    188      *
    189      * @param view The WebView that is initiating the callback.
    190      * @param handler The HttpAuthHandler that will handle the user's response.
    191      * @param host The host requiring authentication.
    192      * @param realm A description to help store user credentials for future
    193      *            visits.
    194      */
    195     public void onReceivedHttpAuthRequest(WebView view,
    196             HttpAuthHandler handler, String host, String realm) {
    197         handler.cancel();
    198     }
    199 
    200     /**
    201      * Give the host application a chance to handle the key event synchronously.
    202      * e.g. menu shortcut key events need to be filtered this way. If return
    203      * true, WebView will not handle the key event. If return false, WebView
    204      * will always handle the key event, so none of the super in the view chain
    205      * will see the key event. The default behavior returns false.
    206      *
    207      * @param view The WebView that is initiating the callback.
    208      * @param event The key event.
    209      * @return True if the host application wants to handle the key event
    210      *         itself, otherwise return false
    211      */
    212     public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
    213         return false;
    214     }
    215 
    216     /**
    217      * Notify the host application that a key was not handled by the WebView.
    218      * Except system keys, WebView always consumes the keys in the normal flow
    219      * or if shouldOverrideKeyEvent returns true. This is called asynchronously
    220      * from where the key is dispatched. It gives the host application an chance
    221      * to handle the unhandled key events.
    222      *
    223      * @param view The WebView that is initiating the callback.
    224      * @param event The key event.
    225      */
    226     public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
    227     }
    228 
    229     /**
    230      * Notify the host application that the scale applied to the WebView has
    231      * changed.
    232      *
    233      * @param view he WebView that is initiating the callback.
    234      * @param oldScale The old scale factor
    235      * @param newScale The new scale factor
    236      */
    237     public void onScaleChanged(WebView view, float oldScale, float newScale) {
    238     }
    239 }
    240