Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef StartupCache_h_ |
michael@0 | 7 | #define StartupCache_h_ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsClassHashtable.h" |
michael@0 | 10 | #include "nsComponentManagerUtils.h" |
michael@0 | 11 | #include "nsZipArchive.h" |
michael@0 | 12 | #include "nsIStartupCache.h" |
michael@0 | 13 | #include "nsITimer.h" |
michael@0 | 14 | #include "nsIMemoryReporter.h" |
michael@0 | 15 | #include "nsIObserverService.h" |
michael@0 | 16 | #include "nsIObserver.h" |
michael@0 | 17 | #include "nsIOutputStream.h" |
michael@0 | 18 | #include "nsIFile.h" |
michael@0 | 19 | #include "mozilla/Attributes.h" |
michael@0 | 20 | #include "mozilla/MemoryReporting.h" |
michael@0 | 21 | #include "mozilla/StaticPtr.h" |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * The StartupCache is a persistent cache of simple key-value pairs, |
michael@0 | 25 | * where the keys are null-terminated c-strings and the values are |
michael@0 | 26 | * arbitrary data, passed as a (char*, size) tuple. |
michael@0 | 27 | * |
michael@0 | 28 | * Clients should use the GetSingleton() static method to access the cache. It |
michael@0 | 29 | * will be available from the end of XPCOM init (NS_InitXPCOM3 in nsXPComInit.cpp), |
michael@0 | 30 | * until XPCOM shutdown begins. The GetSingleton() method will return null if the cache |
michael@0 | 31 | * is unavailable. The cache is only provided for libxul builds -- |
michael@0 | 32 | * it will fail to link in non-libxul builds. The XPCOM interface is provided |
michael@0 | 33 | * only to allow compiled-code tests; clients should avoid using it. |
michael@0 | 34 | * |
michael@0 | 35 | * The API provided is very simple: GetBuffer() returns a buffer that was previously |
michael@0 | 36 | * stored in the cache (if any), and PutBuffer() inserts a buffer into the cache. |
michael@0 | 37 | * GetBuffer returns a new buffer, and the caller must take ownership of it. |
michael@0 | 38 | * PutBuffer will assert if the client attempts to insert a buffer with the same name as |
michael@0 | 39 | * an existing entry. The cache makes a copy of the passed-in buffer, so client |
michael@0 | 40 | * retains ownership. |
michael@0 | 41 | * |
michael@0 | 42 | * InvalidateCache() may be called if a client suspects data corruption |
michael@0 | 43 | * or wishes to invalidate for any other reason. This will remove all existing cache data. |
michael@0 | 44 | * Additionally, the static method IgnoreDiskCache() can be called if it is |
michael@0 | 45 | * believed that the on-disk cache file is itself corrupt. This call implicitly |
michael@0 | 46 | * calls InvalidateCache (if the singleton has been initialized) to ensure any |
michael@0 | 47 | * data already read from disk is discarded. The cache will not load data from |
michael@0 | 48 | * the disk file until a successful write occurs. |
michael@0 | 49 | * |
michael@0 | 50 | * Finally, getDebugObjectOutputStream() allows debug code to wrap an objectstream |
michael@0 | 51 | * with a debug objectstream, to check for multiply-referenced objects. These will |
michael@0 | 52 | * generally fail to deserialize correctly, unless they are stateless singletons or the |
michael@0 | 53 | * client maintains their own object data map for deserialization. |
michael@0 | 54 | * |
michael@0 | 55 | * Writes before the final-ui-startup notification are placed in an intermediate |
michael@0 | 56 | * cache in memory, then written out to disk at a later time, to get writes off the |
michael@0 | 57 | * startup path. In any case, clients should not rely on being able to GetBuffer() |
michael@0 | 58 | * data that is written to the cache, since it may not have been written to disk or |
michael@0 | 59 | * another client may have invalidated the cache. In other words, it should be used as |
michael@0 | 60 | * a cache only, and not a reliable persistent store. |
michael@0 | 61 | * |
michael@0 | 62 | * Some utility functions are provided in StartupCacheUtils. These functions wrap the |
michael@0 | 63 | * buffers into object streams, which may be useful for serializing objects. Note |
michael@0 | 64 | * the above caution about multiply-referenced objects, though -- the streams are just |
michael@0 | 65 | * as 'dumb' as the underlying buffers about multiply-referenced objects. They just |
michael@0 | 66 | * provide some convenience in writing out data. |
michael@0 | 67 | */ |
michael@0 | 68 | |
michael@0 | 69 | namespace mozilla { |
michael@0 | 70 | |
michael@0 | 71 | namespace scache { |
michael@0 | 72 | |
michael@0 | 73 | struct CacheEntry |
michael@0 | 74 | { |
michael@0 | 75 | nsAutoArrayPtr<char> data; |
michael@0 | 76 | uint32_t size; |
michael@0 | 77 | |
michael@0 | 78 | CacheEntry() : data(nullptr), size(0) { } |
michael@0 | 79 | |
michael@0 | 80 | // Takes possession of buf |
michael@0 | 81 | CacheEntry(char* buf, uint32_t len) : data(buf), size(len) { } |
michael@0 | 82 | |
michael@0 | 83 | ~CacheEntry() |
michael@0 | 84 | { |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) { |
michael@0 | 88 | return mallocSizeOf(data); |
michael@0 | 89 | } |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | // We don't want to refcount StartupCache, and ObserverService wants to |
michael@0 | 93 | // refcount its listeners, so we'll let it refcount this instead. |
michael@0 | 94 | class StartupCacheListener MOZ_FINAL : public nsIObserver |
michael@0 | 95 | { |
michael@0 | 96 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 97 | NS_DECL_NSIOBSERVER |
michael@0 | 98 | }; |
michael@0 | 99 | |
michael@0 | 100 | class StartupCache : public nsIMemoryReporter |
michael@0 | 101 | { |
michael@0 | 102 | |
michael@0 | 103 | friend class StartupCacheListener; |
michael@0 | 104 | friend class StartupCacheWrapper; |
michael@0 | 105 | |
michael@0 | 106 | public: |
michael@0 | 107 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 108 | NS_DECL_NSIMEMORYREPORTER |
michael@0 | 109 | |
michael@0 | 110 | // StartupCache methods. See above comments for a more detailed description. |
michael@0 | 111 | |
michael@0 | 112 | // Returns a buffer that was previously stored, caller takes ownership. |
michael@0 | 113 | nsresult GetBuffer(const char* id, char** outbuf, uint32_t* length); |
michael@0 | 114 | |
michael@0 | 115 | // Stores a buffer. Caller keeps ownership, we make a copy. |
michael@0 | 116 | nsresult PutBuffer(const char* id, const char* inbuf, uint32_t length); |
michael@0 | 117 | |
michael@0 | 118 | // Removes the cache file. |
michael@0 | 119 | void InvalidateCache(); |
michael@0 | 120 | |
michael@0 | 121 | // Signal that data should not be loaded from the cache file |
michael@0 | 122 | static void IgnoreDiskCache(); |
michael@0 | 123 | |
michael@0 | 124 | // In DEBUG builds, returns a stream that will attempt to check for |
michael@0 | 125 | // and disallow multiple writes of the same object. |
michael@0 | 126 | nsresult GetDebugObjectOutputStream(nsIObjectOutputStream* aStream, |
michael@0 | 127 | nsIObjectOutputStream** outStream); |
michael@0 | 128 | |
michael@0 | 129 | nsresult RecordAgesAlways(); |
michael@0 | 130 | |
michael@0 | 131 | static StartupCache* GetSingleton(); |
michael@0 | 132 | static void DeleteSingleton(); |
michael@0 | 133 | |
michael@0 | 134 | // This measures all the heap memory used by the StartupCache, i.e. it |
michael@0 | 135 | // excludes the mapping. |
michael@0 | 136 | size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf); |
michael@0 | 137 | |
michael@0 | 138 | size_t SizeOfMapping(); |
michael@0 | 139 | |
michael@0 | 140 | private: |
michael@0 | 141 | StartupCache(); |
michael@0 | 142 | virtual ~StartupCache(); |
michael@0 | 143 | |
michael@0 | 144 | enum TelemetrifyAge { |
michael@0 | 145 | IGNORE_AGE = 0, |
michael@0 | 146 | RECORD_AGE = 1 |
michael@0 | 147 | }; |
michael@0 | 148 | static enum TelemetrifyAge gPostFlushAgeAction; |
michael@0 | 149 | |
michael@0 | 150 | nsresult LoadArchive(enum TelemetrifyAge flag); |
michael@0 | 151 | nsresult Init(); |
michael@0 | 152 | void WriteToDisk(); |
michael@0 | 153 | nsresult ResetStartupWriteTimer(); |
michael@0 | 154 | void WaitOnWriteThread(); |
michael@0 | 155 | |
michael@0 | 156 | static nsresult InitSingleton(); |
michael@0 | 157 | static void WriteTimeout(nsITimer *aTimer, void *aClosure); |
michael@0 | 158 | static void ThreadedWrite(void *aClosure); |
michael@0 | 159 | |
michael@0 | 160 | static size_t SizeOfEntryExcludingThis(const nsACString& key, |
michael@0 | 161 | const nsAutoPtr<CacheEntry>& data, |
michael@0 | 162 | mozilla::MallocSizeOf mallocSizeOf, |
michael@0 | 163 | void *); |
michael@0 | 164 | |
michael@0 | 165 | nsClassHashtable<nsCStringHashKey, CacheEntry> mTable; |
michael@0 | 166 | nsRefPtr<nsZipArchive> mArchive; |
michael@0 | 167 | nsCOMPtr<nsIFile> mFile; |
michael@0 | 168 | |
michael@0 | 169 | nsCOMPtr<nsIObserverService> mObserverService; |
michael@0 | 170 | nsRefPtr<StartupCacheListener> mListener; |
michael@0 | 171 | nsCOMPtr<nsITimer> mTimer; |
michael@0 | 172 | |
michael@0 | 173 | bool mStartupWriteInitiated; |
michael@0 | 174 | |
michael@0 | 175 | static StaticRefPtr<StartupCache> gStartupCache; |
michael@0 | 176 | static bool gShutdownInitiated; |
michael@0 | 177 | static bool gIgnoreDiskCache; |
michael@0 | 178 | PRThread *mWriteThread; |
michael@0 | 179 | #ifdef DEBUG |
michael@0 | 180 | nsTHashtable<nsISupportsHashKey> mWriteObjectMap; |
michael@0 | 181 | #endif |
michael@0 | 182 | }; |
michael@0 | 183 | |
michael@0 | 184 | // This debug outputstream attempts to detect if clients are writing multiple |
michael@0 | 185 | // references to the same object. We only support that if that object |
michael@0 | 186 | // is a singleton. |
michael@0 | 187 | #ifdef DEBUG |
michael@0 | 188 | class StartupCacheDebugOutputStream MOZ_FINAL |
michael@0 | 189 | : public nsIObjectOutputStream |
michael@0 | 190 | { |
michael@0 | 191 | NS_DECL_ISUPPORTS |
michael@0 | 192 | NS_DECL_NSIOBJECTOUTPUTSTREAM |
michael@0 | 193 | |
michael@0 | 194 | StartupCacheDebugOutputStream (nsIObjectOutputStream* binaryStream, |
michael@0 | 195 | nsTHashtable<nsISupportsHashKey>* objectMap) |
michael@0 | 196 | : mBinaryStream(binaryStream), mObjectMap(objectMap) { } |
michael@0 | 197 | |
michael@0 | 198 | NS_FORWARD_SAFE_NSIBINARYOUTPUTSTREAM(mBinaryStream) |
michael@0 | 199 | NS_FORWARD_SAFE_NSIOUTPUTSTREAM(mBinaryStream) |
michael@0 | 200 | |
michael@0 | 201 | bool CheckReferences(nsISupports* aObject); |
michael@0 | 202 | |
michael@0 | 203 | nsCOMPtr<nsIObjectOutputStream> mBinaryStream; |
michael@0 | 204 | nsTHashtable<nsISupportsHashKey> *mObjectMap; |
michael@0 | 205 | }; |
michael@0 | 206 | #endif // DEBUG |
michael@0 | 207 | |
michael@0 | 208 | // XPCOM wrapper interface provided for tests only. |
michael@0 | 209 | #define NS_STARTUPCACHE_CID \ |
michael@0 | 210 | {0xae4505a9, 0x87ab, 0x477c, \ |
michael@0 | 211 | {0xb5, 0x77, 0xf9, 0x23, 0x57, 0xed, 0xa8, 0x84}} |
michael@0 | 212 | // contract id: "@mozilla.org/startupcache/cache;1" |
michael@0 | 213 | |
michael@0 | 214 | class StartupCacheWrapper MOZ_FINAL |
michael@0 | 215 | : public nsIStartupCache |
michael@0 | 216 | { |
michael@0 | 217 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 218 | NS_DECL_NSISTARTUPCACHE |
michael@0 | 219 | |
michael@0 | 220 | static StartupCacheWrapper* GetSingleton(); |
michael@0 | 221 | static StartupCacheWrapper *gStartupCacheWrapper; |
michael@0 | 222 | }; |
michael@0 | 223 | |
michael@0 | 224 | } // namespace scache |
michael@0 | 225 | } // namespace mozilla |
michael@0 | 226 | #endif //StartupCache_h_ |