blob: ef69b6333cce0e29f861ff060c238823d32c775b [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;
Mathew Inwood62d83fb2018-08-16 19:09:48 +010022import android.annotation.UnsupportedAppUsage;
Ignacio Solla94ef7892014-10-29 12:47:46 +000023import android.app.ActivityThread;
24import android.app.Application;
Adam Lesinski25f48882016-06-14 11:05:23 -070025import android.app.ResourcesManager;
Ignacio Solla94ef7892014-10-29 12:47:46 +000026import android.content.Context;
Adam Lesinski25f48882016-06-14 11:05:23 -070027import android.content.pm.ApplicationInfo;
Ignacio Solla94ef7892014-10-29 12:47:46 +000028import android.content.res.Resources;
29import android.graphics.Canvas;
John Reck32f140aa62018-10-04 15:08:24 -070030import android.graphics.RecordingCanvas;
Torne (Richard Coles)b26428b2017-01-23 15:09:55 +000031import android.os.RemoteException;
Ignacio Solla94ef7892014-10-29 12:47:46 +000032import android.os.SystemProperties;
33import android.os.Trace;
34import android.util.SparseArray;
Ignacio Solla94ef7892014-10-29 12:47:46 +000035import android.view.View;
36import android.view.ViewRootImpl;
37
Adam Lesinski25f48882016-06-14 11:05:23 -070038import com.android.internal.util.ArrayUtils;
39
Ignacio Solla94ef7892014-10-29 12:47:46 +000040/**
41 * Delegate used by the WebView provider implementation to access
42 * the required framework functionality needed to implement a {@link WebView}.
43 *
44 * @hide
45 */
Ignacio Solla451e3382014-11-10 10:35:54 +000046@SystemApi
Ignacio Solla94ef7892014-10-29 12:47:46 +000047public final class WebViewDelegate {
48
Mathew Inwood62d83fb2018-08-16 19:09:48 +010049 @UnsupportedAppUsage
Ignacio Solla94ef7892014-10-29 12:47:46 +000050 /* package */ WebViewDelegate() { }
51
52 /**
53 * Listener that gets notified whenever tracing has been enabled/disabled.
54 */
55 public interface OnTraceEnabledChangeListener {
56 void onTraceEnabledChange(boolean enabled);
57 }
58
59 /**
60 * Register a callback to be invoked when tracing for the WebView component has been
61 * enabled/disabled.
62 */
63 public void setOnTraceEnabledChangeListener(final OnTraceEnabledChangeListener listener) {
64 SystemProperties.addChangeCallback(new Runnable() {
65 @Override
66 public void run() {
67 listener.onTraceEnabledChange(isTraceTagEnabled());
68 }
69 });
70 }
71
72 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -070073 * Returns {@code true} if the WebView trace tag is enabled and {@code false} otherwise.
Ignacio Solla94ef7892014-10-29 12:47:46 +000074 */
75 public boolean isTraceTagEnabled() {
76 return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW);
77 }
78
79 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -070080 * Returns {@code true} if the draw GL functor can be invoked (see {@link #invokeDrawGlFunctor})
81 * and {@code false} otherwise.
Ignacio Solla94ef7892014-10-29 12:47:46 +000082 */
83 public boolean canInvokeDrawGlFunctor(View containerView) {
Bo Liu439266b2016-04-20 09:03:53 -070084 return true;
Ignacio Solla94ef7892014-10-29 12:47:46 +000085 }
86
87 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -070088 * Invokes the draw GL functor. If waitForCompletion is {@code false} the functor
Ignacio Solla94ef7892014-10-29 12:47:46 +000089 * may be invoked asynchronously.
90 *
91 * @param nativeDrawGLFunctor the pointer to the native functor that implements
92 * system/core/include/utils/Functor.h
93 */
94 public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor,
95 boolean waitForCompletion) {
John Reck44b49f02016-03-25 14:29:48 -070096 ViewRootImpl.invokeFunctor(nativeDrawGLFunctor, waitForCompletion);
Ignacio Solla94ef7892014-10-29 12:47:46 +000097 }
98
99 /**
100 * Calls the function specified with the nativeDrawGLFunctor functor pointer. This
101 * functionality is used by the WebView for calling into their renderer from the
102 * framework display lists.
103 *
104 * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()})
105 * @param nativeDrawGLFunctor the pointer to the native functor that implements
106 * system/core/include/utils/Functor.h
Ignacio Solla53f25692014-11-12 21:55:47 +0000107 * @throws IllegalArgumentException if the canvas is not hardware accelerated
Ignacio Solla94ef7892014-10-29 12:47:46 +0000108 */
109 public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) {
John Reck32f140aa62018-10-04 15:08:24 -0700110 if (!(canvas instanceof RecordingCanvas)) {
Ignacio Solla94ef7892014-10-29 12:47:46 +0000111 // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas.
112 throw new IllegalArgumentException(canvas.getClass().getName()
Chris Craikf6829a02015-03-10 10:28:59 -0700113 + " is not a DisplayList canvas");
Ignacio Solla94ef7892014-10-29 12:47:46 +0000114 }
John Reck32f140aa62018-10-04 15:08:24 -0700115 ((RecordingCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, null);
Bo Liue6647112016-04-15 16:28:18 -0700116 }
117
118 /**
119 * Calls the function specified with the nativeDrawGLFunctor functor pointer. This
120 * functionality is used by the WebView for calling into their renderer from the
121 * framework display lists.
122 *
123 * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()})
124 * @param nativeDrawGLFunctor the pointer to the native functor that implements
125 * system/core/include/utils/Functor.h
126 * @param releasedRunnable Called when this nativeDrawGLFunctor is no longer referenced by this
127 * canvas, so is safe to be destroyed.
128 * @throws IllegalArgumentException if the canvas is not hardware accelerated
129 */
130 public void callDrawGlFunction(@NonNull Canvas canvas, long nativeDrawGLFunctor,
131 @Nullable Runnable releasedRunnable) {
John Reck32f140aa62018-10-04 15:08:24 -0700132 if (!(canvas instanceof RecordingCanvas)) {
Bo Liue6647112016-04-15 16:28:18 -0700133 // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas.
134 throw new IllegalArgumentException(canvas.getClass().getName()
135 + " is not a DisplayList canvas");
136 }
John Reck32f140aa62018-10-04 15:08:24 -0700137 ((RecordingCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, releasedRunnable);
Ignacio Solla94ef7892014-10-29 12:47:46 +0000138 }
139
140 /**
Bo Liud6668e72018-12-14 19:37:41 -0800141 * Call webview draw functor. See API in draw_fn.h.
142 * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()}).
143 * @param functor created by AwDrawFn_CreateFunctor in draw_fn.h.
144 */
145 public void drawWebViewFunctor(@NonNull Canvas canvas, int functor) {
146 if (!(canvas instanceof RecordingCanvas)) {
147 // Canvas#isHardwareAccelerated() is only true for subclasses of RecordingCanvas.
148 throw new IllegalArgumentException(canvas.getClass().getName()
149 + " is not a RecordingCanvas canvas");
150 }
151 ((RecordingCanvas) canvas).drawWebViewFunctor(functor);
152 }
153
154 /**
Ignacio Solla94ef7892014-10-29 12:47:46 +0000155 * Detaches the draw GL functor.
156 *
157 * @param nativeDrawGLFunctor the pointer to the native functor that implements
158 * system/core/include/utils/Functor.h
159 */
160 public void detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor) {
161 ViewRootImpl viewRootImpl = containerView.getViewRootImpl();
162 if (nativeDrawGLFunctor != 0 && viewRootImpl != null) {
163 viewRootImpl.detachFunctor(nativeDrawGLFunctor);
164 }
165 }
166
167 /**
168 * Returns the package id of the given {@code packageName}.
169 */
170 public int getPackageId(Resources resources, String packageName) {
171 SparseArray<String> packageIdentifiers =
172 resources.getAssets().getAssignedPackageIdentifiers();
173 for (int i = 0; i < packageIdentifiers.size(); i++) {
174 final String name = packageIdentifiers.valueAt(i);
175
176 if (packageName.equals(name)) {
177 return packageIdentifiers.keyAt(i);
178 }
179 }
180 throw new RuntimeException("Package not found: " + packageName);
181 }
182
183 /**
184 * Returns the application which is embedding the WebView.
185 */
186 public Application getApplication() {
187 return ActivityThread.currentApplication();
188 }
189
190 /**
191 * Returns the error string for the given {@code errorCode}.
192 */
193 public String getErrorString(Context context, int errorCode) {
Narayan Kamathf1a9b1b2014-11-27 17:20:21 +0000194 return LegacyErrorStrings.getString(errorCode, context);
Ignacio Solla94ef7892014-10-29 12:47:46 +0000195 }
196
197 /**
Chris Craikf6829a02015-03-10 10:28:59 -0700198 * Adds the WebView asset path to {@link android.content.res.AssetManager}.
Ignacio Solla94ef7892014-10-29 12:47:46 +0000199 */
200 public void addWebViewAssetPath(Context context) {
Adam Lesinski25f48882016-06-14 11:05:23 -0700201 final String newAssetPath = WebViewFactory.getLoadedPackageInfo().applicationInfo.sourceDir;
202
203 final ApplicationInfo appInfo = context.getApplicationInfo();
204 final String[] libs = appInfo.sharedLibraryFiles;
205 if (!ArrayUtils.contains(libs, newAssetPath)) {
206 // Build the new library asset path list.
207 final int newLibAssetsCount = 1 + (libs != null ? libs.length : 0);
208 final String[] newLibAssets = new String[newLibAssetsCount];
209 if (libs != null) {
210 System.arraycopy(libs, 0, newLibAssets, 0, libs.length);
211 }
212 newLibAssets[newLibAssetsCount - 1] = newAssetPath;
213
214 // Update the ApplicationInfo object with the new list.
215 // We know this will persist and future Resources created via ResourcesManager
216 // will include the shared library because this ApplicationInfo comes from the
217 // underlying LoadedApk in ContextImpl, which does not change during the life of the
218 // application.
219 appInfo.sharedLibraryFiles = newLibAssets;
220
221 // Update existing Resources with the WebView library.
222 ResourcesManager.getInstance().appendLibAssetForMainAssetPath(
223 appInfo.getBaseResourcePath(), newAssetPath);
224 }
Ignacio Solla94ef7892014-10-29 12:47:46 +0000225 }
Torne (Richard Coles)b26428b2017-01-23 15:09:55 +0000226
227 /**
228 * Returns whether WebView should run in multiprocess mode.
229 */
230 public boolean isMultiProcessEnabled() {
231 try {
232 return WebViewFactory.getUpdateService().isMultiProcessEnabled();
233 } catch (RemoteException e) {
234 throw e.rethrowFromSystemServer();
235 }
236 }
Torne (Richard Coles)12f64da2017-10-24 14:59:07 -0400237
238 /**
239 * Returns the data directory suffix to use, or null for none.
240 */
241 public String getDataDirectorySuffix() {
242 return WebViewFactory.getDataDirectorySuffix();
243 }
Ignacio Solla94ef7892014-10-29 12:47:46 +0000244}