• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.app.ActivityThread;
23 import android.app.Application;
24 import android.app.ResourcesManager;
25 import android.compat.annotation.UnsupportedAppUsage;
26 import android.content.Context;
27 import android.content.pm.ApplicationInfo;
28 import android.content.res.Resources;
29 import android.graphics.Canvas;
30 import android.graphics.RecordingCanvas;
31 import android.os.RemoteException;
32 import android.os.SystemProperties;
33 import android.os.Trace;
34 import android.util.SparseArray;
35 import android.view.View;
36 import android.view.ViewRootImpl;
37 
38 import com.android.internal.util.ArrayUtils;
39 
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  */
46 @SystemApi
47 public final class WebViewDelegate {
48 
49     @UnsupportedAppUsage
WebViewDelegate()50     /* package */ WebViewDelegate() { }
51 
52     /**
53      * Listener that gets notified whenever tracing has been enabled/disabled.
54      */
55     public interface OnTraceEnabledChangeListener {
onTraceEnabledChange(boolean enabled)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      */
setOnTraceEnabledChangeListener(final OnTraceEnabledChangeListener listener)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     /**
73      * Returns {@code true} if the WebView trace tag is enabled and {@code false} otherwise.
74      */
isTraceTagEnabled()75     public boolean isTraceTagEnabled() {
76         return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW);
77     }
78 
79     /**
80      * Throws {@link UnsupportedOperationException}
81      * @deprecated Use {@link #drawWebViewFunctor(Canvas, int)}
82      */
83     @Deprecated
canInvokeDrawGlFunctor(View containerView)84     public boolean canInvokeDrawGlFunctor(View containerView) {
85         throw new UnsupportedOperationException();
86     }
87 
88     /**
89      * Throws {@link UnsupportedOperationException}
90      * @deprecated Use {@link #drawWebViewFunctor(Canvas, int)}
91      */
92     @Deprecated
invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor, boolean waitForCompletion)93     public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor,
94             boolean waitForCompletion) {
95         throw new UnsupportedOperationException();
96     }
97 
98     /**
99      * Throws {@link UnsupportedOperationException}
100      * @deprecated Use {@link #drawWebViewFunctor(Canvas, int)}
101      */
102     @Deprecated
callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor)103     public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) {
104         throw new UnsupportedOperationException();
105     }
106 
107     /**
108      * Throws {@link UnsupportedOperationException}
109      * @deprecated Use {@link #drawWebViewFunctor(Canvas, int)}
110      */
111     @Deprecated
callDrawGlFunction(@onNull Canvas canvas, long nativeDrawGLFunctor, @Nullable Runnable releasedRunnable)112     public void callDrawGlFunction(@NonNull Canvas canvas, long nativeDrawGLFunctor,
113             @Nullable Runnable releasedRunnable) {
114         throw new UnsupportedOperationException();
115     }
116 
117     /**
118      * Call webview draw functor. See API in draw_fn.h.
119      * @param canvas a {@link RecordingCanvas}.
120      * @param functor created by AwDrawFn_CreateFunctor in draw_fn.h.
121      */
drawWebViewFunctor(@onNull Canvas canvas, int functor)122     public void drawWebViewFunctor(@NonNull Canvas canvas, int functor) {
123         if (!(canvas instanceof RecordingCanvas)) {
124             // Canvas#isHardwareAccelerated() is only true for subclasses of RecordingCanvas.
125             throw new IllegalArgumentException(canvas.getClass().getName()
126                     + " is not a RecordingCanvas canvas");
127         }
128         ((RecordingCanvas) canvas).drawWebViewFunctor(functor);
129     }
130 
131     /**
132      * 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      * @deprecated Use {@link #drawWebViewFunctor(Canvas, int)}
137      */
138     @Deprecated
detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor)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      */
getPackageId(Resources resources, String packageName)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      */
getApplication()165     public Application getApplication() {
166         return ActivityThread.currentApplication();
167     }
168 
169     /**
170      * Returns the error string for the given {@code errorCode}.
171      */
getErrorString(Context context, int errorCode)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      */
addWebViewAssetPath(Context context)179     public void addWebViewAssetPath(Context context) {
180         final String[] newAssetPaths =
181                 WebViewFactory.getLoadedPackageInfo().applicationInfo.getAllApkPaths();
182         final ApplicationInfo appInfo = context.getApplicationInfo();
183 
184         // Build the new library asset path list.
185         String[] newLibAssets = appInfo.sharedLibraryFiles;
186         for (String newAssetPath : newAssetPaths) {
187             newLibAssets = ArrayUtils.appendElement(String.class, newLibAssets, newAssetPath);
188         }
189 
190         if (newLibAssets != appInfo.sharedLibraryFiles) {
191             // Update the ApplicationInfo object with the new list.
192             // We know this will persist and future Resources created via ResourcesManager
193             // will include the shared library because this ApplicationInfo comes from the
194             // underlying LoadedApk in ContextImpl, which does not change during the life of the
195             // application.
196             appInfo.sharedLibraryFiles = newLibAssets;
197 
198             // Update existing Resources with the WebView library.
199             ResourcesManager.getInstance().appendLibAssetsForMainAssetPath(
200                     appInfo.getBaseResourcePath(), newAssetPaths);
201         }
202     }
203 
204     /**
205      * Returns whether WebView should run in multiprocess mode.
206      */
isMultiProcessEnabled()207     public boolean isMultiProcessEnabled() {
208         try {
209             return WebViewFactory.getUpdateService().isMultiProcessEnabled();
210         } catch (RemoteException e) {
211             throw e.rethrowFromSystemServer();
212         }
213     }
214 
215     /**
216      * Returns the data directory suffix to use, or null for none.
217      */
getDataDirectorySuffix()218     public String getDataDirectorySuffix() {
219         return WebViewFactory.getDataDirectorySuffix();
220     }
221 
222     /**
223      * Get the timestamps at which various WebView startup events occurred in this process.
224      * This method must be called on the same thread where the
225      * WebViewChromiumFactoryProvider#create method was invoked.
226      */
227     @NonNull
getStartupTimestamps()228     public WebViewFactory.StartupTimestamps getStartupTimestamps() {
229         return WebViewFactory.getStartupTimestamps();
230     }
231 }
232