blob: 62b80c4a516bd4ba52cc84bda17d466690dec9c7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.webkit;
18
19import android.graphics.Bitmap;
20import android.net.http.SslError;
21import android.os.Message;
Michael Wright899d7052014-04-23 17:23:39 -070022import android.view.InputEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.view.KeyEvent;
John Reckd6b10982012-04-19 18:01:35 -070024import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
Selim Gurunb6aa97e2014-03-26 14:10:28 -070026import java.security.Principal;
27
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028public 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 Kosiba881c49c2012-11-13 14:29:30 +000037 * This method is not called for requests using the POST "method".
Brian Carlstroma1477592011-02-11 13:39:56 -080038 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 * @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 Carlstroma1477592011-02-11 13:39:56 -080054 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 * @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 Carlstroma1477592011-02-11 13:39:56 -080068 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 * @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 Carlstroma1477592011-02-11 13:39:56 -080078 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * @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 Scottc12544a2010-11-11 13:16:44 -050086 * 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 Kosiba15cb8252012-11-13 14:40:41 +000089 * 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 Scottc12544a2010-11-11 13:16:44 -050092 *
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 Project9066cfe2009-03-03 19:31:44 -0800106 * 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 Carlstroma1477592011-02-11 13:39:56 -0800110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * @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 Scott6a5b0ec2010-01-08 09:55:33 -0500114 * @deprecated This method is no longer called. When the WebView encounters
115 * a redirect loop, it will cancel the load.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 */
Kristian Monsenb5503c12010-08-31 14:02:38 +0100117 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public void onTooManyRedirects(WebView view, Message cancelMsg,
119 Message continueMsg) {
120 cancelMsg.sendToTarget();
121 }
122
Patrick Scott05c9ed92009-08-25 13:51:08 -0400123 // 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 Project9066cfe2009-03-03 19:31:44 -0800155 /**
Patrick Scott05c9ed92009-08-25 13:51:08 -0400156 * 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 Project9066cfe2009-03-03 19:31:44 -0800159 * @param view The WebView that is initiating the callback.
Patrick Scott447811c2009-09-01 09:57:20 -0400160 * @param errorCode The error code corresponding to an ERROR_* value.
Patrick Scott05c9ed92009-08-25 13:51:08 -0400161 * @param description A String describing the error.
162 * @param failingUrl The url that failed to load.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 */
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 Carlstroma1477592011-02-11 13:39:56 -0800172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * @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 Carlstroma1477592011-02-11 13:39:56 -0800184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * @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 Blockfe33a752011-10-04 19:09:13 +0100194 * 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 Carlstroma1477592011-02-11 13:39:56 -0800199 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * @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 Project9066cfe2009-03-03 19:31:44 -0800204 */
205 public void onReceivedSslError(WebView view, SslErrorHandler handler,
206 SslError error) {
207 handler.cancel();
208 }
209
210 /**
Selim Gurunb6aa97e2014-03-26 14:10:28 -0700211 * 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 Gurunb6aa97e2014-03-26 14:10:28 -0700227 */
228 public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
229 request.cancel();
230 }
231
232 /**
Steve Block46ce1db2012-07-17 16:43:00 +0100233 * 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 Carlstroma1477592011-02-11 13:39:56 -0800237 *
Steve Block46ce1db2012-07-17 16:43:00 +0100238 * @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 Dixon47aaba32012-11-30 16:32:17 -0800242 * @see WebView#getHttpAuthUsernamePassword
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 */
244 public void onReceivedHttpAuthRequest(WebView view,
245 HttpAuthHandler handler, String host, String realm) {
246 handler.cancel();
247 }
248
249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * 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 Carlstroma1477592011-02-11 13:39:56 -0800255 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 * @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 Wakasaf76a50c2012-03-09 19:56:35 +0900269 * from where the key is dispatched. It gives the host application a chance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * to handle the unhandled key events.
Brian Carlstroma1477592011-02-11 13:39:56 -0800271 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 * @param view The WebView that is initiating the callback.
273 * @param event The key event.
Michael Wright899d7052014-04-23 17:23:39 -0700274 * @deprecated This method is subsumed by the more generic onUnhandledInputEvent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 */
Michael Wright899d7052014-04-23 17:23:39 -0700276 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Wright899d7052014-04-23 17:23:39 -0700278 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 Wright1feb11f2014-05-01 15:41:33 -0700288 * 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 Wright899d7052014-04-23 17:23:39 -0700291 *
Michael Wright1feb11f2014-05-01 15:41:33 -0700292 * It is the responsibility of overriders of this method to call
293 * {@link #onUnhandledKeyEvent(WebView, KeyEvent)}
Michael Wright899d7052014-04-23 17:23:39 -0700294 * 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 Reckd6b10982012-04-19 18:01:35 -0700308 ViewRootImpl root = view.getViewRootImpl();
309 if (root != null) {
Michael Wright899d7052014-04-23 17:23:39 -0700310 root.dispatchUnhandledInputEvent(event);
John Reckd6b10982012-04-19 18:01:35 -0700311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 }
313
314 /**
315 * Notify the host application that the scale applied to the WebView has
316 * changed.
Brian Carlstroma1477592011-02-11 13:39:56 -0800317 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * @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 Scott85a50ff2011-01-25 14:42:12 -0500324
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 Project9066cfe2009-03-03 19:31:44 -0800338}