toolkit/modules/RemoteAddonsChild.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/RemoteAddonsChild.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +// -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +// This Source Code Form is subject to the terms of the Mozilla Public
     1.6 +// License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 +// file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.8 +
     1.9 +this.EXPORTED_SYMBOLS = ["RemoteAddonsChild"];
    1.10 +
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cc = Components.classes;
    1.13 +const Cu = Components.utils;
    1.14 +
    1.15 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.16 +Cu.import('resource://gre/modules/Services.jsm');
    1.17 +
    1.18 +/**
    1.19 + * This code registers an nsIContentPolicy in the child process. When
    1.20 + * it runs, it notifies the parent that it needs to run its own
    1.21 + * nsIContentPolicy list. If any policy in the parent rejects the load,
    1.22 + * that answer is returned to the child.
    1.23 + */
    1.24 +let ContentPolicyChild = {
    1.25 +  _classDescription: "Addon shim content policy",
    1.26 +  _classID: Components.ID("6e869130-635c-11e2-bcfd-0800200c9a66"),
    1.27 +  _contractID: "@mozilla.org/addon-child/policy;1",
    1.28 +
    1.29 +  /**
    1.30 +   * We only notify the parent of the load if it has any
    1.31 +   * non-builtin policies registered.
    1.32 +   */
    1.33 +  _childNeedsHook: false,
    1.34 +
    1.35 +  init: function(aContentGlobal) {
    1.36 +    let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
    1.37 +    registrar.registerFactory(this._classID, this._classDescription, this._contractID, this);
    1.38 +
    1.39 +    var catMan = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
    1.40 +    catMan.addCategoryEntry("content-policy", this._contractID, this._contractID, false, true);
    1.41 +
    1.42 +    let policiesToIgnore = [];
    1.43 +    let services = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager)
    1.44 +                                                       .enumerateCategory("content-policy");
    1.45 +    while (services.hasMoreElements()) {
    1.46 +      let item = services.getNext();
    1.47 +      let name = item.QueryInterface(Components.interfaces.nsISupportsCString).toString();
    1.48 +      policiesToIgnore.push(name);
    1.49 +    }
    1.50 +
    1.51 +    let cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
    1.52 +               .getService(Ci.nsISyncMessageSender);
    1.53 +    cpmm.addMessageListener("Addons:ContentPolicy:NeedHook", this);
    1.54 +    cpmm.sendAsyncMessage("Addons:ContentPolicy:IgnorePolicies", { "policies": policiesToIgnore });
    1.55 +  },
    1.56 +
    1.57 +  receiveMessage: function(aMessage) {
    1.58 +    switch (aMessage.name) {
    1.59 +      case "Addons:ContentPolicy:NeedHook":
    1.60 +        this._childNeedsHook = aMessage.data.needed;
    1.61 +        break;
    1.62 +    }
    1.63 +  },
    1.64 +
    1.65 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIObserver,
    1.66 +                                         Ci.nsIChannelEventSink, Ci.nsIFactory,
    1.67 +                                         Ci.nsISupportsWeakReference]),
    1.68 +
    1.69 +  shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) {
    1.70 +    if (!this._childNeedsHook)
    1.71 +      return Ci.nsIContentPolicy.ACCEPT;
    1.72 +
    1.73 +    let cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
    1.74 +               .getService(Ci.nsISyncMessageSender);
    1.75 +    var rval = cpmm.sendRpcMessage("Addons:ContentPolicy:Run", {
    1.76 +      contentType: contentType,
    1.77 +      mimeTypeGuess: mimeTypeGuess
    1.78 +    }, {
    1.79 +      contentLocation: contentLocation,
    1.80 +      requestOrigin: requestOrigin,
    1.81 +      node: node
    1.82 +    });
    1.83 +    if (rval.length != 1)
    1.84 +      return Ci.nsIContentPolicy.ACCEPT;
    1.85 +
    1.86 +    return rval[0];
    1.87 +  },
    1.88 +
    1.89 +  shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode, mimeType, extra) {
    1.90 +    return Ci.nsIContentPolicy.ACCEPT;
    1.91 +  },
    1.92 +
    1.93 +  createInstance: function(outer, iid) {
    1.94 +    if (outer)
    1.95 +      throw Cr.NS_ERROR_NO_AGGREGATION;
    1.96 +    return this.QueryInterface(iid);
    1.97 +  },
    1.98 +};
    1.99 +
   1.100 +let RemoteAddonsChild = {
   1.101 +  initialized: false,
   1.102 +
   1.103 +  init: function(aContentGlobal) {
   1.104 +    if (this.initialized)
   1.105 +      return;
   1.106 +
   1.107 +    this.initialized = true;
   1.108 +
   1.109 +    ContentPolicyChild.init(aContentGlobal);
   1.110 +  },
   1.111 +};

mercurial