2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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/. */
|
2006-12-22 19:11:16 +03:00
|
|
|
|
|
|
|
#ifndef nsXULPrototypeCache_h__
|
|
|
|
#define nsXULPrototypeCache_h__
|
|
|
|
|
2021-03-02 12:02:19 +03:00
|
|
|
#include "nsBaseHashtable.h"
|
2006-12-22 19:11:16 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsInterfaceHashtable.h"
|
2007-03-12 08:53:33 +03:00
|
|
|
#include "nsRefPtrHashtable.h"
|
2006-12-22 19:11:16 +03:00
|
|
|
#include "nsURIHashKey.h"
|
2007-03-12 08:53:33 +03:00
|
|
|
#include "nsXULPrototypeDocument.h"
|
2010-08-12 23:42:36 +04:00
|
|
|
#include "nsIStorageStream.h"
|
2011-10-30 00:14:13 +04:00
|
|
|
|
2010-08-12 23:42:36 +04:00
|
|
|
#include "mozilla/scache/StartupCache.h"
|
2021-09-21 06:18:02 +03:00
|
|
|
#include "js/experimental/JSStencil.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
2010-08-12 23:42:36 +04:00
|
|
|
|
2018-02-19 13:07:44 +03:00
|
|
|
class nsIHandleReportCallback;
|
2014-06-20 14:32:49 +04:00
|
|
|
namespace mozilla {
|
2017-01-31 23:21:04 +03:00
|
|
|
class StyleSheet;
|
2014-06-20 14:32:49 +04:00
|
|
|
} // namespace mozilla
|
2006-12-22 19:11:16 +03:00
|
|
|
|
2007-03-12 08:53:33 +03:00
|
|
|
/**
|
|
|
|
* The XUL prototype cache can be used to store and retrieve shared data for
|
|
|
|
* XUL documents, style sheets, XBL, and scripts.
|
|
|
|
*
|
|
|
|
* The cache has two levels:
|
|
|
|
* 1. In-memory hashtables
|
2010-08-12 23:42:36 +04:00
|
|
|
* 2. The on-disk cache file.
|
2007-03-12 08:53:33 +03:00
|
|
|
*/
|
2011-08-26 23:50:23 +04:00
|
|
|
class nsXULPrototypeCache : public nsIObserver {
|
2006-12-22 19:11:16 +03:00
|
|
|
public:
|
2022-04-13 17:08:07 +03:00
|
|
|
enum class CacheType { Prototype, Script };
|
|
|
|
|
2006-12-22 19:11:16 +03:00
|
|
|
// nsISupports
|
2013-07-19 06:21:19 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2006-12-22 19:11:16 +03:00
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
bool IsCached(nsIURI* aURI) { return GetPrototype(aURI) != nullptr; }
|
2011-08-26 23:50:23 +04:00
|
|
|
void AbortCaching();
|
2007-03-12 08:53:33 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/**
|
2007-03-12 08:53:33 +03:00
|
|
|
* Whether the prototype cache is enabled.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsEnabled();
|
2007-03-12 08:53:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush the cache; remove all XUL prototype documents, style
|
|
|
|
* sheets, and scripts.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
void Flush();
|
2007-03-12 08:53:33 +03:00
|
|
|
|
|
|
|
// The following methods are used to put and retrive various items into and
|
|
|
|
// from the cache.
|
|
|
|
|
|
|
|
nsXULPrototypeDocument* GetPrototype(nsIURI* aURI);
|
|
|
|
nsresult PutPrototype(nsXULPrototypeDocument* aDocument);
|
|
|
|
|
2021-09-21 06:18:02 +03:00
|
|
|
JS::Stencil* GetStencil(nsIURI* aURI);
|
|
|
|
nsresult PutStencil(nsIURI* aURI, JS::Stencil* aStencil);
|
2007-03-12 08:53:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the XUL prototype document to a cache file. The proto must be
|
2012-07-30 18:20:58 +04:00
|
|
|
* fully loaded.
|
2007-03-12 08:53:33 +03:00
|
|
|
*/
|
2018-03-28 18:31:23 +03:00
|
|
|
nsresult WritePrototype(nsXULPrototypeDocument* aPrototypeDocument);
|
2007-03-12 08:53:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This interface allows partial reads and writes from the buffers in the
|
|
|
|
* startupCache.
|
|
|
|
*/
|
2022-04-13 17:08:07 +03:00
|
|
|
|
|
|
|
inline nsresult GetPrototypeInputStream(nsIURI* aURI,
|
|
|
|
nsIObjectInputStream** objectInput) {
|
|
|
|
return GetInputStream(CacheType::Prototype, aURI, objectInput);
|
|
|
|
}
|
|
|
|
inline nsresult GetScriptInputStream(nsIURI* aURI,
|
|
|
|
nsIObjectInputStream** objectInput) {
|
|
|
|
return GetInputStream(CacheType::Script, aURI, objectInput);
|
|
|
|
}
|
|
|
|
inline nsresult FinishScriptInputStream(nsIURI* aURI) {
|
|
|
|
return FinishInputStream(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult GetPrototypeOutputStream(
|
|
|
|
nsIURI* aURI, nsIObjectOutputStream** objectOutput) {
|
|
|
|
return GetOutputStream(aURI, objectOutput);
|
|
|
|
}
|
|
|
|
inline nsresult GetScriptOutputStream(nsIURI* aURI,
|
|
|
|
nsIObjectOutputStream** objectOutput) {
|
|
|
|
return GetOutputStream(aURI, objectOutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult FinishPrototypeOutputStream(nsIURI* aURI) {
|
|
|
|
return FinishOutputStream(CacheType::Prototype, aURI);
|
|
|
|
}
|
|
|
|
inline nsresult FinishScriptOutputStream(nsIURI* aURI) {
|
|
|
|
return FinishOutputStream(CacheType::Script, aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult HasPrototype(nsIURI* aURI, bool* exists) {
|
|
|
|
return HasData(CacheType::Prototype, aURI, exists);
|
|
|
|
}
|
|
|
|
inline nsresult HasScript(nsIURI* aURI, bool* exists) {
|
|
|
|
return HasData(CacheType::Script, aURI, exists);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsresult GetInputStream(CacheType cacheType, nsIURI* uri,
|
|
|
|
nsIObjectInputStream** stream);
|
2018-03-28 18:31:23 +03:00
|
|
|
nsresult FinishInputStream(nsIURI* aURI);
|
2022-04-13 17:08:07 +03:00
|
|
|
|
2018-03-28 18:31:23 +03:00
|
|
|
nsresult GetOutputStream(nsIURI* aURI, nsIObjectOutputStream** objectOutput);
|
2022-04-13 17:08:07 +03:00
|
|
|
nsresult FinishOutputStream(CacheType cacheType, nsIURI* aURI);
|
|
|
|
nsresult HasData(CacheType cacheType, nsIURI* aURI, bool* exists);
|
2007-03-12 08:53:33 +03:00
|
|
|
|
2022-04-13 17:08:07 +03:00
|
|
|
public:
|
2010-08-12 23:42:36 +04:00
|
|
|
static nsXULPrototypeCache* GetInstance();
|
|
|
|
static nsXULPrototypeCache* MaybeGetInstance() { return sInstance; }
|
|
|
|
|
|
|
|
static void ReleaseGlobals() { NS_IF_RELEASE(sInstance); }
|
|
|
|
|
2013-03-19 21:20:21 +04:00
|
|
|
void MarkInCCGeneration(uint32_t aGeneration);
|
2007-03-12 08:53:33 +03:00
|
|
|
|
|
|
|
static void CollectMemoryReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData);
|
2006-12-22 19:11:16 +03:00
|
|
|
|
|
|
|
protected:
|
2012-08-22 19:56:38 +04:00
|
|
|
friend nsresult NS_NewXULPrototypeCache(nsISupports* aOuter, REFNSIID aIID,
|
2013-03-19 21:20:21 +04:00
|
|
|
void** aResult);
|
2018-02-19 13:07:44 +03:00
|
|
|
|
2006-12-22 19:11:16 +03:00
|
|
|
nsXULPrototypeCache();
|
2021-09-21 06:18:02 +03:00
|
|
|
virtual ~nsXULPrototypeCache() = default;
|
2018-02-19 13:07:44 +03:00
|
|
|
|
2006-12-22 19:11:16 +03:00
|
|
|
static nsXULPrototypeCache* sInstance;
|
|
|
|
|
2013-07-23 13:58:27 +04:00
|
|
|
nsRefPtrHashtable<nsURIHashKey, nsXULPrototypeDocument>
|
|
|
|
mPrototypeTable; // owns the prototypes
|
2021-03-02 12:02:19 +03:00
|
|
|
|
2021-09-21 06:18:02 +03:00
|
|
|
class StencilHashKey : public nsURIHashKey {
|
2021-03-02 12:02:19 +03:00
|
|
|
public:
|
2021-09-21 06:18:02 +03:00
|
|
|
explicit StencilHashKey(const nsIURI* aKey) : nsURIHashKey(aKey) {}
|
|
|
|
StencilHashKey(StencilHashKey&&) = default;
|
2021-03-02 12:02:19 +03:00
|
|
|
|
2021-09-21 06:18:02 +03:00
|
|
|
RefPtr<JS::Stencil> mStencil;
|
2021-03-02 12:02:19 +03:00
|
|
|
};
|
|
|
|
|
2021-09-21 06:18:02 +03:00
|
|
|
nsTHashtable<StencilHashKey> mStencilTable;
|
2006-12-22 19:11:16 +03:00
|
|
|
|
2017-05-20 01:18:00 +03:00
|
|
|
// URIs already written to the startup cache, to prevent double-caching.
|
|
|
|
nsTHashtable<nsURIHashKey> mStartupCacheURITable;
|
|
|
|
|
2013-07-23 13:58:27 +04:00
|
|
|
nsInterfaceHashtable<nsURIHashKey, nsIStorageStream> mOutputStreamTable;
|
|
|
|
nsInterfaceHashtable<nsURIHashKey, nsIObjectInputStream> mInputStreamTable;
|
2006-12-22 19:11:16 +03:00
|
|
|
|
2010-08-12 23:42:36 +04:00
|
|
|
// Bootstrap caching service
|
|
|
|
nsresult BeginCaching(nsIURI* aDocumentURI);
|
2006-12-22 19:11:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsXULPrototypeCache_h__
|