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": "experimental" |
michael@0 | 9 | }; |
michael@0 | 10 | |
michael@0 | 11 | |
michael@0 | 12 | const { Class } = require("./heritage"); |
michael@0 | 13 | const { Observer, subscribe, unsubscribe, observe } = require("./observer"); |
michael@0 | 14 | const { isWeak, WeakReference } = require("./reference"); |
michael@0 | 15 | const method = require("../../method/core"); |
michael@0 | 16 | |
michael@0 | 17 | const unloadSubject = require('@loader/unload'); |
michael@0 | 18 | const addonUnloadTopic = "sdk:loader:destroy"; |
michael@0 | 19 | |
michael@0 | 20 | |
michael@0 | 21 | |
michael@0 | 22 | const uninstall = method("disposable/uninstall"); |
michael@0 | 23 | exports.uninstall = uninstall; |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | const shutdown = method("disposable/shutdown"); |
michael@0 | 27 | exports.shutdown = shutdown; |
michael@0 | 28 | |
michael@0 | 29 | const disable = method("disposable/disable"); |
michael@0 | 30 | exports.disable = disable; |
michael@0 | 31 | |
michael@0 | 32 | const upgrade = method("disposable/upgrade"); |
michael@0 | 33 | exports.upgrade = upgrade; |
michael@0 | 34 | |
michael@0 | 35 | const downgrade = method("disposable/downgrade"); |
michael@0 | 36 | exports.downgrade = downgrade; |
michael@0 | 37 | |
michael@0 | 38 | const unload = method("disposable/unload"); |
michael@0 | 39 | exports.unload = unload; |
michael@0 | 40 | |
michael@0 | 41 | const dispose = method("disposable/dispose"); |
michael@0 | 42 | exports.dispose = dispose; |
michael@0 | 43 | dispose.define(Object, object => object.dispose()); |
michael@0 | 44 | |
michael@0 | 45 | |
michael@0 | 46 | const setup = method("disposable/setup"); |
michael@0 | 47 | exports.setup = setup; |
michael@0 | 48 | setup.define(Object, (object, ...args) => object.setup(...args)); |
michael@0 | 49 | |
michael@0 | 50 | |
michael@0 | 51 | // Set's up disposable instance. |
michael@0 | 52 | const setupDisposable = disposable => { |
michael@0 | 53 | subscribe(disposable, addonUnloadTopic, isWeak(disposable)); |
michael@0 | 54 | }; |
michael@0 | 55 | |
michael@0 | 56 | // Tears down disposable instance. |
michael@0 | 57 | const disposeDisposable = disposable => { |
michael@0 | 58 | unsubscribe(disposable, addonUnloadTopic); |
michael@0 | 59 | }; |
michael@0 | 60 | |
michael@0 | 61 | // Base type that takes care of disposing it's instances on add-on unload. |
michael@0 | 62 | // Also makes sure to remove unload listener if it's already being disposed. |
michael@0 | 63 | const Disposable = Class({ |
michael@0 | 64 | implements: [Observer], |
michael@0 | 65 | initialize: function(...args) { |
michael@0 | 66 | // First setup instance before initializing it's disposal. If instance |
michael@0 | 67 | // fails to initialize then there is no instance to be disposed at the |
michael@0 | 68 | // unload. |
michael@0 | 69 | setup(this, ...args); |
michael@0 | 70 | setupDisposable(this); |
michael@0 | 71 | }, |
michael@0 | 72 | destroy: function(reason) { |
michael@0 | 73 | // Destroying disposable removes unload handler so that attempt to dispose |
michael@0 | 74 | // won't be made at unload & delegates to dispose. |
michael@0 | 75 | disposeDisposable(this); |
michael@0 | 76 | unload(this, reason); |
michael@0 | 77 | }, |
michael@0 | 78 | setup: function() { |
michael@0 | 79 | // Implement your initialize logic here. |
michael@0 | 80 | }, |
michael@0 | 81 | dispose: function() { |
michael@0 | 82 | // Implement your cleanup logic here. |
michael@0 | 83 | } |
michael@0 | 84 | }); |
michael@0 | 85 | exports.Disposable = Disposable; |
michael@0 | 86 | |
michael@0 | 87 | // Disposable instances observe add-on unload notifications in |
michael@0 | 88 | // order to trigger `unload` on them. |
michael@0 | 89 | observe.define(Disposable, (disposable, subject, topic, data) => { |
michael@0 | 90 | const isUnloadTopic = topic === addonUnloadTopic; |
michael@0 | 91 | const isUnloadSubject = subject.wrappedJSObject === unloadSubject; |
michael@0 | 92 | if (isUnloadTopic && isUnloadSubject) { |
michael@0 | 93 | unsubscribe(disposable, topic); |
michael@0 | 94 | unload(disposable); |
michael@0 | 95 | } |
michael@0 | 96 | }); |
michael@0 | 97 | |
michael@0 | 98 | const unloaders = { |
michael@0 | 99 | destroy: dispose, |
michael@0 | 100 | uninstall: uninstall, |
michael@0 | 101 | shutdown: shutdown, |
michael@0 | 102 | disable: disable, |
michael@0 | 103 | upgrade: upgrade, |
michael@0 | 104 | downgrade: downgrade |
michael@0 | 105 | } |
michael@0 | 106 | const unloaded = new WeakMap(); |
michael@0 | 107 | unload.define(Disposable, (disposable, reason) => { |
michael@0 | 108 | if (!unloaded.get(disposable)) { |
michael@0 | 109 | unloaded.set(disposable, true); |
michael@0 | 110 | // Pick an unload handler associated with an unload |
michael@0 | 111 | // reason (falling back to destroy if not found) and |
michael@0 | 112 | // delegate unloading to it. |
michael@0 | 113 | const unload = unloaders[reason] || unloaders.destroy; |
michael@0 | 114 | unload(disposable); |
michael@0 | 115 | } |
michael@0 | 116 | }); |
michael@0 | 117 | |
michael@0 | 118 | |
michael@0 | 119 | // If add-on is disabled munally, it's being upgraded, downgraded |
michael@0 | 120 | // or uniststalled `dispose` is invoked to undo any changes that |
michael@0 | 121 | // has being done by it in this session. |
michael@0 | 122 | disable.define(Disposable, dispose); |
michael@0 | 123 | downgrade.define(Disposable, dispose); |
michael@0 | 124 | upgrade.define(Disposable, dispose); |
michael@0 | 125 | uninstall.define(Disposable, dispose); |
michael@0 | 126 | |
michael@0 | 127 | // If application is shut down no dispose is invoked as undo-ing |
michael@0 | 128 | // changes made by instance is likely to just waste of resources & |
michael@0 | 129 | // increase shutdown time. Although specefic components may choose |
michael@0 | 130 | // to implement shutdown handler that does something better. |
michael@0 | 131 | shutdown.define(Disposable, disposable => {}); |
michael@0 | 132 |