diff options
author | primiano@chromium.org <primiano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-18 22:50:46 +0000 |
---|---|---|
committer | primiano@chromium.org <primiano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-18 22:50:46 +0000 |
commit | 1b9f6bf9c0126652ae2b1aea33fc8032832472f0 (patch) | |
tree | 425ab9aa4b62da16e3114dc010937dfb140fd73c /android_webview | |
parent | c2baa092fcb370d3788ab6ae53079773706e7158 (diff) | |
download | chromium_src-1b9f6bf9c0126652ae2b1aea33fc8032832472f0.zip chromium_src-1b9f6bf9c0126652ae2b1aea33fc8032832472f0.tar.gz chromium_src-1b9f6bf9c0126652ae2b1aea33fc8032832472f0.tar.bz2 |
[android_webview] Introduce AwAssets to reference assets inside the apk.
This change introduces a utility class AwAssets, accessible by native,
which is able to retrieve references (fd + offset + size) of assets
inside the apk. This is to enable direct mmap of uncompressed assets.
This change does NOT introduce yet any change to the WebView apk itself.
At current state, no behavioral change is intended.
BUG=394502
Review URL: https://codereview.chromium.org/401743003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284238 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'android_webview')
-rw-r--r-- | android_webview/java/src/org/chromium/android_webview/AwAssets.java | 38 | ||||
-rw-r--r-- | android_webview/native/android_webview_jni_registrar.cc | 2 | ||||
-rw-r--r-- | android_webview/native/aw_assets.cc | 41 | ||||
-rw-r--r-- | android_webview/native/aw_assets.h | 32 | ||||
-rw-r--r-- | android_webview/native/webview_native.gyp | 3 |
5 files changed, 116 insertions, 0 deletions
diff --git a/android_webview/java/src/org/chromium/android_webview/AwAssets.java b/android_webview/java/src/org/chromium/android_webview/AwAssets.java new file mode 100644 index 0000000..97e292f --- /dev/null +++ b/android_webview/java/src/org/chromium/android_webview/AwAssets.java @@ -0,0 +1,38 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.android_webview; + +import android.content.Context; +import android.content.res.AssetFileDescriptor; +import android.content.res.AssetManager; +import android.util.Log; + +import org.chromium.base.CalledByNative; +import org.chromium.base.JNINamespace; + +import java.io.IOException; + +/** + * A utility class to retrieve references to uncompressed assets insides the apk. A reference is + * defined as tuple (file descriptor, offset, size) enabling direct mapping without deflation. + */ +@JNINamespace("android_webview") +public class AwAssets { + private static final String LOGTAG = "AwAssets"; + + @CalledByNative + public static long[] openAsset(Context context, String fileName) { + try { + AssetManager manager = context.getAssets(); + AssetFileDescriptor afd = manager.openFd(fileName); + return new long[] { afd.getParcelFileDescriptor().detachFd(), + afd.getStartOffset(), + afd.getLength() }; + } catch (IOException e) { + Log.e(LOGTAG, "Error while loading asset " + fileName + ": " + e.getMessage()); + return new long[] {-1, -1, -1}; + } + } +} diff --git a/android_webview/native/android_webview_jni_registrar.cc b/android_webview/native/android_webview_jni_registrar.cc index 8db9713..253a070 100644 --- a/android_webview/native/android_webview_jni_registrar.cc +++ b/android_webview/native/android_webview_jni_registrar.cc @@ -5,6 +5,7 @@ #include "android_webview/native/android_webview_jni_registrar.h" #include "android_webview/native/android_protocol_handler.h" +#include "android_webview/native/aw_assets.h" #include "android_webview/native/aw_autofill_client.h" #include "android_webview/native/aw_contents.h" #include "android_webview/native/aw_contents_client_bridge.h" @@ -35,6 +36,7 @@ static base::android::RegistrationMethod kWebViewRegisteredMethods[] = { // Register JNI for android_webview classes. { "AndroidProtocolHandler", RegisterAndroidProtocolHandler }, { "AwAutofillClient", RegisterAwAutofillClient }, + { "AwAssets", RegisterAwAssets }, { "AwContents", RegisterAwContents }, { "AwContentsClientBridge", RegisterAwContentsClientBridge }, { "AwContentsIoThreadClientImpl", RegisterAwContentsIoThreadClientImpl }, diff --git a/android_webview/native/aw_assets.cc b/android_webview/native/aw_assets.cc new file mode 100644 index 0000000..6d5ad72 --- /dev/null +++ b/android_webview/native/aw_assets.cc @@ -0,0 +1,41 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <jni.h> + +#include "android_webview/native/aw_assets.h" + +#include "base/android/jni_array.h" +#include "base/android/jni_string.h" +#include "base/android/scoped_java_ref.h" +#include "jni/AwAssets_jni.h" + +namespace android_webview { +namespace AwAssets { + +bool OpenAsset(const std::string& filename, + int* fd, + int64* offset, + int64* size) { + JNIEnv* env = base::android::AttachCurrentThread(); + ScopedJavaLocalRef<jlongArray> jarr = Java_AwAssets_openAsset( + env, + base::android::GetApplicationContext(), + base::android::ConvertUTF8ToJavaString(env, filename).Release()); + std::vector<long> results; + base::android::JavaLongArrayToLongVector(env, jarr.obj(), &results); + DCHECK_EQ(3U, results.size()); + *fd = static_cast<int>(results[0]); + *offset = results[1]; + *size = results[2]; + return *fd != -1; +} + +} // namespace AwAssets + +bool RegisterAwAssets(JNIEnv* env) { + return RegisterNativesImpl(env); +} + +} // namespace android_webview diff --git a/android_webview/native/aw_assets.h b/android_webview/native/aw_assets.h new file mode 100644 index 0000000..229392c --- /dev/null +++ b/android_webview/native/aw_assets.h @@ -0,0 +1,32 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ANDROID_WEBVIEW_NATIVE_AW_ASSETS_H_ +#define ANDROID_WEBVIEW_NATIVE_AW_ASSETS_H_ + +#include <string> + +#include "base/android/jni_android.h" + +namespace android_webview { +namespace AwAssets { + +// Called by native to retrieve an asset (e.g. a .pak file) from the apk. +// Returns: true in case of success, false otherwise. +// Output arguments: +// - |fd|: file descriptor to the apk. The caller takes the ownership. +// - |offset|: offset in bytes from the start of the file +// - |size|: size in bytes of the asset / resource. +bool OpenAsset(const std::string& filename, + int* fd, + int64* offset, + int64* size); + +} // namespace AwAssets + +bool RegisterAwAssets(JNIEnv* env); + +} // namsespace android_webview + +#endif // ANDROID_WEBVIEW_NATIVE_AW_ASSETS_H_ diff --git a/android_webview/native/webview_native.gyp b/android_webview/native/webview_native.gyp index 83f68d9..2e965e6 100644 --- a/android_webview/native/webview_native.gyp +++ b/android_webview/native/webview_native.gyp @@ -35,6 +35,8 @@ 'android_protocol_handler.h', 'android_webview_jni_registrar.cc', 'android_webview_jni_registrar.h', + 'aw_assets.cc', + 'aw_assets.h', 'aw_autofill_client.cc', 'aw_autofill_client.h', 'aw_browser_dependency_factory.cc', @@ -125,6 +127,7 @@ 'type': 'none', 'sources': [ '../java/src/org/chromium/android_webview/AndroidProtocolHandler.java', + '../java/src/org/chromium/android_webview/AwAssets.java', '../java/src/org/chromium/android_webview/AwAutofillClient.java', '../java/src/org/chromium/android_webview/AwContents.java', '../java/src/org/chromium/android_webview/AwContentsClientBridge.java', |