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
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-19 17:05:34 -08:00
|
|
|
#ifndef nsXBLService_h_
|
|
|
|
#define nsXBLService_h_
|
|
|
|
|
2013-08-21 23:34:44 -07:00
|
|
|
#include "mozilla/LinkedList.h"
|
2012-05-23 11:46:04 -07:00
|
|
|
#include "nsString.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsWeakReference.h"
|
2013-09-05 16:08:06 -07:00
|
|
|
#include "js/Class.h" // nsXBLJSClass derives from JSClass
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsTArray.h"
|
2014-03-18 14:25:39 -07:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsXBLBinding;
|
2010-07-14 18:53:11 -07:00
|
|
|
class nsXBLDocumentInfo;
|
2013-08-21 23:34:44 -07:00
|
|
|
class nsXBLJSClass;
|
2007-03-22 10:30:00 -07:00
|
|
|
class nsIContent;
|
|
|
|
class nsIDocument;
|
|
|
|
class nsString;
|
|
|
|
class nsIURI;
|
2012-05-23 11:46:04 -07:00
|
|
|
class nsIPrincipal;
|
2013-04-05 17:44:26 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class EventTarget;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-04 09:49:39 -08:00
|
|
|
class nsXBLService MOZ_FINAL : public nsIObserver,
|
|
|
|
public nsSupportsWeakReference
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2012-05-23 11:46:04 -07:00
|
|
|
static nsXBLService* gInstance;
|
|
|
|
|
|
|
|
static void Init();
|
|
|
|
|
|
|
|
static void Shutdown() {
|
|
|
|
NS_IF_RELEASE(gInstance);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsXBLService* GetInstance() { return gInstance; }
|
|
|
|
|
2011-11-03 13:39:07 -07:00
|
|
|
static bool IsChromeOrResourceURI(nsIURI* aURI);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// This function loads a particular XBL file and installs all of the bindings
|
2007-07-18 14:56:57 -07:00
|
|
|
// onto the element. aOriginPrincipal must not be null here.
|
2012-05-23 11:46:04 -07:00
|
|
|
nsresult LoadBindings(nsIContent* aContent, nsIURI* aURL,
|
2013-01-24 09:45:50 -08:00
|
|
|
nsIPrincipal* aOriginPrincipal,
|
2012-05-23 11:46:04 -07:00
|
|
|
nsXBLBinding** aBinding, bool* aResolveStyle);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Indicates whether or not a binding is fully loaded.
|
2012-05-23 11:46:04 -07:00
|
|
|
nsresult BindingReady(nsIContent* aBoundElement, nsIURI* aURI, bool* aIsReady);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-18 14:56:57 -07:00
|
|
|
// This method checks the hashtable and then calls FetchBindingDocument on a
|
|
|
|
// miss. aOriginPrincipal or aBoundDocument may be null to bypass security
|
|
|
|
// checks.
|
2012-05-23 11:46:04 -07:00
|
|
|
nsresult LoadBindingDocumentInfo(nsIContent* aBoundElement,
|
|
|
|
nsIDocument* aBoundDocument,
|
|
|
|
nsIURI* aBindingURI,
|
|
|
|
nsIPrincipal* aOriginPrincipal,
|
|
|
|
bool aForceSyncLoad,
|
|
|
|
nsXBLDocumentInfo** aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Used by XUL key bindings and for window XBL.
|
2013-04-05 17:44:26 -07:00
|
|
|
static nsresult AttachGlobalKeyHandler(mozilla::dom::EventTarget* aTarget);
|
|
|
|
static nsresult DetachGlobalKeyHandler(mozilla::dom::EventTarget* aTarget);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2012-05-23 11:46:04 -07:00
|
|
|
private:
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXBLService();
|
|
|
|
virtual ~nsXBLService();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// This function clears out the bindings on a given content node.
|
|
|
|
nsresult FlushStyleBindings(nsIContent* aContent);
|
|
|
|
|
|
|
|
// Release any memory that we can
|
|
|
|
nsresult FlushMemory();
|
2014-03-18 14:25:39 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// This method synchronously loads and parses an XBL file.
|
|
|
|
nsresult FetchBindingDocument(nsIContent* aBoundElement, nsIDocument* aBoundDocument,
|
2014-03-18 14:25:39 -07:00
|
|
|
nsIURI* aDocumentURI, nsIURI* aBindingURI,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aForceSyncLoad, nsIDocument** aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method calls the one below with an empty |aDontExtendURIs| array.
|
|
|
|
*/
|
|
|
|
nsresult GetBinding(nsIContent* aBoundElement, nsIURI* aURI,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aPeekFlag, nsIPrincipal* aOriginPrincipal,
|
|
|
|
bool* aIsReady, nsXBLBinding** aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method loads a binding doc and then builds the specific binding
|
|
|
|
* required. It can also peek without building.
|
|
|
|
* @param aBoundElement the element to get a binding for
|
|
|
|
* @param aURI the binding URI
|
|
|
|
* @param aPeekFlag if true then just peek to see if the binding is ready
|
|
|
|
* @param aIsReady [out] if the binding is ready or not
|
|
|
|
* @param aResult [out] where to store the resulting binding (not used if
|
|
|
|
* aPeekFlag is true, otherwise it must be non-null)
|
|
|
|
* @param aDontExtendURIs a set of URIs that are already bound to this
|
|
|
|
* element. If a binding extends any of these then further loading
|
|
|
|
* is aborted (because it would lead to the binding extending itself)
|
|
|
|
* and NS_ERROR_ILLEGAL_VALUE is returned.
|
2007-07-18 14:56:57 -07:00
|
|
|
*
|
|
|
|
* @note This method always calls LoadBindingDocumentInfo(), so it's
|
|
|
|
* enough to funnel all security checks through that function.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
nsresult GetBinding(nsIContent* aBoundElement, nsIURI* aURI,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aPeekFlag, nsIPrincipal* aOriginPrincipal,
|
|
|
|
bool* aIsReady, nsXBLBinding** aResult,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTArray<nsIURI*>& aDontExtendURIs);
|
|
|
|
|
|
|
|
// MEMBER VARIABLES
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gDisableChromeCache;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-18 14:25:39 -07:00
|
|
|
typedef nsDataHashtable<nsCStringHashKey, nsXBLJSClass*> ClassTable;
|
|
|
|
static ClassTable* gClassTable; // A table of nsXBLJSClass objects.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-21 23:34:44 -07:00
|
|
|
static mozilla::LinkedList<nsXBLJSClass>* gClassLRUList;
|
|
|
|
// LRU list of cached classes.
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t gClassLRUListLength; // Number of classes on LRU list.
|
|
|
|
static uint32_t gClassLRUListQuota; // Quota on class LRU list.
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gAllowDataURIs; // Whether we should allow data
|
2008-04-28 16:56:07 -07:00
|
|
|
// urls in -moz-binding. Needed for
|
|
|
|
// testing.
|
2013-09-11 05:49:04 -07:00
|
|
|
|
|
|
|
// Look up the class by key in gClassTable.
|
|
|
|
static nsXBLJSClass *getClass(const nsCString &key);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-08-21 23:34:44 -07:00
|
|
|
class nsXBLJSClass : public mozilla::LinkedListElement<nsXBLJSClass>
|
|
|
|
, public JSClass
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsrefcnt mRefCnt;
|
2012-12-12 07:10:02 -08:00
|
|
|
nsCString mKey;
|
|
|
|
static uint64_t sIdCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsrefcnt Destroy();
|
|
|
|
|
|
|
|
public:
|
2012-12-12 07:10:02 -08:00
|
|
|
nsXBLJSClass(const nsAFlatCString& aClassName, const nsCString& aKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
~nsXBLJSClass() { nsMemory::Free((void*) name); }
|
|
|
|
|
2012-12-12 07:10:02 -08:00
|
|
|
static uint64_t NewId() { return ++sIdCount; }
|
|
|
|
|
|
|
|
nsCString& Key() { return mKey; }
|
|
|
|
void SetKey(const nsCString& aKey) { mKey = aKey; }
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsrefcnt Hold() { return ++mRefCnt; }
|
|
|
|
nsrefcnt Drop() { return --mRefCnt ? mRefCnt : Destroy(); }
|
2013-02-08 06:24:20 -08:00
|
|
|
nsrefcnt AddRef() { return Hold(); }
|
|
|
|
nsrefcnt Release() { return Drop(); }
|
2013-09-11 05:49:04 -07:00
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
// Downcast from a pointer to const JSClass to a pointer to non-const
|
|
|
|
// nsXBLJSClass.
|
|
|
|
//
|
|
|
|
// The const_cast is safe because nsXBLJSClass instances are never actually
|
|
|
|
// const. It's necessary because we pass pointers to nsXBLJSClass to code
|
|
|
|
// which uses pointers to const JSClass, and returns them back to us that
|
|
|
|
// way, and we need to convert them back to pointers to non-const
|
|
|
|
// nsXBLJSClass so that we can modify the reference count and add them to
|
|
|
|
// the gClassLRUList list.
|
2013-09-11 05:49:04 -07:00
|
|
|
static nsXBLJSClass*
|
2013-09-11 05:49:05 -07:00
|
|
|
fromJSClass(const JSClass* c)
|
2013-09-11 05:49:04 -07:00
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
nsXBLJSClass* x = const_cast<nsXBLJSClass*>(static_cast<const nsXBLJSClass*>(c));
|
2013-09-11 05:49:04 -07:00
|
|
|
MOZ_ASSERT(nsXBLService::getClass(x->mKey) == x);
|
|
|
|
return x;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2012-11-19 17:05:34 -08:00
|
|
|
#endif
|