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
17package android.webkit;
18
19import android.graphics.Bitmap;
20import android.net.http.SslError;
21import android.os.Message;
22import android.view.InputEvent;
23import android.view.KeyEvent;
24import android.view.ViewRootImpl;
25
26import java.security.Principal;
27
28public 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.
37     * This method is not called for requests using the POST "method".
38     *
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.
54     *
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}.
68     *
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.
78     *
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    /**
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
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.
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     * @deprecated Use {@link #shouldInterceptRequest(WebView, WebResourceRequest)
100     *             shouldInterceptRequest(WebView, WebResourceRequest)} instead.
101     */
102    @Deprecated
103    public WebResourceResponse shouldInterceptRequest(WebView view,
104            String url) {
105        return null;
106    }
107
108    /**
109     * Notify the host application of a resource request and allow the
110     * application to return the data.  If the return value is null, the WebView
111     * will continue to load the resource as usual.  Otherwise, the return
112     * response and data will be used.  NOTE: This method is called on a thread
113     * other than the UI thread so clients should exercise caution
114     * when accessing private data or the view system.
115     *
116     * @param view The {@link android.webkit.WebView} that is requesting the
117     *             resource.
118     * @param request Object containing the details of the request.
119     * @return A {@link android.webkit.WebResourceResponse} containing the
120     *         response information or null if the WebView should load the
121     *         resource itself.
122     */
123    public WebResourceResponse shouldInterceptRequest(WebView view,
124            WebResourceRequest request) {
125        return shouldInterceptRequest(view, request.getUrl().toString());
126    }
127
128    /**
129     * Notify the host application that there have been an excessive number of
130     * HTTP redirects. As the host application if it would like to continue
131     * trying to load the resource. The default behavior is to send the cancel
132     * message.
133     *
134     * @param view The WebView that is initiating the callback.
135     * @param cancelMsg The message to send if the host wants to cancel
136     * @param continueMsg The message to send if the host wants to continue
137     * @deprecated This method is no longer called. When the WebView encounters
138     *             a redirect loop, it will cancel the load.
139     */
140    @Deprecated
141    public void onTooManyRedirects(WebView view, Message cancelMsg,
142            Message continueMsg) {
143        cancelMsg.sendToTarget();
144    }
145
146    // These ints must match up to the hidden values in EventHandler.
147    /** Generic error */
148    public static final int ERROR_UNKNOWN = -1;
149    /** Server or proxy hostname lookup failed */
150    public static final int ERROR_HOST_LOOKUP = -2;
151    /** Unsupported authentication scheme (not basic or digest) */
152    public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3;
153    /** User authentication failed on server */
154    public static final int ERROR_AUTHENTICATION = -4;
155    /** User authentication failed on proxy */
156    public static final int ERROR_PROXY_AUTHENTICATION = -5;
157    /** Failed to connect to the server */
158    public static final int ERROR_CONNECT = -6;
159    /** Failed to read or write to the server */
160    public static final int ERROR_IO = -7;
161    /** Connection timed out */
162    public static final int ERROR_TIMEOUT = -8;
163    /** Too many redirects */
164    public static final int ERROR_REDIRECT_LOOP = -9;
165    /** Unsupported URI scheme */
166    public static final int ERROR_UNSUPPORTED_SCHEME = -10;
167    /** Failed to perform SSL handshake */
168    public static final int ERROR_FAILED_SSL_HANDSHAKE = -11;
169    /** Malformed URL */
170    public static final int ERROR_BAD_URL = -12;
171    /** Generic file error */
172    public static final int ERROR_FILE = -13;
173    /** File not found */
174    public static final int ERROR_FILE_NOT_FOUND = -14;
175    /** Too many requests during this load */
176    public static final int ERROR_TOO_MANY_REQUESTS = -15;
177
178    /**
179     * Report an error to the host application. These errors are unrecoverable
180     * (i.e. the main resource is unavailable). The errorCode parameter
181     * corresponds to one of the ERROR_* constants.
182     * @param view The WebView that is initiating the callback.
183     * @param errorCode The error code corresponding to an ERROR_* value.
184     * @param description A String describing the error.
185     * @param failingUrl The url that failed to load.
186     */
187    public void onReceivedError(WebView view, int errorCode,
188            String description, String failingUrl) {
189    }
190
191    /**
192     * As the host application if the browser should resend data as the
193     * requested page was a result of a POST. The default is to not resend the
194     * data.
195     *
196     * @param view The WebView that is initiating the callback.
197     * @param dontResend The message to send if the browser should not resend
198     * @param resend The message to send if the browser should resend data
199     */
200    public void onFormResubmission(WebView view, Message dontResend,
201            Message resend) {
202        dontResend.sendToTarget();
203    }
204
205    /**
206     * Notify the host application to update its visited links database.
207     *
208     * @param view The WebView that is initiating the callback.
209     * @param url The url being visited.
210     * @param isReload True if this url is being reloaded.
211     */
212    public void doUpdateVisitedHistory(WebView view, String url,
213            boolean isReload) {
214    }
215
216    /**
217     * Notify the host application that an SSL error occurred while loading a
218     * resource. The host application must call either handler.cancel() or
219     * handler.proceed(). Note that the decision may be retained for use in
220     * response to future SSL errors. The default behavior is to cancel the
221     * load.
222     *
223     * @param view The WebView that is initiating the callback.
224     * @param handler An SslErrorHandler object that will handle the user's
225     *            response.
226     * @param error The SSL error object.
227     */
228    public void onReceivedSslError(WebView view, SslErrorHandler handler,
229            SslError error) {
230        handler.cancel();
231    }
232
233    /**
234     * Notify the host application to handle a SSL client certificate
235     * request. The host application is responsible for showing the UI
236     * if desired and providing the keys. There are three ways to
237     * respond: proceed(), cancel() or ignore(). Webview remembers the
238     * response if proceed() or cancel() is called and does not
239     * call onReceivedClientCertRequest() again for the same host and port
240     * pair. Webview does not remember the response if ignore() is called.
241     *
242     * This method is called on the UI thread. During the callback, the
243     * connection is suspended.
244     *
245     * The default behavior is to cancel, returning no client certificate.
246     *
247     * @param view The WebView that is initiating the callback
248     * @param request An instance of a {@link ClientCertRequest}
249     *
250     */
251    public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
252        request.cancel();
253    }
254
255    /**
256     * Notifies the host application that the WebView received an HTTP
257     * authentication request. The host application can use the supplied
258     * {@link HttpAuthHandler} to set the WebView's response to the request.
259     * The default behavior is to cancel the request.
260     *
261     * @param view the WebView that is initiating the callback
262     * @param handler the HttpAuthHandler used to set the WebView's response
263     * @param host the host requiring authentication
264     * @param realm the realm for which authentication is required
265     * @see WebView#getHttpAuthUsernamePassword
266     */
267    public void onReceivedHttpAuthRequest(WebView view,
268            HttpAuthHandler handler, String host, String realm) {
269        handler.cancel();
270    }
271
272    /**
273     * Give the host application a chance to handle the key event synchronously.
274     * e.g. menu shortcut key events need to be filtered this way. If return
275     * true, WebView will not handle the key event. If return false, WebView
276     * will always handle the key event, so none of the super in the view chain
277     * will see the key event. The default behavior returns false.
278     *
279     * @param view The WebView that is initiating the callback.
280     * @param event The key event.
281     * @return True if the host application wants to handle the key event
282     *         itself, otherwise return false
283     */
284    public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
285        return false;
286    }
287
288    /**
289     * Notify the host application that a key was not handled by the WebView.
290     * Except system keys, WebView always consumes the keys in the normal flow
291     * or if shouldOverrideKeyEvent returns true. This is called asynchronously
292     * from where the key is dispatched. It gives the host application a chance
293     * to handle the unhandled key events.
294     *
295     * @param view The WebView that is initiating the callback.
296     * @param event The key event.
297     * @deprecated This method is subsumed by the more generic onUnhandledInputEvent.
298     */
299    @Deprecated
300    public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
301        onUnhandledInputEventInternal(view, event);
302    }
303
304    /**
305     * Notify the host application that a input event was not handled by the WebView.
306     * Except system keys, WebView always consumes input events in the normal flow
307     * or if shouldOverrideKeyEvent returns true. This is called asynchronously
308     * from where the event is dispatched. It gives the host application a chance
309     * to handle the unhandled input events.
310     *
311     * Note that if the event is a {@link android.view.MotionEvent}, then it's lifetime is only
312     * that of the function call. If the WebViewClient wishes to use the event beyond that, then it
313     * <i>must</i> create a copy of the event.
314     *
315     * It is the responsibility of overriders of this method to call
316     * {@link #onUnhandledKeyEvent(WebView, KeyEvent)}
317     * when appropriate if they wish to continue receiving events through it.
318     *
319     * @param view The WebView that is initiating the callback.
320     * @param event The input event.
321     */
322    public void onUnhandledInputEvent(WebView view, InputEvent event) {
323        if (event instanceof KeyEvent) {
324            onUnhandledKeyEvent(view, (KeyEvent) event);
325            return;
326        }
327        onUnhandledInputEventInternal(view, event);
328    }
329
330    private void onUnhandledInputEventInternal(WebView view, InputEvent event) {
331        ViewRootImpl root = view.getViewRootImpl();
332        if (root != null) {
333            root.dispatchUnhandledInputEvent(event);
334        }
335    }
336
337    /**
338     * Notify the host application that the scale applied to the WebView has
339     * changed.
340     *
341     * @param view he WebView that is initiating the callback.
342     * @param oldScale The old scale factor
343     * @param newScale The new scale factor
344     */
345    public void onScaleChanged(WebView view, float oldScale, float newScale) {
346    }
347
348    /**
349     * Notify the host application that a request to automatically log in the
350     * user has been processed.
351     * @param view The WebView requesting the login.
352     * @param realm The account realm used to look up accounts.
353     * @param account An optional account. If not null, the account should be
354     *                checked against accounts on the device. If it is a valid
355     *                account, it should be used to log in the user.
356     * @param args Authenticator specific arguments used to log in the user.
357     */
358    public void onReceivedLoginRequest(WebView view, String realm,
359            String account, String args) {
360    }
361}
362