@ -25,7 +25,7 @@
/******************************************************************************/
/******************************************************************************/
this . EXPORTED _SYMBOLS = [ 'content Policy', 'doc Observer'] ;
this . EXPORTED _SYMBOLS = [ 'content Observer'] ;
const { interfaces : Ci , utils : Cu } = Components ;
const { interfaces : Ci , utils : Cu } = Components ;
const appName = _ _URI _ _ . match ( /:\/\/([^\/]+)/ ) [ 1 ] ;
const appName = _ _URI _ _ . match ( /:\/\/([^\/]+)/ ) [ 1 ] ;
@ -48,11 +48,13 @@ const getMessageManager = function(context) {
/******************************************************************************/
/******************************************************************************/
const content Policy = {
const content Observer = {
classDescription : 'content-policy implementation for ' + appName ,
classDescription : 'content-policy for ' + appName ,
classID : Components . ID ( '{e6d173c8-8dbf-4189-a6fd-189e8acffd27}' ) ,
classID : Components . ID ( '{e6d173c8-8dbf-4189-a6fd-189e8acffd27}' ) ,
contractID : '@' + appName + '/content-policy;1' ,
contractID : '@' + appName + '/content-policy;1' ,
ACCEPT : Ci . nsIContentPolicy . ACCEPT ,
ACCEPT : Ci . nsIContentPolicy . ACCEPT ,
MAIN _FRAME : Ci . nsIContentPolicy . TYPE _DOCUMENT ,
contentBaseURI : 'chrome://' + appName + '/content/js/' ,
messageName : appName + ':shouldLoad' ,
messageName : appName + ':shouldLoad' ,
get componentRegistrar ( ) {
get componentRegistrar ( ) {
@ -66,6 +68,7 @@ const contentPolicy = {
QueryInterface : XPCOMUtils . generateQI ( [
QueryInterface : XPCOMUtils . generateQI ( [
Ci . nsIFactory ,
Ci . nsIFactory ,
Ci . nsIObserver ,
Ci . nsIContentPolicy ,
Ci . nsIContentPolicy ,
Ci . nsISupportsWeakReference
Ci . nsISupportsWeakReference
] ) ,
] ) ,
@ -77,7 +80,10 @@ const contentPolicy = {
return this . QueryInterface ( iid ) ;
return this . QueryInterface ( iid ) ;
} ,
} ,
register : function ( ) {
register : function ( ) {
Services . obs . addObserver ( this , 'document-element-inserted' , true ) ;
this . componentRegistrar . registerFactory (
this . componentRegistrar . registerFactory (
this . classID ,
this . classID ,
this . classDescription ,
this . classDescription ,
@ -94,6 +100,8 @@ const contentPolicy = {
} ,
} ,
unregister : function ( ) {
unregister : function ( ) {
Services . obs . removeObserver ( this , 'document-element-inserted' ) ;
this . componentRegistrar . unregisterFactory ( this . classID , this ) ;
this . componentRegistrar . unregisterFactory ( this . classID , this ) ;
this . categoryManager . deleteCategoryEntry (
this . categoryManager . deleteCategoryEntry (
'content-policy' ,
'content-policy' ,
@ -109,38 +117,51 @@ const contentPolicy = {
return this . ACCEPT ;
return this . ACCEPT ;
}
}
let opener ;
if ( location . scheme !== 'http' && location . scheme !== 'https' ) {
if ( location . scheme !== 'http' && location . scheme !== 'https' ) {
return this . ACCEPT ;
if ( type !== this . MAIN _FRAME ) {
return this . ACCEPT ;
}
context = context . contentWindow || context ;
try {
opener = context . opener . location . href ;
} catch ( ex ) { }
let isPopup = location . spec === 'about:blank' && opener ;
if ( location . scheme !== 'data' && ! isPopup ) {
return this . ACCEPT ;
}
} else if ( type === this . MAIN _FRAME ) {
context = context . contentWindow || context ;
try {
opener = context . opener . location . href ;
} catch ( ex ) { }
} else {
context = ( context . ownerDocument || context ) . defaultView ;
}
}
let win = type === 6
// The context for the popups is an iframe element here,
? context . contentWindow || context
// so check context.top instead
: ( context . ownerDocument || context ) . defaultView ;
if ( win ) {
if ( context . top && context . location ) {
getMessageManager ( win ) . sendSyncMessage ( this . messageName , {
getMessageManager ( context ) . sendSyncMessage ( this . messageName , {
opener : opener || null ,
url : location . spec ,
url : location . spec ,
type : type ,
type : type ,
frameId : type === 6 ? - 1 : ( win === win . top ? 0 : 1 ) ,
frameId : type === this . MAIN _FRAME ? - 1 : ( context === context . top ? 0 : 1 ) ,
parentFrameId : win === win . top ? - 1 : 0
parentFrameId : context === context . top ? - 1 : 0
} ) ;
} ) ;
}
}
return this . ACCEPT ;
return this . ACCEPT ;
}
} ,
} ;
/******************************************************************************/
const docObserver = {
contentBaseURI : 'chrome://' + appName + '/content/js/' ,
QueryInterface : XPCOMUtils . generateQI ( [
Ci . nsIObserver ,
Ci . nsISupportsWeakReference
] ) ,
initConte xt : function ( win , sandbox ) {
initContentScripts : function ( win , sandbox ) {
let messager = getMessageManager ( win ) ;
let messager = getMessageManager ( win ) ;
if ( sandbox ) {
if ( sandbox ) {
@ -173,14 +194,6 @@ const docObserver = {
return win ;
return win ;
} ,
} ,
register : function ( ) {
Services . obs . addObserver ( this , 'document-element-inserted' , true ) ;
} ,
unregister : function ( ) {
Services . obs . removeObserver ( this , 'document-element-inserted' ) ;
} ,
observe : function ( doc ) {
observe : function ( doc ) {
let win = doc . defaultView ;
let win = doc . defaultView ;
@ -192,21 +205,21 @@ const docObserver = {
if ( loc . protocol !== 'http:' && loc . protocol !== 'https:' ) {
if ( loc . protocol !== 'http:' && loc . protocol !== 'https:' ) {
if ( loc . protocol === 'chrome:' && loc . host === appName ) {
if ( loc . protocol === 'chrome:' && loc . host === appName ) {
this . initConte xt ( win ) ;
this . initConte ntScripts ( win ) ;
}
}
return ;
return ;
}
}
let lss = Services . scriptloader . loadSubScript ;
let lss = Services . scriptloader . loadSubScript ;
win = this . initConte xt ( win , true ) ;
win = this . initConte ntScripts ( win , true ) ;
lss ( this . contentBaseURI + 'vapi-client.js' , win ) ;
lss ( this . contentBaseURI + 'vapi-client.js' , win ) ;
lss ( this . contentBaseURI + 'contentscript-start.js' , win ) ;
lss ( this . contentBaseURI + 'contentscript-start.js' , win ) ;
let docReady = function ( e ) {
let docReady = function ( e ) {
this . removeEventListener ( e . type , docReady , true ) ;
this . removeEventListener ( e . type , docReady , true ) ;
lss ( do cObserver. contentBaseURI + 'contentscript-end.js' , win ) ;
lss ( content Observer. contentBaseURI + 'contentscript-end.js' , win ) ;
} ;
} ;
win . document . addEventListener ( 'DOMContentLoaded' , docReady , true ) ;
win . document . addEventListener ( 'DOMContentLoaded' , docReady , true ) ;
@ -215,7 +228,6 @@ const docObserver = {
/******************************************************************************/
/******************************************************************************/
contentPolicy . register ( ) ;
contentObserver . register ( ) ;
docObserver . register ( ) ;
/******************************************************************************/
/******************************************************************************/