addon-sdk/source/lib/sdk/tabs/observer.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 module.metadata = {
michael@0 7 "stability": "unstable"
michael@0 8 };
michael@0 9
michael@0 10 const { EventEmitterTrait: EventEmitter } = require("../deprecated/events");
michael@0 11 const { DOMEventAssembler } = require("../deprecated/events/assembler");
michael@0 12 const { Trait } = require("../deprecated/light-traits");
michael@0 13 const { getActiveTab, getTabs, getTabContainer } = require("./utils");
michael@0 14 const { browserWindowIterator } = require("../deprecated/window-utils");
michael@0 15 const { isBrowser } = require('../window/utils');
michael@0 16 const { observer: windowObserver } = require("../windows/observer");
michael@0 17
michael@0 18 const EVENTS = {
michael@0 19 "TabOpen": "open",
michael@0 20 "TabClose": "close",
michael@0 21 "TabSelect": "select",
michael@0 22 "TabMove": "move",
michael@0 23 "TabPinned": "pinned",
michael@0 24 "TabUnpinned": "unpinned"
michael@0 25 };
michael@0 26
michael@0 27
michael@0 28 // Event emitter objects used to register listeners and emit events on them
michael@0 29 // when they occur.
michael@0 30 const observer = Trait.compose(DOMEventAssembler, EventEmitter).create({
michael@0 31 /**
michael@0 32 * Method is implemented by `EventEmitter` and is used just for emitting
michael@0 33 * events on registered listeners.
michael@0 34 */
michael@0 35 _emit: Trait.required,
michael@0 36 /**
michael@0 37 * Events that are supported and emitted by the module.
michael@0 38 */
michael@0 39 supportedEventsTypes: Object.keys(EVENTS),
michael@0 40 /**
michael@0 41 * Function handles all the supported events on all the windows that are
michael@0 42 * observed. Method is used to proxy events to the listeners registered on
michael@0 43 * this event emitter.
michael@0 44 * @param {Event} event
michael@0 45 * Keyboard event being emitted.
michael@0 46 */
michael@0 47 handleEvent: function handleEvent(event) {
michael@0 48 this._emit(EVENTS[event.type], event.target, event);
michael@0 49 }
michael@0 50 });
michael@0 51
michael@0 52 // Currently Gecko does not dispatch any event on the previously selected
michael@0 53 // tab before / after "TabSelect" is dispatched. In order to work around this
michael@0 54 // limitation we keep track of selected tab and emit "deactivate" event with
michael@0 55 // that before emitting "activate" on selected tab.
michael@0 56 var selectedTab = null;
michael@0 57 function onTabSelect(tab) {
michael@0 58 if (selectedTab !== tab) {
michael@0 59 if (selectedTab) observer._emit('deactivate', selectedTab);
michael@0 60 if (tab) observer._emit('activate', selectedTab = tab);
michael@0 61 }
michael@0 62 };
michael@0 63 observer.on('select', onTabSelect);
michael@0 64
michael@0 65 // We also observe opening / closing windows in order to add / remove it's
michael@0 66 // containers to the observed list.
michael@0 67 function onWindowOpen(chromeWindow) {
michael@0 68 if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window.
michael@0 69 observer.observe(getTabContainer(chromeWindow));
michael@0 70 }
michael@0 71 windowObserver.on("open", onWindowOpen);
michael@0 72
michael@0 73 function onWindowClose(chromeWindow) {
michael@0 74 if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window.
michael@0 75 // Bug 751546: Emit `deactivate` event on window close immediatly
michael@0 76 // Otherwise we are going to face "dead object" exception on `select` event
michael@0 77 if (getActiveTab(chromeWindow) == selectedTab) {
michael@0 78 observer._emit("deactivate", selectedTab);
michael@0 79 selectedTab = null;
michael@0 80 }
michael@0 81 observer.ignore(getTabContainer(chromeWindow));
michael@0 82 }
michael@0 83 windowObserver.on("close", onWindowClose);
michael@0 84
michael@0 85
michael@0 86 // Currently gecko does not dispatches "TabSelect" events when different
michael@0 87 // window gets activated. To work around this limitation we emulate "select"
michael@0 88 // event for this case.
michael@0 89 windowObserver.on("activate", function onWindowActivate(chromeWindow) {
michael@0 90 if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window.
michael@0 91 observer._emit("select", getActiveTab(chromeWindow));
michael@0 92 });
michael@0 93
michael@0 94 // We should synchronize state, since probably we already have at least one
michael@0 95 // window open.
michael@0 96 for each (let window in browserWindowIterator()) onWindowOpen(window);
michael@0 97
michael@0 98 exports.observer = observer;

mercurial