Bug 1465480 - Allow GeckoView modules to clean up when window closes r=jchen draft
authorJames Willcox <snorp@snorp.net>
Thu, 31 May 2018 11:55:30 -0500
changeset 804895 aa6477eb011817a63687b5e03ab5f46bba6631a0
parent 804894 6b35cd1a9d4016b7f411abf3640361d3fa156ee3
child 804896 8326be23b1c7f1e222cded46bcac33ded8184e22
push id112488
push userbmo:snorp@snorp.net
push dateWed, 06 Jun 2018 17:59:13 +0000
reviewersjchen
bugs1465480
milestone62.0a1
Bug 1465480 - Allow GeckoView modules to clean up when window closes r=jchen MozReview-Commit-ID: 2erYZpnBTnq
mobile/android/chrome/geckoview/geckoview.js
mobile/android/modules/geckoview/GeckoViewModule.jsm
--- a/mobile/android/chrome/geckoview/geckoview.js
+++ b/mobile/android/chrome/geckoview/geckoview.js
@@ -58,16 +58,25 @@ var ModuleManager = {
     ]);
 
     this.messageManager.addMessageListener("GeckoView:ContentModuleLoaded",
                                            this);
 
     this.forEach(module => {
       module.onInit();
     });
+
+    window.addEventListener("unload", () => {
+      this.forEach(module => {
+        module.enabled = false;
+        module.onDestroy();
+      });
+
+      this._modules.clear();
+    });
   },
 
   get window() {
     return window;
   },
 
   get browser() {
     return this._browser;
@@ -195,16 +204,22 @@ class ModuleInfo {
       this._impl.onSettingsUpdate();
     }
     this._loadPhase(this._onInitPhase);
     this._onInitPhase = null;
 
     this.enabled = this._enabledOnInit;
   }
 
+  onDestroy() {
+    if (this._impl) {
+      this._impl.onDestroy();
+    }
+  }
+
   /**
    * Load resources according to a phase object that contains possible keys,
    *
    * "resource": specify the JSM resource to load for this module.
    * "frameScript": specify a content JS frame script to load for this module.
    */
   _loadPhase(aPhase) {
     if (!aPhase) {
--- a/mobile/android/modules/geckoview/GeckoViewModule.jsm
+++ b/mobile/android/modules/geckoview/GeckoViewModule.jsm
@@ -50,16 +50,19 @@ class GeckoViewModule {
   }
 
   // Override to initialize the browser before it is bound to the window.
   onInitBrowser() {}
 
   // Override to initialize module.
   onInit() {}
 
+  // Override to cleanup when the window is closed
+  onDestroy() {}
+
   // Override to detect settings change. Access settings via this.settings.
   onSettingsUpdate() {}
 
   // Override to enable module after setting a Java delegate.
   onEnable() {}
 
   // Override to disable module after clearing the Java delegate.
   onDisable() {}