WebViewDelegate.java revision e66471103abfc8b9142d895bac93dade67f558c3
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
19ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liuimport android.annotation.NonNull;
20ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liuimport android.annotation.Nullable;
21451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Sollaimport android.annotation.SystemApi;
2294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.app.ActivityThread;
2394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.app.Application;
2494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.content.Context;
2594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.content.res.Resources;
2694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.graphics.Canvas;
2794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.os.SystemProperties;
2894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.os.Trace;
2994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.util.SparseArray;
30f6829a0a618b4523619ec53c996b04d67e3186b9Chris Craikimport android.view.DisplayListCanvas;
3194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.view.View;
3294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.view.ViewRootImpl;
3394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
3494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla/**
3594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Delegate used by the WebView provider implementation to access
3694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * the required framework functionality needed to implement a {@link WebView}.
3794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla *
3894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * @hide
3994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */
40451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Solla@SystemApi
4194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollapublic final class WebViewDelegate {
4294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
4394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /* package */ WebViewDelegate() { }
4494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
4594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
4694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Listener that gets notified whenever tracing has been enabled/disabled.
4794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
4894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public interface OnTraceEnabledChangeListener {
4994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        void onTraceEnabledChange(boolean enabled);
5094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
5194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
5294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
5394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Register a callback to be invoked when tracing for the WebView component has been
5494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * enabled/disabled.
5594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
5694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public void setOnTraceEnabledChangeListener(final OnTraceEnabledChangeListener listener) {
5794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        SystemProperties.addChangeCallback(new Runnable() {
5894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            @Override
5994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            public void run() {
6094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla                listener.onTraceEnabledChange(isTraceTagEnabled());
6194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            }
6294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        });
6394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
6494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
6594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
6694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns true if the WebView trace tag is enabled and false otherwise.
6794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
6894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public boolean isTraceTagEnabled() {
6994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW);
7094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
7194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
7294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
7394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns true if the draw GL functor can be invoked (see {@link #invokeDrawGlFunctor})
7494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * and false otherwise.
7594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
7694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public boolean canInvokeDrawGlFunctor(View containerView) {
7794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        ViewRootImpl viewRootImpl = containerView.getViewRootImpl();
7894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla         // viewRootImpl can be null during teardown when window is leaked.
7994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        return viewRootImpl != null;
8094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
8194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
8294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
8394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Invokes the draw GL functor. If waitForCompletion is false the functor
8494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * may be invoked asynchronously.
8594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *
8694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * @param nativeDrawGLFunctor the pointer to the native functor that implements
8794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *        system/core/include/utils/Functor.h
8894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
8994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor,
9094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            boolean waitForCompletion) {
9144b49f070aafe8ad44efae87341121cce49ff11cJohn Reck        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) {
105f6829a0a618b4523619ec53c996b04d67e3186b9Chris Craik        if (!(canvas instanceof DisplayListCanvas)) {
10694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas.
10794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            throw new IllegalArgumentException(canvas.getClass().getName()
108f6829a0a618b4523619ec53c996b04d67e3186b9Chris Craik                    + " is not a DisplayList canvas");
10994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        }
110e66471103abfc8b9142d895bac93dade67f558c3Bo Liu        ((DisplayListCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, null);
111e66471103abfc8b9142d895bac93dade67f558c3Bo Liu    }
112e66471103abfc8b9142d895bac93dade67f558c3Bo Liu
113e66471103abfc8b9142d895bac93dade67f558c3Bo Liu    /**
114e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     * Calls the function specified with the nativeDrawGLFunctor functor pointer. This
115e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     * functionality is used by the WebView for calling into their renderer from the
116e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     * framework display lists.
117e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     *
118e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()})
119e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     * @param nativeDrawGLFunctor the pointer to the native functor that implements
120e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     *        system/core/include/utils/Functor.h
121e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     * @param releasedRunnable Called when this nativeDrawGLFunctor is no longer referenced by this
122e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     *        canvas, so is safe to be destroyed.
123e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     * @throws IllegalArgumentException if the canvas is not hardware accelerated
124e66471103abfc8b9142d895bac93dade67f558c3Bo Liu     */
125e66471103abfc8b9142d895bac93dade67f558c3Bo Liu    public void callDrawGlFunction(@NonNull Canvas canvas, long nativeDrawGLFunctor,
126e66471103abfc8b9142d895bac93dade67f558c3Bo Liu            @Nullable Runnable releasedRunnable) {
127e66471103abfc8b9142d895bac93dade67f558c3Bo Liu        if (!(canvas instanceof DisplayListCanvas)) {
128e66471103abfc8b9142d895bac93dade67f558c3Bo Liu            // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas.
129e66471103abfc8b9142d895bac93dade67f558c3Bo Liu            throw new IllegalArgumentException(canvas.getClass().getName()
130e66471103abfc8b9142d895bac93dade67f558c3Bo Liu                    + " is not a DisplayList canvas");
131e66471103abfc8b9142d895bac93dade67f558c3Bo Liu        }
132e66471103abfc8b9142d895bac93dade67f558c3Bo Liu        ((DisplayListCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, releasedRunnable);
13394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
13494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
13594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
136ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu     * Set the Runnable callback the DrawGlFunction functor is detached and free to be destroyed.
137ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu     * This will replace the previous callback, if any.
138ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu     *
139ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu     * @param view The view to set the callback. Should be the view where onDraw inserted
140ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu     *        DrawGLFunctor.
141ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu     * @param callback The new callback to set on the view.
142ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu     * @throws IllegalArgumentException if view is null.
143ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu     * @return The previous callback on this view.
144ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu     */
145ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu    public Runnable setDrawGlFunctionDetachedCallback(
146ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu        @NonNull View view, @Nullable Runnable callback) {
147ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu        if (view == null) {
148ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu            throw new IllegalArgumentException("View cannot be null");
149ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu        }
150ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu        return view.setRenderNodeDetachedCallback(callback);
151ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu    }
152ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu
153ee34ef1633592e31d4ea99c3a41492a82a970c11Bo Liu    /**
15494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Detaches the draw GL functor.
15594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *
15694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * @param nativeDrawGLFunctor the pointer to the native functor that implements
15794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     *        system/core/include/utils/Functor.h
15894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
15994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public void detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor) {
16094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        ViewRootImpl viewRootImpl = containerView.getViewRootImpl();
16194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        if (nativeDrawGLFunctor != 0 && viewRootImpl != null) {
16294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            viewRootImpl.detachFunctor(nativeDrawGLFunctor);
16394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        }
16494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
16594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
16694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
16794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns the package id of the given {@code packageName}.
16894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
16994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public int getPackageId(Resources resources, String packageName) {
17094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        SparseArray<String> packageIdentifiers =
17194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla                resources.getAssets().getAssignedPackageIdentifiers();
17294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        for (int i = 0; i < packageIdentifiers.size(); i++) {
17394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            final String name = packageIdentifiers.valueAt(i);
17494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
17594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            if (packageName.equals(name)) {
17694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla                return packageIdentifiers.keyAt(i);
17794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla            }
17894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        }
17994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        throw new RuntimeException("Package not found: " + packageName);
18094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
18194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
18294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
18394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns the application which is embedding the WebView.
18494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
18594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public Application getApplication() {
18694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla        return ActivityThread.currentApplication();
18794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
18894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
18994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
19094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     * Returns the error string for the given {@code errorCode}.
19194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
19294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public String getErrorString(Context context, int errorCode) {
193f1a9b1bc249161fe1a9b0d85d4ed31153e4421c1Narayan Kamath        return LegacyErrorStrings.getString(errorCode, context);
19494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
19594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla
19694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    /**
197f6829a0a618b4523619ec53c996b04d67e3186b9Chris Craik     * Adds the WebView asset path to {@link android.content.res.AssetManager}.
19894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla     */
19994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    public void addWebViewAssetPath(Context context) {
200eb1368ba02ed23fb0f5101e0697a88cd8f420a4cTao Bai        context.getAssets().addAssetPathAsSharedLibrary(
20194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla                WebViewFactory.getLoadedPackageInfo().applicationInfo.sourceDir);
20294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla    }
20394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla}
204