forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
primiano@chromium.org
committed
Jul 18, 2014
1 parent
c2baa09
commit 1b9f6bf
Showing
7 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
android_webview/java/src/org/chromium/android_webview/AwAssets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters