Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | // This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | // License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 5 | |
michael@0 | 6 | this.EXPORTED_SYMBOLS = ["RemoteAddonsParent"]; |
michael@0 | 7 | |
michael@0 | 8 | const Ci = Components.interfaces; |
michael@0 | 9 | const Cc = Components.classes; |
michael@0 | 10 | const Cu = Components.utils; |
michael@0 | 11 | |
michael@0 | 12 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 13 | Cu.import('resource://gre/modules/Services.jsm'); |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * This code listens for nsIContentPolicy hooks firing in child |
michael@0 | 17 | * processes. It then fires all hooks in the parent process and |
michael@0 | 18 | * returns the result to the child. |
michael@0 | 19 | */ |
michael@0 | 20 | let ContentPolicyParent = { |
michael@0 | 21 | /** |
michael@0 | 22 | * Some builtin policies will have already run in the child, and |
michael@0 | 23 | * there's no reason to run them in the parent. This is a list of |
michael@0 | 24 | * those policies. We assume that all child processes have the same |
michael@0 | 25 | * set of built-in policies. |
michael@0 | 26 | */ |
michael@0 | 27 | _policiesToIgnore: [], |
michael@0 | 28 | |
michael@0 | 29 | init: function() { |
michael@0 | 30 | let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"] |
michael@0 | 31 | .getService(Ci.nsIMessageBroadcaster); |
michael@0 | 32 | ppmm.addMessageListener("Addons:ContentPolicy:IgnorePolicies", this); |
michael@0 | 33 | ppmm.addMessageListener("Addons:ContentPolicy:Run", this); |
michael@0 | 34 | |
michael@0 | 35 | Services.obs.addObserver(this, "xpcom-category-entry-added", true); |
michael@0 | 36 | Services.obs.addObserver(this, "xpcom-category-entry-removed", true); |
michael@0 | 37 | }, |
michael@0 | 38 | |
michael@0 | 39 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 40 | switch (aTopic) { |
michael@0 | 41 | case "xpcom-category-entry-added": |
michael@0 | 42 | case "xpcom-category-entry-removed": |
michael@0 | 43 | if (aData == "content-policy") |
michael@0 | 44 | this.updatePolicies(); |
michael@0 | 45 | break; |
michael@0 | 46 | } |
michael@0 | 47 | }, |
michael@0 | 48 | |
michael@0 | 49 | /** |
michael@0 | 50 | * There's no need for the child process to inform us about the |
michael@0 | 51 | * shouldLoad hook if we don't have any policies in the parent to |
michael@0 | 52 | * run. This code iterates over the parent's policies, looking for |
michael@0 | 53 | * ones that should not be ignored. Based on that, it tells the |
michael@0 | 54 | * children whether it needs to be informed about the shouldLoad |
michael@0 | 55 | * hook. |
michael@0 | 56 | */ |
michael@0 | 57 | updatePolicies: function() { |
michael@0 | 58 | let needHook = false; |
michael@0 | 59 | |
michael@0 | 60 | let services = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager) |
michael@0 | 61 | .enumerateCategory("content-policy"); |
michael@0 | 62 | while (services.hasMoreElements()) { |
michael@0 | 63 | let item = services.getNext(); |
michael@0 | 64 | let name = item.QueryInterface(Components.interfaces.nsISupportsCString).toString(); |
michael@0 | 65 | |
michael@0 | 66 | if (this._policiesToIgnore.indexOf(name) == -1) { |
michael@0 | 67 | needHook = true; |
michael@0 | 68 | break; |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"] |
michael@0 | 73 | .getService(Ci.nsIMessageBroadcaster); |
michael@0 | 74 | ppmm.broadcastAsyncMessage("Addons:ContentPolicy:NeedHook", { needed: needHook }); |
michael@0 | 75 | }, |
michael@0 | 76 | |
michael@0 | 77 | receiveMessage: function (aMessage) { |
michael@0 | 78 | switch (aMessage.name) { |
michael@0 | 79 | case "Addons:ContentPolicy:IgnorePolicies": |
michael@0 | 80 | this._policiesToIgnore = aMessage.data.policies; |
michael@0 | 81 | this.updatePolicies(); |
michael@0 | 82 | break; |
michael@0 | 83 | |
michael@0 | 84 | case "Addons:ContentPolicy:Run": |
michael@0 | 85 | return this.shouldLoad(aMessage.data, aMessage.objects); |
michael@0 | 86 | break; |
michael@0 | 87 | } |
michael@0 | 88 | }, |
michael@0 | 89 | |
michael@0 | 90 | shouldLoad: function(aData, aObjects) { |
michael@0 | 91 | let services = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager) |
michael@0 | 92 | .enumerateCategory("content-policy"); |
michael@0 | 93 | while (services.hasMoreElements()) { |
michael@0 | 94 | let item = services.getNext(); |
michael@0 | 95 | let name = item.QueryInterface(Components.interfaces.nsISupportsCString).toString(); |
michael@0 | 96 | if (this._policiesToIgnore.indexOf(name) != -1) |
michael@0 | 97 | continue; |
michael@0 | 98 | |
michael@0 | 99 | let policy = Cc[name].getService(Ci.nsIContentPolicy); |
michael@0 | 100 | try { |
michael@0 | 101 | let result = policy.shouldLoad(aData.contentType, |
michael@0 | 102 | aObjects.contentLocation, |
michael@0 | 103 | aObjects.requestOrigin, |
michael@0 | 104 | aObjects.node, |
michael@0 | 105 | aData.mimeTypeGuess, |
michael@0 | 106 | null); |
michael@0 | 107 | if (result != Ci.nsIContentPolicy.ACCEPT && result != 0) |
michael@0 | 108 | return result; |
michael@0 | 109 | } catch (e) {} |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | return Ci.nsIContentPolicy.ACCEPT; |
michael@0 | 113 | }, |
michael@0 | 114 | |
michael@0 | 115 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]), |
michael@0 | 116 | }; |
michael@0 | 117 | |
michael@0 | 118 | let RemoteAddonsParent = { |
michael@0 | 119 | initialized: false, |
michael@0 | 120 | |
michael@0 | 121 | init: function() { |
michael@0 | 122 | if (this.initialized) |
michael@0 | 123 | return; |
michael@0 | 124 | |
michael@0 | 125 | this.initialized = true; |
michael@0 | 126 | |
michael@0 | 127 | ContentPolicyParent.init(); |
michael@0 | 128 | }, |
michael@0 | 129 | }; |