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 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 19 | import android.os.Build; |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 20 | import android.os.Trace; |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 21 | import android.util.ArrayMap; |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 22 | import com.android.internal.os.PathClassLoaderFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import dalvik.system.PathClassLoader; |
| 24 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 25 | /** @hide */ |
| 26 | public class ApplicationLoaders { |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 27 | public static ApplicationLoaders getDefault() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | return gApplicationLoaders; |
| 29 | } |
| 30 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 31 | ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled, |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 32 | String librarySearchPath, String libraryPermittedPath, |
| 33 | ClassLoader parent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | /* |
| 35 | * This is the parent we use if they pass "null" in. In theory |
| 36 | * this should be the "system" class loader; in practice we |
| 37 | * don't use that and can happily (and more efficiently) use the |
| 38 | * bootstrap class loader. |
| 39 | */ |
| 40 | ClassLoader baseParent = ClassLoader.getSystemClassLoader().getParent(); |
| 41 | |
| 42 | synchronized (mLoaders) { |
| 43 | if (parent == null) { |
| 44 | parent = baseParent; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * If we're one step up from the base class loader, find |
| 49 | * something in our cache. Otherwise, we create a whole |
| 50 | * new ClassLoader for the zip archive. |
| 51 | */ |
| 52 | if (parent == baseParent) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 53 | ClassLoader loader = mLoaders.get(zip); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | if (loader != null) { |
| 55 | return loader; |
| 56 | } |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 57 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 58 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 59 | |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 60 | PathClassLoader pathClassloader = PathClassLoaderFactory.createClassLoader( |
| 61 | zip, |
| 62 | librarySearchPath, |
| 63 | libraryPermittedPath, |
| 64 | parent, |
| 65 | targetSdkVersion, |
| 66 | isBundled); |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 67 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 68 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 69 | |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 70 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setupVulkanLayerPath"); |
Dimitry Ivanov | 0997908 | 2016-04-19 11:11:01 -0700 | [diff] [blame] | 71 | setupVulkanLayerPath(pathClassloader, librarySearchPath); |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 72 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Dimitry Ivanov | 0997908 | 2016-04-19 11:11:01 -0700 | [diff] [blame] | 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | mLoaders.put(zip, pathClassloader); |
| 75 | return pathClassloader; |
| 76 | } |
| 77 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 78 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
| 79 | PathClassLoader pathClassloader = new PathClassLoader(zip, parent); |
| 80 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 81 | return pathClassloader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 85 | /** |
| 86 | * Creates a classloader for the WebView APK and places it in the cache of loaders maintained |
| 87 | * by this class. This is used in the WebView zygote, where its presence in the cache speeds up |
| 88 | * startup and enables memory sharing. |
| 89 | */ |
| 90 | public ClassLoader createAndCacheWebViewClassLoader(String packagePath, String libsPath) { |
| 91 | // The correct paths are calculated by WebViewZygote in the system server and passed to |
| 92 | // us here. We hardcode the other parameters: WebView always targets the current SDK, |
| 93 | // does not need to use non-public system libraries, and uses the base classloader as its |
| 94 | // parent to permit usage of the cache. |
| 95 | return getClassLoader(packagePath, Build.VERSION.SDK_INT, false, libsPath, null, null); |
| 96 | } |
| 97 | |
Dimitry Ivanov | 0997908 | 2016-04-19 11:11:01 -0700 | [diff] [blame] | 98 | private static native void setupVulkanLayerPath(ClassLoader classLoader, String librarySearchPath); |
| 99 | |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Adds a new path the classpath of the given loader. |
| 102 | * @throws IllegalStateException if the provided class loader is not a {@link PathClassLoader}. |
| 103 | */ |
| 104 | void addPath(ClassLoader classLoader, String dexPath) { |
| 105 | if (!(classLoader instanceof PathClassLoader)) { |
| 106 | throw new IllegalStateException("class loader is not a PathClassLoader"); |
| 107 | } |
| 108 | final PathClassLoader baseDexClassLoader = (PathClassLoader) classLoader; |
| 109 | baseDexClassLoader.addDexPath(dexPath); |
| 110 | } |
| 111 | |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 112 | private final ArrayMap<String, ClassLoader> mLoaders = new ArrayMap<String, ClassLoader>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 114 | private static final ApplicationLoaders gApplicationLoaders = new ApplicationLoaders(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | } |