2013-09-20 13:11:26 +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/. */
|
|
|
|
|
|
|
|
#ifndef CacheFile__h__
|
|
|
|
#define CacheFile__h__
|
|
|
|
|
|
|
|
#include "CacheFileChunk.h"
|
|
|
|
#include "CacheFileIOManager.h"
|
|
|
|
#include "CacheFileMetadata.h"
|
|
|
|
#include "nsRefPtrHashtable.h"
|
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
|
2021-01-25 15:23:13 +03:00
|
|
|
class nsIAsyncOutputStream;
|
|
|
|
class nsICacheEntry;
|
|
|
|
class nsICacheEntryMetaDataVisitor;
|
2013-09-20 13:11:26 +04:00
|
|
|
class nsIInputStream;
|
|
|
|
class nsIOutputStream;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
class CacheFileInputStream;
|
|
|
|
class CacheFileOutputStream;
|
2013-11-21 02:20:17 +04:00
|
|
|
class CacheOutputCloseListener;
|
2013-09-20 13:11:26 +04:00
|
|
|
class MetadataWriteTimer;
|
|
|
|
|
|
|
|
#define CACHEFILELISTENER_IID \
|
|
|
|
{ /* 95e7f284-84ba-48f9-b1fc-3a7336b4c33c */ \
|
|
|
|
0x95e7f284, 0x84ba, 0x48f9, { \
|
|
|
|
0xb1, 0xfc, 0x3a, 0x73, 0x36, 0xb4, 0xc3, 0x3c \
|
|
|
|
} \
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
class CacheFileListener : public nsISupports {
|
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(CACHEFILELISTENER_IID)
|
|
|
|
|
|
|
|
NS_IMETHOD OnFileReady(nsresult aResult, bool aIsNew) = 0;
|
|
|
|
NS_IMETHOD OnFileDoomed(nsresult aResult) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileListener, CACHEFILELISTENER_IID)
|
|
|
|
|
2015-03-21 21:35:18 +03:00
|
|
|
class CacheFile final : public CacheFileChunkListener,
|
|
|
|
public CacheFileIOListener,
|
|
|
|
public CacheFileMetadataListener {
|
2013-09-20 13:11:26 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
CacheFile();
|
|
|
|
|
|
|
|
nsresult Init(const nsACString& aKey, bool aCreateNew, bool aMemoryOnly,
|
2015-09-04 02:05:42 +03:00
|
|
|
bool aSkipSizeCheck, bool aPriority, bool aPinned,
|
2013-09-20 13:11:26 +04:00
|
|
|
CacheFileListener* aCallback);
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnChunkRead(nsresult aResult, CacheFileChunk* aChunk) override;
|
|
|
|
NS_IMETHOD OnChunkWritten(nsresult aResult, CacheFileChunk* aChunk) override;
|
2013-09-20 13:11:26 +04:00
|
|
|
NS_IMETHOD OnChunkAvailable(nsresult aResult, uint32_t aChunkIdx,
|
2015-03-21 19:28:04 +03:00
|
|
|
CacheFileChunk* aChunk) override;
|
|
|
|
NS_IMETHOD OnChunkUpdated(CacheFileChunk* aChunk) override;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnFileOpened(CacheFileHandle* aHandle, nsresult aResult) override;
|
2013-09-20 13:11:26 +04:00
|
|
|
NS_IMETHOD OnDataWritten(CacheFileHandle* aHandle, const char* aBuf,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsresult aResult) override;
|
|
|
|
NS_IMETHOD OnDataRead(CacheFileHandle* aHandle, char* aBuf,
|
|
|
|
nsresult aResult) override;
|
|
|
|
NS_IMETHOD OnFileDoomed(CacheFileHandle* aHandle, nsresult aResult) override;
|
|
|
|
NS_IMETHOD OnEOFSet(CacheFileHandle* aHandle, nsresult aResult) override;
|
|
|
|
NS_IMETHOD OnFileRenamed(CacheFileHandle* aHandle, nsresult aResult) override;
|
2016-02-12 16:45:00 +03:00
|
|
|
virtual bool IsKilled() override;
|
2015-03-21 19:28:04 +03:00
|
|
|
|
|
|
|
NS_IMETHOD OnMetadataRead(nsresult aResult) override;
|
|
|
|
NS_IMETHOD OnMetadataWritten(nsresult aResult) override;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-02-12 16:45:00 +03:00
|
|
|
NS_IMETHOD OpenInputStream(nsICacheEntry* aCacheEntryHandle,
|
|
|
|
nsIInputStream** _retval);
|
2016-08-17 13:58:44 +03:00
|
|
|
NS_IMETHOD OpenAlternativeInputStream(nsICacheEntry* aCacheEntryHandle,
|
|
|
|
const char* aAltDataType,
|
|
|
|
nsIInputStream** _retval);
|
2013-11-21 02:20:17 +04:00
|
|
|
NS_IMETHOD OpenOutputStream(CacheOutputCloseListener* aCloseListener,
|
|
|
|
nsIOutputStream** _retval);
|
2016-08-17 13:58:44 +03:00
|
|
|
NS_IMETHOD OpenAlternativeOutputStream(
|
|
|
|
CacheOutputCloseListener* aCloseListener, const char* aAltDataType,
|
2019-03-05 04:35:41 +03:00
|
|
|
nsIAsyncOutputStream** _retval);
|
2013-09-20 13:11:26 +04:00
|
|
|
NS_IMETHOD SetMemoryOnly();
|
|
|
|
NS_IMETHOD Doom(CacheFileListener* aCallback);
|
|
|
|
|
2016-02-12 16:45:00 +03:00
|
|
|
void Kill() { mKill = true; }
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult ThrowMemoryCachedData();
|
|
|
|
|
2016-09-27 11:26:00 +03:00
|
|
|
nsresult GetAltDataSize(int64_t* aSize);
|
Bug 1487100 - Allow calling nsICacheInfoChannel.preferAlternativeDataType(altDataType, contentType) multiple times r=michal,luke
This patch changes the way we set and handle the preferred alternate data type.
It is no longer just one choice, but a set of preferences, each conditional
on the contentType of the resource.
For example:
var cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
cc.preferAlternativeDataType("js-bytecode", "text/javascript");
cc.preferAlternativeDataType("ammended-text", "text/plain");
cc.preferAlternativeDataType("something-else", "");
When loaded from the cache, the available alt-data type will be checked against
"js-bytecode" if the contentType is "text/javascript", "ammended-text" if the contentType is "text/plain" or "something-else" for all contentTypes.
Note that the alt-data type could be "something-else" even if the contentType is "text/javascript".
The preferences are saved as an nsTArray<mozilla::Tuple<nsCString, nsCString>>.
Differential Revision: https://phabricator.services.mozilla.com/D8071
--HG--
extra : rebase_source : eb4961f05a52e557e7d2d986d59e0a2cf18a3447
extra : source : dd1c31ea78c2b15d14750d137037a54d50719997
2018-10-17 16:58:30 +03:00
|
|
|
nsresult GetAltDataType(nsACString& aType);
|
2016-09-27 11:26:00 +03:00
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
// metadata forwarders
|
2014-03-05 21:14:19 +04:00
|
|
|
nsresult GetElement(const char* aKey, char** _retval);
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult SetElement(const char* aKey, const char* aValue);
|
2014-05-01 15:28:12 +04:00
|
|
|
nsresult VisitMetaData(nsICacheEntryMetaDataVisitor* aVisitor);
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult ElementsSize(uint32_t* _retval);
|
|
|
|
nsresult SetExpirationTime(uint32_t aExpirationTime);
|
|
|
|
nsresult GetExpirationTime(uint32_t* _retval);
|
2014-01-09 03:27:33 +04:00
|
|
|
nsresult SetFrecency(uint32_t aFrecency);
|
|
|
|
nsresult GetFrecency(uint32_t* _retval);
|
2017-03-28 07:02:45 +03:00
|
|
|
nsresult SetNetworkTimes(uint64_t aOnStartTime, uint64_t aOnStopTime);
|
2019-03-27 17:32:12 +03:00
|
|
|
nsresult SetContentType(uint8_t aContentType);
|
2017-03-28 07:02:45 +03:00
|
|
|
nsresult GetOnStartTime(uint64_t* _retval);
|
|
|
|
nsresult GetOnStopTime(uint64_t* _retval);
|
2014-09-16 17:51:50 +04:00
|
|
|
nsresult GetLastModified(uint32_t* _retval);
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult GetLastFetched(uint32_t* _retval);
|
|
|
|
nsresult GetFetchCount(uint32_t* _retval);
|
2016-11-10 18:14:34 +03:00
|
|
|
nsresult GetDiskStorageSizeInKB(uint32_t* aDiskStorageSize);
|
2014-09-16 17:51:50 +04:00
|
|
|
// Called by upper layers to indicated the entry has been fetched,
|
|
|
|
// i.e. delivered to the consumer.
|
|
|
|
nsresult OnFetched();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
bool DataSize(int64_t* aSize);
|
|
|
|
void Key(nsACString& aKey) { aKey = mKey; }
|
2014-02-18 21:26:48 +04:00
|
|
|
bool IsDoomed();
|
2015-10-22 13:11:00 +03:00
|
|
|
bool IsPinned() const { return mPinned; }
|
2020-01-23 22:18:06 +03:00
|
|
|
// Returns true when there is a potentially unfinished write operation.
|
2014-04-06 22:45:18 +04:00
|
|
|
bool IsWriteInProgress();
|
2018-04-25 07:01:00 +03:00
|
|
|
bool EntryWouldExceedLimit(int64_t aOffset, int64_t aSize, bool aIsAltData);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
// Memory reporting
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
private:
|
2014-01-07 00:27:18 +04:00
|
|
|
friend class CacheFileIOManager;
|
2013-09-20 13:11:26 +04:00
|
|
|
friend class CacheFileChunk;
|
|
|
|
friend class CacheFileInputStream;
|
|
|
|
friend class CacheFileOutputStream;
|
|
|
|
friend class CacheFileAutoLock;
|
|
|
|
friend class MetadataWriteTimer;
|
|
|
|
|
|
|
|
virtual ~CacheFile();
|
|
|
|
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
2014-02-27 03:11:40 +04:00
|
|
|
void AssertOwnsLock() const;
|
2015-10-18 08:24:48 +03:00
|
|
|
void ReleaseOutsideLock(RefPtr<nsISupports> aObject);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-05-02 22:15:14 +04:00
|
|
|
enum ECallerType { READER = 0, WRITER = 1, PRELOADER = 2 };
|
|
|
|
|
2014-05-19 16:21:18 +04:00
|
|
|
nsresult DoomLocked(CacheFileListener* aCallback);
|
|
|
|
|
2014-05-02 22:15:14 +04:00
|
|
|
nsresult GetChunkLocked(uint32_t aIndex, ECallerType aCaller,
|
2013-09-20 13:11:26 +04:00
|
|
|
CacheFileChunkListener* aCallback,
|
|
|
|
CacheFileChunk** _retval);
|
2014-05-02 22:15:14 +04:00
|
|
|
|
|
|
|
void PreloadChunks(uint32_t aIndex);
|
2014-05-18 11:31:38 +04:00
|
|
|
bool ShouldCacheChunk(uint32_t aIndex);
|
|
|
|
bool MustKeepCachedChunk(uint32_t aIndex);
|
2014-05-02 22:15:14 +04:00
|
|
|
|
2014-05-28 18:16:03 +04:00
|
|
|
nsresult DeactivateChunk(CacheFileChunk* aChunk);
|
2014-04-22 15:23:17 +04:00
|
|
|
void RemoveChunkInternal(CacheFileChunk* aChunk, bool aCacheChunk);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-08-17 13:58:44 +03:00
|
|
|
bool OutputStreamExists(bool aAlternativeData);
|
|
|
|
// Returns number of bytes that are available and can be read by input stream
|
|
|
|
// without waiting for the data. The amount is counted from the start of
|
|
|
|
// aIndex chunk and it is guaranteed that this data won't be released by
|
|
|
|
// CleanUpCachedChunks().
|
|
|
|
int64_t BytesFromChunk(uint32_t aIndex, bool aAlternativeData);
|
|
|
|
nsresult Truncate(int64_t aOffset);
|
2014-05-15 21:24:55 +04:00
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
void RemoveInput(CacheFileInputStream* aInput, nsresult aStatus);
|
|
|
|
void RemoveOutput(CacheFileOutputStream* aOutput, nsresult aStatus);
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult NotifyChunkListener(CacheFileChunkListener* aCallback,
|
|
|
|
nsIEventTarget* aTarget, nsresult aResult,
|
|
|
|
uint32_t aChunkIdx, CacheFileChunk* aChunk);
|
2019-12-02 16:21:06 +03:00
|
|
|
void QueueChunkListener(uint32_t aIndex, CacheFileChunkListener* aCallback);
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult NotifyChunkListeners(uint32_t aIndex, nsresult aResult,
|
|
|
|
CacheFileChunk* aChunk);
|
|
|
|
bool HaveChunkListeners(uint32_t aIndex);
|
|
|
|
void NotifyListenersAboutOutputRemoval();
|
|
|
|
|
|
|
|
bool IsDirty();
|
|
|
|
void WriteMetadataIfNeeded();
|
2014-01-07 00:27:18 +04:00
|
|
|
void WriteMetadataIfNeededLocked(bool aFireAndForget = false);
|
2013-09-20 13:11:26 +04:00
|
|
|
void PostWriteTimer();
|
|
|
|
|
2015-12-04 01:00:03 +03:00
|
|
|
void CleanUpCachedChunks();
|
2014-05-02 22:15:14 +04:00
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult PadChunkWithZeroes(uint32_t aChunkIdx);
|
|
|
|
|
2014-04-22 15:23:17 +04:00
|
|
|
void SetError(nsresult aStatus);
|
2017-03-06 13:21:59 +03:00
|
|
|
nsresult SetAltMetadata(const char* aAltMetadata);
|
2014-04-22 15:23:17 +04:00
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult InitIndexEntry();
|
|
|
|
|
2021-06-11 10:10:41 +03:00
|
|
|
mozilla::Mutex mLock{"CacheFile.mLock"};
|
|
|
|
bool mOpeningFile{false};
|
|
|
|
bool mReady{false};
|
|
|
|
bool mMemoryOnly{false};
|
|
|
|
bool mSkipSizeCheck{false};
|
|
|
|
bool mOpenAsMemoryOnly{false};
|
|
|
|
bool mPinned{false};
|
|
|
|
bool mPriority{false};
|
|
|
|
bool mDataAccessed{false};
|
|
|
|
bool mDataIsDirty{false};
|
|
|
|
bool mWritingMetadata{false};
|
|
|
|
bool mPreloadWithoutInputStreams{true};
|
|
|
|
uint32_t mPreloadChunkCount{0};
|
|
|
|
nsresult mStatus{NS_OK};
|
|
|
|
// Size of the whole data including eventual alternative data represenation.
|
|
|
|
int64_t mDataSize{-1};
|
|
|
|
|
|
|
|
// If there is alternative data present, it contains size of the original
|
|
|
|
// data, i.e. offset where alternative data starts. Otherwise it is -1.
|
|
|
|
int64_t mAltDataOffset{-1};
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
nsCString mKey;
|
Bug 1487100 - Allow calling nsICacheInfoChannel.preferAlternativeDataType(altDataType, contentType) multiple times r=michal,luke
This patch changes the way we set and handle the preferred alternate data type.
It is no longer just one choice, but a set of preferences, each conditional
on the contentType of the resource.
For example:
var cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
cc.preferAlternativeDataType("js-bytecode", "text/javascript");
cc.preferAlternativeDataType("ammended-text", "text/plain");
cc.preferAlternativeDataType("something-else", "");
When loaded from the cache, the available alt-data type will be checked against
"js-bytecode" if the contentType is "text/javascript", "ammended-text" if the contentType is "text/plain" or "something-else" for all contentTypes.
Note that the alt-data type could be "something-else" even if the contentType is "text/javascript".
The preferences are saved as an nsTArray<mozilla::Tuple<nsCString, nsCString>>.
Differential Revision: https://phabricator.services.mozilla.com/D8071
--HG--
extra : rebase_source : eb4961f05a52e557e7d2d986d59e0a2cf18a3447
extra : source : dd1c31ea78c2b15d14750d137037a54d50719997
2018-10-17 16:58:30 +03:00
|
|
|
nsCString mAltDataType; // The type of the saved alt-data. May be empty.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
RefPtr<CacheFileHandle> mHandle;
|
|
|
|
RefPtr<CacheFileMetadata> mMetadata;
|
2013-09-20 13:11:26 +04:00
|
|
|
nsCOMPtr<CacheFileListener> mListener;
|
2014-01-09 03:27:32 +04:00
|
|
|
nsCOMPtr<CacheFileIOListener> mDoomAfterOpenListener;
|
2021-06-11 10:10:41 +03:00
|
|
|
Atomic<bool, Relaxed> mKill{false};
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
nsRefPtrHashtable<nsUint32HashKey, CacheFileChunk> mChunks;
|
|
|
|
nsClassHashtable<nsUint32HashKey, ChunkListeners> mChunkListeners;
|
|
|
|
nsRefPtrHashtable<nsUint32HashKey, CacheFileChunk> mCachedChunks;
|
2016-08-17 13:58:44 +03:00
|
|
|
// We can truncate data only if there is no input/output stream beyond the
|
|
|
|
// truncate position, so only unused chunks can be thrown away. But it can
|
|
|
|
// happen that we need to throw away a chunk that is still in mChunks (i.e.
|
|
|
|
// an active chunk) because deactivation happens with a small delay. We cannot
|
|
|
|
// delete such chunk immediately but we need to ensure that such chunk won't
|
|
|
|
// be returned by GetChunkLocked, so we move this chunk into mDiscardedChunks
|
|
|
|
// and mark it as discarded.
|
|
|
|
nsTArray<RefPtr<CacheFileChunk>> mDiscardedChunks;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
nsTArray<CacheFileInputStream*> mInputs;
|
2021-06-11 10:10:41 +03:00
|
|
|
CacheFileOutputStream* mOutput{nullptr};
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<nsISupports>> mObjsToRelease;
|
2013-09-20 13:11:26 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class CacheFileAutoLock {
|
|
|
|
public:
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit CacheFileAutoLock(CacheFile* aFile) : mFile(aFile), mLocked(true) {
|
2013-09-20 13:11:26 +04:00
|
|
|
mFile->Lock();
|
|
|
|
}
|
|
|
|
~CacheFileAutoLock() {
|
|
|
|
if (mLocked) mFile->Unlock();
|
|
|
|
}
|
|
|
|
void Lock() {
|
|
|
|
MOZ_ASSERT(!mLocked);
|
|
|
|
mFile->Lock();
|
|
|
|
mLocked = true;
|
|
|
|
}
|
|
|
|
void Unlock() {
|
|
|
|
MOZ_ASSERT(mLocked);
|
|
|
|
mFile->Unlock();
|
|
|
|
mLocked = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheFile> mFile;
|
2013-09-20 13:11:26 +04:00
|
|
|
bool mLocked;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
#endif
|