blob: bd4862dfb08de13de7a876a7f25a586699ce64c2 [file] [log] [blame]
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001/*
2 * Copyright (C) 2017 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#define ATRACE_TAG ATRACE_TAG_RESOURCES
18
19#include "android-base/macros.h"
20#include "android-base/stringprintf.h"
21#include "android-base/unique_fd.h"
22#include "androidfw/ApkAssets.h"
23#include "utils/misc.h"
24#include "utils/Trace.h"
25
26#include "core_jni_helpers.h"
27#include "jni.h"
28#include "nativehelper/ScopedUtfChars.h"
29
30using ::android::base::unique_fd;
31
32namespace android {
33
34static jlong NativeLoad(JNIEnv* env, jclass /*clazz*/, jstring java_path, jboolean system,
35 jboolean force_shared_lib, jboolean overlay) {
36 ScopedUtfChars path(env, java_path);
37 if (path.c_str() == nullptr) {
38 return 0;
39 }
40
41 ATRACE_NAME(base::StringPrintf("LoadApkAssets(%s)", path.c_str()).c_str());
42
43 std::unique_ptr<const ApkAssets> apk_assets;
44 if (overlay) {
45 apk_assets = ApkAssets::LoadOverlay(path.c_str(), system);
46 } else if (force_shared_lib) {
47 apk_assets = ApkAssets::LoadAsSharedLibrary(path.c_str(), system);
48 } else {
49 apk_assets = ApkAssets::Load(path.c_str(), system);
50 }
51
52 if (apk_assets == nullptr) {
53 std::string error_msg = base::StringPrintf("Failed to load asset path %s", path.c_str());
54 jniThrowException(env, "java/io/IOException", error_msg.c_str());
55 return 0;
56 }
57 return reinterpret_cast<jlong>(apk_assets.release());
58}
59
60static jlong NativeLoadFromFd(JNIEnv* env, jclass /*clazz*/, jobject file_descriptor,
61 jstring friendly_name, jboolean system, jboolean force_shared_lib) {
62 ScopedUtfChars friendly_name_utf8(env, friendly_name);
63 if (friendly_name_utf8.c_str() == nullptr) {
64 return 0;
65 }
66
67 ATRACE_NAME(base::StringPrintf("LoadApkAssetsFd(%s)", friendly_name_utf8.c_str()).c_str());
68
69 int fd = jniGetFDFromFileDescriptor(env, file_descriptor);
70 if (fd < 0) {
71 jniThrowException(env, "java/lang/IllegalArgumentException", "Bad FileDescriptor");
72 return 0;
73 }
74
Nick Kralevich4b3a08c2019-01-28 10:39:10 -080075 unique_fd dup_fd(::fcntl(fd, F_DUPFD_CLOEXEC, 0));
Adam Lesinskibebfcc42018-02-12 14:27:46 -080076 if (dup_fd < 0) {
77 jniThrowIOException(env, errno);
78 return 0;
79 }
80
81 std::unique_ptr<const ApkAssets> apk_assets = ApkAssets::LoadFromFd(std::move(dup_fd),
82 friendly_name_utf8.c_str(),
83 system, force_shared_lib);
84 if (apk_assets == nullptr) {
85 std::string error_msg = base::StringPrintf("Failed to load asset path %s from fd %d",
86 friendly_name_utf8.c_str(), dup_fd.get());
87 jniThrowException(env, "java/io/IOException", error_msg.c_str());
88 return 0;
89 }
90 return reinterpret_cast<jlong>(apk_assets.release());
91}
92
93static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
94 delete reinterpret_cast<ApkAssets*>(ptr);
95}
96
97static jstring NativeGetAssetPath(JNIEnv* env, jclass /*clazz*/, jlong ptr) {
98 const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr);
99 return env->NewStringUTF(apk_assets->GetPath().c_str());
100}
101
102static jlong NativeGetStringBlock(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
103 const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr);
104 return reinterpret_cast<jlong>(apk_assets->GetLoadedArsc()->GetStringPool());
105}
106
107static jboolean NativeIsUpToDate(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
108 const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr);
Winsonb0085ce2019-02-19 12:48:22 -0800109 return apk_assets->IsUpToDate() ? JNI_TRUE : JNI_FALSE;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800110}
111
112static jlong NativeOpenXml(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring file_name) {
113 ScopedUtfChars path_utf8(env, file_name);
114 if (path_utf8.c_str() == nullptr) {
115 return 0;
116 }
117
118 const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr);
119 std::unique_ptr<Asset> asset = apk_assets->Open(path_utf8.c_str(),
120 Asset::AccessMode::ACCESS_RANDOM);
121 if (asset == nullptr) {
122 jniThrowException(env, "java/io/FileNotFoundException", path_utf8.c_str());
123 return 0;
124 }
125
126 // DynamicRefTable is only needed when looking up resource references. Opening an XML file
127 // directly from an ApkAssets has no notion of proper resource references.
128 std::unique_ptr<ResXMLTree> xml_tree = util::make_unique<ResXMLTree>(nullptr /*dynamicRefTable*/);
129 status_t err = xml_tree->setTo(asset->getBuffer(true), asset->getLength(), true);
130 asset.reset();
131
132 if (err != NO_ERROR) {
133 jniThrowException(env, "java/io/FileNotFoundException", "Corrupt XML binary file");
134 return 0;
135 }
136 return reinterpret_cast<jlong>(xml_tree.release());
137}
138
139// JNI registration.
140static const JNINativeMethod gApkAssetsMethods[] = {
141 {"nativeLoad", "(Ljava/lang/String;ZZZ)J", (void*)NativeLoad},
142 {"nativeLoadFromFd", "(Ljava/io/FileDescriptor;Ljava/lang/String;ZZ)J",
143 (void*)NativeLoadFromFd},
144 {"nativeDestroy", "(J)V", (void*)NativeDestroy},
145 {"nativeGetAssetPath", "(J)Ljava/lang/String;", (void*)NativeGetAssetPath},
146 {"nativeGetStringBlock", "(J)J", (void*)NativeGetStringBlock},
147 {"nativeIsUpToDate", "(J)Z", (void*)NativeIsUpToDate},
148 {"nativeOpenXml", "(JLjava/lang/String;)J", (void*)NativeOpenXml},
149};
150
151int register_android_content_res_ApkAssets(JNIEnv* env) {
152 return RegisterMethodsOrDie(env, "android/content/res/ApkAssets", gApkAssetsMethods,
153 arraysize(gApkAssetsMethods));
154}
155
156} // namespace android