• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <jni.h>
6 
7 #include "base/android/apk_assets.h"
8 
9 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/debug/crash_logging.h"
13 #include "base/debug/dump_without_crashing.h"
14 #include "base/file_descriptor_store.h"
15 
16 // Must come after all headers that specialize FromJniType() / ToJniType().
17 #include "base/base_jni/ApkAssets_jni.h"
18 
19 namespace base {
20 namespace android {
21 
OpenApkAsset(const std::string & file_path,const std::string & split_name,base::MemoryMappedFile::Region * region)22 int OpenApkAsset(const std::string& file_path,
23                  const std::string& split_name,
24                  base::MemoryMappedFile::Region* region) {
25   // The AssetManager API of the NDK does not expose a method for accessing raw
26   // resources :(
27   JNIEnv* env = base::android::AttachCurrentThread();
28   ScopedJavaLocalRef<jlongArray> jarr =
29       Java_ApkAssets_open(env, ConvertUTF8ToJavaString(env, file_path),
30                           ConvertUTF8ToJavaString(env, split_name));
31   std::vector<jlong> results;
32   base::android::JavaLongArrayToLongVector(env, jarr, &results);
33   CHECK_EQ(3U, results.size());
34   int fd = static_cast<int>(results[0]);
35   region->offset = results[1];
36   // Not a checked_cast because open() may return -1.
37   region->size = static_cast<size_t>(results[2]);
38   return fd;
39 }
40 
OpenApkAsset(const std::string & file_path,base::MemoryMappedFile::Region * region)41 int OpenApkAsset(const std::string& file_path,
42                  base::MemoryMappedFile::Region* region) {
43   return OpenApkAsset(file_path, std::string(), region);
44 }
45 
RegisterApkAssetWithFileDescriptorStore(const std::string & key,const base::FilePath & file_path)46 bool RegisterApkAssetWithFileDescriptorStore(const std::string& key,
47                                              const base::FilePath& file_path) {
48   base::MemoryMappedFile::Region region =
49       base::MemoryMappedFile::Region::kWholeFile;
50   int asset_fd = OpenApkAsset(file_path.value(), &region);
51   if (asset_fd == -1)
52     return false;
53   base::FileDescriptorStore::GetInstance().Set(key, base::ScopedFD(asset_fd),
54                                                region);
55   return true;
56 }
57 
DumpLastOpenApkAssetFailure()58 void DumpLastOpenApkAssetFailure() {
59   JNIEnv* env = base::android::AttachCurrentThread();
60   base::android::ScopedJavaLocalRef<jstring> error =
61       Java_ApkAssets_takeLastErrorString(env);
62   if (!error) {
63     return;
64   }
65   SCOPED_CRASH_KEY_STRING256("base", "OpenApkAssetError",
66                              ConvertJavaStringToUTF8(env, error));
67   base::debug::DumpWithoutCrashing();
68 }
69 
70 }  // namespace android
71 }  // namespace base
72