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