kludge issue 513
[conkeror.git] / components / content-policy.js
blob9ff33a5d3624fe41077888b60231dd196a6df09e
1 /**
2  * (C) Copyright 2010 John J. Foerch
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 const Cc = Components.classes;
9 const Ci = Components.interfaces;
10 const Cu = Components.utils;
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
13 var content_policy_listener;
15 function content_policy () {
16     this.conkeror = Cc["@conkeror.mozdev.org/application;1"]
17         .getService()
18         .wrappedJSObject;
19     this.conkeror.define_hook("content_policy_hook", "RUN_HOOK_UNTIL_SUCCESS");
20     this.conkeror.define_variable("content_policy_scheme_whitelist",
21         { about: true, chrome: true, data: true, javascript: true,
22           mailto: true, "moz-icon": true },
23         "Requests whose scheme is in this structure (with a true value) "+
24         "will be whitelisted before calling content_policy_hook.");
25     this.conkeror.content_policy_listener = this;
27 content_policy.prototype = {
28     QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy,
29                                            Ci.nsIObserverService]),
30     contractID: "@conkeror.org/content-policy-listener;1",
31     classID: Components.ID("{2926dd11-4d76-4965-bcdc-4aaad70ada04}"),
32     classDescription: "content_policy",
33     _xpcom_factory: {
34         createInstance: function (outer, iid) {
35             if (outer)
36                 throw Cr.NS_ERROR_NO_AGGREGATION;
37             if (! content_policy_listener)
38                 content_policy_listener = new content_policy();
39             return content_policy_listener;
40         }
41     },
42     _xpcom_categories: [{category: "content-policy"}],
44     enabled: true,
45     shouldLoad: function (content_type,     //unsigned long
46                           content_location, //nsIURI
47                           request_origin,   //nsIURI
48                           context,          //nsISupports
49                           mime_type_guess,  //ACString
50                           extra)            //nsISupports
51     {
52         if (this.enabled) {
53             if (this.conkeror.content_policy_scheme_whitelist[content_location.scheme])
54                 return Ci.nsIContentPolicy.ACCEPT;
55             var action = this.conkeror.content_policy_hook.run(
56                 content_type, content_location, request_origin,
57                 context, mime_type_guess, extra);
58         }
59         return (action || Ci.nsIContentPolicy.ACCEPT);
60     },
61     shouldProcess: function (content_type,     //unsigned long
62                              content_location, //nsIURI
63                              request_origin,   //nsIURI
64                              context,          //nsISupports
65                              mime_type,        //ACString
66                              extra)            //nsISupports
67     {
68         return Ci.nsIContentPolicy.ACCEPT;
69     }
72 if (XPCOMUtils.generateNSGetFactory)
73     var NSGetFactory = XPCOMUtils.generateNSGetFactory([content_policy]); //XULRunner 2.0
74 else
75     var NSGetModule = XPCOMUtils.generateNSGetModule([content_policy]);