Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2d9c4afc authored by Abhijith Nair's avatar Abhijith Nair
Browse files

Disallow WV APIs being used from different threads

Currently we throw an exception if WebView APIs are
used from different threads if the target SDK version
of the app is >= JBMR2 unless the app uses reflection
to alter the value of the sEnforceThreadChecking variable.

Given that Android ToT doesn't allow apps to target
<JMBR2, the only way apps can get around this restriction
is through reflection.

This CL adds a flag to always enforce thread checking.
Bug: 440569275
Test: Existing presubmit tests
Flag: android.webkit.always_enforce_thread_checking

Change-Id: I7dc83c640829a664d68521904a0cc17c3a6536f5
parent f96b7617
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -121,8 +121,10 @@ public class WebView extends AbsoluteLayout
    // Throwing an exception for incorrect thread usage if the
    // build target is JB MR2 or newer. Defaults to false, and is
    // set in the WebView constructor.
    // If `alwaysEnforceThreadChecking` flag is true, we will always throw an exception
    // irrespective of the value of this field.
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    private static volatile boolean sEnforceThreadChecking = false;
    private static volatile boolean sEnforceThreadChecking = Flags.alwaysEnforceThreadChecking();

    /**
     *  Transportation object for returning WebView across thread boundaries.
@@ -437,8 +439,10 @@ public class WebView extends AbsoluteLayout
            throw new RuntimeException(
                "WebView cannot be initialized on a thread that has no Looper.");
        }
        sEnforceThreadChecking = context.getApplicationInfo().targetSdkVersion >=
                Build.VERSION_CODES.JELLY_BEAN_MR2;
        if (!Flags.alwaysEnforceThreadChecking()) {
            sEnforceThreadChecking = context.getApplicationInfo().targetSdkVersion
                    >= Build.VERSION_CODES.JELLY_BEAN_MR2;
        }
        checkThread();

        ensureProviderCreated();
@@ -2661,7 +2665,7 @@ public class WebView extends AbsoluteLayout
            Log.w(LOGTAG, Log.getStackTraceString(throwable));
            StrictMode.onWebViewMethodCalledOnWrongThread(throwable);

            if (sEnforceThreadChecking) {
            if (Flags.alwaysEnforceThreadChecking() || sEnforceThreadChecking) {
                throw new RuntimeException(throwable);
            }
        }
+7 −0
Original line number Diff line number Diff line
@@ -42,3 +42,10 @@ flag {
    description: "New APIs required by File System Access"
    bug: "40101963"
}

flag {
    name: "always_enforce_thread_checking"
    namespace: "webview"
    description: "Always enforce that WebView APIs are called on the same thread"
    bug: "440569275"
}
 No newline at end of file