1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 : * Chris Waterson <waterson@netscape.com>
24 : * Brendan Eich <brendan@mozilla.org>
25 : * Ben Goodger <ben@netscape.com>
26 : * Benjamin Smedberg <bsmedberg@covad.net>
27 : * Mark Hammond <mhammond@skippinet.com.au>
28 : *
29 : * Alternatively, the contents of this file may be used under the terms of
30 : * either of the GNU General Public License Version 2 or later (the "GPL"),
31 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 : * in which case the provisions of the GPL or the LGPL are applicable instead
33 : * of those above. If you wish to allow use of your version of this file only
34 : * under the terms of either the GPL or the LGPL, and not to allow others to
35 : * use your version of this file under the terms of the MPL, indicate your
36 : * decision by deleting the provisions above and replace them with the notice
37 : * and other provisions required by the GPL or the LGPL. If you do not delete
38 : * the provisions above, a recipient may use your version of this file under
39 : * the terms of any one of the MPL, the GPL or the LGPL.
40 : *
41 : * ***** END LICENSE BLOCK ***** */
42 :
43 : #ifndef nsXULPrototypeCache_h__
44 : #define nsXULPrototypeCache_h__
45 :
46 : #include "nsCOMPtr.h"
47 : #include "nsIObserver.h"
48 : #include "nsXBLDocumentInfo.h"
49 : #include "nsDataHashtable.h"
50 : #include "nsInterfaceHashtable.h"
51 : #include "nsRefPtrHashtable.h"
52 : #include "nsURIHashKey.h"
53 : #include "nsXULPrototypeDocument.h"
54 : #include "nsIInputStream.h"
55 : #include "nsIStorageStream.h"
56 :
57 : #include "jspubtd.h"
58 :
59 : #include "mozilla/scache/StartupCache.h"
60 :
61 : using namespace mozilla::scache;
62 :
63 : class nsCSSStyleSheet;
64 :
65 : struct CacheScriptEntry
66 : {
67 : PRUint32 mScriptTypeID; // the script language ID.
68 : JSScript* mScriptObject; // the script object.
69 : };
70 :
71 : /**
72 : * The XUL prototype cache can be used to store and retrieve shared data for
73 : * XUL documents, style sheets, XBL, and scripts.
74 : *
75 : * The cache has two levels:
76 : * 1. In-memory hashtables
77 : * 2. The on-disk cache file.
78 : */
79 : class nsXULPrototypeCache : public nsIObserver
80 : {
81 : public:
82 : // nsISupports
83 : NS_DECL_ISUPPORTS
84 : NS_DECL_NSIOBSERVER
85 :
86 : bool IsCached(nsIURI* aURI) {
87 : return GetPrototype(aURI) != nsnull;
88 : }
89 : void AbortCaching();
90 :
91 :
92 : /**
93 : * Whether the prototype cache is enabled.
94 : */
95 : bool IsEnabled();
96 :
97 : /**
98 : * Flush the cache; remove all XUL prototype documents, style
99 : * sheets, and scripts.
100 : */
101 : void Flush();
102 :
103 :
104 : // The following methods are used to put and retrive various items into and
105 : // from the cache.
106 :
107 : nsXULPrototypeDocument* GetPrototype(nsIURI* aURI);
108 : nsresult PutPrototype(nsXULPrototypeDocument* aDocument);
109 :
110 : JSScript* GetScript(nsIURI* aURI, PRUint32* langID);
111 : nsresult PutScript(nsIURI* aURI, PRUint32 langID, JSScript* aScriptObject);
112 :
113 0 : nsXBLDocumentInfo* GetXBLDocumentInfo(nsIURI* aURL) {
114 0 : return mXBLDocTable.GetWeak(aURL);
115 : }
116 : nsresult PutXBLDocumentInfo(nsXBLDocumentInfo* aDocumentInfo);
117 :
118 : /**
119 : * Get a style sheet by URI. If the style sheet is not in the cache,
120 : * returns nsnull.
121 : */
122 0 : nsCSSStyleSheet* GetStyleSheet(nsIURI* aURI) {
123 0 : return mStyleSheetTable.GetWeak(aURI);
124 : }
125 :
126 : /**
127 : * Store a style sheet in the cache. The key, style sheet's URI is obtained
128 : * from the style sheet itself.
129 : */
130 : nsresult PutStyleSheet(nsCSSStyleSheet* aStyleSheet);
131 :
132 : /**
133 : * Remove a XUL document from the set of loading documents.
134 : */
135 : void RemoveFromCacheSet(nsIURI* aDocumentURI);
136 :
137 : /**
138 : * Write the XUL prototype document to a cache file. The proto must be
139 : * fully loaded.
140 : */
141 : nsresult WritePrototype(nsXULPrototypeDocument* aPrototypeDocument);
142 :
143 : /**
144 : * This interface allows partial reads and writes from the buffers in the
145 : * startupCache.
146 : */
147 : nsresult GetInputStream(nsIURI* aURI, nsIObjectInputStream** objectInput);
148 : nsresult FinishInputStream(nsIURI* aURI);
149 : nsresult GetOutputStream(nsIURI* aURI, nsIObjectOutputStream** objectOutput);
150 : nsresult FinishOutputStream(nsIURI* aURI);
151 : nsresult HasData(nsIURI* aURI, bool* exists);
152 :
153 : static StartupCache* GetStartupCache();
154 :
155 : static nsXULPrototypeCache* GetInstance();
156 :
157 1403 : static void ReleaseGlobals()
158 : {
159 1403 : NS_IF_RELEASE(sInstance);
160 1403 : }
161 :
162 : void MarkInCCGeneration(PRUint32 aGeneration);
163 : protected:
164 : friend nsresult
165 : NS_NewXULPrototypeCache(nsISupports* aOuter, REFNSIID aIID, void** aResult);
166 :
167 : nsXULPrototypeCache();
168 : virtual ~nsXULPrototypeCache();
169 :
170 : static nsXULPrototypeCache* sInstance;
171 :
172 : void FlushScripts();
173 : void FlushSkinFiles();
174 :
175 : nsRefPtrHashtable<nsURIHashKey,nsXULPrototypeDocument> mPrototypeTable; // owns the prototypes
176 : nsRefPtrHashtable<nsURIHashKey,nsCSSStyleSheet> mStyleSheetTable;
177 : nsDataHashtable<nsURIHashKey,CacheScriptEntry> mScriptTable;
178 : nsRefPtrHashtable<nsURIHashKey,nsXBLDocumentInfo> mXBLDocTable;
179 :
180 : ///////////////////////////////////////////////////////////////////////////
181 : // StartupCache
182 : // this is really a hash set, with a dummy data parameter
183 : nsDataHashtable<nsURIHashKey,PRUint32> mCacheURITable;
184 :
185 : static StartupCache* gStartupCache;
186 : nsInterfaceHashtable<nsURIHashKey, nsIStorageStream> mOutputStreamTable;
187 : nsInterfaceHashtable<nsURIHashKey, nsIObjectInputStream> mInputStreamTable;
188 :
189 : // Bootstrap caching service
190 : nsresult BeginCaching(nsIURI* aDocumentURI);
191 : };
192 :
193 : #endif // nsXULPrototypeCache_h__
|