blob: b6516c85d0949ed4fb03b54b342dc9661347ea02 [file] [log] [blame]
Ignacio Solla94ef7892014-10-29 12:47:46 +00001/*
2 * Copyright (C) 2014 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
Bo Liuee34ef12016-04-01 10:29:05 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Ignacio Solla451e3382014-11-10 10:35:54 +000021import android.annotation.SystemApi;
Ignacio Solla94ef7892014-10-29 12:47:46 +000022import android.app.ActivityThread;
23import android.app.Application;
24import android.content.Context;
25import android.content.res.Resources;
26import android.graphics.Canvas;
Ignacio Solla94ef7892014-10-29 12:47:46 +000027import android.os.SystemProperties;
28import android.os.Trace;
29import android.util.SparseArray;
Chris Craikf6829a02015-03-10 10:28:59 -070030import android.view.DisplayListCanvas;
Ignacio Solla94ef7892014-10-29 12:47:46 +000031import android.view.View;
32import android.view.ViewRootImpl;
33
34/**
35 * Delegate used by the WebView provider implementation to access
36 * the required framework functionality needed to implement a {@link WebView}.
37 *
38 * @hide
39 */
Ignacio Solla451e3382014-11-10 10:35:54 +000040@SystemApi
Ignacio Solla94ef7892014-10-29 12:47:46 +000041public final class WebViewDelegate {
42
43 /* package */ WebViewDelegate() { }
44
45 /**
46 * Listener that gets notified whenever tracing has been enabled/disabled.
47 */
48 public interface OnTraceEnabledChangeListener {
49 void onTraceEnabledChange(boolean enabled);
50 }
51
52 /**
53 * Register a callback to be invoked when tracing for the WebView component has been
54 * enabled/disabled.
55 */
56 public void setOnTraceEnabledChangeListener(final OnTraceEnabledChangeListener listener) {
57 SystemProperties.addChangeCallback(new Runnable() {
58 @Override
59 public void run() {
60 listener.onTraceEnabledChange(isTraceTagEnabled());
61 }
62 });
63 }
64
65 /**
66 * Returns true if the WebView trace tag is enabled and false otherwise.
67 */
68 public boolean isTraceTagEnabled() {
69 return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW);
70 }
71
72 /**
73 * Returns true if the draw GL functor can be invoked (see {@link #invokeDrawGlFunctor})
74 * and false otherwise.
75 */
76 public boolean canInvokeDrawGlFunctor(View containerView) {
77 ViewRootImpl viewRootImpl = containerView.getViewRootImpl();
78 // viewRootImpl can be null during teardown when window is leaked.
79 return viewRootImpl != null;
80 }
81
82 /**
83 * Invokes the draw GL functor. If waitForCompletion is false the functor
84 * may be invoked asynchronously.
85 *
86 * @param nativeDrawGLFunctor the pointer to the native functor that implements
87 * system/core/include/utils/Functor.h
88 */
89 public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor,
90 boolean waitForCompletion) {
John Reck44b49f02016-03-25 14:29:48 -070091 ViewRootImpl.invokeFunctor(nativeDrawGLFunctor, waitForCompletion);
Ignacio Solla94ef7892014-10-29 12:47:46 +000092 }
93
94 /**
95 * Calls the function specified with the nativeDrawGLFunctor functor pointer. This
96 * functionality is used by the WebView for calling into their renderer from the
97 * framework display lists.
98 *
99 * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()})
100 * @param nativeDrawGLFunctor the pointer to the native functor that implements
101 * system/core/include/utils/Functor.h
Ignacio Solla53f25692014-11-12 21:55:47 +0000102 * @throws IllegalArgumentException if the canvas is not hardware accelerated
Ignacio Solla94ef7892014-10-29 12:47:46 +0000103 */
104 public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) {
Chris Craikf6829a02015-03-10 10:28:59 -0700105 if (!(canvas instanceof DisplayListCanvas)) {
Ignacio Solla94ef7892014-10-29 12:47:46 +0000106 // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas.
107 throw new IllegalArgumentException(canvas.getClass().getName()
Chris Craikf6829a02015-03-10 10:28:59 -0700108 + " is not a DisplayList canvas");
Ignacio Solla94ef7892014-10-29 12:47:46 +0000109 }
Chris Craikf6829a02015-03-10 10:28:59 -0700110 ((DisplayListCanvas) canvas).callDrawGLFunction2(nativeDrawGLFunctor);
Ignacio Solla94ef7892014-10-29 12:47:46 +0000111 }
112
113 /**
Bo Liuee34ef12016-04-01 10:29:05 -0700114 * Set the Runnable callback the DrawGlFunction functor is detached and free to be destroyed.
115 * This will replace the previous callback, if any.
116 *
117 * @param view The view to set the callback. Should be the view where onDraw inserted
118 * DrawGLFunctor.
119 * @param callback The new callback to set on the view.
120 * @throws IllegalArgumentException if view is null.
121 * @return The previous callback on this view.
122 */
123 public Runnable setDrawGlFunctionDetachedCallback(
124 @NonNull View view, @Nullable Runnable callback) {
125 if (view == null) {
126 throw new IllegalArgumentException("View cannot be null");
127 }
128 return view.setRenderNodeDetachedCallback(callback);
129 }
130
131 /**
Ignacio Solla94ef7892014-10-29 12:47:46 +0000132 * Detaches the draw GL functor.
133 *
134 * @param nativeDrawGLFunctor the pointer to the native functor that implements
135 * system/core/include/utils/Functor.h
136 */
137 public void detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor) {
138 ViewRootImpl viewRootImpl = containerView.getViewRootImpl();
139 if (nativeDrawGLFunctor != 0 && viewRootImpl != null) {
140 viewRootImpl.detachFunctor(nativeDrawGLFunctor);
141 }
142 }
143
144 /**
145 * Returns the package id of the given {@code packageName}.
146 */
147 public int getPackageId(Resources resources, String packageName) {
148 SparseArray<String> packageIdentifiers =
149 resources.getAssets().getAssignedPackageIdentifiers();
150 for (int i = 0; i < packageIdentifiers.size(); i++) {
151 final String name = packageIdentifiers.valueAt(i);
152
153 if (packageName.equals(name)) {
154 return packageIdentifiers.keyAt(i);
155 }
156 }
157 throw new RuntimeException("Package not found: " + packageName);
158 }
159
160 /**
161 * Returns the application which is embedding the WebView.
162 */
163 public Application getApplication() {
164 return ActivityThread.currentApplication();
165 }
166
167 /**
168 * Returns the error string for the given {@code errorCode}.
169 */
170 public String getErrorString(Context context, int errorCode) {
Narayan Kamathf1a9b1b2014-11-27 17:20:21 +0000171 return LegacyErrorStrings.getString(errorCode, context);
Ignacio Solla94ef7892014-10-29 12:47:46 +0000172 }
173
174 /**
Chris Craikf6829a02015-03-10 10:28:59 -0700175 * Adds the WebView asset path to {@link android.content.res.AssetManager}.
Ignacio Solla94ef7892014-10-29 12:47:46 +0000176 */
177 public void addWebViewAssetPath(Context context) {
Tao Baieb1368b2016-02-23 09:01:28 -0800178 context.getAssets().addAssetPathAsSharedLibrary(
Ignacio Solla94ef7892014-10-29 12:47:46 +0000179 WebViewFactory.getLoadedPackageInfo().applicationInfo.sourceDir);
180 }
181}