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