Bug 1026669 - Limit SSDP polling to when Firefox is in the foreground. r=margaret, a=lizzard
authorMark Finkle <mfinkle@mozilla.com>
Tue, 06 Oct 2015 08:11:52 -0400
changeset 296546 477c92901c3ac4dcf837bfb62e73c00a476f8903
parent 296545 d817942ac0b7394df73800914d5b02729468ca39
child 296547 4b93d228a7e42c6ffe5d1b95dfa88c1d4c2ec3b2
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret, lizzard
bugs1026669
milestone43.0a2
Bug 1026669 - Limit SSDP polling to when Firefox is in the foreground. r=margaret, a=lizzard
mobile/android/chrome/content/CastingApps.js
--- a/mobile/android/chrome/content/CastingApps.js
+++ b/mobile/android/chrome/content/CastingApps.js
@@ -60,44 +60,47 @@ var mediaPlayerDevice = {
 };
 
 var CastingApps = {
   _castMenuId: -1,
   mirrorStartMenuId: -1,
   mirrorStopMenuId: -1,
   _blocked: null,
   _bound: null,
+  _interval: 120 * 1000, // 120 seconds
 
   init: function ca_init() {
     if (!this.isCastingEnabled()) {
       return;
     }
 
     // Register targets
     SimpleServiceDiscovery.registerDevice(rokuDevice);
 
     // MediaPlayerDevice will notify us any time the native device list changes.
     mediaPlayerDevice.init();
     SimpleServiceDiscovery.registerDevice(mediaPlayerDevice);
 
-    // Search for devices continuously every 120 seconds
-    SimpleServiceDiscovery.search(120 * 1000);
+    // Search for devices continuously
+    SimpleServiceDiscovery.search(this._interval);
 
     this._castMenuId = NativeWindow.contextmenus.add(
       Strings.browser.GetStringFromName("contextmenu.sendToDevice"),
       this.filterCast,
       this.handleContextMenu.bind(this)
     );
 
     Services.obs.addObserver(this, "Casting:Play", false);
     Services.obs.addObserver(this, "Casting:Pause", false);
     Services.obs.addObserver(this, "Casting:Stop", false);
     Services.obs.addObserver(this, "Casting:Mirror", false);
     Services.obs.addObserver(this, "ssdp-service-found", false);
     Services.obs.addObserver(this, "ssdp-service-lost", false);
+    Services.obs.addObserver(this, "application-background", false);
+    Services.obs.addObserver(this, "application-foreground", false);
 
     BrowserApp.deck.addEventListener("TabSelect", this, true);
     BrowserApp.deck.addEventListener("pageshow", this, true);
     BrowserApp.deck.addEventListener("playing", this, true);
     BrowserApp.deck.addEventListener("ended", this, true);
     BrowserApp.deck.addEventListener("MozAutoplayMediaBlocked", this, true);
     // Note that the XBL binding is untrusted
     BrowserApp.deck.addEventListener("MozNoControlsVideoBindingAttached", this, true, true);
@@ -190,25 +193,30 @@ var CastingApps = {
         {
           Cu.import("resource://gre/modules/TabMirror.jsm");
           this.tabMirror = new TabMirror(aData, window);
           NativeWindow.menu.update(this.mirrorStartMenuId, { visible: false });
           NativeWindow.menu.update(this.mirrorStopMenuId, { visible: true });
         }
         break;
       case "ssdp-service-found":
-        {
-          this.serviceAdded(SimpleServiceDiscovery.findServiceForID(aData));
-          break;
-        }
+        this.serviceAdded(SimpleServiceDiscovery.findServiceForID(aData));
+        break;
       case "ssdp-service-lost":
-        {
-          this.serviceLost(SimpleServiceDiscovery.findServiceForID(aData));
-          break;
-        }
+        this.serviceLost(SimpleServiceDiscovery.findServiceForID(aData));
+        break;
+      case "application-background":
+        // Turn off polling while in the background
+        this._interval = SimpleServiceDiscovery.search(0);
+        SimpleServiceDiscovery.stopSearch();
+        break;
+      case "application-foreground":
+        // Turn polling on when app comes back to foreground
+        SimpleServiceDiscovery.search(this._interval);
+        break;
     }
   },
 
   handleEvent: function(aEvent) {
     switch (aEvent.type) {
       case "TabSelect": {
         let tab = BrowserApp.getTabForBrowser(aEvent.target);
         this._updatePageActionForTab(tab, aEvent);