WebViewDelegate.java revision d443f36957a2f8a6aa295bd88a5bf7886d56a064
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 17package android.webkit; 18 19import android.annotation.NonNull; 20import android.annotation.Nullable; 21import android.annotation.SystemApi; 22import android.app.ActivityThread; 23import android.app.Application; 24import android.content.Context; 25import android.content.res.Resources; 26import android.graphics.Canvas; 27import android.os.SystemProperties; 28import android.os.Trace; 29import android.util.SparseArray; 30import android.view.DisplayListCanvas; 31import 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 */ 40@SystemApi 41public 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 return true; 78 } 79 80 /** 81 * Invokes the draw GL functor. If waitForCompletion is false the functor 82 * may be invoked asynchronously. 83 * 84 * @param nativeDrawGLFunctor the pointer to the native functor that implements 85 * system/core/include/utils/Functor.h 86 */ 87 public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor, 88 boolean waitForCompletion) { 89 ViewRootImpl.invokeFunctor(nativeDrawGLFunctor, waitForCompletion); 90 } 91 92 /** 93 * Calls the function specified with the nativeDrawGLFunctor functor pointer. This 94 * functionality is used by the WebView for calling into their renderer from the 95 * framework display lists. 96 * 97 * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()}) 98 * @param nativeDrawGLFunctor the pointer to the native functor that implements 99 * system/core/include/utils/Functor.h 100 * @throws IllegalArgumentException if the canvas is not hardware accelerated 101 */ 102 public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) { 103 if (!(canvas instanceof DisplayListCanvas)) { 104 // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas. 105 throw new IllegalArgumentException(canvas.getClass().getName() 106 + " is not a DisplayList canvas"); 107 } 108 ((DisplayListCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, null); 109 } 110 111 /** 112 * Calls the function specified with the nativeDrawGLFunctor functor pointer. This 113 * functionality is used by the WebView for calling into their renderer from the 114 * framework display lists. 115 * 116 * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()}) 117 * @param nativeDrawGLFunctor the pointer to the native functor that implements 118 * system/core/include/utils/Functor.h 119 * @param releasedRunnable Called when this nativeDrawGLFunctor is no longer referenced by this 120 * canvas, so is safe to be destroyed. 121 * @throws IllegalArgumentException if the canvas is not hardware accelerated 122 */ 123 public void callDrawGlFunction(@NonNull Canvas canvas, long nativeDrawGLFunctor, 124 @Nullable Runnable releasedRunnable) { 125 if (!(canvas instanceof DisplayListCanvas)) { 126 // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas. 127 throw new IllegalArgumentException(canvas.getClass().getName() 128 + " is not a DisplayList canvas"); 129 } 130 ((DisplayListCanvas) canvas).drawGLFunctor2(nativeDrawGLFunctor, releasedRunnable); 131 } 132 133 /** 134 * Detaches the draw GL functor. 135 * 136 * @param nativeDrawGLFunctor the pointer to the native functor that implements 137 * system/core/include/utils/Functor.h 138 */ 139 public void detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor) { 140 ViewRootImpl viewRootImpl = containerView.getViewRootImpl(); 141 if (nativeDrawGLFunctor != 0 && viewRootImpl != null) { 142 viewRootImpl.detachFunctor(nativeDrawGLFunctor); 143 } 144 } 145 146 /** 147 * Returns the package id of the given {@code packageName}. 148 */ 149 public int getPackageId(Resources resources, String packageName) { 150 SparseArray<String> packageIdentifiers = 151 resources.getAssets().getAssignedPackageIdentifiers(); 152 for (int i = 0; i < packageIdentifiers.size(); i++) { 153 final String name = packageIdentifiers.valueAt(i); 154 155 if (packageName.equals(name)) { 156 return packageIdentifiers.keyAt(i); 157 } 158 } 159 throw new RuntimeException("Package not found: " + packageName); 160 } 161 162 /** 163 * Returns the application which is embedding the WebView. 164 */ 165 public Application getApplication() { 166 return ActivityThread.currentApplication(); 167 } 168 169 /** 170 * Returns the error string for the given {@code errorCode}. 171 */ 172 public String getErrorString(Context context, int errorCode) { 173 return LegacyErrorStrings.getString(errorCode, context); 174 } 175 176 /** 177 * Adds the WebView asset path to {@link android.content.res.AssetManager}. 178 */ 179 public void addWebViewAssetPath(Context context) { 180 context.getAssets().addAssetPathAsSharedLibrary( 181 WebViewFactory.getLoadedPackageInfo().applicationInfo.sourceDir); 182 } 183} 184