1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Original Author: David W. Hyatt (hyatt@netscape.com)
24 : * - Brendan Eich (brendan@mozilla.org)
25 : * - Mike Pinkerton (pinkerton@netscape.com)
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : //////////////////////////////////////////////////////////////////////////////////////////
42 :
43 : #include "nsIXBLService.h"
44 : #include "nsIObserver.h"
45 : #include "nsWeakReference.h"
46 : #include "jsapi.h" // nsXBLJSClass derives from JSClass
47 : #include "jsclist.h" // nsXBLJSClass derives from JSCList
48 : #include "nsFixedSizeAllocator.h"
49 : #include "nsTArray.h"
50 :
51 : class nsXBLBinding;
52 : class nsXBLDocumentInfo;
53 : class nsIContent;
54 : class nsIDocument;
55 : class nsIAtom;
56 : class nsString;
57 : class nsIURI;
58 : class nsSupportsHashtable;
59 : class nsHashtable;
60 :
61 : class nsXBLService : public nsIXBLService,
62 : public nsIObserver,
63 : public nsSupportsWeakReference
64 : {
65 : NS_DECL_ISUPPORTS
66 :
67 : static bool IsChromeOrResourceURI(nsIURI* aURI);
68 :
69 : // This function loads a particular XBL file and installs all of the bindings
70 : // onto the element. aOriginPrincipal must not be null here.
71 : NS_IMETHOD LoadBindings(nsIContent* aContent, nsIURI* aURL,
72 : nsIPrincipal* aOriginPrincipal, bool aAugmentFlag,
73 : nsXBLBinding** aBinding, bool* aResolveStyle);
74 :
75 : // Indicates whether or not a binding is fully loaded.
76 : NS_IMETHOD BindingReady(nsIContent* aBoundElement, nsIURI* aURI, bool* aIsReady);
77 :
78 : // Gets the object's base class type.
79 : NS_IMETHOD ResolveTag(nsIContent* aContent, PRInt32* aNameSpaceID, nsIAtom** aResult);
80 :
81 : // This method checks the hashtable and then calls FetchBindingDocument on a
82 : // miss. aOriginPrincipal or aBoundDocument may be null to bypass security
83 : // checks.
84 : NS_IMETHOD LoadBindingDocumentInfo(nsIContent* aBoundElement,
85 : nsIDocument* aBoundDocument,
86 : nsIURI* aBindingURI,
87 : nsIPrincipal* aOriginPrincipal,
88 : bool aForceSyncLoad,
89 : nsXBLDocumentInfo** aResult);
90 :
91 : // Used by XUL key bindings and for window XBL.
92 : NS_IMETHOD AttachGlobalKeyHandler(nsIDOMEventTarget* aTarget);
93 : NS_IMETHOD DetachGlobalKeyHandler(nsIDOMEventTarget* aTarget);
94 :
95 : NS_DECL_NSIOBSERVER
96 :
97 : public:
98 : nsXBLService();
99 : virtual ~nsXBLService();
100 :
101 : protected:
102 : // This function clears out the bindings on a given content node.
103 : nsresult FlushStyleBindings(nsIContent* aContent);
104 :
105 : // Release any memory that we can
106 : nsresult FlushMemory();
107 :
108 : // This method synchronously loads and parses an XBL file.
109 : nsresult FetchBindingDocument(nsIContent* aBoundElement, nsIDocument* aBoundDocument,
110 : nsIURI* aDocumentURI, nsIURI* aBindingURI,
111 : bool aForceSyncLoad, nsIDocument** aResult);
112 :
113 : /**
114 : * This method calls the one below with an empty |aDontExtendURIs| array.
115 : */
116 : nsresult GetBinding(nsIContent* aBoundElement, nsIURI* aURI,
117 : bool aPeekFlag, nsIPrincipal* aOriginPrincipal,
118 : bool* aIsReady, nsXBLBinding** aResult);
119 :
120 : /**
121 : * This method loads a binding doc and then builds the specific binding
122 : * required. It can also peek without building.
123 : * @param aBoundElement the element to get a binding for
124 : * @param aURI the binding URI
125 : * @param aPeekFlag if true then just peek to see if the binding is ready
126 : * @param aIsReady [out] if the binding is ready or not
127 : * @param aResult [out] where to store the resulting binding (not used if
128 : * aPeekFlag is true, otherwise it must be non-null)
129 : * @param aDontExtendURIs a set of URIs that are already bound to this
130 : * element. If a binding extends any of these then further loading
131 : * is aborted (because it would lead to the binding extending itself)
132 : * and NS_ERROR_ILLEGAL_VALUE is returned.
133 : *
134 : * @note This method always calls LoadBindingDocumentInfo(), so it's
135 : * enough to funnel all security checks through that function.
136 : */
137 : nsresult GetBinding(nsIContent* aBoundElement, nsIURI* aURI,
138 : bool aPeekFlag, nsIPrincipal* aOriginPrincipal,
139 : bool* aIsReady, nsXBLBinding** aResult,
140 : nsTArray<nsIURI*>& aDontExtendURIs);
141 :
142 : // MEMBER VARIABLES
143 : public:
144 : static PRUint32 gRefCnt; // A count of XBLservice instances.
145 :
146 : static bool gDisableChromeCache;
147 :
148 : static nsHashtable* gClassTable; // A table of nsXBLJSClass objects.
149 :
150 : static JSCList gClassLRUList; // LRU list of cached classes.
151 : static PRUint32 gClassLRUListLength; // Number of classes on LRU list.
152 : static PRUint32 gClassLRUListQuota; // Quota on class LRU list.
153 : static bool gAllowDataURIs; // Whether we should allow data
154 : // urls in -moz-binding. Needed for
155 : // testing.
156 :
157 : nsFixedSizeAllocator mPool;
158 : };
159 :
160 : class nsXBLJSClass : public JSCList, public JSClass
161 : {
162 : private:
163 : nsrefcnt mRefCnt;
164 : nsrefcnt Destroy();
165 :
166 : public:
167 : nsXBLJSClass(const nsAFlatCString& aClassName);
168 0 : ~nsXBLJSClass() { nsMemory::Free((void*) name); }
169 :
170 0 : nsrefcnt Hold() { return ++mRefCnt; }
171 0 : nsrefcnt Drop() { return --mRefCnt ? mRefCnt : Destroy(); }
172 : };
173 :
|