blob: 92d0d71413701fe3c00e16f03d06e6b2b5728710 [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;
Adam Lesinski25f48882016-06-14 11:05:23 -070024import android.app.ResourcesManager;
Ignacio Solla94ef7892014-10-29 12:47:46 +000025import android.content.Context;
Adam Lesinski25f48882016-06-14 11:05:23 -070026import android.content.pm.ApplicationInfo;
Ignacio Solla94ef7892014-10-29 12:47:46 +000027import android.content.res.Resources;
28import android.graphics.Canvas;
Torne (Richard Coles)b26428b2017-01-23 15:09:55 +000029import android.os.RemoteException;
Ignacio Solla94ef7892014-10-29 12:47:46 +000030import android.os.SystemProperties;
31import android.os.Trace;
32import android.util.SparseArray;
Chris Craikf6829a02015-03-10 10:28:59 -070033import android.view.DisplayListCanvas;
Ignacio Solla94ef7892014-10-29 12:47:46 +000034import android.view.View;
35import android.view.ViewRootImpl;
36
Adam Lesinski25f48882016-06-14 11:05:23 -070037import com.android.internal.util.ArrayUtils;
38
Ignacio Solla94ef7892014-10-29 12:47:46 +000039/**
40 * Delegate used by the WebView provider implementation to access
41 * the required framework functionality needed to implement a {@link WebView}.
42 *
43 * @hide
44 */
Ignacio Solla451e3382014-11-10 10:35:54 +000045@SystemApi
Ignacio Solla94ef7892014-10-29 12:47:46 +000046public final class WebViewDelegate {
47
48 /* package */ WebViewDelegate() { }
49
50 /**
51 * Listener that gets notified whenever tracing has been enabled/disabled.
52 */
53 public interface OnTraceEnabledChangeListener {
54 void onTraceEnabledChange(boolean enabled);
55 }
56
57 /**
58 * Register a callback to be invoked when tracing for the WebView component has been
59 * enabled/disabled.
60 */
61 public void setOnTraceEnabledChangeListener(final OnTraceEnabledChangeListener listener) {
62 SystemProperties.addChangeCallback(new Runnable() {
63 @Override
64 public void run() {
65 listener.onTraceEnabledChange(isTraceTagEnabled());
66 }
67 });
68 }
69
70 /**
71 * Returns true if the WebView trace tag is enabled and false otherwise.
72 */
73 public boolean isTraceTagEnabled() {
74 return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW);
75 }
76
77 /**
78 * Returns true if the draw GL functor can be invoked (see {@link #invokeDrawGlFunctor})
79 * and false otherwise.
80 */
81 public boolean canInvokeDrawGlFunctor(View containerView) {
Bo Liu439266b2016-04-20 09:03:53 -070082 return true;
Ignacio Solla94ef7892014-10-29 12:47:46 +000083 }
84
85 /**
86 * Invokes the draw GL functor. If waitForCompletion is false the functor
87 * may be invoked asynchronously.
88 *
89 * @param nativeDrawGLFunctor the pointer to the native functor that implements
90 * system/core/include/utils/Functor.h
91 */
92 public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor,
93 boolean waitForCompletion) {
John Reck44b49f02016-03-25 14:29:48 -070094 ViewRootImpl.invokeFunctor(nativeDrawGLFunctor, waitForCompletion);
Ignacio Solla94ef7892014-10-29 12:47:46 +000095 }
96
97 /**
98 * Calls the function specified with the nativeDrawGLFunctor functor pointer. This
99 * functionality is used by the WebView for calling into their renderer from the
100 * framework display lists.
101 *
102 * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()})
103 * @param nativeDrawGLFunctor the pointer to the native functor that implements
104 * system/core/include/utils/Functor.h
Ignacio Solla53f25692014-11-12 21:55:47 +0000105 * @throws IllegalArgumentException if the canvas is not hardware accelerated
Ignacio Solla94ef7892014-10-29 12:47:46 +0000106 */
107 public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) {
Chris Craikf6829a02015-03-10 10:28:59 -0700108 if (!(canvas instanceof DisplayListCanvas)) {
Ignacio Solla94ef7892014-10-29 12:47:46 +0000109 // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas.
110 throw new IllegalArgumentException(canvas.getClass().getName()
Chris Craikf6829a02015-03-10 10:28:59 -0700111 + " is not a DisplayList canvas");
Ignacio Solla94ef7892014-10-29 12:47:46 +0000112 }
Bo Liue6647112016-04-15 16:28:18 -0700113 ((DisplayListCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, null);
114 }
115
116 /**
117 * Calls the function specified with the nativeDrawGLFunctor functor pointer. This
118 * functionality is used by the WebView for calling into their renderer from the
119 * framework display lists.
120 *
121 * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()})
122 * @param nativeDrawGLFunctor the pointer to the native functor that implements
123 * system/core/include/utils/Functor.h
124 * @param releasedRunnable Called when this nativeDrawGLFunctor is no longer referenced by this
125 * canvas, so is safe to be destroyed.
126 * @throws IllegalArgumentException if the canvas is not hardware accelerated
127 */
128 public void callDrawGlFunction(@NonNull Canvas canvas, long nativeDrawGLFunctor,
129 @Nullable Runnable releasedRunnable) {
130 if (!(canvas instanceof DisplayListCanvas)) {
131 // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas.
132 throw new IllegalArgumentException(canvas.getClass().getName()
133 + " is not a DisplayList canvas");
134 }
135 ((DisplayListCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, releasedRunnable);
Ignacio Solla94ef7892014-10-29 12:47:46 +0000136 }
137
138 /**
139 * Detaches the draw GL functor.
140 *
141 * @param nativeDrawGLFunctor the pointer to the native functor that implements
142 * system/core/include/utils/Functor.h
143 */
144 public void detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor) {
145 ViewRootImpl viewRootImpl = containerView.getViewRootImpl();
146 if (nativeDrawGLFunctor != 0 && viewRootImpl != null) {
147 viewRootImpl.detachFunctor(nativeDrawGLFunctor);
148 }
149 }
150
151 /**
152 * Returns the package id of the given {@code packageName}.
153 */
154 public int getPackageId(Resources resources, String packageName) {
155 SparseArray<String> packageIdentifiers =
156 resources.getAssets().getAssignedPackageIdentifiers();
157 for (int i = 0; i < packageIdentifiers.size(); i++) {
158 final String name = packageIdentifiers.valueAt(i);
159
160 if (packageName.equals(name)) {
161 return packageIdentifiers.keyAt(i);
162 }
163 }
164 throw new RuntimeException("Package not found: " + packageName);
165 }
166
167 /**
168 * Returns the application which is embedding the WebView.
169 */
170 public Application getApplication() {
171 return ActivityThread.currentApplication();
172 }
173
174 /**
175 * Returns the error string for the given {@code errorCode}.
176 */
177 public String getErrorString(Context context, int errorCode) {
Narayan Kamathf1a9b1b2014-11-27 17:20:21 +0000178 return LegacyErrorStrings.getString(errorCode, context);
Ignacio Solla94ef7892014-10-29 12:47:46 +0000179 }
180
181 /**
Chris Craikf6829a02015-03-10 10:28:59 -0700182 * Adds the WebView asset path to {@link android.content.res.AssetManager}.
Ignacio Solla94ef7892014-10-29 12:47:46 +0000183 */
184 public void addWebViewAssetPath(Context context) {
Adam Lesinski25f48882016-06-14 11:05:23 -0700185 final String newAssetPath = WebViewFactory.getLoadedPackageInfo().applicationInfo.sourceDir;
186
187 final ApplicationInfo appInfo = context.getApplicationInfo();
188 final String[] libs = appInfo.sharedLibraryFiles;
189 if (!ArrayUtils.contains(libs, newAssetPath)) {
190 // Build the new library asset path list.
191 final int newLibAssetsCount = 1 + (libs != null ? libs.length : 0);
192 final String[] newLibAssets = new String[newLibAssetsCount];
193 if (libs != null) {
194 System.arraycopy(libs, 0, newLibAssets, 0, libs.length);
195 }
196 newLibAssets[newLibAssetsCount - 1] = newAssetPath;
197
198 // Update the ApplicationInfo object with the new list.
199 // We know this will persist and future Resources created via ResourcesManager
200 // will include the shared library because this ApplicationInfo comes from the
201 // underlying LoadedApk in ContextImpl, which does not change during the life of the
202 // application.
203 appInfo.sharedLibraryFiles = newLibAssets;
204
205 // Update existing Resources with the WebView library.
206 ResourcesManager.getInstance().appendLibAssetForMainAssetPath(
207 appInfo.getBaseResourcePath(), newAssetPath);
208 }
Ignacio Solla94ef7892014-10-29 12:47:46 +0000209 }
Torne (Richard Coles)b26428b2017-01-23 15:09:55 +0000210
211 /**
212 * Returns whether WebView should run in multiprocess mode.
213 */
214 public boolean isMultiProcessEnabled() {
215 try {
216 return WebViewFactory.getUpdateService().isMultiProcessEnabled();
217 } catch (RemoteException e) {
218 throw e.rethrowFromSystemServer();
219 }
220 }
Ignacio Solla94ef7892014-10-29 12:47:46 +0000221}