Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.webkit; |
| 18 | |
Bo Liu | ee34ef1 | 2016-04-01 10:29:05 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Mathew Inwood | 62d83fb | 2018-08-16 19:09:48 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 23 | import android.app.ActivityThread; |
| 24 | import android.app.Application; |
Adam Lesinski | 25f4888 | 2016-06-14 11:05:23 -0700 | [diff] [blame] | 25 | import android.app.ResourcesManager; |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 26 | import android.content.Context; |
Adam Lesinski | 25f4888 | 2016-06-14 11:05:23 -0700 | [diff] [blame] | 27 | import android.content.pm.ApplicationInfo; |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 28 | import android.content.res.Resources; |
| 29 | import android.graphics.Canvas; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 30 | import android.graphics.RecordingCanvas; |
Torne (Richard Coles) | b26428b | 2017-01-23 15:09:55 +0000 | [diff] [blame] | 31 | import android.os.RemoteException; |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 32 | import android.os.SystemProperties; |
| 33 | import android.os.Trace; |
| 34 | import android.util.SparseArray; |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 35 | import android.view.View; |
| 36 | import android.view.ViewRootImpl; |
| 37 | |
Adam Lesinski | 25f4888 | 2016-06-14 11:05:23 -0700 | [diff] [blame] | 38 | import com.android.internal.util.ArrayUtils; |
| 39 | |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 40 | /** |
| 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 Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 46 | @SystemApi |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 47 | public final class WebViewDelegate { |
| 48 | |
Mathew Inwood | 62d83fb | 2018-08-16 19:09:48 +0100 | [diff] [blame] | 49 | @UnsupportedAppUsage |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 50 | /* 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 Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 73 | * Returns {@code true} if the WebView trace tag is enabled and {@code false} otherwise. |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 74 | */ |
| 75 | public boolean isTraceTagEnabled() { |
| 76 | return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW); |
| 77 | } |
| 78 | |
| 79 | /** |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 80 | * Returns {@code true} if the draw GL functor can be invoked (see {@link #invokeDrawGlFunctor}) |
| 81 | * and {@code false} otherwise. |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 82 | */ |
| 83 | public boolean canInvokeDrawGlFunctor(View containerView) { |
Bo Liu | 439266b | 2016-04-20 09:03:53 -0700 | [diff] [blame] | 84 | return true; |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 88 | * Invokes the draw GL functor. If waitForCompletion is {@code false} the functor |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 89 | * 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 Reck | 44b49f0 | 2016-03-25 14:29:48 -0700 | [diff] [blame] | 96 | ViewRootImpl.invokeFunctor(nativeDrawGLFunctor, waitForCompletion); |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 97 | } |
| 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 Solla | 53f2569 | 2014-11-12 21:55:47 +0000 | [diff] [blame] | 107 | * @throws IllegalArgumentException if the canvas is not hardware accelerated |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 108 | */ |
| 109 | public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) { |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 110 | if (!(canvas instanceof RecordingCanvas)) { |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 111 | // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas. |
| 112 | throw new IllegalArgumentException(canvas.getClass().getName() |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 113 | + " is not a DisplayList canvas"); |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 114 | } |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 115 | ((RecordingCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, null); |
Bo Liu | e664711 | 2016-04-15 16:28:18 -0700 | [diff] [blame] | 116 | } |
| 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 Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 132 | if (!(canvas instanceof RecordingCanvas)) { |
Bo Liu | e664711 | 2016-04-15 16:28:18 -0700 | [diff] [blame] | 133 | // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas. |
| 134 | throw new IllegalArgumentException(canvas.getClass().getName() |
| 135 | + " is not a DisplayList canvas"); |
| 136 | } |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 137 | ((RecordingCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, releasedRunnable); |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 141 | * 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 Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 155 | * 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 Kamath | f1a9b1b | 2014-11-27 17:20:21 +0000 | [diff] [blame] | 194 | return LegacyErrorStrings.getString(errorCode, context); |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 198 | * Adds the WebView asset path to {@link android.content.res.AssetManager}. |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 199 | */ |
| 200 | public void addWebViewAssetPath(Context context) { |
Adam Lesinski | 25f4888 | 2016-06-14 11:05:23 -0700 | [diff] [blame] | 201 | 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 Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 225 | } |
Torne (Richard Coles) | b26428b | 2017-01-23 15:09:55 +0000 | [diff] [blame] | 226 | |
| 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) | 12f64da | 2017-10-24 14:59:07 -0400 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * Returns the data directory suffix to use, or null for none. |
| 240 | */ |
| 241 | public String getDataDirectorySuffix() { |
| 242 | return WebViewFactory.getDataDirectorySuffix(); |
| 243 | } |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 244 | } |