whitespace
[conkeror.git] / components / content-policy.js
blob1aab83665d15f067e6cd18b5a5534c4f0c55c974
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 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
12 var content_policy_listener;
14 function content_policy () {
15     this.conkeror = Cc["@conkeror.mozdev.org/application;1"]
16         .getService()
17         .wrappedJSObject;
18     this.conkeror.define_hook("content_policy_hook", "RUN_HOOK_UNTIL_SUCCESS");
19     this.conkeror.define_variable("content_policy_scheme_whitelist",
20         { about: true, chrome: true, data: true, javascript: true, "moz-icon": true },
21         "Requests whose scheme is in this structure (with a true value) "+
22         "will be whitelisted before calling content_policy_hook.");
23     this.conkeror.content_policy_listener = this;
25 content_policy.prototype = {
26     QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy,
27                                            Ci.nsIObserverService]),
28     contractID: "@conkeror.org/content-policy-listener;1",
29     classID: Components.ID("{2926dd11-4d76-4965-bcdc-4aaad70ada04}"),
30     classDescription: "content_policy",
31     _xpcom_factory: {
32         createInstance: function (outer, iid) {
33             if (outer)
34                 throw Cr.NS_ERROR_NO_AGGREGATION;
35             if (! content_policy_listener)
36                 content_policy_listener = new content_policy();
37             return content_policy_listener;
38         }
39     },
40     _xpcom_categories: [{category: "content-policy"}],
42     enabled: false,
43     shouldLoad: function (content_type,     //unsigned long
44                           content_location, //nsIURI
45                           request_origin,   //nsIURI
46                           context,          //nsISupports
47                           mime_type_guess,  //ACString
48                           extra)            //nsISupports
49     {
50         if (this.enabled) {
51             if (this.conkeror.content_policy_scheme_whitelist[content_location.scheme])
52                 return Ci.nsIContentPolicy.ACCEPT;
53             var action = this.conkeror.content_policy_hook.run(
54                 content_type, content_location, request_origin,
55                 context, mime_type_guess, extra);
56         }
57         return (action || Ci.nsIContentPolicy.ACCEPT);
58     },
59     shouldProcess: function (content_type,     //unsigned long
60                              content_location, //nsIURI
61                              request_origin,   //nsIURI
62                              context,          //nsISupports
63                              mime_type,        //ACString
64                              extra)            //nsISupports
65     {
66         return Ci.nsIContentPolicy.ACCEPT;
67     }
70 if (XPCOMUtils.generateNSGetFactory)
71     var NSGetFactory = XPCOMUtils.generateNSGetFactory([content_policy]); //XULRunner 2.0
72 else
73     var NSGetModule = XPCOMUtils.generateNSGetModule([content_policy]);