OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <jni.h> | 5 #include <jni.h> |
6 | 6 |
7 #include "base/android/apk_assets.h" | 7 #include "base/android/apk_assets.h" |
8 | 8 |
9 #include "base/android/context_utils.h" | 9 #include "base/android/context_utils.h" |
10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
13 #include "base/file_descriptor_store.h" | |
13 #include "jni/ApkAssets_jni.h" | 14 #include "jni/ApkAssets_jni.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 namespace android { | 17 namespace android { |
17 | 18 |
18 int OpenApkAsset(const std::string& file_path, | 19 int OpenApkAsset(const std::string& file_path, |
19 base::MemoryMappedFile::Region* region) { | 20 base::MemoryMappedFile::Region* region) { |
20 // The AAssetManager API of the NDK is does not expose a method for accessing | 21 // The AssetManager API of the NDK does not expose a method for accessing raw |
21 // raw resources :(. | 22 // resources :( |
22 JNIEnv* env = base::android::AttachCurrentThread(); | 23 JNIEnv* env = base::android::AttachCurrentThread(); |
23 ScopedJavaLocalRef<jlongArray> jarr = Java_ApkAssets_open( | 24 ScopedJavaLocalRef<jlongArray> jarr = Java_ApkAssets_open( |
24 env, base::android::GetApplicationContext(), | 25 env, base::android::GetApplicationContext(), |
25 base::android::ConvertUTF8ToJavaString(env, file_path)); | 26 base::android::ConvertUTF8ToJavaString(env, file_path)); |
26 std::vector<jlong> results; | 27 std::vector<jlong> results; |
27 base::android::JavaLongArrayToLongVector(env, jarr.obj(), &results); | 28 base::android::JavaLongArrayToLongVector(env, jarr.obj(), &results); |
28 CHECK_EQ(3U, results.size()); | 29 CHECK_EQ(3U, results.size()); |
29 int fd = static_cast<int>(results[0]); | 30 int fd = static_cast<int>(results[0]); |
30 region->offset = results[1]; | 31 region->offset = results[1]; |
31 region->size = results[2]; | 32 region->size = results[2]; |
32 return fd; | 33 return fd; |
33 } | 34 } |
34 | 35 |
36 bool RegisterApkAssetWithFileDescriptorStore(const std::string& key, | |
37 const std::string& file_path) { | |
38 base::MemoryMappedFile::Region region = | |
39 base::MemoryMappedFile::Region::kWholeFile; | |
40 int asset_fd = OpenApkAsset(file_path, ®ion); | |
41 if (asset_fd == -1) | |
42 return false; | |
nyquist
2017/02/10 23:37:50
Should the function for global descriptors follow
Jay Civelli
2017/02/13 18:48:25
Anything to make you happy, Tommy :-)
| |
43 base::FileDescriptorStore::GetInstance().Set(key, base::ScopedFD(asset_fd), | |
44 region); | |
45 return true; | |
46 } | |
47 | |
35 bool RegisterApkAssetWithGlobalDescriptors(base::GlobalDescriptors::Key key, | 48 bool RegisterApkAssetWithGlobalDescriptors(base::GlobalDescriptors::Key key, |
36 const std::string& file_path) { | 49 const std::string& file_path) { |
37 base::MemoryMappedFile::Region region = | 50 base::MemoryMappedFile::Region region = |
38 base::MemoryMappedFile::Region::kWholeFile; | 51 base::MemoryMappedFile::Region::kWholeFile; |
39 int asset_fd = OpenApkAsset(file_path, ®ion); | 52 int asset_fd = OpenApkAsset(file_path, ®ion); |
40 if (asset_fd != -1) { | 53 if (asset_fd != -1) { |
41 base::GlobalDescriptors::GetInstance()->Set(key, asset_fd, region); | 54 base::GlobalDescriptors::GetInstance()->Set(key, asset_fd, region); |
42 } | 55 } |
43 return asset_fd != -1; | 56 return asset_fd != -1; |
44 } | 57 } |
45 | 58 |
46 } // namespace android | 59 } // namespace android |
47 } // namespace base | 60 } // namespace base |
OLD | NEW |