2007-03-22 10:30:00 -07:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2012-05-21 04:12:37 -07:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/. */
2007-03-22 10:30:00 -07:00
2013-12-08 18:52:54 -08:00
# include "mozilla/ArrayUtils.h"
2011-10-10 22:50:08 -07:00
2007-03-22 10:30:00 -07:00
# include "nsCOMPtr.h"
# include "nsNetUtil.h"
# include "nsXBLService.h"
# include "nsXBLWindowKeyHandler.h"
# include "nsIInputStream.h"
2014-02-27 15:04:46 -08:00
# include "nsNameSpaceManager.h"
2007-03-22 10:30:00 -07:00
# include "nsIURI.h"
# include "nsIDOMElement.h"
# include "nsIURL.h"
# include "nsIChannel.h"
# include "nsXPIDLString.h"
# include "plstr.h"
# include "nsIContent.h"
# include "nsIDocument.h"
# include "nsIXMLContentSink.h"
# include "nsContentCID.h"
2013-03-20 09:22:26 -07:00
# include "mozilla/dom/XMLDocument.h"
2007-03-22 10:30:00 -07:00
# include "nsGkAtoms.h"
# include "nsIMemory.h"
# include "nsIObserverService.h"
# include "nsIDOMNodeList.h"
# include "nsXBLContentSink.h"
# include "nsXBLBinding.h"
# include "nsXBLPrototypeBinding.h"
2010-07-14 18:53:11 -07:00
# include "nsXBLDocumentInfo.h"
2007-03-22 10:30:00 -07:00
# include "nsCRT.h"
# include "nsContentUtils.h"
# include "nsSyncLoadService.h"
# include "nsContentPolicyUtils.h"
2009-03-20 01:15:35 -07:00
# include "nsTArray.h"
2012-07-27 07:03:27 -07:00
# include "nsError.h"
2007-03-22 10:30:00 -07:00
# include "nsIPresShell.h"
# include "nsIDocumentObserver.h"
# include "nsFrameManager.h"
# include "nsStyleContext.h"
# include "nsIScriptSecurityManager.h"
# include "nsIScriptError.h"
2011-11-03 13:39:08 -07:00
# include "nsXBLSerialize.h"
2007-03-22 10:30:00 -07:00
# ifdef MOZ_XUL
# include "nsXULPrototypeCache.h"
# endif
2011-06-24 16:12:34 -07:00
# include "nsIDOMEventListener.h"
2014-03-04 16:37:43 -08:00
# include "mozilla/Attributes.h"
2014-03-16 23:56:53 -07:00
# include "mozilla/EventListenerManager.h"
2011-05-28 16:42:57 -07:00
# include "mozilla/Preferences.h"
2014-03-04 16:37:43 -08:00
# include "mozilla/dom/Event.h"
2011-07-20 12:18:54 -07:00
# include "mozilla/dom/Element.h"
2011-05-28 16:42:57 -07:00
using namespace mozilla ;
2013-04-05 17:44:15 -07:00
using namespace mozilla : : dom ;
2007-03-22 10:30:00 -07:00
2007-04-01 05:19:44 -07:00
# define NS_MAX_XBL_BINDING_RECURSION 20
2012-07-30 07:20:58 -07:00
nsXBLService * nsXBLService : : gInstance = nullptr ;
2012-05-23 11:46:04 -07:00
2011-09-28 23:19:26 -07:00
static bool
2007-03-22 10:30:00 -07:00
IsAncestorBinding ( nsIDocument * aDocument ,
nsIURI * aChildBindingURI ,
nsIContent * aChild )
{
NS_ASSERTION ( aDocument , " expected a document " ) ;
NS_ASSERTION ( aChildBindingURI , " expected a binding URI " ) ;
NS_ASSERTION ( aChild , " expected a child content " ) ;
2012-08-22 08:56:38 -07:00
uint32_t bindingRecursion = 0 ;
2008-07-22 21:50:20 -07:00
for ( nsIContent * bindingParent = aChild - > GetBindingParent ( ) ;
bindingParent ;
bindingParent = bindingParent - > GetBindingParent ( ) ) {
2013-07-17 09:05:03 -07:00
nsXBLBinding * binding = bindingParent - > GetXBLBinding ( ) ;
2007-03-22 10:30:00 -07:00
if ( ! binding ) {
continue ;
}
2008-09-21 16:40:02 -07:00
2009-11-03 13:45:10 -08:00
if ( binding - > PrototypeBinding ( ) - > CompareBindingURI ( aChildBindingURI ) ) {
2007-04-01 05:19:44 -07:00
+ + bindingRecursion ;
if ( bindingRecursion < NS_MAX_XBL_BINDING_RECURSION ) {
continue ;
}
2012-09-01 19:35:17 -07:00
nsAutoCString spec ;
2007-03-22 10:30:00 -07:00
aChildBindingURI - > GetSpec ( spec ) ;
NS_ConvertUTF8toUTF16 bindingURI ( spec ) ;
2014-01-04 07:02:17 -08:00
const char16_t * params [ ] = { bindingURI . get ( ) } ;
2011-12-15 06:47:03 -08:00
nsContentUtils : : ReportToConsole ( nsIScriptError : : warningFlag ,
2013-08-21 12:28:26 -07:00
NS_LITERAL_CSTRING ( " XBL " ) , aDocument ,
2011-12-15 06:47:03 -08:00
nsContentUtils : : eXBL_PROPERTIES ,
2007-04-01 05:19:44 -07:00
" TooDeepBindingRecursion " ,
2011-12-15 06:47:03 -08:00
params , ArrayLength ( params ) ) ;
2011-10-17 07:59:28 -07:00
return true ;
2007-03-22 10:30:00 -07:00
}
}
2011-10-17 07:59:28 -07:00
return false ;
2007-03-22 10:30:00 -07:00
}
// Individual binding requests.
class nsXBLBindingRequest
{
public :
nsCOMPtr < nsIURI > mBindingURI ;
nsCOMPtr < nsIContent > mBoundElement ;
void DocumentLoaded ( nsIDocument * aBindingDoc )
{
// We only need the document here to cause frame construction, so
// we need the current doc, not the owner doc.
nsIDocument * doc = mBoundElement - > GetCurrentDoc ( ) ;
if ( ! doc )
return ;
2014-10-23 06:19:26 -07:00
// Destroy the frames for mBoundElement.
nsIContent * destroyedFramesFor = nullptr ;
nsIPresShell * shell = doc - > GetShell ( ) ;
if ( shell ) {
shell - > DestroyFramesFor ( mBoundElement , & destroyedFramesFor ) ;
}
MOZ_ASSERT ( ! mBoundElement - > GetPrimaryFrame ( ) ) ;
2007-03-22 10:30:00 -07:00
// Get the binding.
2011-09-28 23:19:26 -07:00
bool ready = false ;
2012-05-23 11:46:04 -07:00
nsXBLService : : GetInstance ( ) - > BindingReady ( mBoundElement , mBindingURI , & ready ) ;
2007-03-22 10:30:00 -07:00
if ( ! ready )
return ;
// If |mBoundElement| is (in addition to having binding |mBinding|)
// also a descendant of another element with binding |mBinding|,
// then we might have just constructed it due to the
// notification of its parent. (We can know about both if the
// binding loads were triggered from the DOM rather than frame
// construction.) So we have to check both whether the element
2014-11-20 10:24:09 -08:00
// has a primary frame and whether it's in the frame manager maps
2007-03-22 10:30:00 -07:00
// before sending a ContentInserted notification, or bad things
// will happen.
2014-10-23 06:19:26 -07:00
MOZ_ASSERT ( shell = = doc - > GetShell ( ) ) ;
2007-03-22 10:30:00 -07:00
if ( shell ) {
2009-12-24 13:20:05 -08:00
nsIFrame * childFrame = mBoundElement - > GetPrimaryFrame ( ) ;
2007-03-22 10:30:00 -07:00
if ( ! childFrame ) {
2014-11-20 10:24:09 -08:00
// Check to see if it's in the undisplayed content map...
nsFrameManager * fm = shell - > FrameManager ( ) ;
nsStyleContext * sc = fm - > GetUndisplayedContent ( mBoundElement ) ;
if ( ! sc ) {
// or in the display:contents map.
sc = fm - > GetDisplayContentsStyleFor ( mBoundElement ) ;
}
2007-03-22 10:30:00 -07:00
if ( ! sc ) {
2014-10-23 06:19:26 -07:00
shell - > CreateFramesFor ( destroyedFramesFor ) ;
2007-03-22 10:30:00 -07:00
}
}
}
}
nsXBLBindingRequest ( nsIURI * aURI , nsIContent * aBoundElement )
: mBindingURI ( aURI ) ,
mBoundElement ( aBoundElement )
{
}
} ;
2014-03-18 14:25:39 -07:00
// nsXBLStreamListener, a helper class used for
2007-03-22 10:30:00 -07:00
// asynchronous parsing of URLs
/* Header file */
2012-06-18 19:30:09 -07:00
class nsXBLStreamListener MOZ_FINAL : public nsIStreamListener ,
public nsIDOMEventListener
2007-03-22 10:30:00 -07:00
{
public :
NS_DECL_ISUPPORTS
NS_DECL_NSISTREAMLISTENER
NS_DECL_NSIREQUESTOBSERVER
2011-06-24 16:12:34 -07:00
NS_DECL_NSIDOMEVENTLISTENER
2007-03-22 10:30:00 -07:00
2012-05-23 11:46:04 -07:00
nsXBLStreamListener ( nsIDocument * aBoundDocument ,
2009-01-14 03:24:26 -08:00
nsIXMLContentSink * aSink ,
nsIDocument * aBindingDocument ) ;
2007-11-07 16:05:03 -08:00
2007-04-23 07:21:53 -07:00
void AddRequest ( nsXBLBindingRequest * aRequest ) { mBindingRequests . AppendElement ( aRequest ) ; }
2011-09-28 23:19:26 -07:00
bool HasRequest ( nsIURI * aURI , nsIContent * aBoundElement ) ;
2007-03-22 10:30:00 -07:00
private :
2014-06-23 12:56:07 -07:00
~ nsXBLStreamListener ( ) ;
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIStreamListener > mInner ;
2009-03-20 01:15:35 -07:00
nsAutoTArray < nsXBLBindingRequest * , 8 > mBindingRequests ;
2014-03-18 14:25:39 -07:00
2009-01-14 03:24:26 -08:00
nsCOMPtr < nsIWeakReference > mBoundDocument ;
nsCOMPtr < nsIXMLContentSink > mSink ; // Only set until OnStartRequest
nsCOMPtr < nsIDocument > mBindingDocument ; // Only set until OnStartRequest
2007-03-22 10:30:00 -07:00
} ;
/* Implementation file */
2014-04-27 00:06:00 -07:00
NS_IMPL_ISUPPORTS ( nsXBLStreamListener ,
nsIStreamListener ,
nsIRequestObserver ,
nsIDOMEventListener )
2007-03-22 10:30:00 -07:00
2012-05-23 11:46:04 -07:00
nsXBLStreamListener : : nsXBLStreamListener ( nsIDocument * aBoundDocument ,
2009-01-14 03:24:26 -08:00
nsIXMLContentSink * aSink ,
nsIDocument * aBindingDocument )
: mSink ( aSink ) , mBindingDocument ( aBindingDocument )
2007-03-22 10:30:00 -07:00
{
/* member initializers and constructor code */
2009-01-14 03:24:26 -08:00
mBoundDocument = do_GetWeakReference ( aBoundDocument ) ;
2007-11-07 16:05:03 -08:00
}
nsXBLStreamListener : : ~ nsXBLStreamListener ( )
{
2012-08-22 08:56:38 -07:00
for ( uint32_t i = 0 ; i < mBindingRequests . Length ( ) ; i + + ) {
2009-03-20 01:15:35 -07:00
nsXBLBindingRequest * req = mBindingRequests . ElementAt ( i ) ;
2013-03-05 04:37:47 -08:00
delete req ;
2007-11-07 16:05:03 -08:00
}
2007-03-22 10:30:00 -07:00
}
NS_IMETHODIMP
2012-09-05 19:41:02 -07:00
nsXBLStreamListener : : OnDataAvailable ( nsIRequest * request , nsISupports * aCtxt ,
2013-03-05 04:37:47 -08:00
nsIInputStream * aInStr ,
2012-09-05 19:41:02 -07:00
uint64_t aSourceOffset , uint32_t aCount )
2007-03-22 10:30:00 -07:00
{
if ( mInner )
return mInner - > OnDataAvailable ( request , aCtxt , aInStr , aSourceOffset , aCount ) ;
return NS_ERROR_FAILURE ;
}
NS_IMETHODIMP
nsXBLStreamListener : : OnStartRequest ( nsIRequest * request , nsISupports * aCtxt )
{
2009-01-14 03:24:26 -08:00
// Make sure we don't hold on to the sink and binding document past this point
nsCOMPtr < nsIXMLContentSink > sink ;
mSink . swap ( sink ) ;
nsCOMPtr < nsIDocument > doc ;
mBindingDocument . swap ( doc ) ;
nsCOMPtr < nsIChannel > channel = do_QueryInterface ( request ) ;
NS_ENSURE_TRUE ( channel , NS_ERROR_UNEXPECTED ) ;
nsCOMPtr < nsILoadGroup > group ;
request - > GetLoadGroup ( getter_AddRefs ( group ) ) ;
nsresult rv = doc - > StartDocumentLoad ( " loadAsInteractiveData " ,
channel ,
group ,
2012-07-30 07:20:58 -07:00
nullptr ,
2009-01-14 03:24:26 -08:00
getter_AddRefs ( mInner ) ,
2011-10-17 07:59:28 -07:00
true ,
2009-01-14 03:24:26 -08:00
sink ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// Make sure to add ourselves as a listener after StartDocumentLoad,
// since that resets the event listners on the document.
2013-04-05 17:44:15 -07:00
doc - > AddEventListener ( NS_LITERAL_STRING ( " load " ) , this , false ) ;
2009-01-14 03:24:26 -08:00
return mInner - > OnStartRequest ( request , aCtxt ) ;
2007-03-22 10:30:00 -07:00
}
2014-03-18 14:25:39 -07:00
NS_IMETHODIMP
2007-03-22 10:30:00 -07:00
nsXBLStreamListener : : OnStopRequest ( nsIRequest * request , nsISupports * aCtxt , nsresult aStatus )
{
nsresult rv = NS_OK ;
if ( mInner ) {
rv = mInner - > OnStopRequest ( request , aCtxt , aStatus ) ;
}
2007-11-07 16:05:03 -08:00
// Don't hold onto the inner listener; holding onto it can create a cycle
// with the document
2012-07-30 07:20:58 -07:00
mInner = nullptr ;
2007-03-22 10:30:00 -07:00
return rv ;
}
2011-09-28 23:19:26 -07:00
bool
2007-03-22 10:30:00 -07:00
nsXBLStreamListener : : HasRequest ( nsIURI * aURI , nsIContent * aElt )
{
// XXX Could be more efficient.
2012-08-22 08:56:38 -07:00
uint32_t count = mBindingRequests . Length ( ) ;
for ( uint32_t i = 0 ; i < count ; i + + ) {
2009-03-20 01:15:35 -07:00
nsXBLBindingRequest * req = mBindingRequests . ElementAt ( i ) ;
2011-09-28 23:19:26 -07:00
bool eq ;
2007-03-22 10:30:00 -07:00
if ( req - > mBoundElement = = aElt & &
NS_SUCCEEDED ( req - > mBindingURI - > Equals ( aURI , & eq ) ) & & eq )
2011-10-17 07:59:28 -07:00
return true ;
2007-03-22 10:30:00 -07:00
}
2011-10-17 07:59:28 -07:00
return false ;
2007-03-22 10:30:00 -07:00
}
nsresult
2011-06-24 16:12:34 -07:00
nsXBLStreamListener : : HandleEvent ( nsIDOMEvent * aEvent )
2007-03-22 10:30:00 -07:00
{
nsresult rv = NS_OK ;
2012-08-22 08:56:38 -07:00
uint32_t i ;
uint32_t count = mBindingRequests . Length ( ) ;
2007-11-07 16:05:03 -08:00
// Get the binding document; note that we don't hold onto it in this object
// to avoid creating a cycle
2014-03-04 16:37:43 -08:00
Event * event = aEvent - > InternalDOMEvent ( ) ;
2013-04-05 17:44:26 -07:00
EventTarget * target = event - > GetCurrentTarget ( ) ;
2007-11-07 16:05:03 -08:00
nsCOMPtr < nsIDocument > bindingDocument = do_QueryInterface ( target ) ;
NS_ASSERTION ( bindingDocument , " Event not targeted at document?! " ) ;
2007-03-22 10:30:00 -07:00
// See if we're still alive.
2009-01-14 03:24:26 -08:00
nsCOMPtr < nsIDocument > doc ( do_QueryReferent ( mBoundDocument ) ) ;
2007-03-22 10:30:00 -07:00
if ( ! doc ) {
NS_WARNING ( " XBL load did not complete until after document went away! Modal dialog bug? \n " ) ;
}
else {
// We have to do a flush prior to notification of the document load.
// This has to happen since the HTML content sink can be holding on
// to notifications related to our children (e.g., if you bind to the
2014-03-18 14:25:39 -07:00
// <body> tag) that result in duplication of content.
2007-03-22 10:30:00 -07:00
// We need to get the sink's notifications flushed and then make the binding
// ready.
if ( count > 0 ) {
2009-03-20 01:15:35 -07:00
nsXBLBindingRequest * req = mBindingRequests . ElementAt ( 0 ) ;
2007-03-22 10:30:00 -07:00
nsIDocument * document = req - > mBoundElement - > GetCurrentDoc ( ) ;
if ( document )
document - > FlushPendingNotifications ( Flush_ContentAndNotify ) ;
}
// Remove ourselves from the set of pending docs.
nsBindingManager * bindingManager = doc - > BindingManager ( ) ;
2007-11-07 16:05:03 -08:00
nsIURI * documentURI = bindingDocument - > GetDocumentURI ( ) ;
2007-03-22 10:30:00 -07:00
bindingManager - > RemoveLoadingDocListener ( documentURI ) ;
2010-04-30 06:12:05 -07:00
if ( ! bindingDocument - > GetRootElement ( ) ) {
2010-05-04 21:28:19 -07:00
// FIXME: How about an error console warning?
2012-04-08 18:37:41 -07:00
NS_WARNING ( " XBL doc with no root element - this usually shouldn't happen " ) ;
2007-03-22 10:30:00 -07:00
return NS_ERROR_FAILURE ;
}
// Put our doc info in the doc table.
2007-11-07 16:05:03 -08:00
nsBindingManager * xblDocBindingManager = bindingDocument - > BindingManager ( ) ;
2010-07-14 18:53:11 -07:00
nsRefPtr < nsXBLDocumentInfo > info =
2007-03-22 10:30:00 -07:00
xblDocBindingManager - > GetXBLDocumentInfo ( documentURI ) ;
xblDocBindingManager - > RemoveXBLDocumentInfo ( info ) ; // Break the self-imposed cycle.
if ( ! info ) {
2011-11-03 13:39:07 -07:00
if ( nsXBLService : : IsChromeOrResourceURI ( documentURI ) ) {
2010-05-04 21:28:19 -07:00
NS_WARNING ( " An XBL file is malformed. Did you forget the XBL namespace on the bindings tag? " ) ;
}
2011-12-15 06:47:03 -08:00
nsContentUtils : : ReportToConsole ( nsIScriptError : : warningFlag ,
2013-08-21 12:28:26 -07:00
NS_LITERAL_CSTRING ( " XBL " ) , nullptr ,
2011-12-15 06:47:03 -08:00
nsContentUtils : : eXBL_PROPERTIES ,
2010-05-04 21:28:19 -07:00
" MalformedXBL " ,
2012-07-30 07:20:58 -07:00
nullptr , 0 , documentURI ) ;
2007-03-22 10:30:00 -07:00
return NS_ERROR_FAILURE ;
}
// If the doc is a chrome URI, then we put it into the XUL cache.
# ifdef MOZ_XUL
2011-11-03 13:39:07 -07:00
if ( nsXBLService : : IsChromeOrResourceURI ( documentURI ) ) {
2007-03-22 10:30:00 -07:00
nsXULPrototypeCache * cache = nsXULPrototypeCache : : GetInstance ( ) ;
if ( cache & & cache - > IsEnabled ( ) )
cache - > PutXBLDocumentInfo ( info ) ;
}
# endif
2014-03-18 14:25:39 -07:00
2007-03-22 10:30:00 -07:00
bindingManager - > PutXBLDocumentInfo ( info ) ;
// Notify all pending requests that their bindings are
// ready and can be installed.
for ( i = 0 ; i < count ; i + + ) {
2009-03-20 01:15:35 -07:00
nsXBLBindingRequest * req = mBindingRequests . ElementAt ( i ) ;
2007-11-07 16:05:03 -08:00
req - > DocumentLoaded ( bindingDocument ) ;
2007-03-22 10:30:00 -07:00
}
}
2011-10-17 07:59:28 -07:00
target - > RemoveEventListener ( NS_LITERAL_STRING ( " load " ) , this , false ) ;
2007-03-22 10:30:00 -07:00
return rv ;
}
// Implementation /////////////////////////////////////////////////////////////////
// Static member variable initialization
2011-09-28 23:19:26 -07:00
bool nsXBLService : : gAllowDataURIs = false ;
2008-04-28 16:56:07 -07:00
2007-03-22 10:30:00 -07:00
// Implement our nsISupports methods
2014-04-27 00:06:00 -07:00
NS_IMPL_ISUPPORTS ( nsXBLService , nsISupportsWeakReference )
2012-05-23 11:46:04 -07:00
void
nsXBLService : : Init ( )
{
gInstance = new nsXBLService ( ) ;
NS_ADDREF ( gInstance ) ;
}
2007-03-22 10:30:00 -07:00
// Constructors/Destructors
nsXBLService : : nsXBLService ( void )
{
2011-05-28 16:42:57 -07:00
Preferences : : AddBoolVarCache ( & gAllowDataURIs , " layout.debug.enable_data_xbl " ) ;
2007-03-22 10:30:00 -07:00
}
nsXBLService : : ~ nsXBLService ( void )
{
}
2011-11-03 13:39:07 -07:00
// static
bool
nsXBLService : : IsChromeOrResourceURI ( nsIURI * aURI )
{
bool isChrome = false ;
bool isResource = false ;
2014-03-18 14:25:39 -07:00
if ( NS_SUCCEEDED ( aURI - > SchemeIs ( " chrome " , & isChrome ) ) & &
2011-11-03 13:39:07 -07:00
NS_SUCCEEDED ( aURI - > SchemeIs ( " resource " , & isResource ) ) )
return ( isChrome | | isResource ) ;
return false ;
}
2007-03-22 10:30:00 -07:00
// This function loads a particular XBL file and installs all of the bindings
// onto the element.
2012-05-23 11:46:04 -07:00
nsresult
2007-07-18 14:56:57 -07:00
nsXBLService : : LoadBindings ( nsIContent * aContent , nsIURI * aURL ,
2013-01-24 09:45:50 -08:00
nsIPrincipal * aOriginPrincipal ,
2014-03-18 14:25:39 -07:00
nsXBLBinding * * aBinding , bool * aResolveStyle )
2007-07-18 14:56:57 -07:00
{
NS_PRECONDITION ( aOriginPrincipal , " Must have an origin principal " ) ;
2014-03-18 14:25:39 -07:00
2012-07-30 07:20:58 -07:00
* aBinding = nullptr ;
2011-10-17 07:59:28 -07:00
* aResolveStyle = false ;
2007-03-22 10:30:00 -07:00
nsresult rv ;
2011-10-18 03:53:36 -07:00
nsCOMPtr < nsIDocument > document = aContent - > OwnerDoc ( ) ;
2007-03-22 10:30:00 -07:00
2012-09-01 19:35:17 -07:00
nsAutoCString urlspec ;
2008-11-13 16:00:11 -08:00
if ( nsContentUtils : : GetWrapperSafeScriptFilename ( document , aURL , urlspec ) ) {
// Block an attempt to load a binding that has special wrapper
// automation needs.
return NS_OK ;
}
2013-07-17 09:05:03 -07:00
nsXBLBinding * binding = aContent - > GetXBLBinding ( ) ;
2013-01-24 09:45:50 -08:00
if ( binding ) {
2013-07-11 08:58:28 -07:00
if ( binding - > MarkedForDeath ( ) ) {
FlushStyleBindings ( aContent ) ;
binding = nullptr ;
}
else {
// See if the URIs match.
if ( binding - > PrototypeBinding ( ) - > CompareBindingURI ( aURL ) )
return NS_OK ;
FlushStyleBindings ( aContent ) ;
binding = nullptr ;
2007-03-22 10:30:00 -07:00
}
}
2011-09-28 23:19:26 -07:00
bool ready ;
2007-03-22 10:30:00 -07:00
nsRefPtr < nsXBLBinding > newBinding ;
2011-10-17 07:59:28 -07:00
if ( NS_FAILED ( rv = GetBinding ( aContent , aURL , false , aOriginPrincipal ,
2007-07-18 14:56:57 -07:00
& ready , getter_AddRefs ( newBinding ) ) ) ) {
2007-03-22 10:30:00 -07:00
return rv ;
}
if ( ! newBinding ) {
# ifdef DEBUG
2012-09-01 19:35:17 -07:00
nsAutoCString spec ;
2007-03-22 10:30:00 -07:00
aURL - > GetSpec ( spec ) ;
2012-09-01 19:35:17 -07:00
nsAutoCString str ( NS_LITERAL_CSTRING ( " Failed to locate XBL binding. XBL is now using id instead of name to reference bindings. Make sure you have switched over. The invalid binding name is: " ) + spec ) ;
2007-03-22 10:30:00 -07:00
NS_ERROR ( str . get ( ) ) ;
# endif
return NS_OK ;
}
if ( : : IsAncestorBinding ( document , aURL , aContent ) ) {
return NS_ERROR_ILLEGAL_VALUE ;
}
2013-01-24 09:45:50 -08:00
// We loaded a style binding. It goes on the end.
if ( binding ) {
// Get the last binding that is in the append layer.
binding - > RootBinding ( ) - > SetBaseBinding ( newBinding ) ;
2007-03-22 10:30:00 -07:00
}
else {
2013-01-24 09:45:50 -08:00
// Install the binding on the content node.
2013-07-17 09:05:03 -07:00
aContent - > SetXBLBinding ( newBinding ) ;
2007-03-22 10:30:00 -07:00
}
2009-11-18 07:14:14 -08:00
{
nsAutoScriptBlocker scriptBlocker ;
2007-03-22 10:30:00 -07:00
2009-11-18 07:14:14 -08:00
// Set the binding's bound element.
newBinding - > SetBoundElement ( aContent ) ;
2007-03-22 10:30:00 -07:00
2009-11-18 07:14:14 -08:00
// Tell the binding to build the anonymous content.
newBinding - > GenerateAnonymousContent ( ) ;
2008-03-14 16:08:57 -07:00
2009-11-18 07:14:14 -08:00
// Tell the binding to install event handlers
newBinding - > InstallEventHandlers ( ) ;
2008-03-14 16:08:57 -07:00
2009-11-18 07:14:14 -08:00
// Set up our properties
rv = newBinding - > InstallImplementation ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// Figure out if we have any scoped sheets. If so, we do a second resolve.
* aResolveStyle = newBinding - > HasStyleSheets ( ) ;
2014-03-18 14:25:39 -07:00
2009-11-18 07:14:14 -08:00
newBinding . swap ( * aBinding ) ;
}
2007-10-26 17:14:43 -07:00
2014-03-18 14:25:39 -07:00
return NS_OK ;
2007-03-22 10:30:00 -07:00
}
nsresult
nsXBLService : : FlushStyleBindings ( nsIContent * aContent )
{
2011-10-18 03:53:36 -07:00
nsCOMPtr < nsIDocument > document = aContent - > OwnerDoc ( ) ;
2007-03-22 10:30:00 -07:00
2013-07-17 09:05:03 -07:00
nsXBLBinding * binding = aContent - > GetXBLBinding ( ) ;
2007-03-22 10:30:00 -07:00
if ( binding ) {
2013-07-11 08:58:28 -07:00
// Clear out the script references.
binding - > ChangeDocument ( document , nullptr ) ;
2007-03-22 10:30:00 -07:00
2013-07-17 09:05:03 -07:00
aContent - > SetXBLBinding ( nullptr ) ; // Flush old style bindings
2007-03-22 10:30:00 -07:00
}
2014-03-18 14:25:39 -07:00
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
//
// AttachGlobalKeyHandler
//
// Creates a new key handler and prepares to listen to key events on the given
// event receiver (either a document or an content node). If the receiver is content,
// then extra work needs to be done to hook it up to the document (XXX WHY??)
//
2012-05-23 11:46:04 -07:00
nsresult
2013-04-05 17:44:26 -07:00
nsXBLService : : AttachGlobalKeyHandler ( EventTarget * aTarget )
2007-03-22 10:30:00 -07:00
{
// check if the receiver is a content node (not a document), and hook
// it to the document if that is the case.
2013-04-05 17:44:26 -07:00
nsCOMPtr < EventTarget > piTarget = aTarget ;
2007-05-14 02:11:38 -07:00
nsCOMPtr < nsIContent > contentNode ( do_QueryInterface ( aTarget ) ) ;
2007-03-22 10:30:00 -07:00
if ( contentNode ) {
// Only attach if we're really in a document
nsCOMPtr < nsIDocument > doc = contentNode - > GetCurrentDoc ( ) ;
if ( doc )
2011-06-23 19:18:02 -07:00
piTarget = doc ; // We're a XUL keyset. Attach to our document.
2007-03-22 10:30:00 -07:00
}
2011-06-23 19:18:02 -07:00
2014-03-16 23:56:53 -07:00
EventListenerManager * manager = piTarget - > GetOrCreateListenerManager ( ) ;
2013-04-05 17:44:26 -07:00
2011-06-23 19:18:02 -07:00
if ( ! piTarget | | ! manager )
2007-03-22 10:30:00 -07:00
return NS_ERROR_FAILURE ;
2008-08-06 07:32:09 -07:00
// the listener already exists, so skip this
if ( contentNode & & contentNode - > GetProperty ( nsGkAtoms : : listener ) )
return NS_OK ;
2013-04-14 11:27:33 -07:00
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIDOMElement > elt ( do_QueryInterface ( contentNode ) ) ;
// Create the key handler
2013-04-14 11:27:33 -07:00
nsRefPtr < nsXBLWindowKeyHandler > handler =
NS_NewXBLWindowKeyHandler ( elt , piTarget ) ;
2007-03-22 10:30:00 -07:00
// listen to these events
2011-06-23 19:18:02 -07:00
manager - > AddEventListenerByType ( handler , NS_LITERAL_STRING ( " keydown " ) ,
2014-03-16 23:56:52 -07:00
TrustedEventsAtSystemGroupBubble ( ) ) ;
2011-06-23 19:18:02 -07:00
manager - > AddEventListenerByType ( handler , NS_LITERAL_STRING ( " keyup " ) ,
2014-03-16 23:56:52 -07:00
TrustedEventsAtSystemGroupBubble ( ) ) ;
2011-06-23 19:18:02 -07:00
manager - > AddEventListenerByType ( handler , NS_LITERAL_STRING ( " keypress " ) ,
2014-03-16 23:56:52 -07:00
TrustedEventsAtSystemGroupBubble ( ) ) ;
2007-03-22 10:30:00 -07:00
2014-03-18 08:16:47 -07:00
// The capturing listener is only used for XUL keysets to properly handle
// shortcut keys in a multi-process environment.
manager - > AddEventListenerByType ( handler , NS_LITERAL_STRING ( " keydown " ) ,
TrustedEventsAtSystemGroupCapture ( ) ) ;
manager - > AddEventListenerByType ( handler , NS_LITERAL_STRING ( " keyup " ) ,
TrustedEventsAtSystemGroupCapture ( ) ) ;
manager - > AddEventListenerByType ( handler , NS_LITERAL_STRING ( " keypress " ) ,
TrustedEventsAtSystemGroupCapture ( ) ) ;
2008-08-06 07:32:09 -07:00
if ( contentNode )
2014-03-15 12:00:15 -07:00
return contentNode - > SetProperty ( nsGkAtoms : : listener ,
handler . forget ( ) . take ( ) ,
2011-10-17 07:59:28 -07:00
nsPropertyTable : : SupportsDtorFunc , true ) ;
2008-08-06 07:32:09 -07:00
2013-04-14 11:27:33 -07:00
// The reference to the handler will be maintained by the event target,
2008-08-06 07:32:09 -07:00
// and, if there is a content node, the property.
return NS_OK ;
}
//
// DetachGlobalKeyHandler
//
// Removes a key handler added by DeatchGlobalKeyHandler.
//
2012-05-23 11:46:04 -07:00
nsresult
2013-04-05 17:44:26 -07:00
nsXBLService : : DetachGlobalKeyHandler ( EventTarget * aTarget )
2008-08-06 07:32:09 -07:00
{
2013-04-05 17:44:26 -07:00
nsCOMPtr < EventTarget > piTarget = aTarget ;
2008-08-06 07:32:09 -07:00
nsCOMPtr < nsIContent > contentNode ( do_QueryInterface ( aTarget ) ) ;
if ( ! contentNode ) // detaching is only supported for content nodes
return NS_ERROR_FAILURE ;
// Only attach if we're really in a document
nsCOMPtr < nsIDocument > doc = contentNode - > GetCurrentDoc ( ) ;
if ( doc )
piTarget = do_QueryInterface ( doc ) ;
2011-06-23 19:18:02 -07:00
2014-03-16 23:56:53 -07:00
EventListenerManager * manager = piTarget - > GetOrCreateListenerManager ( ) ;
2013-04-05 17:44:26 -07:00
2011-06-23 19:18:02 -07:00
if ( ! piTarget | | ! manager )
2008-08-06 07:32:09 -07:00
return NS_ERROR_FAILURE ;
nsIDOMEventListener * handler =
static_cast < nsIDOMEventListener * > ( contentNode - > GetProperty ( nsGkAtoms : : listener ) ) ;
if ( ! handler )
return NS_ERROR_FAILURE ;
2011-06-23 19:18:02 -07:00
manager - > RemoveEventListenerByType ( handler , NS_LITERAL_STRING ( " keydown " ) ,
2014-03-16 23:56:52 -07:00
TrustedEventsAtSystemGroupBubble ( ) ) ;
2011-06-23 19:18:02 -07:00
manager - > RemoveEventListenerByType ( handler , NS_LITERAL_STRING ( " keyup " ) ,
2014-03-16 23:56:52 -07:00
TrustedEventsAtSystemGroupBubble ( ) ) ;
2011-06-23 19:18:02 -07:00
manager - > RemoveEventListenerByType ( handler , NS_LITERAL_STRING ( " keypress " ) ,
2014-03-16 23:56:52 -07:00
TrustedEventsAtSystemGroupBubble ( ) ) ;
2008-08-06 07:32:09 -07:00
2014-03-18 08:16:47 -07:00
manager - > RemoveEventListenerByType ( handler , NS_LITERAL_STRING ( " keydown " ) ,
TrustedEventsAtSystemGroupCapture ( ) ) ;
manager - > RemoveEventListenerByType ( handler , NS_LITERAL_STRING ( " keyup " ) ,
TrustedEventsAtSystemGroupCapture ( ) ) ;
manager - > RemoveEventListenerByType ( handler , NS_LITERAL_STRING ( " keypress " ) ,
TrustedEventsAtSystemGroupCapture ( ) ) ;
2008-08-06 07:32:09 -07:00
contentNode - > DeleteProperty ( nsGkAtoms : : listener ) ;
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
// Internal helper methods ////////////////////////////////////////////////////////////////
2012-05-23 11:46:04 -07:00
nsresult
nsXBLService : : BindingReady ( nsIContent * aBoundElement ,
2014-03-18 14:25:39 -07:00
nsIURI * aURI ,
2012-05-23 11:46:04 -07:00
bool * aIsReady )
2007-03-22 10:30:00 -07:00
{
2007-07-18 14:56:57 -07:00
// Don't do a security check here; we know this binding is set to go.
2012-07-30 07:20:58 -07:00
return GetBinding ( aBoundElement , aURI , true , nullptr , aIsReady , nullptr ) ;
2007-03-22 10:30:00 -07:00
}
nsresult
2014-03-18 14:25:39 -07:00
nsXBLService : : GetBinding ( nsIContent * aBoundElement , nsIURI * aURI ,
2011-09-28 23:19:26 -07:00
bool aPeekOnly , nsIPrincipal * aOriginPrincipal ,
bool * aIsReady , nsXBLBinding * * aResult )
2007-03-22 10:30:00 -07:00
{
// More than 6 binding URIs are rare, see bug 55070 comment 18.
2007-12-12 20:17:14 -08:00
nsAutoTArray < nsIURI * , 6 > uris ;
2007-07-18 14:56:57 -07:00
return GetBinding ( aBoundElement , aURI , aPeekOnly , aOriginPrincipal , aIsReady ,
aResult , uris ) ;
2007-03-22 10:30:00 -07:00
}
2014-08-11 20:06:55 -07:00
static bool
MayBindToContent ( nsXBLPrototypeBinding * aProtoBinding , nsIContent * aBoundElement ,
nsIURI * aURI )
{
// If this binding explicitly allows untrusted content, we're done.
if ( aProtoBinding - > BindToUntrustedContent ( ) ) {
return true ;
}
// We let XUL content and content in XUL documents through, since XUL is
// restricted anyway and we want to minimize remote XUL breakage.
2015-03-03 03:08:59 -08:00
if ( aBoundElement - > IsXULElement ( ) | |
aBoundElement - > OwnerDoc ( ) - > IsXULElement ( ) ) {
2014-08-11 20:06:55 -07:00
return true ;
}
// Similarly, we make an exception for anonymous content (which
// lives in the XBL scope), because it's already protected from content,
// and tends to use a lot of bindings that we wouldn't otherwise need to
// whitelist.
if ( aBoundElement - > IsInAnonymousSubtree ( ) ) {
return true ;
}
// Allow if the bound content subsumes the binding.
nsCOMPtr < nsIDocument > bindingDoc = aProtoBinding - > XBLDocumentInfo ( ) - > GetDocument ( ) ;
NS_ENSURE_TRUE ( bindingDoc , false ) ;
if ( aBoundElement - > NodePrincipal ( ) - > Subsumes ( bindingDoc - > NodePrincipal ( ) ) ) {
return true ;
}
// One last special case: we need to watch out for in-document data: URI
// bindings from remote-XUL-whitelisted domains (especially tests), because
// they end up with a null principal (rather than inheriting the document's
// principal), which causes them to fail the check above.
if ( nsContentUtils : : AllowXULXBLForPrincipal ( aBoundElement - > NodePrincipal ( ) ) ) {
bool isDataURI = false ;
nsresult rv = aURI - > SchemeIs ( " data " , & isDataURI ) ;
NS_ENSURE_SUCCESS ( rv , false ) ;
if ( isDataURI ) {
return true ;
}
}
// Disallow.
return false ;
}
2007-03-22 10:30:00 -07:00
nsresult
2014-03-18 14:25:39 -07:00
nsXBLService : : GetBinding ( nsIContent * aBoundElement , nsIURI * aURI ,
2011-09-28 23:19:26 -07:00
bool aPeekOnly , nsIPrincipal * aOriginPrincipal ,
bool * aIsReady , nsXBLBinding * * aResult ,
2007-03-22 10:30:00 -07:00
nsTArray < nsIURI * > & aDontExtendURIs )
{
NS_ASSERTION ( aPeekOnly | | aResult ,
" Must have non-null out param if not just peeking to see "
" whether the binding is ready " ) ;
2014-03-18 14:25:39 -07:00
2007-03-22 10:30:00 -07:00
if ( aResult )
2012-07-30 07:20:58 -07:00
* aResult = nullptr ;
2007-03-22 10:30:00 -07:00
if ( ! aURI )
return NS_ERROR_FAILURE ;
2012-09-01 19:35:17 -07:00
nsAutoCString ref ;
2011-05-21 18:12:46 -07:00
aURI - > GetRef ( ref ) ;
2007-03-22 10:30:00 -07:00
2011-10-18 03:53:36 -07:00
nsCOMPtr < nsIDocument > boundDocument = aBoundElement - > OwnerDoc ( ) ;
2007-03-22 10:30:00 -07:00
2010-07-14 18:53:11 -07:00
nsRefPtr < nsXBLDocumentInfo > docInfo ;
2007-07-18 14:56:57 -07:00
nsresult rv = LoadBindingDocumentInfo ( aBoundElement , boundDocument , aURI ,
aOriginPrincipal ,
2011-10-17 07:59:28 -07:00
false , getter_AddRefs ( docInfo ) ) ;
2007-07-18 14:56:57 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2014-03-18 14:25:39 -07:00
2007-03-22 10:30:00 -07:00
if ( ! docInfo )
return NS_ERROR_FAILURE ;
2010-07-14 18:55:54 -07:00
nsXBLPrototypeBinding * protoBinding = docInfo - > GetPrototypeBinding ( ref ) ;
2007-03-22 10:30:00 -07:00
2012-05-09 15:57:44 -07:00
if ( ! protoBinding ) {
# ifdef DEBUG
2012-09-01 19:35:17 -07:00
nsAutoCString uriSpec ;
2012-05-09 15:57:44 -07:00
aURI - > GetSpec ( uriSpec ) ;
2012-09-01 19:35:17 -07:00
nsAutoCString doc ;
2012-05-09 15:57:44 -07:00
boundDocument - > GetDocumentURI ( ) - > GetSpec ( doc ) ;
2012-09-01 19:35:17 -07:00
nsAutoCString message ( " Unable to locate an XBL binding for URI " ) ;
2012-05-09 15:57:44 -07:00
message + = uriSpec ;
message + = " in document " ;
message + = doc ;
NS_WARNING ( message . get ( ) ) ;
# endif
2007-03-22 10:30:00 -07:00
return NS_ERROR_FAILURE ;
2012-05-09 15:57:44 -07:00
}
2007-03-22 10:30:00 -07:00
2014-08-11 20:06:55 -07:00
// If the binding isn't whitelisted, refuse to apply it to content that
// doesn't subsume it (modulo a few exceptions).
if ( ! MayBindToContent ( protoBinding , aBoundElement , aURI ) ) {
# ifdef DEBUG
nsAutoCString uriSpec ;
aURI - > GetSpec ( uriSpec ) ;
nsAutoCString message ( " Permission denied to apply binding " ) ;
message + = uriSpec ;
message + = " to unprivileged content. Set bindToUntrustedContent=true on "
" the binding to override this restriction. " ;
NS_WARNING ( message . get ( ) ) ;
# endif
return NS_ERROR_FAILURE ;
}
2015-01-30 10:55:11 -08:00
aDontExtendURIs . AppendElement ( protoBinding - > BindingURI ( ) ) ;
2009-11-03 13:45:10 -08:00
nsCOMPtr < nsIURI > altBindingURI = protoBinding - > AlternateBindingURI ( ) ;
if ( altBindingURI ) {
2015-01-30 10:55:11 -08:00
aDontExtendURIs . AppendElement ( altBindingURI ) ;
2009-11-03 13:45:10 -08:00
}
2007-03-22 10:30:00 -07:00
// Our prototype binding must have all its resources loaded.
2011-09-28 23:19:26 -07:00
bool ready = protoBinding - > LoadResources ( ) ;
2007-03-22 10:30:00 -07:00
if ( ! ready ) {
// Add our bound element to the protos list of elts that should
// be notified when the stylesheets and scripts finish loading.
protoBinding - > AddResourceListener ( aBoundElement ) ;
return NS_ERROR_FAILURE ; // The binding isn't ready yet.
}
2011-11-03 13:39:07 -07:00
rv = protoBinding - > ResolveBaseBinding ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsIURI * baseBindingURI ;
2007-03-22 10:30:00 -07:00
nsXBLPrototypeBinding * baseProto = protoBinding - > GetBasePrototype ( ) ;
if ( baseProto ) {
2011-11-03 13:39:07 -07:00
baseBindingURI = baseProto - > BindingURI ( ) ;
2007-03-22 10:30:00 -07:00
}
2011-11-03 13:39:07 -07:00
else {
baseBindingURI = protoBinding - > GetBaseBindingURI ( ) ;
if ( baseBindingURI ) {
2012-08-22 08:56:38 -07:00
uint32_t count = aDontExtendURIs . Length ( ) ;
for ( uint32_t index = 0 ; index < count ; + + index ) {
2011-11-03 13:39:07 -07:00
bool equal ;
rv = aDontExtendURIs [ index ] - > Equals ( baseBindingURI , & equal ) ;
2007-03-22 10:30:00 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2011-11-03 13:39:07 -07:00
if ( equal ) {
2012-09-01 19:35:17 -07:00
nsAutoCString spec , basespec ;
2011-11-03 13:39:07 -07:00
protoBinding - > BindingURI ( ) - > GetSpec ( spec ) ;
NS_ConvertUTF8toUTF16 protoSpec ( spec ) ;
baseBindingURI - > GetSpec ( basespec ) ;
NS_ConvertUTF8toUTF16 baseSpecUTF16 ( basespec ) ;
2014-01-04 07:02:17 -08:00
const char16_t * params [ ] = { protoSpec . get ( ) , baseSpecUTF16 . get ( ) } ;
2011-12-15 06:47:03 -08:00
nsContentUtils : : ReportToConsole ( nsIScriptError : : warningFlag ,
2013-08-21 12:28:26 -07:00
NS_LITERAL_CSTRING ( " XBL " ) , nullptr ,
2011-12-15 06:47:03 -08:00
nsContentUtils : : eXBL_PROPERTIES ,
2011-11-03 13:39:07 -07:00
" CircularExtendsBinding " ,
2011-12-15 06:47:03 -08:00
params , ArrayLength ( params ) ,
boundDocument - > GetDocumentURI ( ) ) ;
2011-11-03 13:39:07 -07:00
return NS_ERROR_ILLEGAL_VALUE ;
2007-03-22 10:30:00 -07:00
}
}
}
}
2011-11-03 13:39:07 -07:00
nsRefPtr < nsXBLBinding > baseBinding ;
if ( baseBindingURI ) {
2013-07-18 21:13:35 -07:00
nsIContent * child = protoBinding - > GetBindingElement ( ) ;
2011-11-03 13:39:07 -07:00
rv = GetBinding ( aBoundElement , baseBindingURI , aPeekOnly ,
child - > NodePrincipal ( ) , aIsReady ,
getter_AddRefs ( baseBinding ) , aDontExtendURIs ) ;
if ( NS_FAILED ( rv ) )
return rv ; // We aren't ready yet.
}
2011-10-17 07:59:28 -07:00
* aIsReady = true ;
2011-11-03 13:39:07 -07:00
2007-03-22 10:30:00 -07:00
if ( ! aPeekOnly ) {
// Make a new binding
nsXBLBinding * newBinding = new nsXBLBinding ( protoBinding ) ;
2011-11-03 13:39:07 -07:00
if ( baseBinding ) {
if ( ! baseProto ) {
protoBinding - > SetBasePrototype ( baseBinding - > PrototypeBinding ( ) ) ;
}
newBinding - > SetBaseBinding ( baseBinding ) ;
}
2007-03-22 10:30:00 -07:00
NS_ADDREF ( * aResult = newBinding ) ;
}
return NS_OK ;
}
2011-09-28 23:19:26 -07:00
static bool SchemeIs ( nsIURI * aURI , const char * aScheme )
2009-03-10 01:35:00 -07:00
{
nsCOMPtr < nsIURI > baseURI = NS_GetInnermostURI ( aURI ) ;
2011-10-17 07:59:28 -07:00
NS_ENSURE_TRUE ( baseURI , false ) ;
2009-03-10 01:35:00 -07:00
2011-09-28 23:19:26 -07:00
bool isScheme = false ;
2009-03-10 01:35:00 -07:00
return NS_SUCCEEDED ( baseURI - > SchemeIs ( aScheme , & isScheme ) ) & & isScheme ;
}
2011-09-28 23:19:26 -07:00
static bool
2010-08-19 16:12:46 -07:00
IsSystemOrChromeURLPrincipal ( nsIPrincipal * aPrincipal )
{
if ( nsContentUtils : : IsSystemPrincipal ( aPrincipal ) ) {
2011-10-17 07:59:28 -07:00
return true ;
2010-08-19 16:12:46 -07:00
}
2014-03-18 14:25:39 -07:00
2010-08-19 16:12:46 -07:00
nsCOMPtr < nsIURI > uri ;
aPrincipal - > GetURI ( getter_AddRefs ( uri ) ) ;
2011-10-17 07:59:28 -07:00
NS_ENSURE_TRUE ( uri , false ) ;
2014-03-18 14:25:39 -07:00
2011-09-28 23:19:26 -07:00
bool isChrome = false ;
2010-08-19 16:12:46 -07:00
return NS_SUCCEEDED ( uri - > SchemeIs ( " chrome " , & isChrome ) ) & & isChrome ;
}
2012-05-23 11:46:04 -07:00
nsresult
2007-03-22 10:30:00 -07:00
nsXBLService : : LoadBindingDocumentInfo ( nsIContent * aBoundElement ,
nsIDocument * aBoundDocument ,
nsIURI * aBindingURI ,
2007-07-18 14:56:57 -07:00
nsIPrincipal * aOriginPrincipal ,
2011-09-28 23:19:26 -07:00
bool aForceSyncLoad ,
2010-07-14 18:53:11 -07:00
nsXBLDocumentInfo * * aResult )
2007-03-22 10:30:00 -07:00
{
NS_PRECONDITION ( aBindingURI , " Must have a binding URI " ) ;
2007-07-18 14:56:57 -07:00
NS_PRECONDITION ( ! aOriginPrincipal | | aBoundDocument ,
" If we're doing a security check, we better have a document! " ) ;
2014-03-18 14:25:39 -07:00
2007-06-12 14:56:06 -07:00
nsresult rv ;
2007-07-18 14:56:57 -07:00
if ( aOriginPrincipal ) {
// Security check - Enforce same-origin policy, except to chrome.
2014-03-18 14:25:39 -07:00
// We have to be careful to not pass aContent as the context here.
2007-07-18 14:56:57 -07:00
// Otherwise, if there is a JS-implemented content policy, we will attempt
// to wrap the content node, which will try to load XBL bindings for it, if
// any. Since we're not done loading this binding yet, that will reenter
// this method and we'll end up creating a binding and then immediately
// clobbering it in our table. That makes things very confused, leading to
// misbehavior and crashes.
2007-06-12 14:56:06 -07:00
rv = nsContentUtils : :
2007-07-18 14:56:57 -07:00
CheckSecurityBeforeLoad ( aBindingURI , aOriginPrincipal ,
2007-06-12 14:56:06 -07:00
nsIScriptSecurityManager : : ALLOW_CHROME ,
2008-04-28 16:56:07 -07:00
gAllowDataURIs ,
2007-06-17 06:50:50 -07:00
nsIContentPolicy : : TYPE_XBL ,
2007-06-12 14:56:06 -07:00
aBoundDocument ) ;
2010-09-15 15:53:01 -07:00
NS_ENSURE_SUCCESS ( rv , NS_ERROR_XBL_BLOCKED ) ;
2009-03-09 23:15:06 -07:00
2010-08-19 16:12:46 -07:00
if ( ! IsSystemOrChromeURLPrincipal ( aOriginPrincipal ) ) {
// Also make sure that we're same-origin with the bound document
// except if the stylesheet has the system principal.
if ( ! ( gAllowDataURIs & & SchemeIs ( aBindingURI , " data " ) ) & &
! SchemeIs ( aBindingURI , " chrome " ) ) {
rv = aBoundDocument - > NodePrincipal ( ) - > CheckMayLoad ( aBindingURI ,
2012-08-20 11:34:33 -07:00
true , false ) ;
2010-09-15 15:53:01 -07:00
NS_ENSURE_SUCCESS ( rv , NS_ERROR_XBL_BLOCKED ) ;
2010-08-19 16:12:46 -07:00
}
2009-03-09 23:15:06 -07:00
2010-08-19 16:12:46 -07:00
// Finally check if this document is allowed to use XBL at all.
NS_ENSURE_TRUE ( aBoundDocument - > AllowXULXBL ( ) ,
2010-09-15 15:53:01 -07:00
NS_ERROR_XBL_BLOCKED ) ;
2009-03-09 23:15:06 -07:00
}
2007-06-12 14:56:06 -07:00
}
2007-03-22 10:30:00 -07:00
2012-07-30 07:20:58 -07:00
* aResult = nullptr ;
2010-07-14 18:53:11 -07:00
nsRefPtr < nsXBLDocumentInfo > info ;
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIURI > documentURI ;
2011-05-21 18:12:46 -07:00
rv = aBindingURI - > CloneIgnoringRef ( getter_AddRefs ( documentURI ) ) ;
2007-03-22 10:30:00 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
# ifdef MOZ_XUL
// We've got a file. Check our XBL document cache.
nsXULPrototypeCache * cache = nsXULPrototypeCache : : GetInstance ( ) ;
2014-03-18 14:25:39 -07:00
bool useXULCache = cache & & cache - > IsEnabled ( ) ;
2007-03-22 10:30:00 -07:00
if ( useXULCache ) {
2014-03-18 14:25:39 -07:00
// The first line of defense is the chrome cache.
2007-03-22 10:30:00 -07:00
// This cache crosses the entire product, so that any XBL bindings that are
// part of chrome will be reused across all XUL documents.
2014-03-18 14:25:39 -07:00
info = cache - > GetXBLDocumentInfo ( documentURI ) ;
2007-03-22 10:30:00 -07:00
}
# endif
if ( ! info ) {
// The second line of defense is the binding manager's document table.
2012-07-30 07:20:58 -07:00
nsBindingManager * bindingManager = nullptr ;
2007-03-22 10:30:00 -07:00
if ( aBoundDocument ) {
bindingManager = aBoundDocument - > BindingManager ( ) ;
info = bindingManager - > GetXBLDocumentInfo ( documentURI ) ;
2013-08-02 02:26:21 -07:00
if ( aBoundDocument - > IsStaticDocument ( ) & &
IsChromeOrResourceURI ( aBindingURI ) ) {
aForceSyncLoad = true ;
}
2007-03-22 10:30:00 -07:00
}
2014-06-19 19:01:40 -07:00
NodeInfo * ni = nullptr ;
2007-03-22 10:30:00 -07:00
if ( aBoundElement )
ni = aBoundElement - > NodeInfo ( ) ;
if ( ! info & & bindingManager & &
( ! ni | | ! ( ni - > Equals ( nsGkAtoms : : scrollbar , kNameSpaceID_XUL ) | |
ni - > Equals ( nsGkAtoms : : thumb , kNameSpaceID_XUL ) | |
( ( ni - > Equals ( nsGkAtoms : : input ) | |
ni - > Equals ( nsGkAtoms : : select ) ) & &
2015-03-03 03:08:59 -08:00
aBoundElement - > IsHTMLElement ( ) ) ) ) & & ! aForceSyncLoad ) {
2007-03-22 10:30:00 -07:00
// The third line of defense is to investigate whether or not the
// document is currently being loaded asynchronously. If so, there's no
// document yet, but we need to glom on our request so that it will be
// processed whenever the doc does finish loading.
nsCOMPtr < nsIStreamListener > listener ;
if ( bindingManager )
listener = bindingManager - > GetLoadingDocListener ( documentURI ) ;
if ( listener ) {
nsXBLStreamListener * xblListener =
2007-07-08 00:08:04 -07:00
static_cast < nsXBLStreamListener * > ( listener . get ( ) ) ;
2007-03-22 10:30:00 -07:00
// Create a new load observer.
if ( ! xblListener - > HasRequest ( aBindingURI , aBoundElement ) ) {
2013-03-05 04:37:47 -08:00
nsXBLBindingRequest * req = new nsXBLBindingRequest ( aBindingURI , aBoundElement ) ;
2007-03-22 10:30:00 -07:00
xblListener - > AddRequest ( req ) ;
}
return NS_OK ;
}
}
2011-11-03 13:39:08 -07:00
# ifdef MOZ_XUL
// Next, look in the startup cache
bool useStartupCache = useXULCache & & IsChromeOrResourceURI ( documentURI ) ;
if ( ! info & & useStartupCache ) {
rv = nsXBLDocumentInfo : : ReadPrototypeBindings ( documentURI , getter_AddRefs ( info ) ) ;
if ( NS_SUCCEEDED ( rv ) ) {
cache - > PutXBLDocumentInfo ( info ) ;
if ( bindingManager ) {
// Cache it in our binding manager's document table.
bindingManager - > PutXBLDocumentInfo ( info ) ;
}
}
}
# endif
2007-03-22 10:30:00 -07:00
if ( ! info ) {
// Finally, if all lines of defense fail, we go and fetch the binding
// document.
2014-03-18 14:25:39 -07:00
2007-03-22 10:30:00 -07:00
// Always load chrome synchronously
2011-09-28 23:19:26 -07:00
bool chrome ;
2007-03-22 10:30:00 -07:00
if ( NS_SUCCEEDED ( documentURI - > SchemeIs ( " chrome " , & chrome ) ) & & chrome )
2011-10-17 07:59:28 -07:00
aForceSyncLoad = true ;
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIDocument > document ;
FetchBindingDocument ( aBoundElement , aBoundDocument , documentURI ,
2014-09-21 09:38:38 -07:00
aBindingURI , aOriginPrincipal , aForceSyncLoad ,
getter_AddRefs ( document ) ) ;
2011-11-03 13:39:08 -07:00
2007-03-22 10:30:00 -07:00
if ( document ) {
nsBindingManager * xblDocBindingManager = document - > BindingManager ( ) ;
info = xblDocBindingManager - > GetXBLDocumentInfo ( documentURI ) ;
if ( ! info ) {
NS_ERROR ( " An XBL file is malformed. Did you forget the XBL namespace on the bindings tag? " ) ;
return NS_ERROR_FAILURE ;
}
xblDocBindingManager - > RemoveXBLDocumentInfo ( info ) ; // Break the self-imposed cycle.
// If the doc is a chrome URI, then we put it into the XUL cache.
# ifdef MOZ_XUL
2011-11-03 13:39:08 -07:00
if ( useStartupCache ) {
2007-03-22 10:30:00 -07:00
cache - > PutXBLDocumentInfo ( info ) ;
2011-11-03 13:39:08 -07:00
// now write the bindings into the startup cache
info - > WritePrototypeBindings ( ) ;
2007-03-22 10:30:00 -07:00
}
# endif
2014-03-18 14:25:39 -07:00
2007-03-22 10:30:00 -07:00
if ( bindingManager ) {
// Also put it in our binding manager's document table.
bindingManager - > PutXBLDocumentInfo ( info ) ;
}
}
}
}
2012-03-22 13:46:03 -07:00
info . forget ( aResult ) ;
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
nsresult
nsXBLService : : FetchBindingDocument ( nsIContent * aBoundElement , nsIDocument * aBoundDocument ,
2014-03-18 14:25:39 -07:00
nsIURI * aDocumentURI , nsIURI * aBindingURI ,
2014-09-21 09:38:38 -07:00
nsIPrincipal * aOriginPrincipal , bool aForceSyncLoad ,
nsIDocument * * aResult )
2007-03-22 10:30:00 -07:00
{
nsresult rv = NS_OK ;
2012-07-30 07:20:58 -07:00
// Initialize our out pointer to nullptr
* aResult = nullptr ;
2007-03-22 10:30:00 -07:00
2009-01-13 14:53:04 -08:00
// Now we have to synchronously load the binding file.
2014-03-18 14:25:39 -07:00
// Create an XML content sink and a parser.
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsILoadGroup > loadGroup ;
if ( aBoundDocument )
loadGroup = aBoundDocument - > GetDocumentLoadGroup ( ) ;
// We really shouldn't have to force a sync load for anything here... could
// we get away with not doing that? Not sure.
if ( IsChromeOrResourceURI ( aDocumentURI ) )
2011-10-17 07:59:28 -07:00
aForceSyncLoad = true ;
2007-03-22 10:30:00 -07:00
// Create document and contentsink and set them up.
nsCOMPtr < nsIDocument > doc ;
rv = NS_NewXMLDocument ( getter_AddRefs ( doc ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCOMPtr < nsIXMLContentSink > xblSink ;
2012-07-30 07:20:58 -07:00
rv = NS_NewXBLContentSink ( getter_AddRefs ( xblSink ) , doc , aDocumentURI , nullptr ) ;
2007-03-22 10:30:00 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// Open channel
2014-09-21 09:38:38 -07:00
// Note: There are some cases where aOriginPrincipal and aBoundDocument are purposely
// set to null (to bypass security checks) when calling LoadBindingDocumentInfo() which calls
// FetchBindingDocument(). LoadInfo will end up with no principal or node in those cases,
// so we use systemPrincipal. This achieves the same result of bypassing security checks,
// but it gives the wrong information to potential future consumers of loadInfo.
2007-03-22 10:30:00 -07:00
nsCOMPtr < nsIChannel > channel ;
2014-11-14 08:56:55 -08:00
if ( aOriginPrincipal ) {
// if there is an originPrincipal we should also have aBoundDocument
NS_ASSERTION ( aBoundDocument , " can not create a channel without aBoundDocument " ) ;
rv = NS_NewChannelWithTriggeringPrincipal ( getter_AddRefs ( channel ) ,
aDocumentURI ,
aBoundDocument ,
aOriginPrincipal ,
nsILoadInfo : : SEC_NORMAL ,
nsIContentPolicy : : TYPE_OTHER ,
loadGroup ) ;
}
else {
rv = NS_NewChannel ( getter_AddRefs ( channel ) ,
aDocumentURI ,
nsContentUtils : : GetSystemPrincipal ( ) ,
nsILoadInfo : : SEC_NORMAL ,
nsIContentPolicy : : TYPE_OTHER ,
loadGroup ) ;
}
2014-09-21 09:38:38 -07:00
2007-03-22 10:30:00 -07:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2015-01-26 09:28:15 -08:00
nsCOMPtr < nsIInterfaceRequestor > sameOriginChecker = nsContentUtils : : SameOriginChecker ( ) ;
2007-06-12 14:56:06 -07:00
channel - > SetNotificationCallbacks ( sameOriginChecker ) ;
2007-03-22 10:30:00 -07:00
if ( ! aForceSyncLoad ) {
// We can be asynchronous
2009-01-14 03:24:26 -08:00
nsXBLStreamListener * xblListener =
2012-05-23 11:46:04 -07:00
new nsXBLStreamListener ( aBoundDocument , xblSink , doc ) ;
2007-03-22 10:30:00 -07:00
// Add ourselves to the list of loading docs.
nsBindingManager * bindingManager ;
if ( aBoundDocument )
bindingManager = aBoundDocument - > BindingManager ( ) ;
else
2012-07-30 07:20:58 -07:00
bindingManager = nullptr ;
2007-03-22 10:30:00 -07:00
if ( bindingManager )
bindingManager - > PutLoadingDocListener ( aDocumentURI , xblListener ) ;
// Add our request.
2013-03-05 04:37:47 -08:00
nsXBLBindingRequest * req = new nsXBLBindingRequest ( aBindingURI ,
aBoundElement ) ;
2007-03-22 10:30:00 -07:00
xblListener - > AddRequest ( req ) ;
// Now kick off the async read.
2012-07-30 07:20:58 -07:00
rv = channel - > AsyncOpen ( xblListener , nullptr ) ;
2008-12-12 11:46:59 -08:00
if ( NS_FAILED ( rv ) ) {
// Well, we won't be getting a load. Make sure to clean up our stuff!
if ( bindingManager ) {
bindingManager - > RemoveLoadingDocListener ( aDocumentURI ) ;
}
}
2007-03-22 10:30:00 -07:00
return NS_OK ;
}
2009-01-14 03:24:26 -08:00
nsCOMPtr < nsIStreamListener > listener ;
rv = doc - > StartDocumentLoad ( " loadAsInteractiveData " ,
channel ,
loadGroup ,
2012-07-30 07:20:58 -07:00
nullptr ,
2009-01-14 03:24:26 -08:00
getter_AddRefs ( listener ) ,
2011-10-17 07:59:28 -07:00
true ,
2009-01-14 03:24:26 -08:00
xblSink ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-03-22 10:30:00 -07:00
// Now do a blocking synchronous parse of the file.
nsCOMPtr < nsIInputStream > in ;
rv = channel - > Open ( getter_AddRefs ( in ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = nsSyncLoadService : : PushSyncStreamToListener ( in , listener , channel ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
doc . swap ( * aResult ) ;
return NS_OK ;
}