| |
1 // -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| |
2 // This Source Code Form is subject to the terms of the Mozilla Public |
| |
3 // License, v. 2.0. If a copy of the MPL was not distributed with this |
| |
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| |
5 |
| |
6 this.EXPORTED_SYMBOLS = ["RemoteAddonsChild"]; |
| |
7 |
| |
8 const Ci = Components.interfaces; |
| |
9 const Cc = Components.classes; |
| |
10 const Cu = Components.utils; |
| |
11 |
| |
12 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
| |
13 Cu.import('resource://gre/modules/Services.jsm'); |
| |
14 |
| |
15 /** |
| |
16 * This code registers an nsIContentPolicy in the child process. When |
| |
17 * it runs, it notifies the parent that it needs to run its own |
| |
18 * nsIContentPolicy list. If any policy in the parent rejects the load, |
| |
19 * that answer is returned to the child. |
| |
20 */ |
| |
21 let ContentPolicyChild = { |
| |
22 _classDescription: "Addon shim content policy", |
| |
23 _classID: Components.ID("6e869130-635c-11e2-bcfd-0800200c9a66"), |
| |
24 _contractID: "@mozilla.org/addon-child/policy;1", |
| |
25 |
| |
26 /** |
| |
27 * We only notify the parent of the load if it has any |
| |
28 * non-builtin policies registered. |
| |
29 */ |
| |
30 _childNeedsHook: false, |
| |
31 |
| |
32 init: function(aContentGlobal) { |
| |
33 let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); |
| |
34 registrar.registerFactory(this._classID, this._classDescription, this._contractID, this); |
| |
35 |
| |
36 var catMan = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); |
| |
37 catMan.addCategoryEntry("content-policy", this._contractID, this._contractID, false, true); |
| |
38 |
| |
39 let policiesToIgnore = []; |
| |
40 let services = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager) |
| |
41 .enumerateCategory("content-policy"); |
| |
42 while (services.hasMoreElements()) { |
| |
43 let item = services.getNext(); |
| |
44 let name = item.QueryInterface(Components.interfaces.nsISupportsCString).toString(); |
| |
45 policiesToIgnore.push(name); |
| |
46 } |
| |
47 |
| |
48 let cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] |
| |
49 .getService(Ci.nsISyncMessageSender); |
| |
50 cpmm.addMessageListener("Addons:ContentPolicy:NeedHook", this); |
| |
51 cpmm.sendAsyncMessage("Addons:ContentPolicy:IgnorePolicies", { "policies": policiesToIgnore }); |
| |
52 }, |
| |
53 |
| |
54 receiveMessage: function(aMessage) { |
| |
55 switch (aMessage.name) { |
| |
56 case "Addons:ContentPolicy:NeedHook": |
| |
57 this._childNeedsHook = aMessage.data.needed; |
| |
58 break; |
| |
59 } |
| |
60 }, |
| |
61 |
| |
62 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIObserver, |
| |
63 Ci.nsIChannelEventSink, Ci.nsIFactory, |
| |
64 Ci.nsISupportsWeakReference]), |
| |
65 |
| |
66 shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) { |
| |
67 if (!this._childNeedsHook) |
| |
68 return Ci.nsIContentPolicy.ACCEPT; |
| |
69 |
| |
70 let cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] |
| |
71 .getService(Ci.nsISyncMessageSender); |
| |
72 var rval = cpmm.sendRpcMessage("Addons:ContentPolicy:Run", { |
| |
73 contentType: contentType, |
| |
74 mimeTypeGuess: mimeTypeGuess |
| |
75 }, { |
| |
76 contentLocation: contentLocation, |
| |
77 requestOrigin: requestOrigin, |
| |
78 node: node |
| |
79 }); |
| |
80 if (rval.length != 1) |
| |
81 return Ci.nsIContentPolicy.ACCEPT; |
| |
82 |
| |
83 return rval[0]; |
| |
84 }, |
| |
85 |
| |
86 shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode, mimeType, extra) { |
| |
87 return Ci.nsIContentPolicy.ACCEPT; |
| |
88 }, |
| |
89 |
| |
90 createInstance: function(outer, iid) { |
| |
91 if (outer) |
| |
92 throw Cr.NS_ERROR_NO_AGGREGATION; |
| |
93 return this.QueryInterface(iid); |
| |
94 }, |
| |
95 }; |
| |
96 |
| |
97 let RemoteAddonsChild = { |
| |
98 initialized: false, |
| |
99 |
| |
100 init: function(aContentGlobal) { |
| |
101 if (this.initialized) |
| |
102 return; |
| |
103 |
| |
104 this.initialized = true; |
| |
105 |
| |
106 ContentPolicyChild.init(aContentGlobal); |
| |
107 }, |
| |
108 }; |