Home | History | Annotate | Download | only in jni
      1 /*
      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 
     30 using ::android::base::unique_fd;
     31 
     32 namespace android {
     33 
     34 static 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 
     60 static 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 
     75   unique_fd dup_fd(::dup(fd));
     76   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 
     93 static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
     94   delete reinterpret_cast<ApkAssets*>(ptr);
     95 }
     96 
     97 static 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 
    102 static 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 
    107 static jboolean NativeIsUpToDate(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
    108   const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr);
    109   (void)apk_assets;
    110   return JNI_TRUE;
    111 }
    112 
    113 static jlong NativeOpenXml(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring file_name) {
    114   ScopedUtfChars path_utf8(env, file_name);
    115   if (path_utf8.c_str() == nullptr) {
    116     return 0;
    117   }
    118 
    119   const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr);
    120   std::unique_ptr<Asset> asset = apk_assets->Open(path_utf8.c_str(),
    121                                                   Asset::AccessMode::ACCESS_RANDOM);
    122   if (asset == nullptr) {
    123     jniThrowException(env, "java/io/FileNotFoundException", path_utf8.c_str());
    124     return 0;
    125   }
    126 
    127   // DynamicRefTable is only needed when looking up resource references. Opening an XML file
    128   // directly from an ApkAssets has no notion of proper resource references.
    129   std::unique_ptr<ResXMLTree> xml_tree = util::make_unique<ResXMLTree>(nullptr /*dynamicRefTable*/);
    130   status_t err = xml_tree->setTo(asset->getBuffer(true), asset->getLength(), true);
    131   asset.reset();
    132 
    133   if (err != NO_ERROR) {
    134     jniThrowException(env, "java/io/FileNotFoundException", "Corrupt XML binary file");
    135     return 0;
    136   }
    137   return reinterpret_cast<jlong>(xml_tree.release());
    138 }
    139 
    140 // JNI registration.
    141 static const JNINativeMethod gApkAssetsMethods[] = {
    142     {"nativeLoad", "(Ljava/lang/String;ZZZ)J", (void*)NativeLoad},
    143     {"nativeLoadFromFd", "(Ljava/io/FileDescriptor;Ljava/lang/String;ZZ)J",
    144         (void*)NativeLoadFromFd},
    145     {"nativeDestroy", "(J)V", (void*)NativeDestroy},
    146     {"nativeGetAssetPath", "(J)Ljava/lang/String;", (void*)NativeGetAssetPath},
    147     {"nativeGetStringBlock", "(J)J", (void*)NativeGetStringBlock},
    148     {"nativeIsUpToDate", "(J)Z", (void*)NativeIsUpToDate},
    149     {"nativeOpenXml", "(JLjava/lang/String;)J", (void*)NativeOpenXml},
    150 };
    151 
    152 int register_android_content_res_ApkAssets(JNIEnv* env) {
    153   return RegisterMethodsOrDie(env, "android/content/res/ApkAssets", gApkAssetsMethods,
    154                               arraysize(gApkAssetsMethods));
    155 }
    156 
    157 }  // namespace android
    158