From acf35641688397d388e7d4954f0ca1ec28d1bb87 Mon Sep 17 00:00:00 2001 From: Agi Sferro Date: Fri, 27 Mar 2020 21:20:19 +0000 Subject: [PATCH] Bug 1624675 - Don't post on UI thread if already on the UI thread. r=snorp Differential Revision: https://phabricator.services.mozilla.com/D68434 --HG-- extra : moz-landing-system : lando --- .../org/mozilla/gecko/AndroidGamepadManager.java | 6 +++--- .../java/org/mozilla/gecko/GeckoAppShell.java | 4 ++-- .../mozilla/gecko/GeckoScreenOrientation.java | 2 +- .../gecko/permissions/PermissionBlock.java | 2 +- .../gecko/process/GeckoServiceChildProcess.java | 2 +- .../java/org/mozilla/gecko/util/ThreadUtils.java | 16 ++++++++++++++++ .../org/mozilla/geckoview/GeckoEditable.java | 4 ++-- .../mozilla/geckoview/GeckoInputConnection.java | 2 +- .../java/org/mozilla/geckoview/GeckoRuntime.java | 10 +++++----- .../java/org/mozilla/geckoview/GeckoSession.java | 11 +++-------- .../mozilla/geckoview/SessionAccessibility.java | 2 +- 11 files changed, 36 insertions(+), 25 deletions(-) diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/AndroidGamepadManager.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/AndroidGamepadManager.java index 26b9064b9185..0ff791494f9a 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/AndroidGamepadManager.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/AndroidGamepadManager.java @@ -147,7 +147,7 @@ public class AndroidGamepadManager { @WrapForJNI private static void start(final Context context) { - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { doStart(context); @@ -166,7 +166,7 @@ public class AndroidGamepadManager { @WrapForJNI private static void stop(final Context context) { - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { doStop(context); @@ -186,7 +186,7 @@ public class AndroidGamepadManager { @WrapForJNI private static void onGamepadAdded(final int deviceId, final int serviceId) { - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { handleGamepadAdded(deviceId, serviceId); diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java index 9fb42491db80..ca06d1547110 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java @@ -1674,7 +1674,7 @@ public class GeckoAppShell { @WrapForJNI(calledFrom = "gecko") private static void enableNetworkNotifications() { - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { GeckoNetworkManager.getInstance().enableNotifications(); @@ -1684,7 +1684,7 @@ public class GeckoAppShell { @WrapForJNI(calledFrom = "gecko") private static void disableNetworkNotifications() { - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { GeckoNetworkManager.getInstance().disableNotifications(); diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoScreenOrientation.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoScreenOrientation.java index 6ddc3d3c5644..304676b5f321 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoScreenOrientation.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoScreenOrientation.java @@ -204,7 +204,7 @@ public class GeckoScreenOrientation { if (ThreadUtils.isOnUiThread()) { notifier.run(); } else { - ThreadUtils.postToUiThread(notifier); + ThreadUtils.runOnUiThread(notifier); } } diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/permissions/PermissionBlock.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/permissions/PermissionBlock.java index 68ccada37995..9ef9dbd60596 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/permissions/PermissionBlock.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/permissions/PermissionBlock.java @@ -132,7 +132,7 @@ public class PermissionBlock { } if (mOnUiThread && !ThreadUtils.isOnUiThread()) { - ThreadUtils.postToUiThread(runnable); + ThreadUtils.runOnUiThread(runnable); } else if (mOnBackgroundThread && !ThreadUtils.isOnBackgroundThread()) { ThreadUtils.postToBackgroundThread(runnable); } else { diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java index 99bc13f63103..fe5251ab6ead 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java @@ -83,7 +83,7 @@ public class GeckoServiceChildProcess extends Service { final int crashAnnotationFd = crashAnnotationPfd != null ? crashAnnotationPfd.detachFd() : -1; - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { if (crashHandlerService != null) { diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ThreadUtils.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ThreadUtils.java index 8e41c183c9e9..8e3131c230e8 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ThreadUtils.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ThreadUtils.java @@ -90,6 +90,22 @@ public final class ThreadUtils { return sUiHandler; } + /** + * Runs the provided runnable on the UI thread. If this method is called on the UI thread + * the runnable will be executed synchronously. + * + * @param runnable the runnable to be executed. + */ + public static void runOnUiThread(final Runnable runnable) { + // We're on the UI thread already, let's just run this + if (isOnUiThread()) { + runnable.run(); + return; + } + + postToUiThread(runnable); + } + public static void postToUiThread(final Runnable runnable) { sUiHandler.post(runnable); } diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoEditable.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoEditable.java index 9c1523917f98..f578155864b0 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoEditable.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoEditable.java @@ -1486,7 +1486,7 @@ import android.view.inputmethod.EditorInfo; assertOnIcThread(); } - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { if (DEBUG) { @@ -1624,7 +1624,7 @@ import android.view.inputmethod.EditorInfo; // mSoftInputReentrancyGuard is needed to ensure that between the different paths, // the soft input is only toggled exactly once. - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { final int reentrancyGuard = mSoftInputReentrancyGuard.decrementAndGet(); diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoInputConnection.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoInputConnection.java index 0bb74ad22074..5c0c2e4dc7ef 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoInputConnection.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoInputConnection.java @@ -450,7 +450,7 @@ import java.lang.reflect.Proxy; @Override // SessionTextInput.EditableListener public void onDefaultKeyEvent(final KeyEvent event) { - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { GeckoInputConnection.this.performDefaultKeyAction(event); diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java index 33aaf3a83d59..5d27b913eeb9 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java @@ -204,7 +204,7 @@ public final class GeckoRuntime implements Parcelable { final URI actual = URI.create(baseUrl).resolve(url); GeckoResult result = new GeckoResult<>(); // perform the onOpenWindow call in the UI thread - ThreadUtils.postToUiThread(() -> { + ThreadUtils.runOnUiThread(() -> { sRuntime .mServiceWorkerDelegate .onOpenWindow(actual.toString()) @@ -672,9 +672,9 @@ public final class GeckoRuntime implements Parcelable { } @WrapForJNI - @UiThread + @AnyThread private void notifyOnShow(final WebNotification notification) { - ThreadUtils.getUiHandler().post(() -> { + ThreadUtils.runOnUiThread(() -> { if (mNotificationDelegate != null) { mNotificationDelegate.onShowNotification(notification); } @@ -682,9 +682,9 @@ public final class GeckoRuntime implements Parcelable { } @WrapForJNI - @UiThread + @AnyThread private void notifyOnClose(final WebNotification notification) { - ThreadUtils.getUiHandler().post(() -> { + ThreadUtils.runOnUiThread(() -> { if (mNotificationDelegate != null) { mNotificationDelegate.onCloseNotification(notification); } diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java index 5c6236ad12bf..ac288eca4d64 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java @@ -1667,7 +1667,7 @@ public class GeckoSession implements Parcelable { final GeckoResult result = new GeckoResult<>(); - ThreadUtils.getUiHandler().post(() -> { + ThreadUtils.runOnUiThread(() -> { final GeckoResult delegateResult = delegate.onLoadRequest(this, request); @@ -1998,7 +1998,7 @@ public class GeckoSession implements Parcelable { mEventDispatcher.dispatch("GeckoView:FlushSessionState", null); } - ThreadUtils.postToUiThread( + ThreadUtils.runOnUiThread( () -> getAutofillSupport().onActiveChanged(active) ); } @@ -5637,12 +5637,7 @@ public class GeckoSession implements Parcelable { // Delay calling onCompositorReady to avoid deadlock due // to synchronous call to the compositor. - ThreadUtils.postToUiThread(new Runnable() { - @Override - public void run() { - onCompositorReady(); - } - }); + ThreadUtils.postToUiThread(this::onCompositorReady); break; } diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/SessionAccessibility.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/SessionAccessibility.java index 8bcacded302f..ebaea811434a 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/SessionAccessibility.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/SessionAccessibility.java @@ -970,7 +970,7 @@ public class SessionAccessibility { @WrapForJNI(calledFrom = "gecko", stubName = "SendEvent") private void sendEventNative(final int eventType, final int sourceId, final int className, final GeckoBundle eventData) { - ThreadUtils.postToUiThread(new Runnable() { + ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { sendEvent(eventType, sourceId, className, eventData);