content-policy: new module and component
[conkeror.git] / components / content-policy.js
blobf2bafae37fbece59391ccd0a7becdbcead7b923f
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.content_policy_listener = this;
21 content_policy.prototype = {
22     QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy,
23                                            Ci.nsIObserverService]),
24     contractID: "@conkeror.org/content-policy-listener;1",
25     classID: Components.ID("{2926dd11-4d76-4965-bcdc-4aaad70ada04}"),
26     classDescription: "content_policy",
27     _xpcom_factory: {
28         createInstance: function (outer, iid) {
29             if (outer)
30                 throw Cr.NS_ERROR_NO_AGGREGATION;
31             if (! content_policy_listener)
32                 content_policy_listener = new content_policy();
33             return content_policy_listener;
34         }
35     },
36     _xpcom_categories: [{category: "content-policy"}],
38     enabled: false,
39     shouldLoad: function (content_type,     //unsigned long
40                           content_location, //nsIURI
41                           request_origin,   //nsIURI
42                           context,          //nsISupports
43                           mime_type_guess,  //ACString
44                           extra)            //nsISupports
45     {
46         var action;
47         if (this.enabled)
48             action = this.conkeror.content_policy_hook.run(
49                 content_type, content_location, request_origin,
50                 context, mime_type_guess, extra);
51         return (action || Ci.nsIContentPolicy.ACCEPT);
52     },
53     shouldProcess: function (content_type,     //unsigned long
54                              content_location, //nsIURI
55                              request_origin,   //nsIURI
56                              context,          //nsISupports
57                              mime_type,        //ACString
58                              extra)            //nsISupports
59     {
60         return Ci.nsIContentPolicy.ACCEPT;
61     }
64 if (XPCOMUtils.generateNSGetFactory)
65     var NSGetFactory = XPCOMUtils.generateNSGetFactory([content_policy]); //XULRunner 2.0
66 else
67     var NSGetModule = XPCOMUtils.generateNSGetModule([content_policy]);