Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: base/android/apk_assets.cc

Issue 1156573008: Allow OpenApkAsset() to open resource files as well as assets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/android/apk_assets.h ('k') | base/android/java/src/org/chromium/base/ApkAssets.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/jni_array.h" 9 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h" 11 #include "base/android/scoped_java_ref.h"
12 #include "jni/ApkAssets_jni.h" 12 #include "jni/ApkAssets_jni.h"
13 13
14 namespace base { 14 namespace base {
15 namespace android { 15 namespace android {
16 16
17 bool RegisterApkAssets(JNIEnv* env) { 17 bool RegisterApkAssets(JNIEnv* env) {
18 return RegisterNativesImpl(env); 18 return RegisterNativesImpl(env);
19 } 19 }
20 20
21 int OpenApkAsset(const std::string& filename, 21 int OpenApkAsset(const std::string& file_path,
22 base::MemoryMappedFile::Region* region) { 22 base::MemoryMappedFile::Region* region) {
23 // The AAssetManager API of the NDK is does not expose a method for accessing
24 // raw resources :(.
23 JNIEnv* env = base::android::AttachCurrentThread(); 25 JNIEnv* env = base::android::AttachCurrentThread();
24 ScopedJavaLocalRef<jlongArray> jarr = Java_ApkAssets_openAsset( 26 ScopedJavaLocalRef<jlongArray> jarr = Java_ApkAssets_open(
25 env, 27 env,
26 base::android::GetApplicationContext(), 28 base::android::GetApplicationContext(),
27 base::android::ConvertUTF8ToJavaString(env, filename).Release()); 29 base::android::ConvertUTF8ToJavaString(env, file_path).Release());
28 std::vector<jlong> results; 30 std::vector<jlong> results;
29 base::android::JavaLongArrayToLongVector(env, jarr.obj(), &results); 31 base::android::JavaLongArrayToLongVector(env, jarr.obj(), &results);
30 CHECK_EQ(3U, results.size()); 32 CHECK_EQ(3U, results.size());
31 int fd = static_cast<int>(results[0]); 33 int fd = static_cast<int>(results[0]);
32 region->offset = results[1]; 34 region->offset = results[1];
33 region->size = results[2]; 35 region->size = results[2];
34 return fd; 36 return fd;
35 } 37 }
36 38
37 bool RegisterApkAssetWithGlobalDescriptors(base::GlobalDescriptors::Key key, 39 bool RegisterApkAssetWithGlobalDescriptors(base::GlobalDescriptors::Key key,
38 const std::string& asset_filename) { 40 const std::string& file_path) {
39 base::MemoryMappedFile::Region region = 41 base::MemoryMappedFile::Region region =
40 base::MemoryMappedFile::Region::kWholeFile; 42 base::MemoryMappedFile::Region::kWholeFile;
41 int asset_fd = OpenApkAsset(asset_filename, &region); 43 int asset_fd = OpenApkAsset(file_path, &region);
42 if (asset_fd != -1) { 44 if (asset_fd != -1) {
43 base::GlobalDescriptors::GetInstance()->Set(key, asset_fd, region); 45 base::GlobalDescriptors::GetInstance()->Set(key, asset_fd, region);
44 } 46 }
45 return asset_fd != -1; 47 return asset_fd != -1;
46 } 48 }
47 49
48 } // namespace android 50 } // namespace android
49 } // namespace base 51 } // namespace base
OLDNEW
« no previous file with comments | « base/android/apk_assets.h ('k') | base/android/java/src/org/chromium/base/ApkAssets.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698