2010-08-12 19:37:44 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2010-08-12 19:37:44 +00:00
|
|
|
|
|
|
|
#ifndef StartupCache_h_
|
|
|
|
#define StartupCache_h_
|
|
|
|
|
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2015-05-22 20:29:51 +00:00
|
|
|
#include "nsTArray.h"
|
2010-08-12 19:37:44 +00:00
|
|
|
#include "nsZipArchive.h"
|
|
|
|
#include "nsIStartupCache.h"
|
|
|
|
#include "nsITimer.h"
|
2013-11-07 05:35:30 +00:00
|
|
|
#include "nsIMemoryReporter.h"
|
2010-08-12 19:37:44 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIFile.h"
|
2012-06-20 03:45:32 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-11-25 23:57:40 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-11-28 01:05:00 +00:00
|
|
|
|
2010-08-12 19:37:44 +00:00
|
|
|
/**
|
|
|
|
* The StartupCache is a persistent cache of simple key-value pairs,
|
|
|
|
* where the keys are null-terminated c-strings and the values are
|
|
|
|
* arbitrary data, passed as a (char*, size) tuple.
|
|
|
|
*
|
|
|
|
* Clients should use the GetSingleton() static method to access the cache. It
|
2014-09-26 04:20:23 +00:00
|
|
|
* will be available from the end of XPCOM init (NS_InitXPCOM3 in XPCOMInit.cpp),
|
2010-08-12 19:37:44 +00:00
|
|
|
* until XPCOM shutdown begins. The GetSingleton() method will return null if the cache
|
|
|
|
* is unavailable. The cache is only provided for libxul builds --
|
|
|
|
* it will fail to link in non-libxul builds. The XPCOM interface is provided
|
|
|
|
* only to allow compiled-code tests; clients should avoid using it.
|
|
|
|
*
|
|
|
|
* The API provided is very simple: GetBuffer() returns a buffer that was previously
|
|
|
|
* stored in the cache (if any), and PutBuffer() inserts a buffer into the cache.
|
|
|
|
* GetBuffer returns a new buffer, and the caller must take ownership of it.
|
|
|
|
* PutBuffer will assert if the client attempts to insert a buffer with the same name as
|
|
|
|
* an existing entry. The cache makes a copy of the passed-in buffer, so client
|
|
|
|
* retains ownership.
|
|
|
|
*
|
|
|
|
* InvalidateCache() may be called if a client suspects data corruption
|
|
|
|
* or wishes to invalidate for any other reason. This will remove all existing cache data.
|
2012-10-11 08:17:15 +00:00
|
|
|
* Additionally, the static method IgnoreDiskCache() can be called if it is
|
|
|
|
* believed that the on-disk cache file is itself corrupt. This call implicitly
|
|
|
|
* calls InvalidateCache (if the singleton has been initialized) to ensure any
|
|
|
|
* data already read from disk is discarded. The cache will not load data from
|
|
|
|
* the disk file until a successful write occurs.
|
|
|
|
*
|
2010-08-12 19:37:44 +00:00
|
|
|
* Finally, getDebugObjectOutputStream() allows debug code to wrap an objectstream
|
|
|
|
* with a debug objectstream, to check for multiply-referenced objects. These will
|
|
|
|
* generally fail to deserialize correctly, unless they are stateless singletons or the
|
|
|
|
* client maintains their own object data map for deserialization.
|
|
|
|
*
|
|
|
|
* Writes before the final-ui-startup notification are placed in an intermediate
|
|
|
|
* cache in memory, then written out to disk at a later time, to get writes off the
|
|
|
|
* startup path. In any case, clients should not rely on being able to GetBuffer()
|
|
|
|
* data that is written to the cache, since it may not have been written to disk or
|
|
|
|
* another client may have invalidated the cache. In other words, it should be used as
|
|
|
|
* a cache only, and not a reliable persistent store.
|
|
|
|
*
|
|
|
|
* Some utility functions are provided in StartupCacheUtils. These functions wrap the
|
|
|
|
* buffers into object streams, which may be useful for serializing objects. Note
|
|
|
|
* the above caution about multiply-referenced objects, though -- the streams are just
|
|
|
|
* as 'dumb' as the underlying buffers about multiply-referenced objects. They just
|
|
|
|
* provide some convenience in writing out data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace mozilla {
|
2013-11-25 23:57:40 +00:00
|
|
|
|
2010-08-12 19:37:44 +00:00
|
|
|
namespace scache {
|
|
|
|
|
2013-11-25 23:57:40 +00:00
|
|
|
struct CacheEntry
|
2010-08-12 19:37:44 +00:00
|
|
|
{
|
|
|
|
nsAutoArrayPtr<char> data;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t size;
|
2010-08-12 19:37:44 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
CacheEntry() : data(nullptr), size(0) { }
|
2010-08-12 19:37:44 +00:00
|
|
|
|
|
|
|
// Takes possession of buf
|
2012-08-22 15:56:38 +00:00
|
|
|
CacheEntry(char* buf, uint32_t len) : data(buf), size(len) { }
|
2010-08-12 19:37:44 +00:00
|
|
|
|
|
|
|
~CacheEntry()
|
|
|
|
{
|
|
|
|
}
|
2011-12-19 00:20:36 +00:00
|
|
|
|
2015-07-31 04:19:57 +00:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) {
|
|
|
|
return mallocSizeOf(this) + mallocSizeOf(data);
|
2011-12-19 00:20:36 +00:00
|
|
|
}
|
2010-08-12 19:37:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// We don't want to refcount StartupCache, and ObserverService wants to
|
|
|
|
// refcount its listeners, so we'll let it refcount this instead.
|
2015-03-21 16:28:04 +00:00
|
|
|
class StartupCacheListener final : public nsIObserver
|
2010-08-12 19:37:44 +00:00
|
|
|
{
|
2014-06-23 22:40:03 +00:00
|
|
|
~StartupCacheListener() {}
|
2013-07-19 02:24:14 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2010-08-12 19:37:44 +00:00
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
2013-12-08 05:39:47 +00:00
|
|
|
class StartupCache : public nsIMemoryReporter
|
2010-08-12 19:37:44 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
friend class StartupCacheListener;
|
|
|
|
friend class StartupCacheWrapper;
|
2013-11-25 23:57:40 +00:00
|
|
|
|
2010-08-12 19:37:44 +00:00
|
|
|
public:
|
2013-12-08 05:39:47 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIMEMORYREPORTER
|
2010-08-12 19:37:44 +00:00
|
|
|
|
|
|
|
// StartupCache methods. See above comments for a more detailed description.
|
|
|
|
|
2013-11-25 23:57:40 +00:00
|
|
|
// Returns a buffer that was previously stored, caller takes ownership.
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult GetBuffer(const char* id, char** outbuf, uint32_t* length);
|
2010-08-12 19:37:44 +00:00
|
|
|
|
|
|
|
// Stores a buffer. Caller keeps ownership, we make a copy.
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult PutBuffer(const char* id, const char* inbuf, uint32_t length);
|
2010-08-12 19:37:44 +00:00
|
|
|
|
|
|
|
// Removes the cache file.
|
|
|
|
void InvalidateCache();
|
|
|
|
|
2012-10-11 08:17:15 +00:00
|
|
|
// Signal that data should not be loaded from the cache file
|
|
|
|
static void IgnoreDiskCache();
|
|
|
|
|
2010-08-12 19:37:44 +00:00
|
|
|
// In DEBUG builds, returns a stream that will attempt to check for
|
|
|
|
// and disallow multiple writes of the same object.
|
|
|
|
nsresult GetDebugObjectOutputStream(nsIObjectOutputStream* aStream,
|
|
|
|
nsIObjectOutputStream** outStream);
|
|
|
|
|
2012-02-22 19:07:18 +00:00
|
|
|
nsresult RecordAgesAlways();
|
|
|
|
|
2010-08-12 19:37:44 +00:00
|
|
|
static StartupCache* GetSingleton();
|
|
|
|
static void DeleteSingleton();
|
|
|
|
|
2011-12-19 00:20:36 +00:00
|
|
|
// This measures all the heap memory used by the StartupCache, i.e. it
|
|
|
|
// excludes the mapping.
|
2015-07-31 04:19:57 +00:00
|
|
|
size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
2011-12-19 00:20:36 +00:00
|
|
|
|
|
|
|
size_t SizeOfMapping();
|
2011-10-25 00:50:47 +00:00
|
|
|
|
2010-08-12 19:37:44 +00:00
|
|
|
private:
|
|
|
|
StartupCache();
|
2013-11-25 23:57:40 +00:00
|
|
|
virtual ~StartupCache();
|
2010-08-12 19:37:44 +00:00
|
|
|
|
2012-02-22 19:03:52 +00:00
|
|
|
enum TelemetrifyAge {
|
|
|
|
IGNORE_AGE = 0,
|
|
|
|
RECORD_AGE = 1
|
|
|
|
};
|
2012-02-22 19:07:18 +00:00
|
|
|
static enum TelemetrifyAge gPostFlushAgeAction;
|
2012-02-22 19:03:52 +00:00
|
|
|
|
|
|
|
nsresult LoadArchive(enum TelemetrifyAge flag);
|
2010-08-12 19:37:44 +00:00
|
|
|
nsresult Init();
|
|
|
|
void WriteToDisk();
|
2011-01-07 18:55:14 +00:00
|
|
|
nsresult ResetStartupWriteTimer();
|
2011-01-07 18:56:15 +00:00
|
|
|
void WaitOnWriteThread();
|
2010-08-12 19:37:44 +00:00
|
|
|
|
|
|
|
static nsresult InitSingleton();
|
|
|
|
static void WriteTimeout(nsITimer *aTimer, void *aClosure);
|
2011-01-07 18:56:15 +00:00
|
|
|
static void ThreadedWrite(void *aClosure);
|
2010-08-12 19:37:44 +00:00
|
|
|
|
|
|
|
nsClassHashtable<nsCStringHashKey, CacheEntry> mTable;
|
2015-05-22 20:29:51 +00:00
|
|
|
nsTArray<nsCString> mPendingWrites;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsZipArchive> mArchive;
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> mFile;
|
2013-11-25 23:57:40 +00:00
|
|
|
|
2010-08-12 19:37:44 +00:00
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<StartupCacheListener> mListener;
|
2010-08-12 19:37:44 +00:00
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mStartupWriteInitiated;
|
2010-08-12 19:37:44 +00:00
|
|
|
|
2013-11-25 23:57:40 +00:00
|
|
|
static StaticRefPtr<StartupCache> gStartupCache;
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool gShutdownInitiated;
|
2012-10-11 08:17:15 +00:00
|
|
|
static bool gIgnoreDiskCache;
|
2011-01-07 18:56:15 +00:00
|
|
|
PRThread *mWriteThread;
|
2010-08-12 19:37:44 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsTHashtable<nsISupportsHashKey> mWriteObjectMap;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// This debug outputstream attempts to detect if clients are writing multiple
|
|
|
|
// references to the same object. We only support that if that object
|
|
|
|
// is a singleton.
|
|
|
|
#ifdef DEBUG
|
2015-03-21 16:28:04 +00:00
|
|
|
class StartupCacheDebugOutputStream final
|
2010-08-12 19:37:44 +00:00
|
|
|
: public nsIObjectOutputStream
|
2014-06-23 22:40:03 +00:00
|
|
|
{
|
|
|
|
~StartupCacheDebugOutputStream() {}
|
|
|
|
|
2010-08-12 19:37:44 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBJECTOUTPUTSTREAM
|
|
|
|
|
|
|
|
StartupCacheDebugOutputStream (nsIObjectOutputStream* binaryStream,
|
|
|
|
nsTHashtable<nsISupportsHashKey>* objectMap)
|
|
|
|
: mBinaryStream(binaryStream), mObjectMap(objectMap) { }
|
|
|
|
|
|
|
|
NS_FORWARD_SAFE_NSIBINARYOUTPUTSTREAM(mBinaryStream)
|
|
|
|
NS_FORWARD_SAFE_NSIOUTPUTSTREAM(mBinaryStream)
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool CheckReferences(nsISupports* aObject);
|
2010-08-12 19:37:44 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObjectOutputStream> mBinaryStream;
|
|
|
|
nsTHashtable<nsISupportsHashKey> *mObjectMap;
|
|
|
|
};
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
// XPCOM wrapper interface provided for tests only.
|
|
|
|
#define NS_STARTUPCACHE_CID \
|
|
|
|
{0xae4505a9, 0x87ab, 0x477c, \
|
|
|
|
{0xb5, 0x77, 0xf9, 0x23, 0x57, 0xed, 0xa8, 0x84}}
|
|
|
|
// contract id: "@mozilla.org/startupcache/cache;1"
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class StartupCacheWrapper final
|
2010-08-12 19:37:44 +00:00
|
|
|
: public nsIStartupCache
|
|
|
|
{
|
2014-06-23 22:40:03 +00:00
|
|
|
~StartupCacheWrapper() {}
|
|
|
|
|
2013-07-19 02:24:14 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2010-08-12 19:37:44 +00:00
|
|
|
NS_DECL_NSISTARTUPCACHE
|
|
|
|
|
|
|
|
static StartupCacheWrapper* GetSingleton();
|
|
|
|
static StartupCacheWrapper *gStartupCacheWrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace scache
|
|
|
|
} // namespace mozilla
|
2015-07-13 15:25:42 +00:00
|
|
|
|
2010-08-12 19:37:44 +00:00
|
|
|
#endif //StartupCache_h_
|