addon-sdk/source/lib/sdk/core/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
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "experimental"
michael@0 9 };
michael@0 10
michael@0 11
michael@0 12 const { Cc, Ci, Cr } = require("chrome");
michael@0 13 const { Class } = require("./heritage");
michael@0 14 const { isWeak } = require("./reference");
michael@0 15 const method = require("../../method/core");
michael@0 16
michael@0 17 const { addObserver, removeObserver } = Cc['@mozilla.org/observer-service;1'].
michael@0 18 getService(Ci.nsIObserverService);
michael@0 19
michael@0 20
michael@0 21 // This is a method that will be invoked when notification observer
michael@0 22 // subscribed to occurs.
michael@0 23 const observe = method("observer/observe");
michael@0 24 exports.observe = observe;
michael@0 25
michael@0 26 // Method to subscribe to the observer notification.
michael@0 27 const subscribe = method("observe/subscribe");
michael@0 28 exports.subscribe = subscribe;
michael@0 29
michael@0 30
michael@0 31 // Method to unsubscribe from the observer notifications.
michael@0 32 const unsubscribe = method("observer/unsubscribe");
michael@0 33 exports.unsubscribe = unsubscribe;
michael@0 34
michael@0 35
michael@0 36 // This is wrapper class that takes a `delegate` and produces
michael@0 37 // instance of `nsIObserver` which will delegate to a given
michael@0 38 // object when observer notification occurs.
michael@0 39 const ObserverDelegee = Class({
michael@0 40 initialize: function(delegate) {
michael@0 41 this.delegate = delegate;
michael@0 42 },
michael@0 43 QueryInterface: function(iid) {
michael@0 44 const isObserver = iid.equals(Ci.nsIObserver);
michael@0 45 const isWeakReference = iid.equals(Ci.nsISupportsWeakReference);
michael@0 46
michael@0 47 if (!isObserver && !isWeakReference)
michael@0 48 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 49
michael@0 50 return this;
michael@0 51 },
michael@0 52 observe: function(subject, topic, data) {
michael@0 53 observe(this.delegate, subject, topic, data);
michael@0 54 }
michael@0 55 });
michael@0 56
michael@0 57
michael@0 58 // Class that can be either mixed in or inherited from in
michael@0 59 // order to subscribe / unsubscribe for observer notifications.
michael@0 60 const Observer = Class({});
michael@0 61 exports.Observer = Observer;
michael@0 62
michael@0 63 // Weak maps that associates instance of `ObserverDelegee` with
michael@0 64 // an actual observer. It ensures that `ObserverDelegee` instance
michael@0 65 // won't be GC-ed until given `observer` is.
michael@0 66 const subscribers = new WeakMap();
michael@0 67
michael@0 68 // Implementation of `subscribe` for `Observer` type just registers
michael@0 69 // observer for an observer service. If `isWeak(observer)` is `true`
michael@0 70 // observer service won't hold strong reference to a given `observer`.
michael@0 71 subscribe.define(Observer, (observer, topic) => {
michael@0 72 if (!subscribers.has(observer)) {
michael@0 73 const delegee = new ObserverDelegee(observer);
michael@0 74 subscribers.set(observer, delegee);
michael@0 75 addObserver(delegee, topic, isWeak(observer));
michael@0 76 }
michael@0 77 });
michael@0 78
michael@0 79 // Unsubscribes `observer` from observer notifications for the
michael@0 80 // given `topic`.
michael@0 81 unsubscribe.define(Observer, (observer, topic) => {
michael@0 82 const delegee = subscribers.get(observer);
michael@0 83 if (delegee) {
michael@0 84 subscribers.delete(observer);
michael@0 85 removeObserver(delegee, topic);
michael@0 86 }
michael@0 87 });

mercurial