OSDN Git Service

Do not require WebView usage to come from main thread
authorJonathan Dixon <joth@google.com>
Tue, 8 Oct 2013 20:32:11 +0000 (13:32 -0700)
committerJonathan Dixon <joth@google.com>
Wed, 9 Oct 2013 19:03:07 +0000 (12:03 -0700)
So long as all usage is from a single thread per instance it is OK if
it's a non-main thread.
(But note that for apps targeting JB MR2 this was enforced to be the
main thread anyway).

Bug 10937207

Change-Id: Ibc2496d5cef97b4685e001086f712fcaac231024

core/java/android/webkit/WebView.java

index 2cbe0e2..5bc39f1 100644 (file)
@@ -670,7 +670,6 @@ public class WebView extends AbsoluteLayout
      */
     @Deprecated
     public static void enablePlatformNotifications() {
-        checkThread();
         getFactory().getStatics().setPlatformNotificationsEnabled(true);
     }
 
@@ -683,7 +682,6 @@ public class WebView extends AbsoluteLayout
      */
     @Deprecated
     public static void disablePlatformNotifications() {
-        checkThread();
         getFactory().getStatics().setPlatformNotificationsEnabled(false);
     }
 
@@ -1691,7 +1689,6 @@ public class WebView extends AbsoluteLayout
      * @param enabled whether to enable web contents debugging
      */
     public static void setWebContentsDebuggingEnabled(boolean enabled) {
-        checkThread();
         getFactory().getStatics().setWebContentsDebuggingEnabled(enabled);
     }
 
@@ -1704,7 +1701,6 @@ public class WebView extends AbsoluteLayout
      */
     @Deprecated
     public static synchronized PluginList getPluginList() {
-        checkThread();
         return new PluginList();
     }
 
@@ -2058,13 +2054,18 @@ public class WebView extends AbsoluteLayout
         return WebViewFactory.getProvider();
     }
 
-    private static void checkThread() {
-        if (Looper.myLooper() != Looper.getMainLooper()) {
+    private final Looper mWebViewThread = Looper.myLooper();
+
+    private void checkThread() {
+        // Ignore mWebViewThread == null because this can be called during in the super class
+        // constructor, before this class's own constructor has even started.
+        if (mWebViewThread != null && Looper.myLooper() != mWebViewThread) {
             Throwable throwable = new Throwable(
-                    "Warning: A WebView method was called on thread '" +
+                    "A WebView method was called on thread '" +
                     Thread.currentThread().getName() + "'. " +
-                    "All WebView methods must be called on the UI thread. " +
-                    "Future versions of WebView may not support use on other threads.");
+                    "All WebView methods must be called on the same thread. " +
+                    "(Expected Looper " + mWebViewThread + " called on " + Looper.myLooper() +
+                    ", FYI main Looper is " + Looper.getMainLooper() + ")");
             Log.w(LOGTAG, Log.getStackTraceString(throwable));
             StrictMode.onWebViewMethodCalledOnWrongThread(throwable);