Commit 1b9f6bf9 authored by primiano@chromium.org's avatar primiano@chromium.org

[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
parent c2baa092
// 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};
}
}
}
......@@ -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 },
......
// 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
// 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_
......@@ -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',
......
......@@ -167,6 +167,19 @@ void JavaIntArrayToIntVector(JNIEnv* env,
env->ReleaseIntArrayElements(int_array, ints, JNI_ABORT);
}
void JavaLongArrayToLongVector(JNIEnv* env,
jlongArray long_array,
std::vector<long>* out) {
DCHECK(out);
out->clear();
jsize len = env->GetArrayLength(long_array);
jlong* longs = env->GetLongArrayElements(long_array, NULL);
for (jsize i = 0; i < len; ++i) {
out->push_back(static_cast<long>(longs[i]));
}
env->ReleaseLongArrayElements(long_array, longs, JNI_ABORT);
}
void JavaFloatArrayToFloatVector(JNIEnv* env,
jfloatArray float_array,
std::vector<float>* out) {
......
......@@ -73,6 +73,12 @@ BASE_EXPORT void JavaIntArrayToIntVector(
jintArray int_array,
std::vector<int>* out);
// Replaces the content of |out| with the Java longs in |long_array|.
BASE_EXPORT void JavaLongArrayToLongVector(
JNIEnv* env,
jlongArray long_array,
std::vector<long>* out);
// Replaces the content of |out| with the Java floats in |float_array|.
BASE_EXPORT void JavaFloatArrayToFloatVector(
JNIEnv* env,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment