toolkit/modules/RemoteAddonsParent.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/RemoteAddonsParent.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     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 = ["RemoteAddonsParent"];
    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 listens for nsIContentPolicy hooks firing in child
    1.20 + * processes. It then fires all hooks in the parent process and
    1.21 + * returns the result to the child.
    1.22 + */
    1.23 +let ContentPolicyParent = {
    1.24 +  /**
    1.25 +   * Some builtin policies will have already run in the child, and
    1.26 +   * there's no reason to run them in the parent. This is a list of
    1.27 +   * those policies. We assume that all child processes have the same
    1.28 +   * set of built-in policies.
    1.29 +   */
    1.30 +  _policiesToIgnore: [],
    1.31 +
    1.32 +  init: function() {
    1.33 +    let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
    1.34 +               .getService(Ci.nsIMessageBroadcaster);
    1.35 +    ppmm.addMessageListener("Addons:ContentPolicy:IgnorePolicies", this);
    1.36 +    ppmm.addMessageListener("Addons:ContentPolicy:Run", this);
    1.37 +
    1.38 +    Services.obs.addObserver(this, "xpcom-category-entry-added", true);
    1.39 +    Services.obs.addObserver(this, "xpcom-category-entry-removed", true);
    1.40 +  },
    1.41 +
    1.42 +  observe: function(aSubject, aTopic, aData) {
    1.43 +    switch (aTopic) {
    1.44 +      case "xpcom-category-entry-added":
    1.45 +      case "xpcom-category-entry-removed":
    1.46 +        if (aData == "content-policy")
    1.47 +          this.updatePolicies();
    1.48 +        break;
    1.49 +    }
    1.50 +  },
    1.51 +
    1.52 +  /**
    1.53 +   * There's no need for the child process to inform us about the
    1.54 +   * shouldLoad hook if we don't have any policies in the parent to
    1.55 +   * run. This code iterates over the parent's policies, looking for
    1.56 +   * ones that should not be ignored. Based on that, it tells the
    1.57 +   * children whether it needs to be informed about the shouldLoad
    1.58 +   * hook.
    1.59 +   */
    1.60 +  updatePolicies: function() {
    1.61 +    let needHook = false;
    1.62 +
    1.63 +    let services = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager)
    1.64 +                                                       .enumerateCategory("content-policy");
    1.65 +    while (services.hasMoreElements()) {
    1.66 +      let item = services.getNext();
    1.67 +      let name = item.QueryInterface(Components.interfaces.nsISupportsCString).toString();
    1.68 +
    1.69 +      if (this._policiesToIgnore.indexOf(name) == -1) {
    1.70 +        needHook = true;
    1.71 +        break;
    1.72 +      }
    1.73 +    }
    1.74 +
    1.75 +    let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
    1.76 +               .getService(Ci.nsIMessageBroadcaster);
    1.77 +    ppmm.broadcastAsyncMessage("Addons:ContentPolicy:NeedHook", { needed: needHook });
    1.78 +  },
    1.79 +
    1.80 +  receiveMessage: function (aMessage) {
    1.81 +    switch (aMessage.name) {
    1.82 +      case "Addons:ContentPolicy:IgnorePolicies":
    1.83 +        this._policiesToIgnore = aMessage.data.policies;
    1.84 +        this.updatePolicies();
    1.85 +        break;
    1.86 +
    1.87 +      case "Addons:ContentPolicy:Run":
    1.88 +        return this.shouldLoad(aMessage.data, aMessage.objects);
    1.89 +        break;
    1.90 +    }
    1.91 +  },
    1.92 +
    1.93 +  shouldLoad: function(aData, aObjects) {
    1.94 +    let services = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager)
    1.95 +                                                       .enumerateCategory("content-policy");
    1.96 +    while (services.hasMoreElements()) {
    1.97 +      let item = services.getNext();
    1.98 +      let name = item.QueryInterface(Components.interfaces.nsISupportsCString).toString();
    1.99 +      if (this._policiesToIgnore.indexOf(name) != -1)
   1.100 +        continue;
   1.101 +
   1.102 +      let policy = Cc[name].getService(Ci.nsIContentPolicy);
   1.103 +      try {
   1.104 +        let result = policy.shouldLoad(aData.contentType,
   1.105 +                                       aObjects.contentLocation,
   1.106 +                                       aObjects.requestOrigin,
   1.107 +                                       aObjects.node,
   1.108 +                                       aData.mimeTypeGuess,
   1.109 +                                       null);
   1.110 +        if (result != Ci.nsIContentPolicy.ACCEPT && result != 0)
   1.111 +          return result;
   1.112 +      } catch (e) {}
   1.113 +    }
   1.114 +
   1.115 +    return Ci.nsIContentPolicy.ACCEPT;
   1.116 +  },
   1.117 +
   1.118 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
   1.119 +};
   1.120 +
   1.121 +let RemoteAddonsParent = {
   1.122 +  initialized: false,
   1.123 +
   1.124 +  init: function() {
   1.125 +    if (this.initialized)
   1.126 +      return;
   1.127 +
   1.128 +    this.initialized = true;
   1.129 +
   1.130 +    ContentPolicyParent.init();
   1.131 +  },
   1.132 +};

mercurial