| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | */ |
| Orion Hodson | 31b3ffa | 2019-10-14 10:27:00 +0100 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_ |
| 18 | #define ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_ |
| 19 | |
| Nicolas Geoffray | 7ca8b67 | 2020-04-24 15:43:48 +0100 | [diff] [blame] | 20 | #if !defined(ART_TARGET_ANDROID) |
| 21 | #error "Not available for host or linux target" |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | #define LOG_TAG "nativeloader" |
| 25 | |
| 26 | #include "native_loader_namespace.h" |
| 27 | |
| 28 | #include <list> |
| 29 | #include <string> |
| 30 | |
| 31 | #include <android-base/result.h> |
| 32 | #include <jni.h> |
| 33 | |
| 34 | namespace android::nativeloader { |
| 35 | |
| 36 | using android::base::Result; |
| 37 | |
| 38 | // LibraryNamespaces is a singleton object that manages NativeLoaderNamespace |
| 39 | // objects for an app process. Its main job is to create (and configure) a new |
| 40 | // NativeLoaderNamespace object for a Java ClassLoader, and to find an existing |
| 41 | // object for a given ClassLoader. |
| 42 | class LibraryNamespaces { |
| 43 | public: |
| 44 | LibraryNamespaces() : initialized_(false), app_main_namespace_(nullptr) {} |
| 45 | |
| 46 | LibraryNamespaces(LibraryNamespaces&&) = default; |
| 47 | LibraryNamespaces(const LibraryNamespaces&) = delete; |
| 48 | LibraryNamespaces& operator=(const LibraryNamespaces&) = delete; |
| 49 | |
| 50 | void Initialize(); |
| 51 | void Reset() { |
| 52 | namespaces_.clear(); |
| 53 | initialized_ = false; |
| 54 | app_main_namespace_ = nullptr; |
| 55 | } |
| 56 | Result<NativeLoaderNamespace*> Create(JNIEnv* env, uint32_t target_sdk_version, |
| 57 | jobject class_loader, bool is_shared, jstring dex_path, |
| Jiyong Park | e741dfd | 2020-07-02 23:17:58 +0900 | [diff] [blame] | 58 | jstring java_library_path, jstring java_permitted_path, |
| 59 | jstring uses_library_list); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 60 | NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader); |
| 61 | |
| 62 | private: |
| 63 | Result<void> InitPublicNamespace(const char* library_path); |
| 64 | NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader); |
| 65 | |
| 66 | bool initialized_; |
| 67 | NativeLoaderNamespace* app_main_namespace_; |
| 68 | std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_; |
| 69 | }; |
| 70 | |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 71 | Result<std::string> FindApexNamespaceName(const std::string& location); |
| 72 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 73 | } // namespace android::nativeloader |
| Orion Hodson | 31b3ffa | 2019-10-14 10:27:00 +0100 | [diff] [blame] | 74 | |
| 75 | #endif // ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_ |