michael@0: // -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: // This Source Code Form is subject to the terms of the Mozilla Public michael@0: // License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: // file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: this.EXPORTED_SYMBOLS = ["RemoteAddonsChild"]; michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cc = Components.classes; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import('resource://gre/modules/Services.jsm'); michael@0: michael@0: /** michael@0: * This code registers an nsIContentPolicy in the child process. When michael@0: * it runs, it notifies the parent that it needs to run its own michael@0: * nsIContentPolicy list. If any policy in the parent rejects the load, michael@0: * that answer is returned to the child. michael@0: */ michael@0: let ContentPolicyChild = { michael@0: _classDescription: "Addon shim content policy", michael@0: _classID: Components.ID("6e869130-635c-11e2-bcfd-0800200c9a66"), michael@0: _contractID: "@mozilla.org/addon-child/policy;1", michael@0: michael@0: /** michael@0: * We only notify the parent of the load if it has any michael@0: * non-builtin policies registered. michael@0: */ michael@0: _childNeedsHook: false, michael@0: michael@0: init: function(aContentGlobal) { michael@0: let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.registerFactory(this._classID, this._classDescription, this._contractID, this); michael@0: michael@0: var catMan = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); michael@0: catMan.addCategoryEntry("content-policy", this._contractID, this._contractID, false, true); michael@0: michael@0: let policiesToIgnore = []; michael@0: let services = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager) michael@0: .enumerateCategory("content-policy"); michael@0: while (services.hasMoreElements()) { michael@0: let item = services.getNext(); michael@0: let name = item.QueryInterface(Components.interfaces.nsISupportsCString).toString(); michael@0: policiesToIgnore.push(name); michael@0: } michael@0: michael@0: let cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] michael@0: .getService(Ci.nsISyncMessageSender); michael@0: cpmm.addMessageListener("Addons:ContentPolicy:NeedHook", this); michael@0: cpmm.sendAsyncMessage("Addons:ContentPolicy:IgnorePolicies", { "policies": policiesToIgnore }); michael@0: }, michael@0: michael@0: receiveMessage: function(aMessage) { michael@0: switch (aMessage.name) { michael@0: case "Addons:ContentPolicy:NeedHook": michael@0: this._childNeedsHook = aMessage.data.needed; michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIObserver, michael@0: Ci.nsIChannelEventSink, Ci.nsIFactory, michael@0: Ci.nsISupportsWeakReference]), michael@0: michael@0: shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) { michael@0: if (!this._childNeedsHook) michael@0: return Ci.nsIContentPolicy.ACCEPT; michael@0: michael@0: let cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] michael@0: .getService(Ci.nsISyncMessageSender); michael@0: var rval = cpmm.sendRpcMessage("Addons:ContentPolicy:Run", { michael@0: contentType: contentType, michael@0: mimeTypeGuess: mimeTypeGuess michael@0: }, { michael@0: contentLocation: contentLocation, michael@0: requestOrigin: requestOrigin, michael@0: node: node michael@0: }); michael@0: if (rval.length != 1) michael@0: return Ci.nsIContentPolicy.ACCEPT; michael@0: michael@0: return rval[0]; michael@0: }, michael@0: michael@0: shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode, mimeType, extra) { michael@0: return Ci.nsIContentPolicy.ACCEPT; michael@0: }, michael@0: michael@0: createInstance: function(outer, iid) { michael@0: if (outer) michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: return this.QueryInterface(iid); michael@0: }, michael@0: }; michael@0: michael@0: let RemoteAddonsChild = { michael@0: initialized: false, michael@0: michael@0: init: function(aContentGlobal) { michael@0: if (this.initialized) michael@0: return; michael@0: michael@0: this.initialized = true; michael@0: michael@0: ContentPolicyChild.init(aContentGlobal); michael@0: }, michael@0: };