WebViewDelegate.java revision 451e338c51e8c45efc0d21536dfae6f78f6d5e06
194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla/*
294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Copyright (C) 2014 The Android Open Source Project
394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla *
494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Licensed under the Apache License, Version 2.0 (the "License");
594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * you may not use this file except in compliance with the License.
694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * You may obtain a copy of the License at
794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla *
894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla *      http://www.apache.org/licenses/LICENSE-2.0
994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla *
1094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Unless required by applicable law or agreed to in writing, software
1194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * distributed under the License is distributed on an "AS IS" BASIS,
1294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * See the License for the specific language governing permissions and
1494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * limitations under the License.
1594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */
1694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
1794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollapackage android.webkit;
1894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
19451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Sollaimport android.annotation.SystemApi;
2094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.app.ActivityThread;
2194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.app.Application;
2294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.content.Context;
2394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.content.res.Resources;
2494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.graphics.Canvas;
2594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.net.http.ErrorStrings;
2694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.os.SystemProperties;
2794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.os.Trace;
2894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.util.SparseArray;
2994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.view.HardwareCanvas;
3094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.view.View;
3194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.view.ViewRootImpl;
3294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
3394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla/**
3494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Delegate used by the WebView provider implementation to access
3594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * the required framework functionality needed to implement a {@link WebView}.
3694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla *
3794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * @hide
3894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */
39451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Solla@SystemApi
4094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollapublic final class WebViewDelegate {
4194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
4294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /* package */ WebViewDelegate() { }
4394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
4494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
4594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Listener that gets notified whenever tracing has been enabled/disabled.
4694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
4794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public interface OnTraceEnabledChangeListener {
4894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        void onTraceEnabledChange(boolean enabled);
4994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
5094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
5194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
5294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Register a callback to be invoked when tracing for the WebView component has been
5394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * enabled/disabled.
5494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
5594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public void setOnTraceEnabledChangeListener(final OnTraceEnabledChangeListener listener) {
5694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        SystemProperties.addChangeCallback(new Runnable() {
5794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            @Override
5894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            public void run() {
5994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla                listener.onTraceEnabledChange(isTraceTagEnabled());
6094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            }
6194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        });
6294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
6394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
6494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
6594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns true if the WebView trace tag is enabled and false otherwise.
6694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
6794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public boolean isTraceTagEnabled() {
6894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW);
6994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
7094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
7194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
7294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns true if the draw GL functor can be invoked (see {@link #invokeDrawGlFunctor})
7394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * and false otherwise.
7494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
7594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public boolean canInvokeDrawGlFunctor(View containerView) {
7694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        ViewRootImpl viewRootImpl = containerView.getViewRootImpl();
7794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla         // viewRootImpl can be null during teardown when window is leaked.
7894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        return viewRootImpl != null;
7994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
8094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
8194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
8294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Invokes the draw GL functor. If waitForCompletion is false the functor
8394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * may be invoked asynchronously.
8494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *
8594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * @param nativeDrawGLFunctor the pointer to the native functor that implements
8694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *        system/core/include/utils/Functor.h
8794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
8894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor,
8994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            boolean waitForCompletion) {
9094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        ViewRootImpl viewRootImpl = containerView.getViewRootImpl();
9194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        viewRootImpl.invokeFunctor(nativeDrawGLFunctor, waitForCompletion);
9294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
9394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
9494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
9594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Calls the function specified with the nativeDrawGLFunctor functor pointer. This
9694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * functionality is used by the WebView for calling into their renderer from the
9794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * framework display lists.
9894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *
9994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()})
10094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * @param nativeDrawGLFunctor the pointer to the native functor that implements
10194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *        system/core/include/utils/Functor.h
10253f256948724b8d4009d8a8a47da96a06402f9a2Ignacio Solla     * @throws IllegalArgumentException if the canvas is not hardware accelerated
10394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
10494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) {
10594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        if (!(canvas instanceof HardwareCanvas)) {
10694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas.
10794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            throw new IllegalArgumentException(canvas.getClass().getName()
10894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla                    + " is not hardware accelerated");
10994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        }
11094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        ((HardwareCanvas) canvas).callDrawGLFunction(nativeDrawGLFunctor);
11194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
11294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
11394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
11494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Detaches the draw GL functor.
11594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *
11694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * @param nativeDrawGLFunctor the pointer to the native functor that implements
11794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *        system/core/include/utils/Functor.h
11894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
11994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public void detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor) {
12094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        ViewRootImpl viewRootImpl = containerView.getViewRootImpl();
12194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        if (nativeDrawGLFunctor != 0 && viewRootImpl != null) {
12294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            viewRootImpl.detachFunctor(nativeDrawGLFunctor);
12394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        }
12494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
12594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
12694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
12794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns the package id of the given {@code packageName}.
12894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
12994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public int getPackageId(Resources resources, String packageName) {
13094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        SparseArray<String> packageIdentifiers =
13194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla                resources.getAssets().getAssignedPackageIdentifiers();
13294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        for (int i = 0; i < packageIdentifiers.size(); i++) {
13394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            final String name = packageIdentifiers.valueAt(i);
13494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
13594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            if (packageName.equals(name)) {
13694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla                return packageIdentifiers.keyAt(i);
13794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            }
13894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        }
13994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        throw new RuntimeException("Package not found: " + packageName);
14094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
14194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
14294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
14394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns the application which is embedding the WebView.
14494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
14594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public Application getApplication() {
14694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        return ActivityThread.currentApplication();
14794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
14894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
14994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
15094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns the error string for the given {@code errorCode}.
15194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
15294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public String getErrorString(Context context, int errorCode) {
15394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        return ErrorStrings.getString(errorCode, context);
15494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
15594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
15694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
15794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Adds the WebView asset path to {@link AssetManager}.
15894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
15994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public void addWebViewAssetPath(Context context) {
16094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        context.getAssets().addAssetPath(
16194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla                WebViewFactory.getLoadedPackageInfo().applicationInfo.sourceDir);
16294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
16394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla}
164