Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | const { windows, isBrowser, isInteractive, isDocumentLoaded, |
michael@0 | 7 | getOuterId } = require("../window/utils"); |
michael@0 | 8 | const { InputPort } = require("./system"); |
michael@0 | 9 | const { lift, merges, foldp, keepIf, start, Input } = require("../event/utils"); |
michael@0 | 10 | const { patch } = require("diffpatcher/index"); |
michael@0 | 11 | const { Sequence, seq, filter, object, pairs } = require("../util/sequence"); |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | // Create lazy iterators from the regular arrays, although |
michael@0 | 15 | // once https://github.com/mozilla/addon-sdk/pull/1314 lands |
michael@0 | 16 | // `windows` will be transforme to lazy iterators. |
michael@0 | 17 | // When iterated over belowe sequences items will represent |
michael@0 | 18 | // state of windows at the time of iteration. |
michael@0 | 19 | const opened = seq(function*() { |
michael@0 | 20 | const items = windows("navigator:browser", {includePrivates: true}); |
michael@0 | 21 | for (let item of items) { |
michael@0 | 22 | yield [getOuterId(item), item]; |
michael@0 | 23 | } |
michael@0 | 24 | }); |
michael@0 | 25 | const interactive = filter(([_, window]) => isInteractive(window), opened); |
michael@0 | 26 | const loaded = filter(([_, window]) => isDocumentLoaded(window), opened); |
michael@0 | 27 | |
michael@0 | 28 | // Helper function that converts given argument to a delta. |
michael@0 | 29 | const Update = window => window && object([getOuterId(window), window]); |
michael@0 | 30 | const Delete = window => window && object([getOuterId(window), null]); |
michael@0 | 31 | |
michael@0 | 32 | |
michael@0 | 33 | // Signal represents delta for last top level window close. |
michael@0 | 34 | const LastClosed = lift(Delete, |
michael@0 | 35 | keepIf(isBrowser, null, |
michael@0 | 36 | new InputPort({topic: "domwindowclosed"}))); |
michael@0 | 37 | exports.LastClosed = LastClosed; |
michael@0 | 38 | |
michael@0 | 39 | const windowFor = document => document && document.defaultView; |
michael@0 | 40 | |
michael@0 | 41 | // Signal represent delta for last top level window document becoming interactive. |
michael@0 | 42 | const InteractiveDoc = new InputPort({topic: "chrome-document-interactive"}); |
michael@0 | 43 | const InteractiveWin = lift(windowFor, InteractiveDoc); |
michael@0 | 44 | const LastInteractive = lift(Update, keepIf(isBrowser, null, InteractiveWin)); |
michael@0 | 45 | exports.LastInteractive = LastInteractive; |
michael@0 | 46 | |
michael@0 | 47 | // Signal represent delta for last top level window loaded. |
michael@0 | 48 | const LoadedDoc = new InputPort({topic: "chrome-document-loaded"}); |
michael@0 | 49 | const LoadedWin = lift(windowFor, LoadedDoc); |
michael@0 | 50 | const LastLoaded = lift(Update, keepIf(isBrowser, null, LoadedWin)); |
michael@0 | 51 | exports.LastLoaded = LastLoaded; |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | const initialize = input => { |
michael@0 | 55 | if (!input.initialized) { |
michael@0 | 56 | input.value = object(...input.value); |
michael@0 | 57 | Input.start(input); |
michael@0 | 58 | input.initialized = true; |
michael@0 | 59 | } |
michael@0 | 60 | }; |
michael@0 | 61 | |
michael@0 | 62 | // Signal represents set of top level interactive windows, updated any |
michael@0 | 63 | // time new window becomes interactive or one get's closed. |
michael@0 | 64 | const Interactive = foldp(patch, interactive, merges([LastInteractive, |
michael@0 | 65 | LastClosed])); |
michael@0 | 66 | Interactive[start] = initialize; |
michael@0 | 67 | exports.Interactive = Interactive; |
michael@0 | 68 | |
michael@0 | 69 | // Signal represents set of top level loaded window, updated any time |
michael@0 | 70 | // new window becomes interactive or one get's closed. |
michael@0 | 71 | const Loaded = foldp(patch, loaded, merges([LastLoaded, LastClosed])); |
michael@0 | 72 | Loaded[start] = initialize; |
michael@0 | 73 | exports.Loaded = Loaded; |