The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.app; |
| 18 | |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 20 | import android.os.Build; |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 21 | import android.os.GraphicsEnvironment; |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 22 | import android.os.Trace; |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 23 | import android.util.ArrayMap; |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 24 | |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 25 | import com.android.internal.os.ClassLoaderFactory; |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import dalvik.system.PathClassLoader; |
| 28 | |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 29 | import java.util.Collection; |
Nicolas Geoffray | 972b39e | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 30 | import java.util.List; |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 31 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 32 | /** @hide */ |
| 33 | public class ApplicationLoaders { |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 34 | @UnsupportedAppUsage |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 35 | public static ApplicationLoaders getDefault() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | return gApplicationLoaders; |
| 37 | } |
| 38 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 39 | ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled, |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 40 | String librarySearchPath, String libraryPermittedPath, |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 41 | ClassLoader parent, String classLoaderName) { |
Nicolas Geoffray | 972b39e | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 42 | return getClassLoaderWithSharedLibraries(zip, targetSdkVersion, isBundled, |
| 43 | librarySearchPath, libraryPermittedPath, parent, classLoaderName, |
| 44 | null); |
| 45 | } |
| 46 | |
| 47 | ClassLoader getClassLoaderWithSharedLibraries( |
| 48 | String zip, int targetSdkVersion, boolean isBundled, |
| 49 | String librarySearchPath, String libraryPermittedPath, |
| 50 | ClassLoader parent, String classLoaderName, |
| 51 | List<ClassLoader> sharedLibraries) { |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 52 | // For normal usage the cache key used is the same as the zip path. |
| 53 | return getClassLoader(zip, targetSdkVersion, isBundled, librarySearchPath, |
Nicolas Geoffray | 972b39e | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 54 | libraryPermittedPath, parent, zip, classLoaderName, sharedLibraries); |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled, |
| 58 | String librarySearchPath, String libraryPermittedPath, |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 59 | ClassLoader parent, String cacheKey, |
Nicolas Geoffray | 972b39e | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 60 | String classLoaderName, List<ClassLoader> sharedLibraries) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | /* |
| 62 | * This is the parent we use if they pass "null" in. In theory |
| 63 | * this should be the "system" class loader; in practice we |
| 64 | * don't use that and can happily (and more efficiently) use the |
| 65 | * bootstrap class loader. |
| 66 | */ |
| 67 | ClassLoader baseParent = ClassLoader.getSystemClassLoader().getParent(); |
| 68 | |
| 69 | synchronized (mLoaders) { |
| 70 | if (parent == null) { |
| 71 | parent = baseParent; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * If we're one step up from the base class loader, find |
| 76 | * something in our cache. Otherwise, we create a whole |
| 77 | * new ClassLoader for the zip archive. |
| 78 | */ |
| 79 | if (parent == baseParent) { |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 80 | ClassLoader loader = mLoaders.get(cacheKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | if (loader != null) { |
| 82 | return loader; |
| 83 | } |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 84 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 85 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 86 | |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 87 | ClassLoader classloader = ClassLoaderFactory.createClassLoader( |
| 88 | zip, librarySearchPath, libraryPermittedPath, parent, |
Nicolas Geoffray | 972b39e | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 89 | targetSdkVersion, isBundled, classLoaderName, sharedLibraries); |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 90 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 91 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 92 | |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 93 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setLayerPaths"); |
| 94 | GraphicsEnvironment.getInstance().setLayerPaths( |
| 95 | classloader, librarySearchPath, libraryPermittedPath); |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 96 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Dimitry Ivanov | 0997908 | 2016-04-19 11:11:01 -0700 | [diff] [blame] | 97 | |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 98 | mLoaders.put(cacheKey, classloader); |
| 99 | return classloader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 102 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 103 | ClassLoader loader = ClassLoaderFactory.createClassLoader( |
Nicolas Geoffray | 972b39e | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 104 | zip, null, parent, classLoaderName, sharedLibraries); |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 105 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 106 | return loader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 110 | /** |
| 111 | * Creates a classloader for the WebView APK and places it in the cache of loaders maintained |
| 112 | * by this class. This is used in the WebView zygote, where its presence in the cache speeds up |
| 113 | * startup and enables memory sharing. |
| 114 | */ |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 115 | public ClassLoader createAndCacheWebViewClassLoader(String packagePath, String libsPath, |
| 116 | String cacheKey) { |
| 117 | // The correct paths are calculated by WebViewZygote in the system server and passed to |
| 118 | // us here. We hardcode the other parameters: WebView always targets the current SDK, |
| 119 | // does not need to use non-public system libraries, and uses the base classloader as its |
| 120 | // parent to permit usage of the cache. |
| 121 | // The cache key is passed separately to enable the stub WebView to be cached under the |
| 122 | // stub's APK path, when the actual package path is the donor APK. |
| 123 | return getClassLoader(packagePath, Build.VERSION.SDK_INT, false, libsPath, null, null, |
Nicolas Geoffray | 972b39e | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 124 | cacheKey, null /* classLoaderName */, null /* sharedLibraries */); |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 127 | /** |
| 128 | * Adds a new path the classpath of the given loader. |
| 129 | * @throws IllegalStateException if the provided class loader is not a {@link PathClassLoader}. |
| 130 | */ |
| 131 | void addPath(ClassLoader classLoader, String dexPath) { |
| 132 | if (!(classLoader instanceof PathClassLoader)) { |
| 133 | throw new IllegalStateException("class loader is not a PathClassLoader"); |
| 134 | } |
| 135 | final PathClassLoader baseDexClassLoader = (PathClassLoader) classLoader; |
| 136 | baseDexClassLoader.addDexPath(dexPath); |
| 137 | } |
| 138 | |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 139 | /** |
| 140 | * @hide |
| 141 | */ |
| 142 | void addNative(ClassLoader classLoader, Collection<String> libPaths) { |
| 143 | if (!(classLoader instanceof PathClassLoader)) { |
| 144 | throw new IllegalStateException("class loader is not a PathClassLoader"); |
| 145 | } |
| 146 | final PathClassLoader baseDexClassLoader = (PathClassLoader) classLoader; |
| 147 | baseDexClassLoader.addNativePath(libPaths); |
| 148 | } |
| 149 | |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 150 | @UnsupportedAppUsage |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 151 | private final ArrayMap<String, ClassLoader> mLoaders = new ArrayMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 153 | private static final ApplicationLoaders gApplicationLoaders = new ApplicationLoaders(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | } |