diff -r 000000000000 -r 6474c204b198 mobile/android/chrome/content/CastingApps.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mobile/android/chrome/content/CastingApps.js Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,451 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ +"use strict"; + +var CastingApps = { + _castMenuId: -1, + + init: function ca_init() { + if (!this.isEnabled()) { + return; + } + + // Register a service target + SimpleServiceDiscovery.registerTarget("roku:ecp", function(aService, aApp) { + Cu.import("resource://gre/modules/RokuApp.jsm"); + return new RokuApp(aService, "FirefoxTest"); + }); + + // Search for devices continuously every 120 seconds + SimpleServiceDiscovery.search(120 * 1000); + + this._castMenuId = NativeWindow.contextmenus.add( + Strings.browser.GetStringFromName("contextmenu.castToScreen"), + this.filterCast, + this.openExternal.bind(this) + ); + + Services.obs.addObserver(this, "Casting:Play", false); + Services.obs.addObserver(this, "Casting:Pause", false); + Services.obs.addObserver(this, "Casting:Stop", 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); + }, + + uninit: function ca_uninit() { + BrowserApp.deck.removeEventListener("TabSelect", this, true); + BrowserApp.deck.removeEventListener("pageshow", this, true); + BrowserApp.deck.removeEventListener("playing", this, true); + BrowserApp.deck.removeEventListener("ended", this, true); + + Services.obs.removeObserver(this, "Casting:Play"); + Services.obs.removeObserver(this, "Casting:Pause"); + Services.obs.removeObserver(this, "Casting:Stop"); + + NativeWindow.contextmenus.remove(this._castMenuId); + }, + + isEnabled: function isEnabled() { + return Services.prefs.getBoolPref("browser.casting.enabled"); + }, + + observe: function (aSubject, aTopic, aData) { + switch (aTopic) { + case "Casting:Play": + if (this.session && this.session.remoteMedia.status == "paused") { + this.session.remoteMedia.play(); + } + break; + case "Casting:Pause": + if (this.session && this.session.remoteMedia.status == "started") { + this.session.remoteMedia.pause(); + } + break; + case "Casting:Stop": + if (this.session) { + this.closeExternal(); + } + break; + } + }, + + handleEvent: function(aEvent) { + switch (aEvent.type) { + case "TabSelect": { + let tab = BrowserApp.getTabForBrowser(aEvent.target); + this._updatePageActionForTab(tab, aEvent); + break; + } + case "pageshow": { + let tab = BrowserApp.getTabForWindow(aEvent.originalTarget.defaultView); + this._updatePageActionForTab(tab, aEvent); + break; + } + case "playing": + case "ended": { + let video = aEvent.target; + if (video instanceof HTMLVideoElement) { + // If playing, send the