Skip to content

Commit

Permalink
webview: metric for UI thread chosen in old apps.
Browse files Browse the repository at this point in the history
Apps that target pre-JB MR2 have relaxed threading requirements for
WebView. Add a metric that tracks whether deferring the decision of
which thread is the UI thread was actually necessary or not.

This may help us determine how widely this compatibility behaviour is
still relied upon.

Bug: 1106486
Change-Id: Ia0b56930f491d3dd2d42f6cc3197aa1eb23311e1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2303771
Commit-Queue: Richard Coles <[email protected]>
Reviewed-by: Changwan Ryu <[email protected]>
Reviewed-by: Brian White <[email protected]>
Auto-Submit: Richard Coles <[email protected]>
Cr-Commit-Position: refs/heads/master@{#814200}
  • Loading branch information
tornewuff authored and Commit Bot committed Oct 6, 2020
1 parent ef22e39 commit 90706d2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ public void init(final Map<String, Object> javaScriptInterfaces,
} else if (!mFactory.hasStarted()) {
if (Looper.myLooper() == Looper.getMainLooper()) {
mFactory.startYourEngines(true);
} else {
// Record which thread we're on now so we can track whether the final UI thread
// decision differed.
mFactory.getAwInit().setFirstWebViewConstructedOn(Looper.myLooper());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.webkit.WebStorage;
import android.webkit.WebViewDatabase;

import androidx.annotation.IntDef;

import org.chromium.android_webview.AwBrowserContext;
import org.chromium.android_webview.AwBrowserProcess;
import org.chromium.android_webview.AwContents;
Expand Down Expand Up @@ -88,6 +90,7 @@ public class WebViewChromiumAwInit {

// Read/write protected by mLock.
private boolean mStarted;
private Looper mFirstWebViewConstructedOn;

private final WebViewChromiumFactoryProvider mFactory;

Expand Down Expand Up @@ -243,6 +246,32 @@ private void setUpResources(int packageId, Context context) {
}
}

// Only called for apps which target <JB MR2, and which construct WebView on a non-main thread.
void setFirstWebViewConstructedOn(Looper looper) {
synchronized (mLock) {
if (!mStarted && mFirstWebViewConstructedOn == null) {
mFirstWebViewConstructedOn = looper;
}
}
}

// Used to record the UMA histogram Android.WebView.ActualUiThread. Since these values are
// persisted to logs, they should never be renumbered or reused.
@IntDef({ActualUiThread.FIRST_WEBVIEW_CONSTRUCTED, ActualUiThread.MAIN_LOOPER,
ActualUiThread.OTHER})
@interface ActualUiThread {
int FIRST_WEBVIEW_CONSTRUCTED = 0;
int MAIN_LOOPER = 1;
int OTHER = 2;

int COUNT = 3;
}

private static void recordActualUiThread(@ActualUiThread int value) {
RecordHistogram.recordEnumeratedHistogram(
"Android.WebView.ActualUiThread", value, ActualUiThread.COUNT);
}

boolean hasStarted() {
return mStarted;
}
Expand All @@ -268,6 +297,23 @@ void startYourEngines(boolean onMainThread) {
+ " looper " + looper);
ThreadUtils.setUiThread(looper);

// For apps targeting <JBMR2 which aren't required to commit to a thread in
// WebViewChromium.init, record a metric stating which thread we picked.
if (mFirstWebViewConstructedOn != null) {
if (looper == mFirstWebViewConstructedOn) {
// Using the same thread that the first WebView was constructed on.
recordActualUiThread(ActualUiThread.FIRST_WEBVIEW_CONSTRUCTED);
} else if (looper == Looper.getMainLooper()) {
// Using the main looper.
recordActualUiThread(ActualUiThread.MAIN_LOOPER);
} else {
// Using some other thread.
recordActualUiThread(ActualUiThread.OTHER);
}
// Reset to null to avoid leaking the app's looper.
mFirstWebViewConstructedOn = null;
}

if (ThreadUtils.runningOnUiThread()) {
startChromiumLocked();
return;
Expand Down
6 changes: 6 additions & 0 deletions tools/metrics/histograms/enums.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,12 @@ Unknown properties are collapsed to zero. -->
<int value="4" label="Undos Dismissed (Action) (Deprecated)"/>
</enum>

<enum name="AndroidWebViewActualUiThread">
<int value="0" label="The thread on which the first WebView was constructed"/>
<int value="1" label="The main looper"/>
<int value="2" label="Some other thread"/>
</enum>

<enum name="AndroidWebViewClientSafeBrowsingThreatType">
<obsolete>
Removed 04/2020. These metrics are tracked generally for SafeBrowsing, and
Expand Down
11 changes: 11 additions & 0 deletions tools/metrics/histograms/histograms_xml/android/histograms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,17 @@ reviews. Googlers can read more about this at go/gwsq-gerrit.
</summary>
</histogram>

<histogram name="Android.WebView.ActualUiThread"
enum="AndroidWebViewActualUiThread" expires_after="2021-03-31">
<owner>[email protected]</owner>
<owner>src/android_webview/OWNERS</owner>
<summary>
Recorded once per startup for apps targeting releases before JB MR2 if they
create their first WebView off the main thread. This records which thread
they actually ended up using as their UI thread.
</summary>
</histogram>

<histogram name="Android.WebView.AndroidProtocolHandler.ResourceGetIdentifier"
enum="BooleanSuccess" expires_after="2021-01-30">
<owner>[email protected]</owner>
Expand Down

0 comments on commit 90706d2

Please sign in to comment.