OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 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 "jni/ApkAssets_jni.h" |
| 13 |
| 14 namespace base { |
| 15 namespace android { |
| 16 |
| 17 bool RegisterApkAssets(JNIEnv* env) { |
| 18 return RegisterNativesImpl(env); |
| 19 } |
| 20 |
| 21 int OpenApkAsset(const std::string& file_path, |
| 22 base::MemoryMappedFile::Region* region) { |
| 23 // The AAssetManager API of the NDK is does not expose a method for accessing |
| 24 // raw resources :(. |
| 25 JNIEnv* env = base::android::AttachCurrentThread(); |
| 26 ScopedJavaLocalRef<jlongArray> jarr = Java_ApkAssets_open( |
| 27 env, |
| 28 base::android::GetApplicationContext(), |
| 29 base::android::ConvertUTF8ToJavaString(env, file_path).Release()); |
| 30 std::vector<jlong> results; |
| 31 base::android::JavaLongArrayToLongVector(env, jarr.obj(), &results); |
| 32 CHECK_EQ(3U, results.size()); |
| 33 int fd = static_cast<int>(results[0]); |
| 34 region->offset = results[1]; |
| 35 region->size = results[2]; |
| 36 return fd; |
| 37 } |
| 38 |
| 39 bool RegisterApkAssetWithGlobalDescriptors(base::GlobalDescriptors::Key key, |
| 40 const std::string& file_path) { |
| 41 base::MemoryMappedFile::Region region = |
| 42 base::MemoryMappedFile::Region::kWholeFile; |
| 43 int asset_fd = OpenApkAsset(file_path, ®ion); |
| 44 if (asset_fd != -1) { |
| 45 base::GlobalDescriptors::GetInstance()->Set(key, asset_fd, region); |
| 46 } |
| 47 return asset_fd != -1; |
| 48 } |
| 49 |
| 50 } // namespace android |
| 51 } // namespace base |
OLD | NEW |