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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: // This module provides temporary shim until Bug 843901 is shipped. michael@0: // It basically registers tab event listeners on all windows that get michael@0: // opened and forwards them through observer notifications. michael@0: michael@0: module.metadata = { michael@0: "stability": "experimental" michael@0: }; michael@0: michael@0: const { Ci } = require("chrome"); michael@0: const { windows, isInteractive } = require("../window/utils"); michael@0: const { events } = require("../browser/events"); michael@0: const { open } = require("../event/dom"); michael@0: const { filter, map, merge, expand } = require("../event/utils"); michael@0: const isFennec = require("sdk/system/xul-app").is("Fennec"); michael@0: michael@0: // Module provides event stream (in nodejs style) that emits data events michael@0: // for all the tab events that happen in running firefox. At the moment michael@0: // it does it by registering listeners on all browser windows and then michael@0: // forwarding events when they occur to a stream. This will become obsolete michael@0: // once Bug 843901 is fixed, and we'll just leverage observer notifications. michael@0: michael@0: // Set of tab events that this module going to aggregate and expose. michael@0: const TYPES = ["TabOpen","TabClose","TabSelect","TabMove","TabPinned", michael@0: "TabUnpinned"]; michael@0: michael@0: // Utility function that given a browser `window` returns stream of above michael@0: // defined tab events for all tabs on the given window. michael@0: function tabEventsFor(window) { michael@0: // Map supported event types to a streams of those events on the given michael@0: // `window` and than merge these streams into single form stream off michael@0: // all events. michael@0: let channels = TYPES.map(function(type) open(window, type)); michael@0: return merge(channels); michael@0: } michael@0: michael@0: // Filter DOMContentLoaded events from all the browser events. michael@0: let readyEvents = filter(events, function(e) e.type === "DOMContentLoaded"); michael@0: // Map DOMContentLoaded events to it's target browser windows. michael@0: let futureWindows = map(readyEvents, function(e) e.target); michael@0: // Expand all browsers that will become interactive to supported tab events michael@0: // on these windows. Result will be a tab events from all tabs of all windows michael@0: // that will become interactive. michael@0: let eventsFromFuture = expand(futureWindows, tabEventsFor); michael@0: michael@0: // Above covers only windows that will become interactive in a future, but some michael@0: // windows may already be interactive so we pick those and expand to supported michael@0: // tab events for them too. michael@0: let interactiveWindows = windows("navigator:browser", { includePrivate: true }). michael@0: filter(isInteractive); michael@0: let eventsFromInteractive = merge(interactiveWindows.map(tabEventsFor)); michael@0: michael@0: michael@0: // Finally merge stream of tab events from future windows and current windows michael@0: // to cover all tab events on all windows that will open. michael@0: let allEvents = merge([eventsFromInteractive, eventsFromFuture]); michael@0: michael@0: // Map events to Fennec format if necessary michael@0: exports.events = map(allEvents, function (event) { michael@0: return !isFennec ? event : { michael@0: type: event.type, michael@0: target: event.target.ownerDocument.defaultView.BrowserApp michael@0: .getTabForBrowser(event.target) michael@0: }; michael@0: });