The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Michael Wright | 899d705 | 2014-04-23 17:23:39 -0700 | [diff] [blame] | 22 | import android.view.InputEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.view.KeyEvent; |
John Reck | d6b1098 | 2012-04-19 18:01:35 -0700 | [diff] [blame] | 24 | import android.view.ViewRootImpl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Selim Gurun | b6aa97e | 2014-03-26 14:10:28 -0700 | [diff] [blame] | 26 | import java.security.Principal; |
| 27 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | public class WebViewClient { |
| 29 | |
| 30 | /** |
| 31 | * Give the host application a chance to take over the control when a new |
| 32 | * url is about to be loaded in the current WebView. If WebViewClient is not |
| 33 | * provided, by default WebView will ask Activity Manager to choose the |
| 34 | * proper handler for the url. If WebViewClient is provided, return true |
| 35 | * means the host application handles the url, while return false means the |
| 36 | * current WebView handles the url. |
Martin Kosiba | 881c49c | 2012-11-13 14:29:30 +0000 | [diff] [blame] | 37 | * This method is not called for requests using the POST "method". |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 38 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | * @param view The WebView that is initiating the callback. |
| 40 | * @param url The url to be loaded. |
| 41 | * @return True if the host application wants to leave the current WebView |
| 42 | * and handle the url itself, otherwise return false. |
| 43 | */ |
| 44 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Notify the host application that a page has started loading. This method |
| 50 | * is called once for each main frame load so a page with iframes or |
| 51 | * framesets will call onPageStarted one time for the main frame. This also |
| 52 | * means that onPageStarted will not be called when the contents of an |
| 53 | * embedded frame changes, i.e. clicking a link whose target is an iframe. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 54 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | * @param view The WebView that is initiating the callback. |
| 56 | * @param url The url to be loaded. |
| 57 | * @param favicon The favicon for this page if it already exists in the |
| 58 | * database. |
| 59 | */ |
| 60 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Notify the host application that a page has finished loading. This method |
| 65 | * is called only for main frame. When onPageFinished() is called, the |
| 66 | * rendering picture may not be updated yet. To get the notification for the |
| 67 | * new Picture, use {@link WebView.PictureListener#onNewPicture}. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 68 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | * @param view The WebView that is initiating the callback. |
| 70 | * @param url The url of the page. |
| 71 | */ |
| 72 | public void onPageFinished(WebView view, String url) { |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Notify the host application that the WebView will load the resource |
| 77 | * specified by the given url. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 78 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * @param view The WebView that is initiating the callback. |
| 80 | * @param url The url of the resource the WebView will load. |
| 81 | */ |
| 82 | public void onLoadResource(WebView view, String url) { |
| 83 | } |
| 84 | |
| 85 | /** |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 86 | * Notify the host application of a resource request and allow the |
| 87 | * application to return the data. If the return value is null, the WebView |
| 88 | * will continue to load the resource as usual. Otherwise, the return |
Martin Kosiba | 15cb825 | 2012-11-13 14:40:41 +0000 | [diff] [blame] | 89 | * response and data will be used. NOTE: This method is called on a thread |
| 90 | * other than the UI thread so clients should exercise caution |
| 91 | * when accessing private data or the view system. |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 92 | * |
| 93 | * @param view The {@link android.webkit.WebView} that is requesting the |
| 94 | * resource. |
| 95 | * @param url The raw url of the resource. |
| 96 | * @return A {@link android.webkit.WebResourceResponse} containing the |
| 97 | * response information or null if the WebView should load the |
| 98 | * resource itself. |
| 99 | */ |
| 100 | public WebResourceResponse shouldInterceptRequest(WebView view, |
| 101 | String url) { |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | * Notify the host application that there have been an excessive number of |
| 107 | * HTTP redirects. As the host application if it would like to continue |
| 108 | * trying to load the resource. The default behavior is to send the cancel |
| 109 | * message. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 110 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | * @param view The WebView that is initiating the callback. |
| 112 | * @param cancelMsg The message to send if the host wants to cancel |
| 113 | * @param continueMsg The message to send if the host wants to continue |
Patrick Scott | 6a5b0ec | 2010-01-08 09:55:33 -0500 | [diff] [blame] | 114 | * @deprecated This method is no longer called. When the WebView encounters |
| 115 | * a redirect loop, it will cancel the load. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | */ |
Kristian Monsen | b5503c1 | 2010-08-31 14:02:38 +0100 | [diff] [blame] | 117 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | public void onTooManyRedirects(WebView view, Message cancelMsg, |
| 119 | Message continueMsg) { |
| 120 | cancelMsg.sendToTarget(); |
| 121 | } |
| 122 | |
Patrick Scott | 05c9ed9 | 2009-08-25 13:51:08 -0400 | [diff] [blame] | 123 | // These ints must match up to the hidden values in EventHandler. |
| 124 | /** Generic error */ |
| 125 | public static final int ERROR_UNKNOWN = -1; |
| 126 | /** Server or proxy hostname lookup failed */ |
| 127 | public static final int ERROR_HOST_LOOKUP = -2; |
| 128 | /** Unsupported authentication scheme (not basic or digest) */ |
| 129 | public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3; |
| 130 | /** User authentication failed on server */ |
| 131 | public static final int ERROR_AUTHENTICATION = -4; |
| 132 | /** User authentication failed on proxy */ |
| 133 | public static final int ERROR_PROXY_AUTHENTICATION = -5; |
| 134 | /** Failed to connect to the server */ |
| 135 | public static final int ERROR_CONNECT = -6; |
| 136 | /** Failed to read or write to the server */ |
| 137 | public static final int ERROR_IO = -7; |
| 138 | /** Connection timed out */ |
| 139 | public static final int ERROR_TIMEOUT = -8; |
| 140 | /** Too many redirects */ |
| 141 | public static final int ERROR_REDIRECT_LOOP = -9; |
| 142 | /** Unsupported URI scheme */ |
| 143 | public static final int ERROR_UNSUPPORTED_SCHEME = -10; |
| 144 | /** Failed to perform SSL handshake */ |
| 145 | public static final int ERROR_FAILED_SSL_HANDSHAKE = -11; |
| 146 | /** Malformed URL */ |
| 147 | public static final int ERROR_BAD_URL = -12; |
| 148 | /** Generic file error */ |
| 149 | public static final int ERROR_FILE = -13; |
| 150 | /** File not found */ |
| 151 | public static final int ERROR_FILE_NOT_FOUND = -14; |
| 152 | /** Too many requests during this load */ |
| 153 | public static final int ERROR_TOO_MANY_REQUESTS = -15; |
| 154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | /** |
Patrick Scott | 05c9ed9 | 2009-08-25 13:51:08 -0400 | [diff] [blame] | 156 | * Report an error to the host application. These errors are unrecoverable |
| 157 | * (i.e. the main resource is unavailable). The errorCode parameter |
| 158 | * corresponds to one of the ERROR_* constants. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | * @param view The WebView that is initiating the callback. |
Patrick Scott | 447811c | 2009-09-01 09:57:20 -0400 | [diff] [blame] | 160 | * @param errorCode The error code corresponding to an ERROR_* value. |
Patrick Scott | 05c9ed9 | 2009-08-25 13:51:08 -0400 | [diff] [blame] | 161 | * @param description A String describing the error. |
| 162 | * @param failingUrl The url that failed to load. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | */ |
| 164 | public void onReceivedError(WebView view, int errorCode, |
| 165 | String description, String failingUrl) { |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * As the host application if the browser should resend data as the |
| 170 | * requested page was a result of a POST. The default is to not resend the |
| 171 | * data. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 172 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | * @param view The WebView that is initiating the callback. |
| 174 | * @param dontResend The message to send if the browser should not resend |
| 175 | * @param resend The message to send if the browser should resend data |
| 176 | */ |
| 177 | public void onFormResubmission(WebView view, Message dontResend, |
| 178 | Message resend) { |
| 179 | dontResend.sendToTarget(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Notify the host application to update its visited links database. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 184 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | * @param view The WebView that is initiating the callback. |
| 186 | * @param url The url being visited. |
| 187 | * @param isReload True if this url is being reloaded. |
| 188 | */ |
| 189 | public void doUpdateVisitedHistory(WebView view, String url, |
| 190 | boolean isReload) { |
| 191 | } |
| 192 | |
| 193 | /** |
Steve Block | fe33a75 | 2011-10-04 19:09:13 +0100 | [diff] [blame] | 194 | * Notify the host application that an SSL error occurred while loading a |
| 195 | * resource. The host application must call either handler.cancel() or |
| 196 | * handler.proceed(). Note that the decision may be retained for use in |
| 197 | * response to future SSL errors. The default behavior is to cancel the |
| 198 | * load. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 199 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | * @param view The WebView that is initiating the callback. |
| 201 | * @param handler An SslErrorHandler object that will handle the user's |
| 202 | * response. |
| 203 | * @param error The SSL error object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | */ |
| 205 | public void onReceivedSslError(WebView view, SslErrorHandler handler, |
| 206 | SslError error) { |
| 207 | handler.cancel(); |
| 208 | } |
| 209 | |
| 210 | /** |
Selim Gurun | b6aa97e | 2014-03-26 14:10:28 -0700 | [diff] [blame] | 211 | * Notify the host application to handle a SSL client certificate |
| 212 | * request. The host application is responsible for showing the UI |
| 213 | * if desired and providing the keys. There are three ways to |
| 214 | * respond: proceed(), cancel() or ignore(). Webview remembers the |
| 215 | * response if proceed() or cancel() is called and does not |
| 216 | * call onReceivedClientCertRequest() again for the same host and port |
| 217 | * pair. Webview does not remember the response if ignore() is called. |
| 218 | * |
| 219 | * This method is called on the UI thread. During the callback, the |
| 220 | * connection is suspended. |
| 221 | * |
| 222 | * The default behavior is to cancel, returning no client certificate. |
| 223 | * |
| 224 | * @param view The WebView that is initiating the callback |
| 225 | * @param request An instance of a {@link ClientCertRequest} |
| 226 | * |
Selim Gurun | b6aa97e | 2014-03-26 14:10:28 -0700 | [diff] [blame] | 227 | */ |
| 228 | public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) { |
| 229 | request.cancel(); |
| 230 | } |
| 231 | |
| 232 | /** |
Steve Block | 46ce1db | 2012-07-17 16:43:00 +0100 | [diff] [blame] | 233 | * Notifies the host application that the WebView received an HTTP |
| 234 | * authentication request. The host application can use the supplied |
| 235 | * {@link HttpAuthHandler} to set the WebView's response to the request. |
| 236 | * The default behavior is to cancel the request. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 237 | * |
Steve Block | 46ce1db | 2012-07-17 16:43:00 +0100 | [diff] [blame] | 238 | * @param view the WebView that is initiating the callback |
| 239 | * @param handler the HttpAuthHandler used to set the WebView's response |
| 240 | * @param host the host requiring authentication |
| 241 | * @param realm the realm for which authentication is required |
Jonathan Dixon | 47aaba3 | 2012-11-30 16:32:17 -0800 | [diff] [blame] | 242 | * @see WebView#getHttpAuthUsernamePassword |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | */ |
| 244 | public void onReceivedHttpAuthRequest(WebView view, |
| 245 | HttpAuthHandler handler, String host, String realm) { |
| 246 | handler.cancel(); |
| 247 | } |
| 248 | |
| 249 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | * Give the host application a chance to handle the key event synchronously. |
| 251 | * e.g. menu shortcut key events need to be filtered this way. If return |
| 252 | * true, WebView will not handle the key event. If return false, WebView |
| 253 | * will always handle the key event, so none of the super in the view chain |
| 254 | * will see the key event. The default behavior returns false. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 255 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | * @param view The WebView that is initiating the callback. |
| 257 | * @param event The key event. |
| 258 | * @return True if the host application wants to handle the key event |
| 259 | * itself, otherwise return false |
| 260 | */ |
| 261 | public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Notify the host application that a key was not handled by the WebView. |
| 267 | * Except system keys, WebView always consumes the keys in the normal flow |
| 268 | * or if shouldOverrideKeyEvent returns true. This is called asynchronously |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 269 | * from where the key is dispatched. It gives the host application a chance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | * to handle the unhandled key events. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 271 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | * @param view The WebView that is initiating the callback. |
| 273 | * @param event The key event. |
Michael Wright | 899d705 | 2014-04-23 17:23:39 -0700 | [diff] [blame] | 274 | * @deprecated This method is subsumed by the more generic onUnhandledInputEvent. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | */ |
Michael Wright | 899d705 | 2014-04-23 17:23:39 -0700 | [diff] [blame] | 276 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | public void onUnhandledKeyEvent(WebView view, KeyEvent event) { |
Michael Wright | 899d705 | 2014-04-23 17:23:39 -0700 | [diff] [blame] | 278 | onUnhandledInputEventInternal(view, event); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Notify the host application that a input event was not handled by the WebView. |
| 283 | * Except system keys, WebView always consumes input events in the normal flow |
| 284 | * or if shouldOverrideKeyEvent returns true. This is called asynchronously |
| 285 | * from where the event is dispatched. It gives the host application a chance |
| 286 | * to handle the unhandled input events. |
| 287 | * |
Michael Wright | 1feb11f | 2014-05-01 15:41:33 -0700 | [diff] [blame] | 288 | * Note that if the event is a {@link android.view.MotionEvent}, then it's lifetime is only |
| 289 | * that of the function call. If the WebViewClient wishes to use the event beyond that, then it |
| 290 | * <i>must</i> create a copy of the event. |
Michael Wright | 899d705 | 2014-04-23 17:23:39 -0700 | [diff] [blame] | 291 | * |
Michael Wright | 1feb11f | 2014-05-01 15:41:33 -0700 | [diff] [blame] | 292 | * It is the responsibility of overriders of this method to call |
| 293 | * {@link #onUnhandledKeyEvent(WebView, KeyEvent)} |
Michael Wright | 899d705 | 2014-04-23 17:23:39 -0700 | [diff] [blame] | 294 | * when appropriate if they wish to continue receiving events through it. |
| 295 | * |
| 296 | * @param view The WebView that is initiating the callback. |
| 297 | * @param event The input event. |
| 298 | */ |
| 299 | public void onUnhandledInputEvent(WebView view, InputEvent event) { |
| 300 | if (event instanceof KeyEvent) { |
| 301 | onUnhandledKeyEvent(view, (KeyEvent) event); |
| 302 | return; |
| 303 | } |
| 304 | onUnhandledInputEventInternal(view, event); |
| 305 | } |
| 306 | |
| 307 | private void onUnhandledInputEventInternal(WebView view, InputEvent event) { |
John Reck | d6b1098 | 2012-04-19 18:01:35 -0700 | [diff] [blame] | 308 | ViewRootImpl root = view.getViewRootImpl(); |
| 309 | if (root != null) { |
Michael Wright | 899d705 | 2014-04-23 17:23:39 -0700 | [diff] [blame] | 310 | root.dispatchUnhandledInputEvent(event); |
John Reck | d6b1098 | 2012-04-19 18:01:35 -0700 | [diff] [blame] | 311 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Notify the host application that the scale applied to the WebView has |
| 316 | * changed. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 317 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | * @param view he WebView that is initiating the callback. |
| 319 | * @param oldScale The old scale factor |
| 320 | * @param newScale The new scale factor |
| 321 | */ |
| 322 | public void onScaleChanged(WebView view, float oldScale, float newScale) { |
| 323 | } |
Patrick Scott | 85a50ff | 2011-01-25 14:42:12 -0500 | [diff] [blame] | 324 | |
| 325 | /** |
| 326 | * Notify the host application that a request to automatically log in the |
| 327 | * user has been processed. |
| 328 | * @param view The WebView requesting the login. |
| 329 | * @param realm The account realm used to look up accounts. |
| 330 | * @param account An optional account. If not null, the account should be |
| 331 | * checked against accounts on the device. If it is a valid |
| 332 | * account, it should be used to log in the user. |
| 333 | * @param args Authenticator specific arguments used to log in the user. |
| 334 | */ |
| 335 | public void onReceivedLoginRequest(WebView view, String realm, |
| 336 | String account, String args) { |
| 337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |