Skip to content

Commit

Permalink
[AW] lookup resource id in AndroidProtocolHandler using getIdentifier
Browse files Browse the repository at this point in the history
Use android.content.res.Resources#getIdentifier to lookup app resource
id and fall back to class look up by reflection if it fails. Looking up
resources this way may help fixing this long standing bug b/37102241.

This also record a boolean histogram to track lookup success and
failure.

Bug: 923956, b/37102241
Test: Build an app with gradle and applicationId and test load a resource file in WebView
Change-Id: Id42935b763f52572e184ba8564eb95f38df63171
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2288556
Commit-Queue: Hazem Ashmawy <hazems@chromium.org>
Reviewed-by: Brian White <bcwhite@chromium.org>
Reviewed-by: Changwan Ryu <changwan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#787495}
  • Loading branch information
HazemSamir authored and Commit Bot committed Jul 11, 2020
1 parent 4b71ef9 commit 7f46ee7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package org.chromium.android_webview;

import android.content.Context;
import android.content.res.AssetManager;
import android.net.Uri;
import android.util.Log;
Expand All @@ -13,6 +14,7 @@
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.metrics.RecordHistogram;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -93,8 +95,18 @@ private static Class<?> getClazz(String packageName, String assetType)

private static int getFieldId(String assetType, String assetName)
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Context appContext = ContextUtils.getApplicationContext();
String packageName = appContext.getPackageName();
int id = appContext.getResources().getIdentifier(assetName, assetType, packageName);
if (id != 0) {
RecordHistogram.recordBooleanHistogram(
"Android.WebView.AndroidProtocolHandler.ResourceGetIdentifier", true);
return id;
}
// Resource id can't be found using Resources class so fallback to reflection.
// TODO(https://crbug.com/923956) remove reflection fallback if the histogram is always
// true.
Class<?> clazz = null;
String packageName = ContextUtils.getApplicationContext().getPackageName();
try {
clazz = getClazz(packageName, assetType);
} catch (ClassNotFoundException e) {
Expand All @@ -111,9 +123,10 @@ private static int getFieldId(String assetType, String assetName)
}
}
}

RecordHistogram.recordBooleanHistogram(
"Android.WebView.AndroidProtocolHandler.ResourceGetIdentifier", false);
java.lang.reflect.Field field = clazz.getField(assetName);
int id = field.getInt(null);
id = field.getInt(null);
return id;
}

Expand Down
11 changes: 11 additions & 0 deletions tools/metrics/histograms/histograms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4976,6 +4976,17 @@ reviews. Googlers can read more about this at go/gwsq-gerrit.
</summary>
</histogram>

<histogram name="Android.WebView.AndroidProtocolHandler.ResourceGetIdentifier"
enum="BooleanSuccess" expires_after="2021-01-30">
<owner>hazems@chromium.org</owner>
<owner>src/android_webview/OWNERS</owner>
<summary>
Track if looking up Android app resource id using
android.content.res.Resources#getIdentifier succeeds in
AndroidProtocolHandler or if it falls back to reflection look up.
</summary>
</histogram>

<histogram name="Android.WebView.AndroidX.ApiCall" enum="AndroidXWebkitApiCall"
expires_after="2021-01-29">
<owner>laisminchillo@chromium.org</owner>
Expand Down

0 comments on commit 7f46ee7

Please sign in to comment.