diff --git a/mobile/android/components/geckoview/GeckoViewStartup.jsm b/mobile/android/components/geckoview/GeckoViewStartup.jsm index 58bc415f5501..b6457a96df8a 100644 --- a/mobile/android/components/geckoview/GeckoViewStartup.jsm +++ b/mobile/android/components/geckoview/GeckoViewStartup.jsm @@ -213,6 +213,7 @@ class GeckoViewStartup { "GeckoView:SetLocale", ]); + Services.obs.addObserver(this, "xpcom-shutdown"); Services.obs.notifyObservers(null, "geckoview-startup-complete"); break; } @@ -224,6 +225,16 @@ class GeckoViewStartup { Services.startup.trackStartupCrashEnd(); break; } + case "xpcom-shutdown": { + Services.obs.removeObserver(this, "xpcom-shutdown"); + EventDispatcher.instance.unregisterListener(this, [ + "GeckoView:ResetUserPrefs", + "GeckoView:SetDefaultPrefs", + "GeckoView:SetLocale", + ]); + EventDispatcher.instance.shutdown(); + delete EventDispatcher.instance; + } } } diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/EventDispatcher.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/EventDispatcher.java index 65ef7d75b384..2ae0a637e375 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/EventDispatcher.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/EventDispatcher.java @@ -125,7 +125,7 @@ public final class EventDispatcher extends JNIObject { @WrapForJNI @Override // JNIObject protected native void disposeNative(); - @WrapForJNI(stubName = "Shutdown") + @WrapForJNI(stubName = "ShutdownFromJava", dispatchTo = "gecko") protected native void shutdownNative(); @WrapForJNI private static final int DETACHED = 0; diff --git a/mobile/android/modules/geckoview/Messaging.jsm b/mobile/android/modules/geckoview/Messaging.jsm index b8530019e9fe..706ee5ebb2c0 100644 --- a/mobile/android/modules/geckoview/Messaging.jsm +++ b/mobile/android/modules/geckoview/Messaging.jsm @@ -151,6 +151,10 @@ DispatcherDelegate.prototype = { this._replies.clear(); }, + shutdown() { + this._dispatcher.shutdown(); + }, + receiveMessage(aMsg) { const { uuid, type } = aMsg.data; const reply = this._replies.get(uuid); @@ -282,4 +286,12 @@ var EventDispatcher = { if (IS_PARENT_PROCESS) { Services.mm.addMessageListener("GeckoView:Messaging", EventDispatcher); Services.ppmm.addMessageListener("GeckoView:Messaging", EventDispatcher); + + function xpcomShutdown() { + Services.mm.removeMessageListener("GeckoView:Messaging", EventDispatcher); + Services.ppmm.removeMessageListener("GeckoView:Messaging", EventDispatcher); + Services.obs.removeObserver(xpcomShutdown, "xpcom-shutdown"); + } + + Services.obs.addObserver(xpcomShutdown, "xpcom-shutdown"); } diff --git a/widget/android/EventDispatcher.cpp b/widget/android/EventDispatcher.cpp index 1f382f99a984..2e0591123390 100644 --- a/widget/android/EventDispatcher.cpp +++ b/widget/android/EventDispatcher.cpp @@ -949,9 +949,13 @@ void EventDispatcher::Attach(java::EventDispatcher::Param aDispatcher, dispatcher->SetAttachedToGecko(java::EventDispatcher::ATTACHED); } -void EventDispatcher::Shutdown() { +NS_IMETHODIMP +EventDispatcher::Shutdown() { + MOZ_ASSERT(NS_IsMainThread()); mDispatcher = nullptr; mDOMWindow = nullptr; + mListenersMap.Clear(); + return NS_OK; } void EventDispatcher::Detach() { diff --git a/widget/android/EventDispatcher.h b/widget/android/EventDispatcher.h index a17822e248c4..4125fb7c0dcd 100644 --- a/widget/android/EventDispatcher.h +++ b/widget/android/EventDispatcher.h @@ -65,7 +65,7 @@ class EventDispatcher final virtual ~EventDispatcher() {} - void Shutdown(); + void ShutdownFromJava() { Shutdown(); } struct ListenersList { nsCOMArray listeners{/* count */ 1}; diff --git a/widget/android/nsIAndroidBridge.idl b/widget/android/nsIAndroidBridge.idl index b30ed60d7702..a8db4d1ded7f 100644 --- a/widget/android/nsIAndroidBridge.idl +++ b/widget/android/nsIAndroidBridge.idl @@ -43,6 +43,7 @@ interface nsIAndroidEventDispatcher : nsISupports [implicit_jscontext] void unregisterListener(in nsIAndroidEventListener listener, in jsval events); + void shutdown(); }; [scriptable, uuid(60a78a94-6117-432f-9d49-304913a931c5)]