Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 com.android.internal.os; |
| 18 | |
| 19 | import android.os.Trace; |
| 20 | |
| 21 | import dalvik.system.DelegateLastClassLoader; |
Calin Juravle | f8c14e9 | 2017-07-24 17:56:51 -0700 | [diff] [blame] | 22 | import dalvik.system.DexClassLoader; |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 23 | import dalvik.system.PathClassLoader; |
| 24 | |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 25 | import java.util.List; |
| 26 | |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 27 | /** |
| 28 | * Creates class loaders. |
| 29 | * |
| 30 | * @hide |
| 31 | */ |
| 32 | public class ClassLoaderFactory { |
| 33 | // Unconstructable |
| 34 | private ClassLoaderFactory() {} |
| 35 | |
| 36 | private static final String PATH_CLASS_LOADER_NAME = PathClassLoader.class.getName(); |
Calin Juravle | f8c14e9 | 2017-07-24 17:56:51 -0700 | [diff] [blame] | 37 | private static final String DEX_CLASS_LOADER_NAME = DexClassLoader.class.getName(); |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 38 | private static final String DELEGATE_LAST_CLASS_LOADER_NAME = |
| 39 | DelegateLastClassLoader.class.getName(); |
| 40 | |
| 41 | /** |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 42 | * Returns the name of the class for PathClassLoader. |
| 43 | */ |
| 44 | public static String getPathClassLoaderName() { |
| 45 | return PATH_CLASS_LOADER_NAME; |
| 46 | } |
| 47 | |
| 48 | /** |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 49 | * Returns true if {@code name} is a supported classloader. {@code name} must be a |
| 50 | * binary name of a class, as defined by {@code Class.getName}. |
| 51 | */ |
| 52 | public static boolean isValidClassLoaderName(String name) { |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 53 | // This method is used to parse package data and does not accept null names. |
| 54 | return name != null && (isPathClassLoaderName(name) || isDelegateLastClassLoaderName(name)); |
| 55 | } |
| 56 | |
| 57 | /** |
Calin Juravle | f8c14e9 | 2017-07-24 17:56:51 -0700 | [diff] [blame] | 58 | * Returns true if {@code name} is the encoding for either PathClassLoader or DexClassLoader. |
| 59 | * The two class loaders are grouped together because they have the same behaviour. |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 60 | */ |
| 61 | public static boolean isPathClassLoaderName(String name) { |
| 62 | // For null values we default to PathClassLoader. This cover the case when packages |
| 63 | // don't specify any value for their class loaders. |
Calin Juravle | f8c14e9 | 2017-07-24 17:56:51 -0700 | [diff] [blame] | 64 | return name == null || PATH_CLASS_LOADER_NAME.equals(name) || |
| 65 | DEX_CLASS_LOADER_NAME.equals(name); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns true if {@code name} is the encoding for the DelegateLastClassLoader. |
| 70 | */ |
| 71 | public static boolean isDelegateLastClassLoaderName(String name) { |
| 72 | return DELEGATE_LAST_CLASS_LOADER_NAME.equals(name); |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Same as {@code createClassLoader} below, except that no associated namespace |
| 77 | * is created. |
| 78 | */ |
| 79 | public static ClassLoader createClassLoader(String dexPath, |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 80 | String librarySearchPath, ClassLoader parent, String classloaderName, |
| 81 | List<ClassLoader> sharedLibraries) { |
| 82 | ClassLoader[] arrayOfSharedLibraries = (sharedLibraries == null) |
| 83 | ? null |
| 84 | : sharedLibraries.toArray(new ClassLoader[sharedLibraries.size()]); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 85 | if (isPathClassLoaderName(classloaderName)) { |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 86 | return new PathClassLoader(dexPath, librarySearchPath, parent, arrayOfSharedLibraries); |
Narayan Kamath | 590b90f | 2017-08-10 12:09:13 +0100 | [diff] [blame] | 87 | } else if (isDelegateLastClassLoaderName(classloaderName)) { |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 88 | return new DelegateLastClassLoader(dexPath, librarySearchPath, parent, |
| 89 | arrayOfSharedLibraries); |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | throw new AssertionError("Invalid classLoaderName: " + classloaderName); |
| 93 | } |
| 94 | |
| 95 | /** |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 96 | * Same as {@code createClassLoader} below, but passes a null list of shared |
| 97 | * libraries. |
| 98 | */ |
| 99 | public static ClassLoader createClassLoader(String dexPath, |
| 100 | String librarySearchPath, String libraryPermittedPath, ClassLoader parent, |
| 101 | int targetSdkVersion, boolean isNamespaceShared, String classLoaderName) { |
| 102 | return createClassLoader(dexPath, librarySearchPath, libraryPermittedPath, |
| 103 | parent, targetSdkVersion, isNamespaceShared, classLoaderName, null); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 108 | * Create a ClassLoader and initialize a linker-namespace for it. |
| 109 | */ |
| 110 | public static ClassLoader createClassLoader(String dexPath, |
| 111 | String librarySearchPath, String libraryPermittedPath, ClassLoader parent, |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 112 | int targetSdkVersion, boolean isNamespaceShared, String classLoaderName, |
| 113 | List<ClassLoader> sharedLibraries) { |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 114 | |
| 115 | final ClassLoader classLoader = createClassLoader(dexPath, librarySearchPath, parent, |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 116 | classLoaderName, sharedLibraries); |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 117 | |
| 118 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "createClassloaderNamespace"); |
| 119 | String errorMessage = createClassloaderNamespace(classLoader, |
| 120 | targetSdkVersion, |
| 121 | librarySearchPath, |
| 122 | libraryPermittedPath, |
Jiyong Park | fcad696 | 2017-08-22 10:24:28 +0900 | [diff] [blame] | 123 | isNamespaceShared, |
Kiyoung Kim | cfc8da1 | 2019-02-20 18:07:41 +0900 | [diff] [blame] | 124 | dexPath); |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 125 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 126 | |
| 127 | if (errorMessage != null) { |
| 128 | throw new UnsatisfiedLinkError("Unable to create namespace for the classloader " + |
| 129 | classLoader + ": " + errorMessage); |
| 130 | } |
| 131 | |
| 132 | return classLoader; |
| 133 | } |
| 134 | |
| 135 | private static native String createClassloaderNamespace(ClassLoader classLoader, |
| 136 | int targetSdkVersion, |
| 137 | String librarySearchPath, |
| 138 | String libraryPermittedPath, |
Jiyong Park | fcad696 | 2017-08-22 10:24:28 +0900 | [diff] [blame] | 139 | boolean isNamespaceShared, |
Kiyoung Kim | cfc8da1 | 2019-02-20 18:07:41 +0900 | [diff] [blame] | 140 | String dexPath); |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 141 | } |