Last active
December 23, 2015 23:09
-
-
Save shadybones/6708372 to your computer and use it in GitHub Desktop.
Firefox Module barebones view of the required code for a ContentPolicy implementation. nsIContentPolicy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); | |
var EXPORTED_SYMBOLS = ["ContentPolicy"]; | |
var ContentPolicy = { | |
classDescription: "Third Party Filtering", //some description | |
classID: Components.ID("dabef210-7f6b-11e0-b278-0800200c9a66"), //UUID. Generated using UUID tool. | |
contractID: "@${app_name}/filter/thirdparty;1", //structure: "@${app_name}/${generic_descriptor}/${name};1" | |
QueryInterface: XPCOMUtils.generateQI( | |
[ Components.interfaces.nsIContentPolicy, | |
Components.interfaces.nsIObserver, | |
Components.interfaces.nsIChannelEventSink, | |
Components.interfaces.nsIFactory ] ), //all the interfaces the module implements. IMPORTANT!!!!! | |
createInstance: function(outer, iid){ | |
if (outer) throw Components.results.NS_ERROR_NO_AGGREGATION; | |
return this.QueryInterface(iid); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////MAIN METHOD FOR FILTERING////// | |
/** | |
* NOTE!: DO NOT access properties, of any sort, on any DOM object, from here | |
* without using XPCNativeWrapper (either explicitly or implicitly) | |
* | |
* @param contentType : The type of the content requested, one of the following values: | |
* "OTHER", "SCRIPT", "IMAGE", "STYLESHEET", "OBJECT", "DOCUMENT", "SUBDOCUMENT", "REFRESH", | |
* "XBL", "PING", "XMLHTTPREQUEST", "OBJECT_SUBREQUEST", "DTD", "FONT", "MEDIA" | |
* @param location : The location (nsIURI) of the content to be loaded. Cannot be null. | |
* @param origin (optional) : The location (nsIURI) of the requesting resource. Can be null. | |
* @param context (optional) : The nsIDOMWindow or nsIDOMNode that initiated the request. Can be null. | |
* The Context is the -new- tab/window if a tab/window is opened by user action. | |
* @param mimeGuess (optional) : A guess at the content's mime Type. | |
* @param extra (optional) : Extra variable for non-Gecko callers to pass data to callees. | |
* @return 1,-1 Indicating Allow, or Block, the request. | |
*/ | |
shouldLoad: function ContentPolicy_shouldLoad(contentType, location, origin, context, mimeTypeGuess, extra) { | |
if(should_block) return -1; | |
else return 1; | |
} | |
shouldProcess: function(){ | |
return 1; | |
} | |
onChannelRedirect: function ContentPolicy_onChannelRedirect(oldChannel, newChannel, flags){ | |
if(want_to_block) throw Components.results.NS_BASE_STREAM_WOULD_BLOCK; | |
else { /*do nothing*/ } | |
} | |
asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback){ | |
this.onChannelRedirect(oldChannel, newChannel, flags); | |
callback.onRedirectVerifyCallback(Components.results.NS_OK); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///FOR REGISTERING THE MODULE. REQUIRED IF THE BROWSER WILL BE USING YOUR MODULE | |
var cr = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar); | |
try{ | |
cr.registerFactory(this.classID, this.classDescription, this.contractID, ContentPolicy); | |
}catch(e){} | |
///REGISTERING THE MODULE TO CERTAIN BROWSER EVENT LISTENER/OBSERVER | |
var cm = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager); | |
cm.addCategoryEntry("content-policy", this.classDescription, this.contractID, false, true); | |
cm.addCategoryEntry("net-channel-event-sinks", this.classDescription, this.contractID, false, true); | |
///ALSO good to later on remove the listeners | |
cm.deleteCategoryEntry("content-policy", this.classDescription, false); | |
cm.deleteCategoryEntry("net-channel-event-sinks", this.classDescription, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment