blob: 7b3efff4dfaea8b34e1990dcabf60969a333d4b7 [file] [log] [blame]
Jiyong Park6291da22019-04-26 18:55:48 +09001/*
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 */
16#pragma once
17#if !defined(__ANDROID__)
18#error "Not available for host"
19#endif
20
Jiyong Park40a60772019-05-03 16:21:31 +090021#define LOG_TAG "nativeloader"
22
Jiyong Park6291da22019-04-26 18:55:48 +090023#include "native_loader_namespace.h"
24
25#include <list>
26#include <string>
27
Jiyong Park8f4afc82019-07-22 14:20:40 +090028#include <android-base/result.h>
Jiyong Park16a98962019-05-04 03:10:48 +090029#include <jni.h>
Jiyong Park6291da22019-04-26 18:55:48 +090030
Jiyong Park40a60772019-05-03 16:21:31 +090031namespace android::nativeloader {
Jiyong Park6291da22019-04-26 18:55:48 +090032
Jiyong Park8f4afc82019-07-22 14:20:40 +090033using android::base::Result;
34
Jiyong Park6291da22019-04-26 18:55:48 +090035// LibraryNamespaces is a singleton object that manages NativeLoaderNamespace
36// objects for an app process. Its main job is to create (and configure) a new
37// NativeLoaderNamespace object for a Java ClassLoader, and to find an existing
38// object for a given ClassLoader.
39class LibraryNamespaces {
40 public:
Jiyong Parkbb710ee2019-08-15 08:10:55 +090041 LibraryNamespaces() : initialized_(false), app_main_namespace_(nullptr) {}
Jiyong Park6291da22019-04-26 18:55:48 +090042
43 LibraryNamespaces(LibraryNamespaces&&) = default;
44 LibraryNamespaces(const LibraryNamespaces&) = delete;
45 LibraryNamespaces& operator=(const LibraryNamespaces&) = delete;
46
47 void Initialize();
Jiyong Park78cc06a2019-07-19 09:02:38 +090048 void Reset() {
49 namespaces_.clear();
50 initialized_ = false;
Jiyong Parkb37c4812019-05-16 21:03:30 +090051 app_main_namespace_ = nullptr;
Jiyong Park78cc06a2019-07-19 09:02:38 +090052 }
Jiyong Park8f4afc82019-07-22 14:20:40 +090053 Result<NativeLoaderNamespace*> Create(JNIEnv* env, uint32_t target_sdk_version,
54 jobject class_loader, bool is_shared, jstring dex_path,
55 jstring java_library_path, jstring java_permitted_path);
Jiyong Park6291da22019-04-26 18:55:48 +090056 NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader);
57
58 private:
Jiyong Park8f4afc82019-07-22 14:20:40 +090059 Result<void> InitPublicNamespace(const char* library_path);
Jiyong Park6291da22019-04-26 18:55:48 +090060 NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader);
61
62 bool initialized_;
Jiyong Parkb37c4812019-05-16 21:03:30 +090063 NativeLoaderNamespace* app_main_namespace_;
Jiyong Park6291da22019-04-26 18:55:48 +090064 std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
Jiyong Park6291da22019-04-26 18:55:48 +090065};
66
Jiyong Park40a60772019-05-03 16:21:31 +090067} // namespace android::nativeloader