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 file, |
michael@0 | 3 | * 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 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 8 | |
michael@0 | 9 | this.EXPORTED_SYMBOLS = ["DataStoreChangeNotifier"]; |
michael@0 | 10 | |
michael@0 | 11 | function debug(s) { |
michael@0 | 12 | //dump('DEBUG DataStoreChangeNotifier: ' + s + '\n'); |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | // DataStoreServiceInternal should not be converted into a lazy getter as it |
michael@0 | 16 | // runs code during initialization. |
michael@0 | 17 | Cu.import('resource://gre/modules/DataStoreServiceInternal.jsm'); |
michael@0 | 18 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 19 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 20 | |
michael@0 | 21 | XPCOMUtils.defineLazyServiceGetter(this, "ppmm", |
michael@0 | 22 | "@mozilla.org/parentprocessmessagemanager;1", |
michael@0 | 23 | "nsIMessageBroadcaster"); |
michael@0 | 24 | |
michael@0 | 25 | this.DataStoreChangeNotifier = { |
michael@0 | 26 | children: [], |
michael@0 | 27 | messages: [ "DataStore:Changed", "DataStore:RegisterForMessages", |
michael@0 | 28 | "DataStore:UnregisterForMessages", |
michael@0 | 29 | "child-process-shutdown" ], |
michael@0 | 30 | |
michael@0 | 31 | init: function() { |
michael@0 | 32 | debug("init"); |
michael@0 | 33 | |
michael@0 | 34 | this.messages.forEach((function(msgName) { |
michael@0 | 35 | ppmm.addMessageListener(msgName, this); |
michael@0 | 36 | }).bind(this)); |
michael@0 | 37 | |
michael@0 | 38 | Services.obs.addObserver(this, 'xpcom-shutdown', false); |
michael@0 | 39 | }, |
michael@0 | 40 | |
michael@0 | 41 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 42 | debug("observe"); |
michael@0 | 43 | |
michael@0 | 44 | switch (aTopic) { |
michael@0 | 45 | case 'xpcom-shutdown': |
michael@0 | 46 | this.messages.forEach((function(msgName) { |
michael@0 | 47 | ppmm.removeMessageListener(msgName, this); |
michael@0 | 48 | }).bind(this)); |
michael@0 | 49 | |
michael@0 | 50 | Services.obs.removeObserver(this, 'xpcom-shutdown'); |
michael@0 | 51 | ppmm = null; |
michael@0 | 52 | break; |
michael@0 | 53 | |
michael@0 | 54 | default: |
michael@0 | 55 | debug("Wrong observer topic: " + aTopic); |
michael@0 | 56 | break; |
michael@0 | 57 | } |
michael@0 | 58 | }, |
michael@0 | 59 | |
michael@0 | 60 | broadcastMessage: function broadcastMessage(aData) { |
michael@0 | 61 | debug("Broadast"); |
michael@0 | 62 | this.children.forEach(function(obj) { |
michael@0 | 63 | if (obj.store == aData.store && obj.owner == aData.owner) { |
michael@0 | 64 | obj.mm.sendAsyncMessage("DataStore:Changed:Return:OK", aData); |
michael@0 | 65 | } |
michael@0 | 66 | }); |
michael@0 | 67 | }, |
michael@0 | 68 | |
michael@0 | 69 | |
michael@0 | 70 | receiveMessage: function(aMessage) { |
michael@0 | 71 | debug("receiveMessage"); |
michael@0 | 72 | |
michael@0 | 73 | // No check has to be done when the message is 'child-process-shutdown' |
michael@0 | 74 | // because at this point the target is already disconnected from |
michael@0 | 75 | // nsFrameMessageManager, so that assertAppHasStatus will always fail. |
michael@0 | 76 | let prefName = 'dom.testing.datastore_enabled_for_hosted_apps'; |
michael@0 | 77 | if (aMessage.name != 'child-process-shutdown' && |
michael@0 | 78 | (Services.prefs.getPrefType(prefName) == Services.prefs.PREF_INVALID || |
michael@0 | 79 | !Services.prefs.getBoolPref(prefName)) && |
michael@0 | 80 | !aMessage.target.assertAppHasStatus(Ci.nsIPrincipal.APP_STATUS_CERTIFIED)) { |
michael@0 | 81 | return; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | switch (aMessage.name) { |
michael@0 | 85 | case "DataStore:Changed": |
michael@0 | 86 | this.broadcastMessage(aMessage.data); |
michael@0 | 87 | break; |
michael@0 | 88 | |
michael@0 | 89 | case "DataStore:RegisterForMessages": |
michael@0 | 90 | debug("Register!"); |
michael@0 | 91 | |
michael@0 | 92 | for (let i = 0; i < this.children.length; ++i) { |
michael@0 | 93 | if (this.children[i].mm == aMessage.target && |
michael@0 | 94 | this.children[i].store == aMessage.data.store && |
michael@0 | 95 | this.children[i].owner == aMessage.data.owner) { |
michael@0 | 96 | debug("Register on existing index: " + i); |
michael@0 | 97 | ++this.children[i].count; |
michael@0 | 98 | return; |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | this.children.push({ mm: aMessage.target, |
michael@0 | 103 | store: aMessage.data.store, |
michael@0 | 104 | owner: aMessage.data.owner, |
michael@0 | 105 | count: 1 }); |
michael@0 | 106 | break; |
michael@0 | 107 | |
michael@0 | 108 | case "child-process-shutdown": |
michael@0 | 109 | case "DataStore:UnregisterForMessages": |
michael@0 | 110 | debug("Unregister"); |
michael@0 | 111 | |
michael@0 | 112 | for (let i = 0; i < this.children.length;) { |
michael@0 | 113 | if (this.children[i].mm == aMessage.target) { |
michael@0 | 114 | debug("Unregister index: " + i); |
michael@0 | 115 | if (!--this.children[i].count) { |
michael@0 | 116 | debug("Unregister delete index: " + i); |
michael@0 | 117 | this.children.splice(i, 1); |
michael@0 | 118 | } |
michael@0 | 119 | break; |
michael@0 | 120 | } else { |
michael@0 | 121 | ++i; |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | break; |
michael@0 | 125 | |
michael@0 | 126 | default: |
michael@0 | 127 | debug("Wrong message: " + aMessage.name); |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | DataStoreChangeNotifier.init(); |