blob: c41f2e461ff83163214c79f606abb8c3b61adce5 [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +01001/*
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 Hodson31b3ffa2019-10-14 10:27:00 +010016
17#ifndef ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_
18#define ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_
19
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010020#if !defined(ART_TARGET_ANDROID)
21#error "Not available for host or linux target"
Orion Hodson9b16e342019-10-09 13:29:16 +010022#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
34namespace android::nativeloader {
35
36using 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.
42class 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,
58 jstring java_library_path, jstring java_permitted_path);
59 NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader);
60
61 private:
62 Result<void> InitPublicNamespace(const char* library_path);
63 NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader);
64
65 bool initialized_;
66 NativeLoaderNamespace* app_main_namespace_;
67 std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
68};
69
Jooyung Han538f99a2020-03-03 00:46:50 +090070Result<std::string> FindApexNamespaceName(const std::string& location);
71
Orion Hodson9b16e342019-10-09 13:29:16 +010072} // namespace android::nativeloader
Orion Hodson31b3ffa2019-10-14 10:27:00 +010073
74#endif // ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_