2014-04-10 14:47:20 +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 CacheFileContextEvictor__h__
|
|
|
|
#define CacheFileContextEvictor__h__
|
|
|
|
|
2020-02-11 19:20:08 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-04-10 14:47:20 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2021-01-25 15:23:13 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
class nsIFile;
|
|
|
|
class nsILoadContextInfo;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
class CacheIndexIterator;
|
|
|
|
|
|
|
|
struct CacheFileContextEvictorEntry {
|
|
|
|
nsCOMPtr<nsILoadContextInfo> mInfo;
|
2021-06-04 15:35:16 +03:00
|
|
|
bool mPinned = false;
|
|
|
|
nsString mOrigin; // it can be empty
|
|
|
|
PRTime mTimeStamp = 0; // in milliseconds
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndexIterator> mIterator;
|
2014-04-10 14:47:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class CacheFileContextEvictor {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheFileContextEvictor)
|
|
|
|
|
|
|
|
CacheFileContextEvictor();
|
2014-06-20 15:08:24 +04:00
|
|
|
|
|
|
|
private:
|
2014-04-10 14:47:20 +04:00
|
|
|
virtual ~CacheFileContextEvictor();
|
|
|
|
|
2014-06-20 15:08:24 +04:00
|
|
|
public:
|
2014-04-10 14:47:20 +04:00
|
|
|
nsresult Init(nsIFile* aCacheDirectory);
|
2018-01-18 15:14:00 +03:00
|
|
|
void Shutdown();
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
// Returns number of contexts that are being evicted.
|
|
|
|
uint32_t ContextsCount();
|
2018-07-20 14:57:18 +03:00
|
|
|
// Start evicting given context and an origin, if not empty.
|
|
|
|
nsresult AddContext(nsILoadContextInfo* aLoadContextInfo, bool aPinned,
|
|
|
|
const nsAString& aOrigin);
|
2014-04-10 14:47:20 +04:00
|
|
|
// CacheFileIOManager calls this method when CacheIndex's state changes. We
|
|
|
|
// check whether the index is up to date and start or stop evicting according
|
|
|
|
// to index's state.
|
2019-12-02 16:21:06 +03:00
|
|
|
void CacheIndexStateChanged();
|
2014-04-10 14:47:20 +04:00
|
|
|
// CacheFileIOManager calls this method to check whether an entry file should
|
|
|
|
// be considered as evicted. It returns true when there is a matching context
|
|
|
|
// info to the given key and the last modified time of the entry file is
|
|
|
|
// earlier than the time stamp of the time when the context was added to the
|
|
|
|
// evictor.
|
2019-12-02 16:21:06 +03:00
|
|
|
void WasEvicted(const nsACString& aKey, nsIFile* aFile,
|
|
|
|
bool* aEvictedAsPinned, bool* aEvictedAsNonPinned);
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Writes information about eviction of the given context to the disk. This is
|
|
|
|
// done for every context added to the evictor to be able to recover eviction
|
|
|
|
// after a shutdown or crash. When the context file is found after startup, we
|
|
|
|
// restore mTimeStamp from the last modified time of the file.
|
2018-07-20 14:57:18 +03:00
|
|
|
nsresult PersistEvictionInfoToDisk(nsILoadContextInfo* aLoadContextInfo,
|
|
|
|
bool aPinned, const nsAString& aOrigin);
|
2014-04-10 14:47:20 +04:00
|
|
|
// Once we are done with eviction for the given context, the eviction info is
|
|
|
|
// removed from the disk.
|
2018-07-20 14:57:18 +03:00
|
|
|
nsresult RemoveEvictInfoFromDisk(nsILoadContextInfo* aLoadContextInfo,
|
|
|
|
bool aPinned, const nsAString& aOrigin);
|
2014-04-10 14:47:20 +04:00
|
|
|
// Tries to load all contexts from the disk. This method is called just once
|
|
|
|
// after startup.
|
|
|
|
nsresult LoadEvictInfoFromDisk();
|
2015-10-22 13:11:00 +03:00
|
|
|
nsresult GetContextFile(nsILoadContextInfo* aLoadContextInfo, bool aPinned,
|
2018-07-20 14:57:18 +03:00
|
|
|
const nsAString& aOrigin, nsIFile** _retval);
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
void CreateIterators();
|
|
|
|
void CloseIterators();
|
|
|
|
void StartEvicting();
|
2019-12-02 16:21:06 +03:00
|
|
|
void EvictEntries();
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
// Whether eviction is in progress
|
2021-06-11 10:10:41 +03:00
|
|
|
bool mEvicting{false};
|
2014-04-10 14:47:20 +04:00
|
|
|
// Whether index is up to date. We wait with eviction until the index finishes
|
|
|
|
// update process when it is outdated. NOTE: We also stop eviction in progress
|
|
|
|
// when the index is found outdated, the eviction is restarted again once the
|
|
|
|
// update process finishes.
|
2021-06-11 10:10:41 +03:00
|
|
|
bool mIndexIsUpToDate{false};
|
2014-04-10 14:47:20 +04:00
|
|
|
// Whether we already tried to restore unfinished jobs from previous run after
|
|
|
|
// startup.
|
|
|
|
static bool sDiskAlreadySearched;
|
|
|
|
// Array of contexts being evicted.
|
2020-02-11 19:20:08 +03:00
|
|
|
nsTArray<UniquePtr<CacheFileContextEvictorEntry>> mEntries;
|
2014-04-10 14:47:20 +04:00
|
|
|
nsCOMPtr<nsIFile> mCacheDirectory;
|
|
|
|
nsCOMPtr<nsIFile> mEntriesDir;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
#endif
|