WebViewContentsClientAdapter.java revision d32343c77868461af07738ac7abd802000490d3d
1/*
2 * Copyright (C) 2012 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 com.android.webview.chromium;
18
19import android.content.ActivityNotFoundException;
20import android.content.Context;
21import android.content.Intent;
22import android.graphics.Picture;
23import android.os.Handler;
24import android.os.Looper;
25import android.os.Message;
26import android.provider.Browser;
27import android.util.Log;
28import android.view.KeyEvent;
29import android.webkit.ConsoleMessage;
30import android.webkit.DownloadListener;
31import android.webkit.JsPromptResult;
32import android.webkit.JsResult;
33import android.webkit.WebChromeClient;
34import android.webkit.WebResourceResponse;
35import android.webkit.WebView;
36import android.webkit.WebViewClient;
37
38import org.chromium.android_webview.AwContentsClient;
39import org.chromium.android_webview.AwHttpAuthHandler;
40import org.chromium.android_webview.InterceptedRequestData;
41import org.chromium.android_webview.JsPromptResultReceiver;
42import org.chromium.android_webview.JsResultReceiver;
43import org.chromium.content.browser.ContentView;
44import org.chromium.content.browser.ContentViewClient;
45
46import java.net.URISyntaxException;
47
48/**
49 * An adapter class that forwards the callbacks from {@link ContentViewClient}
50 * to the appropriate {@link WebViewClient} or {@link WebChromeClient}.
51 *
52 * An instance of this class is associated with one {@link WebViewChromium}
53 * instance. A WebViewChromium is a WebView implementation provider (that is
54 * android.webkit.WebView delegates all functionality to it) and has exactly
55 * one corresponding {@link ContentView} instance.
56 *
57 * A {@link ContentViewClient} may be shared between multiple {@link ContentView}s,
58 * and hence multiple WebViews. Many WebViewClient methods pass the source
59 * WebView as an argument. This means that we either need to pass the
60 * corresponding ContentView to the corresponding ContentViewClient methods,
61 * or use an instance of ContentViewClientAdapter per WebViewChromium, to
62 * allow the source WebView to be injected by ContentViewClientAdapter. We
63 * choose the latter, because it makes for a cleaner design.
64 */
65public class WebViewContentsClientAdapter extends AwContentsClient {
66    private static final String TAG = "ContentViewClientAdapter";
67    // The WebView instance that this adapter is serving.
68    private final WebView mWebView;
69    // The WebViewClient instance that was passed to WebView.setWebViewClient().
70    private WebViewClient mWebViewClient;
71    // The WebViewClient instance that was passed to WebView.setContentViewClient().
72    private WebChromeClient mWebChromeClient;
73    // The listener receiving find-in-page API results.
74    private WebView.FindListener mFindListener;
75    // The listener receiving notifications of screen updates.
76    private WebView.PictureListener mPictureListener;
77
78    private DownloadListener mDownloadListener;
79
80    private Handler mUiThreadHandler;
81
82    private static final int NEW_WEBVIEW_CREATED = 100;
83
84    /**
85     * Adapter constructor.
86     *
87     * @param webView the {@link WebView} instance that this adapter is serving.
88     */
89    WebViewContentsClientAdapter(WebView webView) {
90        if (webView == null) {
91            throw new IllegalArgumentException("webView can't be null");
92        }
93
94        mWebView = webView;
95        setWebViewClient(null);
96        setWebChromeClient(null);
97
98        mUiThreadHandler = new Handler() {
99
100            @Override
101            public void handleMessage(Message msg) {
102                switch(msg.what) {
103                    case NEW_WEBVIEW_CREATED:
104                        WebView.WebViewTransport t = (WebView.WebViewTransport) msg.obj;
105                        WebView newWebView = t.getWebView();
106                        if (newWebView == null) {
107                            throw new IllegalArgumentException(
108                                    "Must provide a new WebView for the new window.");
109                        }
110                        if (newWebView == mWebView) {
111                            throw new IllegalArgumentException(
112                                    "Parent WebView cannot host it's own popup window. Please " +
113                                    "use WebSettings.setSupportMultipleWindows(false)");
114                        }
115
116                        if (newWebView.copyBackForwardList().getSize() != 0) {
117                            throw new IllegalArgumentException(
118                                    "New WebView for popup window must not have been previously " +
119                                    "navigated.");
120                        }
121
122                        WebViewChromium.completeWindowCreation(mWebView, newWebView);
123                        break;
124                    default:
125                        throw new IllegalStateException();
126                }
127            }
128        };
129
130    }
131
132    // WebViewClassic is coded in such a way that even if a null WebViewClient is set,
133    // certain actions take place.
134    // We choose to replicate this behavior by using a NullWebViewClient implementation (also known
135    // as the Null Object pattern) rather than duplicating the WebViewClassic approach in
136    // ContentView.
137    static class NullWebViewClient extends WebViewClient {
138        @Override
139        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
140            // TODO: Investigate more and add a test case.
141            // This is a copy of what Clank does. The WebViewCore key handling code and Clank key
142            // handling code differ enough that it's not trivial to figure out how keycodes are
143            // being filtered.
144            int keyCode = event.getKeyCode();
145            if (keyCode == KeyEvent.KEYCODE_MENU ||
146                keyCode == KeyEvent.KEYCODE_HOME ||
147                keyCode == KeyEvent.KEYCODE_BACK ||
148                keyCode == KeyEvent.KEYCODE_CALL ||
149                keyCode == KeyEvent.KEYCODE_ENDCALL ||
150                keyCode == KeyEvent.KEYCODE_POWER ||
151                keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
152                keyCode == KeyEvent.KEYCODE_CAMERA ||
153                keyCode == KeyEvent.KEYCODE_FOCUS ||
154                keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
155                keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
156                keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
157                return true;
158            }
159            return false;
160        }
161
162        @Override
163        public boolean shouldOverrideUrlLoading(WebView view, String url) {
164            Intent intent;
165            // Perform generic parsing of the URI to turn it into an Intent.
166            try {
167                intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
168            } catch (URISyntaxException ex) {
169                Log.w(TAG, "Bad URI " + url + ": " + ex.getMessage());
170                return false;
171            }
172            // Sanitize the Intent, ensuring web pages can not bypass browser
173            // security (only access to BROWSABLE activities).
174            intent.addCategory(Intent.CATEGORY_BROWSABLE);
175            intent.setComponent(null);
176            // Pass the package name as application ID so that the intent from the
177            // same application can be opened in the same tab.
178            intent.putExtra(Browser.EXTRA_APPLICATION_ID,
179                    view.getContext().getPackageName());
180            try {
181                view.getContext().startActivity(intent);
182            } catch (ActivityNotFoundException ex) {
183                Log.w(TAG, "No application can handle " + url);
184                return false;
185            }
186            return true;
187        }
188    }
189
190    void setWebViewClient(WebViewClient client) {
191        if (client != null) {
192            mWebViewClient = client;
193        } else {
194            mWebViewClient = new NullWebViewClient();
195        }
196    }
197
198    void setWebChromeClient(WebChromeClient client) {
199        if (client != null) {
200            mWebChromeClient = client;
201        } else {
202            // WebViewClassic doesn't implement any special behavior for a null WebChromeClient.
203            mWebChromeClient = new WebChromeClient();
204        }
205    }
206
207    void setDownloadListener(DownloadListener listener) {
208        mDownloadListener = listener;
209    }
210
211    void setFindListener(WebView.FindListener listener) {
212        mFindListener = listener;
213    }
214
215    void setPictureListener(WebView.PictureListener listener) {
216        mPictureListener = listener;
217    }
218
219    //--------------------------------------------------------------------------------------------
220    //                        Adapter for WebContentsDelegate methods.
221    //--------------------------------------------------------------------------------------------
222
223    /**
224     * @see AwContentsClient#doUpdateVisiteHistory(String, boolean)
225     */
226    @Override
227    public void doUpdateVisitedHistory(String url, boolean isReload) {
228        mWebViewClient.doUpdateVisitedHistory(mWebView, url, isReload);
229    }
230
231    /**
232     * @see AwContentsClient#onProgressChanged(int)
233     */
234    @Override
235    public void onProgressChanged(int progress) {
236        mWebChromeClient.onProgressChanged(mWebView, progress);
237    }
238
239    /**
240     * @see AwContentsClient#shouldInterceptRequest(java.lang.String)
241     */
242    @Override
243    public InterceptedRequestData shouldInterceptRequest(String url) {
244        WebResourceResponse response = mWebViewClient.shouldInterceptRequest(mWebView, url);
245        if (response == null) return null;
246        return new InterceptedRequestData(
247                response.getMimeType(),
248                response.getEncoding(),
249                response.getData());
250    }
251
252    /**
253     * @see AwContentsClient#shouldIgnoreNavigation(java.lang.String)
254     */
255    @Override
256    public boolean shouldIgnoreNavigation(String url) {
257      return mWebViewClient.shouldOverrideUrlLoading(mWebView, url);
258    }
259
260    /**
261     * @see AwContentsClient#onUnhandledKeyEvent(android.view.KeyEvent)
262     */
263    @Override
264    public void onUnhandledKeyEvent(KeyEvent event) {
265        mWebViewClient.onUnhandledKeyEvent(mWebView, event);
266    }
267
268    /**
269     * @see AwContentsClient#onConsoleMessage(android.webkit.ConsoleMessage)
270     */
271    @Override
272    public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
273        return mWebChromeClient.onConsoleMessage(consoleMessage);
274    }
275
276    /**
277     * @see AwContentsClient#onFindResultReceived(int,int,boolean)
278     */
279    @Override
280    public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
281            boolean isDoneCounting) {
282        if (mFindListener == null) return;
283        mFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches, isDoneCounting);
284    }
285
286    @Override
287    public void onLoadResource(String url) {
288        mWebViewClient.onLoadResource(mWebView, url);
289    }
290
291    @Override
292    public boolean onCreateWindow(boolean isDialog, boolean isUserGesture) {
293        Message m = mUiThreadHandler.obtainMessage(
294                NEW_WEBVIEW_CREATED, mWebView.new WebViewTransport());
295        return mWebChromeClient.onCreateWindow(mWebView, isDialog, isUserGesture, m);
296    }
297
298    /**
299     * @see AwContentsClient#onCloseWindow()
300     */
301    /* @Override */
302    public void onCloseWindow() {
303        mWebChromeClient.onCloseWindow(mWebView);
304    }
305
306    /**
307     * @see AwContentsClient#onRequestFocus()
308     */
309    /* @Override */
310    public void onRequestFocus() {
311        mWebChromeClient.onRequestFocus(mWebView);
312    }
313
314    //--------------------------------------------------------------------------------------------
315    //                        Trivial Chrome -> WebViewClient mappings.
316    //--------------------------------------------------------------------------------------------
317
318    /**
319     * @see ContentViewClient#onPageStarted(String)
320     */
321    @Override
322    public void onPageStarted(String url) {
323        //TODO: Can't get the favicon till b/6094807 is fixed.
324        mWebViewClient.onPageStarted(mWebView, url, null);
325    }
326
327    /**
328     * @see ContentViewClient#onPageFinished(String)
329     */
330    @Override
331    public void onPageFinished(String url) {
332        mWebViewClient.onPageFinished(mWebView, url);
333
334        // HACK: Fake a picture listener update, to allow CTS tests to progress.
335        // TODO: Remove when we have real picture listener updates implemented.
336        if (mPictureListener != null) {
337            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
338                @Override
339                public void run() {
340                    UnimplementedWebViewApi.invoke();
341                    if (mPictureListener != null) {
342                        mPictureListener.onNewPicture(mWebView, new Picture());
343                    }
344                }
345            }, 100);
346        }
347    }
348
349    /**
350     * @see ContentViewClient#onReceivedError(int,String,String)
351     */
352    @Override
353    public void onReceivedError(int errorCode, String description, String failingUrl) {
354        mWebViewClient.onReceivedError(mWebView, errorCode, description, failingUrl);
355    }
356
357    /**
358     * @see ContentViewClient#onUpdateTitle(String)
359     */
360    @Override
361    public void onUpdateTitle(String title) {
362        mWebChromeClient.onReceivedTitle(mWebView, title);
363    }
364
365
366    /**
367     * @see ContentViewClient#shouldOverrideKeyEvent(KeyEvent)
368     */
369    @Override
370    public boolean shouldOverrideKeyEvent(KeyEvent event) {
371        // TODO(joth): The expression here is a workaround for http://b/7697782 :-
372        // 1. The check for system key should be made in AwContents or ContentViewCore,
373        //    before shouldOverrideKeyEvent() is called at all.
374        // 2. shouldOverrideKeyEvent() should be called in onKeyDown/onKeyUp, not from
375        //    dispatchKeyEvent().
376        return event.isSystem() ||
377            mWebViewClient.shouldOverrideKeyEvent(mWebView, event);
378    }
379
380
381    //--------------------------------------------------------------------------------------------
382    //                 More complicated mappings (including behavior choices)
383    //--------------------------------------------------------------------------------------------
384
385    /**
386     * @see ContentViewClient#onTabCrash()
387     */
388    @Override
389    public void onTabCrash() {
390        // The WebViewClassic implementation used a single process, so any crash would
391        // cause the application to terminate.  WebViewChromium should have the same
392        // behavior as long as we run the renderer in-process. This needs to be revisited
393        // if we change that decision.
394        Log.e(TAG, "Renderer crash reported.");
395        mWebChromeClient.onCloseWindow(mWebView);
396    }
397
398    //--------------------------------------------------------------------------------------------
399    //                                     The TODO section
400    //--------------------------------------------------------------------------------------------
401
402
403    /**
404     * @see ContentViewClient#onImeEvent()
405     */
406    @Override
407    public void onImeEvent() {
408    }
409
410    /**
411     * @see ContentViewClient#onEvaluateJavaScriptResult(int,String)
412     */
413    @Override
414    public void onEvaluateJavaScriptResult(int id, String jsonResult) {
415    }
416
417    /**
418     * @see ContentViewClient#onStartContentIntent(Context, String)
419     * Callback when detecting a click on a content link.
420     */
421    @Override
422    public void onStartContentIntent(Context context, String contentUrl) {
423        mWebViewClient.shouldOverrideUrlLoading(mWebView, contentUrl);
424    }
425
426    private static class SimpleJsResultReceiver implements JsResult.ResultReceiver {
427        private JsResultReceiver mChromeResultReceiver;
428
429        public SimpleJsResultReceiver(JsResultReceiver receiver) {
430            mChromeResultReceiver = receiver;
431        }
432
433        @Override
434        public void onJsResultComplete(JsResult result) {
435            if (result.getResult()) {
436                mChromeResultReceiver.confirm();
437            } else {
438                mChromeResultReceiver.cancel();
439            }
440        }
441    }
442
443    private static class JsPromptResultReceiverAdapter implements JsResult.ResultReceiver {
444        private JsPromptResultReceiver mChromeResultReceiver;
445        private JsPromptResult mPromptResult;
446
447        public JsPromptResultReceiverAdapter(JsPromptResultReceiver receiver) {
448            mChromeResultReceiver = receiver;
449            // We hold onto the JsPromptResult here, just to avoid the need to downcast
450            // in onJsResultComplete.
451            mPromptResult = new JsPromptResult(this);
452        }
453
454        public JsPromptResult getPromptResult() {
455            return mPromptResult;
456        }
457
458        @Override
459        public void onJsResultComplete(JsResult result) {
460            if (result != mPromptResult) throw new RuntimeException("incorrect JsResult instance");
461            if (mPromptResult.getResult()) {
462                mChromeResultReceiver.confirm(mPromptResult.getStringResult());
463            } else {
464                mChromeResultReceiver.cancel();
465            }
466        }
467    }
468
469    @Override
470    public void handleJsAlert(String url, String message, JsResultReceiver receiver) {
471        JsResult res = new JsResult(new SimpleJsResultReceiver(receiver));
472        mWebChromeClient.onJsAlert(mWebView, url, message, res);
473        // TODO: Handle the case of the client returning false;
474    }
475
476    @Override
477    public void handleJsBeforeUnload(String url, String message, JsResultReceiver receiver) {
478        JsResult res = new JsResult(new SimpleJsResultReceiver(receiver));
479        mWebChromeClient.onJsBeforeUnload(mWebView, url, message, res);
480        // TODO: Handle the case of the client returning false;
481    }
482
483    @Override
484    public void handleJsConfirm(String url, String message, JsResultReceiver receiver) {
485        JsResult res = new JsResult(new SimpleJsResultReceiver(receiver));
486        mWebChromeClient.onJsConfirm(mWebView, url, message, res);
487        // TODO: Handle the case of the client returning false;
488    }
489
490    @Override
491    public void handleJsPrompt(String url, String message, String defaultValue,
492            JsPromptResultReceiver receiver) {
493        JsPromptResult res = new JsPromptResultReceiverAdapter(receiver).getPromptResult();
494        mWebChromeClient.onJsPrompt(mWebView, url, message, defaultValue, res);
495        // TODO: Handle the case of the client returning false;
496    }
497
498    @Override
499    public void onReceivedHttpAuthRequest(AwHttpAuthHandler handler, String host, String realm) {
500        mWebViewClient.onReceivedHttpAuthRequest(mWebView,
501                new AwHttpAuthHandlerAdapter(handler), host, realm);
502    }
503
504    @Override
505    public void onFormResubmission(Message dontResend, Message resend) {
506        mWebViewClient.onFormResubmission(mWebView, dontResend, resend);
507    }
508
509    @Override
510    public void onDownloadStart(String url,
511                                String userAgent,
512                                String contentDisposition,
513                                String mimeType,
514                                long contentLength) {
515        if (mDownloadListener != null) {
516            mDownloadListener.onDownloadStart(url,
517                                              userAgent,
518                                              contentDisposition,
519                                              mimeType,
520                                              contentLength);
521        }
522    }
523
524    @Override
525    public void onScaleChanged(float oldScale, float newScale) {
526        mWebViewClient.onScaleChanged(mWebView, oldScale, newScale);
527    }
528
529    private static class AwHttpAuthHandlerAdapter extends android.webkit.HttpAuthHandler {
530        private AwHttpAuthHandler mAwHandler;
531
532        public AwHttpAuthHandlerAdapter(AwHttpAuthHandler awHandler) {
533            mAwHandler = awHandler;
534        }
535
536        @Override
537        public void proceed(String username, String password) {
538            if (username == null) {
539                username = "";
540            }
541
542            if (password == null) {
543                password = "";
544            }
545            mAwHandler.proceed(username, password);
546        }
547
548        @Override
549        public void cancel() {
550            mAwHandler.cancel();
551        }
552
553        @Override
554        public boolean useHttpAuthUsernamePassword() {
555            return mAwHandler.isFirstAttempt();
556        }
557    }
558}
559