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 | |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | module.metadata = { |
michael@0 | 8 | 'stability': 'unstable' |
michael@0 | 9 | }; |
michael@0 | 10 | |
michael@0 | 11 | const { Cc, Ci, Cu } = require('chrome'); |
michael@0 | 12 | const { Unknown } = require('../platform/xpcom'); |
michael@0 | 13 | const { Class } = require('../core/heritage'); |
michael@0 | 14 | const { ns } = require('../core/namespace'); |
michael@0 | 15 | const { addObserver, removeObserver, notifyObservers } = |
michael@0 | 16 | Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService); |
michael@0 | 17 | const unloadSubject = require('@loader/unload'); |
michael@0 | 18 | |
michael@0 | 19 | const Subject = Class({ |
michael@0 | 20 | extends: Unknown, |
michael@0 | 21 | initialize: function initialize(object) { |
michael@0 | 22 | // Double-wrap the object and set a property identifying the |
michael@0 | 23 | // wrappedJSObject as one of our wrappers to distinguish between |
michael@0 | 24 | // subjects that are one of our wrappers (which we should unwrap |
michael@0 | 25 | // when notifying our observers) and those that are real JS XPCOM |
michael@0 | 26 | // components (which we should pass through unaltered). |
michael@0 | 27 | this.wrappedJSObject = { |
michael@0 | 28 | observersModuleSubjectWrapper: true, |
michael@0 | 29 | object: object |
michael@0 | 30 | }; |
michael@0 | 31 | }, |
michael@0 | 32 | getHelperForLanguage: function() {}, |
michael@0 | 33 | getInterfaces: function() {} |
michael@0 | 34 | }); |
michael@0 | 35 | |
michael@0 | 36 | function emit(type, event) { |
michael@0 | 37 | // From bug 910599 |
michael@0 | 38 | // We must test to see if 'subject' or 'data' is a defined property |
michael@0 | 39 | // of the event object, but also allow primitives to be passed in, |
michael@0 | 40 | // which the `in` operator breaks, yet `null` is an object, hence |
michael@0 | 41 | // the long conditional |
michael@0 | 42 | let subject = event && typeof event === 'object' && 'subject' in event ? |
michael@0 | 43 | Subject(event.subject) : |
michael@0 | 44 | null; |
michael@0 | 45 | let data = event && typeof event === 'object' ? |
michael@0 | 46 | // An object either returns its `data` property or null |
michael@0 | 47 | ('data' in event ? event.data : null) : |
michael@0 | 48 | // All other types return themselves (and cast to strings/null |
michael@0 | 49 | // via observer service) |
michael@0 | 50 | event; |
michael@0 | 51 | notifyObservers(subject, type, data); |
michael@0 | 52 | } |
michael@0 | 53 | exports.emit = emit; |
michael@0 | 54 | |
michael@0 | 55 | const Observer = Class({ |
michael@0 | 56 | extends: Unknown, |
michael@0 | 57 | initialize: function initialize(listener) { |
michael@0 | 58 | this.listener = listener; |
michael@0 | 59 | }, |
michael@0 | 60 | interfaces: [ 'nsIObserver', 'nsISupportsWeakReference' ], |
michael@0 | 61 | observe: function(subject, topic, data) { |
michael@0 | 62 | // Extract the wrapped object for subjects that are one of our |
michael@0 | 63 | // wrappers around a JS object. This way we support both wrapped |
michael@0 | 64 | // subjects created using this module and those that are real |
michael@0 | 65 | // XPCOM components. |
michael@0 | 66 | if (subject && typeof(subject) == 'object' && |
michael@0 | 67 | ('wrappedJSObject' in subject) && |
michael@0 | 68 | ('observersModuleSubjectWrapper' in subject.wrappedJSObject)) |
michael@0 | 69 | subject = subject.wrappedJSObject.object; |
michael@0 | 70 | |
michael@0 | 71 | try { |
michael@0 | 72 | this.listener({ |
michael@0 | 73 | type: topic, |
michael@0 | 74 | subject: subject, |
michael@0 | 75 | data: data |
michael@0 | 76 | }); |
michael@0 | 77 | } |
michael@0 | 78 | catch (error) { |
michael@0 | 79 | console.exception(error); |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | }); |
michael@0 | 83 | |
michael@0 | 84 | const subscribers = ns(); |
michael@0 | 85 | |
michael@0 | 86 | function on(type, listener, strong) { |
michael@0 | 87 | // Unless last optional argument is `true` we use a weak reference to a |
michael@0 | 88 | // listener. |
michael@0 | 89 | let weak = !strong; |
michael@0 | 90 | // Take list of observers associated with given `listener` function. |
michael@0 | 91 | let observers = subscribers(listener); |
michael@0 | 92 | // If `observer` for the given `type` is not registered yet, then |
michael@0 | 93 | // associate an `observer` and register it. |
michael@0 | 94 | if (!(type in observers)) { |
michael@0 | 95 | let observer = Observer(listener); |
michael@0 | 96 | observers[type] = observer; |
michael@0 | 97 | addObserver(observer, type, weak); |
michael@0 | 98 | // WeakRef gymnastics to remove all alive observers on unload |
michael@0 | 99 | let ref = Cu.getWeakReference(observer); |
michael@0 | 100 | weakRefs.set(observer, ref); |
michael@0 | 101 | stillAlive.set(ref, type); |
michael@0 | 102 | } |
michael@0 | 103 | } |
michael@0 | 104 | exports.on = on; |
michael@0 | 105 | |
michael@0 | 106 | function once(type, listener) { |
michael@0 | 107 | // Note: this code assumes order in which listeners are called, which is fine |
michael@0 | 108 | // as long as dispatch happens in same order as listener registration which |
michael@0 | 109 | // is the case now. That being said we should be aware that this may break |
michael@0 | 110 | // in a future if order will change. |
michael@0 | 111 | on(type, listener); |
michael@0 | 112 | on(type, function cleanup() { |
michael@0 | 113 | off(type, listener); |
michael@0 | 114 | off(type, cleanup); |
michael@0 | 115 | }, true); |
michael@0 | 116 | } |
michael@0 | 117 | exports.once = once; |
michael@0 | 118 | |
michael@0 | 119 | function off(type, listener) { |
michael@0 | 120 | // Take list of observers as with the given `listener`. |
michael@0 | 121 | let observers = subscribers(listener); |
michael@0 | 122 | // If `observer` for the given `type` is registered, then |
michael@0 | 123 | // remove it & unregister. |
michael@0 | 124 | if (type in observers) { |
michael@0 | 125 | let observer = observers[type]; |
michael@0 | 126 | delete observers[type]; |
michael@0 | 127 | removeObserver(observer, type); |
michael@0 | 128 | stillAlive.delete(weakRefs.get(observer)); |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | exports.off = off; |
michael@0 | 132 | |
michael@0 | 133 | // must use WeakMap to keep reference to all the WeakRefs (!), see bug 986115 |
michael@0 | 134 | let weakRefs = new WeakMap(); |
michael@0 | 135 | |
michael@0 | 136 | // and we're out of beta, we're releasing on time! |
michael@0 | 137 | let stillAlive = new Map(); |
michael@0 | 138 | |
michael@0 | 139 | on('sdk:loader:destroy', function onunload({ subject, data: reason }) { |
michael@0 | 140 | // using logic from ./unload, to avoid a circular module reference |
michael@0 | 141 | if (subject.wrappedJSObject === unloadSubject) { |
michael@0 | 142 | off('sdk:loader:destroy', onunload); |
michael@0 | 143 | |
michael@0 | 144 | // don't bother |
michael@0 | 145 | if (reason === 'shutdown') |
michael@0 | 146 | return; |
michael@0 | 147 | |
michael@0 | 148 | stillAlive.forEach( (type, ref) => { |
michael@0 | 149 | let observer = ref.get(); |
michael@0 | 150 | if (observer) |
michael@0 | 151 | removeObserver(observer, type); |
michael@0 | 152 | }) |
michael@0 | 153 | } |
michael@0 | 154 | // a strong reference |
michael@0 | 155 | }, true); |