2013-09-20 13:11:25 +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/. */
|
|
|
|
|
2013-10-07 20:58:39 +04:00
|
|
|
#include "CacheLog.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "CacheEntry.h"
|
|
|
|
#include "CacheStorageService.h"
|
2014-02-04 04:53:00 +04:00
|
|
|
#include "CacheObserver.h"
|
2014-04-06 22:44:40 +04:00
|
|
|
#include "CacheFileUtils.h"
|
2014-05-05 21:25:58 +04:00
|
|
|
#include "CacheIndex.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsICacheEntryOpenCallback.h"
|
|
|
|
#include "nsICacheStorage.h"
|
|
|
|
#include "nsISerializable.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
2014-02-27 03:11:40 +04:00
|
|
|
#include "nsISizeOf.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsSerializationHelper.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-09-26 23:37:03 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include <math.h>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
static uint32_t const ENTRY_WANTED =
|
|
|
|
nsICacheEntryOpenCallback::ENTRY_WANTED;
|
2013-11-21 02:20:17 +04:00
|
|
|
static uint32_t const RECHECK_AFTER_WRITE_FINISHED =
|
|
|
|
nsICacheEntryOpenCallback::RECHECK_AFTER_WRITE_FINISHED;
|
2013-09-20 13:11:25 +04:00
|
|
|
static uint32_t const ENTRY_NEEDS_REVALIDATION =
|
|
|
|
nsICacheEntryOpenCallback::ENTRY_NEEDS_REVALIDATION;
|
|
|
|
static uint32_t const ENTRY_NOT_WANTED =
|
|
|
|
nsICacheEntryOpenCallback::ENTRY_NOT_WANTED;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CacheEntryHandle, nsICacheEntry)
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
// CacheEntryHandle
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
CacheEntryHandle::CacheEntryHandle(CacheEntry* aEntry)
|
2013-09-20 13:11:25 +04:00
|
|
|
: mEntry(aEntry)
|
|
|
|
{
|
2014-01-09 02:18:06 +04:00
|
|
|
#ifdef DEBUG
|
2014-04-06 22:45:18 +04:00
|
|
|
if (!mEntry->HandlesCount()) {
|
|
|
|
// CacheEntry.mHandlesCount must go from zero to one only under
|
2014-01-09 02:18:06 +04:00
|
|
|
// the service lock. Can access CacheStorageService::Self() w/o a check
|
|
|
|
// since CacheEntry hrefs it.
|
|
|
|
CacheStorageService::Self()->Lock().AssertCurrentThreadOwns();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
mEntry->AddHandleRef();
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
LOG(("New CacheEntryHandle %p for entry %p", this, aEntry));
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
CacheEntryHandle::~CacheEntryHandle()
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
2014-04-06 22:45:18 +04:00
|
|
|
mEntry->ReleaseHandleRef();
|
2014-01-07 00:24:41 +04:00
|
|
|
mEntry->OnHandleClosed(this);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
// CacheEntry::Callback
|
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
CacheEntry::Callback::Callback(CacheEntry* aEntry,
|
|
|
|
nsICacheEntryOpenCallback *aCallback,
|
2014-09-16 17:51:50 +04:00
|
|
|
bool aReadOnly, bool aCheckOnAnyThread,
|
|
|
|
bool aSecret)
|
2014-01-07 00:24:41 +04:00
|
|
|
: mEntry(aEntry)
|
|
|
|
, mCallback(aCallback)
|
2017-06-01 23:43:44 +03:00
|
|
|
, mTarget(GetCurrentThreadEventTarget())
|
2013-11-21 02:20:17 +04:00
|
|
|
, mReadOnly(aReadOnly)
|
2015-02-20 14:50:00 +03:00
|
|
|
, mRevalidating(false)
|
2013-11-21 02:20:17 +04:00
|
|
|
, mCheckOnAnyThread(aCheckOnAnyThread)
|
|
|
|
, mRecheckAfterWrite(false)
|
|
|
|
, mNotWanted(false)
|
2014-09-16 17:51:50 +04:00
|
|
|
, mSecret(aSecret)
|
2015-10-22 13:11:00 +03:00
|
|
|
, mDoomWhenFoundPinned(false)
|
|
|
|
, mDoomWhenFoundNonPinned(false)
|
2013-11-21 02:20:17 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(CacheEntry::Callback);
|
2014-01-09 02:18:06 +04:00
|
|
|
|
|
|
|
// The counter may go from zero to non-null only under the service lock
|
|
|
|
// but here we expect it to be already positive.
|
2014-04-06 22:45:18 +04:00
|
|
|
MOZ_ASSERT(mEntry->HandlesCount());
|
|
|
|
mEntry->AddHandleRef();
|
2013-11-21 02:20:17 +04:00
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
CacheEntry::Callback::Callback(CacheEntry* aEntry, bool aDoomWhenFoundInPinStatus)
|
|
|
|
: mEntry(aEntry)
|
|
|
|
, mReadOnly(false)
|
|
|
|
, mRevalidating(false)
|
|
|
|
, mCheckOnAnyThread(true)
|
|
|
|
, mRecheckAfterWrite(false)
|
|
|
|
, mNotWanted(false)
|
|
|
|
, mSecret(false)
|
|
|
|
, mDoomWhenFoundPinned(aDoomWhenFoundInPinStatus == true)
|
|
|
|
, mDoomWhenFoundNonPinned(aDoomWhenFoundInPinStatus == false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(CacheEntry::Callback);
|
|
|
|
MOZ_ASSERT(mEntry->HandlesCount());
|
|
|
|
mEntry->AddHandleRef();
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
CacheEntry::Callback::Callback(CacheEntry::Callback const &aThat)
|
2014-01-07 00:24:41 +04:00
|
|
|
: mEntry(aThat.mEntry)
|
|
|
|
, mCallback(aThat.mCallback)
|
2017-06-01 23:43:44 +03:00
|
|
|
, mTarget(aThat.mTarget)
|
2013-11-21 02:20:17 +04:00
|
|
|
, mReadOnly(aThat.mReadOnly)
|
2015-02-20 14:50:00 +03:00
|
|
|
, mRevalidating(aThat.mRevalidating)
|
2013-11-21 02:20:17 +04:00
|
|
|
, mCheckOnAnyThread(aThat.mCheckOnAnyThread)
|
|
|
|
, mRecheckAfterWrite(aThat.mRecheckAfterWrite)
|
|
|
|
, mNotWanted(aThat.mNotWanted)
|
2014-09-16 17:51:50 +04:00
|
|
|
, mSecret(aThat.mSecret)
|
2015-10-22 13:11:00 +03:00
|
|
|
, mDoomWhenFoundPinned(aThat.mDoomWhenFoundPinned)
|
|
|
|
, mDoomWhenFoundNonPinned(aThat.mDoomWhenFoundNonPinned)
|
2013-11-21 02:20:17 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(CacheEntry::Callback);
|
2014-01-09 02:18:06 +04:00
|
|
|
|
|
|
|
// The counter may go from zero to non-null only under the service lock
|
|
|
|
// but here we expect it to be already positive.
|
2014-04-06 22:45:18 +04:00
|
|
|
MOZ_ASSERT(mEntry->HandlesCount());
|
|
|
|
mEntry->AddHandleRef();
|
2013-11-21 02:20:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheEntry::Callback::~Callback()
|
|
|
|
{
|
2017-06-01 23:43:44 +03:00
|
|
|
ProxyRelease(mCallback, mTarget);
|
2014-04-04 16:42:05 +04:00
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
mEntry->ReleaseHandleRef();
|
2013-11-21 02:20:17 +04:00
|
|
|
MOZ_COUNT_DTOR(CacheEntry::Callback);
|
|
|
|
}
|
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
void CacheEntry::Callback::ExchangeEntry(CacheEntry* aEntry)
|
|
|
|
{
|
|
|
|
if (mEntry == aEntry)
|
|
|
|
return;
|
|
|
|
|
2014-01-09 02:18:06 +04:00
|
|
|
// The counter may go from zero to non-null only under the service lock
|
|
|
|
// but here we expect it to be already positive.
|
2014-04-06 22:45:18 +04:00
|
|
|
MOZ_ASSERT(aEntry->HandlesCount());
|
|
|
|
aEntry->AddHandleRef();
|
|
|
|
mEntry->ReleaseHandleRef();
|
2014-01-07 00:24:41 +04:00
|
|
|
mEntry = aEntry;
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
bool CacheEntry::Callback::DeferDoom(bool *aDoom) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mEntry->mPinningKnown);
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(mDoomWhenFoundNonPinned) || MOZ_UNLIKELY(mDoomWhenFoundPinned)) {
|
|
|
|
*aDoom = (MOZ_UNLIKELY(mDoomWhenFoundNonPinned) && MOZ_LIKELY(!mEntry->mPinned)) ||
|
|
|
|
(MOZ_UNLIKELY(mDoomWhenFoundPinned) && MOZ_UNLIKELY(mEntry->mPinned));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-05 21:32:04 +04:00
|
|
|
nsresult CacheEntry::Callback::OnCheckThread(bool *aOnCheckThread) const
|
2013-11-21 02:20:17 +04:00
|
|
|
{
|
|
|
|
if (!mCheckOnAnyThread) {
|
|
|
|
// Check we are on the target
|
2017-06-01 23:43:44 +03:00
|
|
|
return mTarget->IsOnCurrentThread(aOnCheckThread);
|
2013-11-21 02:20:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We can invoke check anywhere
|
|
|
|
*aOnCheckThread = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-05 21:32:04 +04:00
|
|
|
nsresult CacheEntry::Callback::OnAvailThread(bool *aOnAvailThread) const
|
2013-11-21 02:20:17 +04:00
|
|
|
{
|
2017-06-01 23:43:44 +03:00
|
|
|
return mTarget->IsOnCurrentThread(aOnAvailThread);
|
2013-11-21 02:20:17 +04:00
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// CacheEntry
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CacheEntry,
|
|
|
|
nsICacheEntry,
|
|
|
|
nsIRunnable,
|
|
|
|
CacheFileListener)
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
CacheEntry::CacheEntry(const nsACString& aStorageID,
|
2016-05-12 13:38:00 +03:00
|
|
|
const nsACString& aURI,
|
2013-09-20 13:11:25 +04:00
|
|
|
const nsACString& aEnhanceID,
|
2015-09-04 02:05:42 +03:00
|
|
|
bool aUseDisk,
|
2015-10-22 13:11:00 +03:00
|
|
|
bool aSkipSizeCheck,
|
|
|
|
bool aPin)
|
2013-09-20 13:11:25 +04:00
|
|
|
: mFrecency(0)
|
|
|
|
, mSortingExpirationTime(uint32_t(-1))
|
|
|
|
, mLock("CacheEntry")
|
|
|
|
, mFileStatus(NS_ERROR_NOT_INITIALIZED)
|
|
|
|
, mURI(aURI)
|
|
|
|
, mEnhanceID(aEnhanceID)
|
|
|
|
, mStorageID(aStorageID)
|
|
|
|
, mUseDisk(aUseDisk)
|
2015-09-04 02:05:42 +03:00
|
|
|
, mSkipSizeCheck(aSkipSizeCheck)
|
2016-06-30 13:24:54 +03:00
|
|
|
, mIsDoomed(false)
|
2013-09-20 13:11:25 +04:00
|
|
|
, mSecurityInfoLoaded(false)
|
|
|
|
, mPreventCallbacks(false)
|
|
|
|
, mHasData(false)
|
2015-10-22 13:11:00 +03:00
|
|
|
, mPinned(aPin)
|
|
|
|
, mPinningKnown(false)
|
2013-09-20 13:11:25 +04:00
|
|
|
, mState(NOTLOADED)
|
2013-10-02 13:30:42 +04:00
|
|
|
, mRegistration(NEVERREGISTERED)
|
2013-09-20 13:11:25 +04:00
|
|
|
, mWriter(nullptr)
|
|
|
|
, mPredictedDataSize(0)
|
2014-07-08 18:48:00 +04:00
|
|
|
, mUseCount(0)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
2016-05-12 13:38:00 +03:00
|
|
|
LOG(("CacheEntry::CacheEntry [this=%p]", this));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
mService = CacheStorageService::Self();
|
|
|
|
|
|
|
|
CacheStorageService::Self()->RecordMemoryOnlyEntry(
|
|
|
|
this, !aUseDisk, true /* overwrite */);
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheEntry::~CacheEntry()
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::~CacheEntry [this=%p]", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
char const * CacheEntry::StateString(uint32_t aState)
|
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case NOTLOADED: return "NOTLOADED";
|
|
|
|
case LOADING: return "LOADING";
|
|
|
|
case EMPTY: return "EMPTY";
|
|
|
|
case WRITING: return "WRITING";
|
|
|
|
case READY: return "READY";
|
|
|
|
case REVALIDATING: return "REVALIDATING";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
|
2014-07-08 18:48:00 +04:00
|
|
|
nsresult CacheEntry::HashingKeyWithStorage(nsACString &aResult) const
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
return HashingKey(mStorageID, mEnhanceID, mURI, aResult);
|
|
|
|
}
|
|
|
|
|
2014-07-08 18:48:00 +04:00
|
|
|
nsresult CacheEntry::HashingKey(nsACString &aResult) const
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
return HashingKey(EmptyCString(), mEnhanceID, mURI, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheEntry::HashingKey(nsCSubstring const& aStorageID,
|
|
|
|
nsCSubstring const& aEnhanceID,
|
|
|
|
nsIURI* aURI,
|
|
|
|
nsACString &aResult)
|
2014-04-06 22:44:40 +04:00
|
|
|
{
|
|
|
|
nsAutoCString spec;
|
|
|
|
nsresult rv = aURI->GetAsciiSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return HashingKey(aStorageID, aEnhanceID, spec, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheEntry::HashingKey(nsCSubstring const& aStorageID,
|
|
|
|
nsCSubstring const& aEnhanceID,
|
|
|
|
nsCSubstring const& aURISpec,
|
|
|
|
nsACString &aResult)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This key is used to salt hash that is a base for disk file name.
|
|
|
|
* Changing it will cause we will not be able to find files on disk.
|
|
|
|
*/
|
|
|
|
|
2015-03-05 16:42:00 +03:00
|
|
|
aResult.Assign(aStorageID);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-04-06 22:44:40 +04:00
|
|
|
if (!aEnhanceID.IsEmpty()) {
|
|
|
|
CacheFileUtils::AppendTagWithValue(aResult, '~', aEnhanceID);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-04-06 22:44:40 +04:00
|
|
|
// Appending directly
|
|
|
|
aResult.Append(':');
|
|
|
|
aResult.Append(aURISpec);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheEntry::AsyncOpen(nsICacheEntryOpenCallback* aCallback, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::AsyncOpen [this=%p, state=%s, flags=%d, callback=%p]",
|
|
|
|
this, StateString(mState), aFlags, aCallback));
|
|
|
|
|
|
|
|
bool readonly = aFlags & nsICacheStorage::OPEN_READONLY;
|
2014-05-15 01:12:38 +04:00
|
|
|
bool bypassIfBusy = aFlags & nsICacheStorage::OPEN_BYPASS_IF_BUSY;
|
2013-09-20 13:11:25 +04:00
|
|
|
bool truncate = aFlags & nsICacheStorage::OPEN_TRUNCATE;
|
|
|
|
bool priority = aFlags & nsICacheStorage::OPEN_PRIORITY;
|
2013-11-21 02:20:17 +04:00
|
|
|
bool multithread = aFlags & nsICacheStorage::CHECK_MULTITHREADED;
|
2014-09-16 17:51:50 +04:00
|
|
|
bool secret = aFlags & nsICacheStorage::OPEN_SECRETLY;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!readonly || !truncate, "Bad flags combination");
|
|
|
|
MOZ_ASSERT(!(truncate && mState > LOADING), "Must not call truncate on already loaded entry");
|
|
|
|
|
2014-09-16 17:51:50 +04:00
|
|
|
Callback callback(this, aCallback, readonly, multithread, secret);
|
2013-11-21 02:20:17 +04:00
|
|
|
|
2014-07-02 20:16:21 +04:00
|
|
|
if (!Open(callback, truncate, priority, bypassIfBusy)) {
|
|
|
|
// We get here when the callback wants to bypass cache when it's busy.
|
|
|
|
LOG((" writing or revalidating, callback wants to bypass cache"));
|
|
|
|
callback.mNotWanted = true;
|
|
|
|
InvokeAvailableCallback(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CacheEntry::Open(Callback & aCallback, bool aTruncate,
|
|
|
|
bool aPriority, bool aBypassIfBusy)
|
|
|
|
{
|
2013-09-20 13:11:25 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2014-07-02 20:16:21 +04:00
|
|
|
// Check state under the lock
|
|
|
|
if (aBypassIfBusy && (mState == WRITING || mState == REVALIDATING)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RememberCallback(aCallback);
|
2013-11-21 02:20:18 +04:00
|
|
|
|
|
|
|
// Load() opens the lock
|
2014-07-02 20:16:21 +04:00
|
|
|
if (Load(aTruncate, aPriority)) {
|
2013-11-21 02:20:18 +04:00
|
|
|
// Loading is in progress...
|
2014-07-02 20:16:21 +04:00
|
|
|
return true;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
2013-11-21 02:20:18 +04:00
|
|
|
|
|
|
|
InvokeCallbacks();
|
2014-07-02 20:16:21 +04:00
|
|
|
|
|
|
|
return true;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CacheEntry::Load(bool aTruncate, bool aPriority)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::Load [this=%p, trunc=%d]", this, aTruncate));
|
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (mState > LOADING) {
|
|
|
|
LOG((" already loaded"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == LOADING) {
|
|
|
|
LOG((" already loading"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:18 +04:00
|
|
|
mState = LOADING;
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
MOZ_ASSERT(!mFile);
|
|
|
|
|
2014-05-05 21:25:58 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString fileKey;
|
|
|
|
rv = HashingKeyWithStorage(fileKey);
|
|
|
|
|
2015-03-30 22:08:52 +03:00
|
|
|
bool reportMiss = false;
|
|
|
|
|
2014-06-25 20:42:50 +04:00
|
|
|
// Check the index under two conditions for two states and take appropriate action:
|
|
|
|
// 1. When this is a disk entry and not told to truncate, check there is a disk file.
|
|
|
|
// If not, set the 'truncate' flag to true so that this entry will open instantly
|
|
|
|
// as a new one.
|
|
|
|
// 2. When this is a memory-only entry, check there is a disk file.
|
|
|
|
// If there is or could be, doom that file.
|
2015-07-31 08:57:50 +03:00
|
|
|
if ((!aTruncate || !mUseDisk) && NS_SUCCEEDED(rv)) {
|
2014-05-05 21:25:58 +04:00
|
|
|
// Check the index right now to know we have or have not the entry
|
|
|
|
// as soon as possible.
|
|
|
|
CacheIndex::EntryStatus status;
|
2014-06-25 20:42:50 +04:00
|
|
|
if (NS_SUCCEEDED(CacheIndex::HasEntry(fileKey, &status))) {
|
|
|
|
switch (status) {
|
|
|
|
case CacheIndex::DOES_NOT_EXIST:
|
2015-10-22 13:11:00 +03:00
|
|
|
// Doesn't apply to memory-only entries, Load() is called only once for them
|
|
|
|
// and never again for their session lifetime.
|
2015-03-30 22:08:52 +03:00
|
|
|
if (!aTruncate && mUseDisk) {
|
2015-10-22 13:11:00 +03:00
|
|
|
LOG((" entry doesn't exist according information from the index, truncating"));
|
2015-03-30 22:08:52 +03:00
|
|
|
reportMiss = true;
|
2015-10-22 13:11:00 +03:00
|
|
|
aTruncate = true;
|
2015-03-30 22:08:52 +03:00
|
|
|
}
|
2014-06-25 20:42:50 +04:00
|
|
|
break;
|
|
|
|
case CacheIndex::EXISTS:
|
|
|
|
case CacheIndex::DO_NOT_KNOW:
|
|
|
|
if (!mUseDisk) {
|
2015-10-22 13:11:00 +03:00
|
|
|
LOG((" entry open as memory-only, but there is a file, status=%d, dooming it", status));
|
2014-06-25 20:42:50 +04:00
|
|
|
CacheFileIOManager::DoomFileByKey(fileKey, nullptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-05 21:25:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:25:59 +04:00
|
|
|
mFile = new CacheFile();
|
|
|
|
|
|
|
|
BackgroundOp(Ops::REGISTER);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
bool directLoad = aTruncate || !mUseDisk;
|
2014-07-08 18:48:00 +04:00
|
|
|
if (directLoad) {
|
|
|
|
// mLoadStart will be used to calculate telemetry of life-time of this entry.
|
|
|
|
// Low resulution is then enough.
|
|
|
|
mLoadStart = TimeStamp::NowLoRes();
|
2015-10-22 13:11:00 +03:00
|
|
|
mPinningKnown = true;
|
2014-07-08 18:48:00 +04:00
|
|
|
} else {
|
2013-09-26 23:37:03 +04:00
|
|
|
mLoadStart = TimeStamp::Now();
|
2014-07-08 18:48:00 +04:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2013-11-21 02:20:18 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoUnlock unlock(mLock);
|
|
|
|
|
2015-03-30 22:08:52 +03:00
|
|
|
if (reportMiss) {
|
|
|
|
CacheFileUtils::DetailedCacheHitTelemetry::AddRecord(
|
|
|
|
CacheFileUtils::DetailedCacheHitTelemetry::MISS, mLoadStart);
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:18 +04:00
|
|
|
LOG((" performing load, file=%p", mFile.get()));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mFile->Init(fileKey,
|
|
|
|
aTruncate,
|
|
|
|
!mUseDisk,
|
2015-09-04 02:05:42 +03:00
|
|
|
mSkipSizeCheck,
|
2013-11-21 02:20:18 +04:00
|
|
|
aPriority,
|
2015-10-22 13:11:00 +03:00
|
|
|
mPinned,
|
2013-11-21 02:20:18 +04:00
|
|
|
directLoad ? nullptr : this);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2013-11-21 02:20:18 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mFileStatus = rv;
|
|
|
|
AsyncDoom(nullptr);
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:18 +04:00
|
|
|
if (directLoad) {
|
|
|
|
// Just fake the load has already been done as "new".
|
2015-02-13 15:58:00 +03:00
|
|
|
mFileStatus = NS_OK;
|
2013-11-21 02:20:18 +04:00
|
|
|
mState = EMPTY;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mState == LOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::OnFileReady(nsresult aResult, bool aIsNew)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheEntry::OnFileReady [this=%p, rv=0x%08" PRIx32 ", new=%d]",
|
|
|
|
this, static_cast<uint32_t>(aResult), aIsNew));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2013-09-26 23:37:03 +04:00
|
|
|
MOZ_ASSERT(!mLoadStart.IsNull());
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aResult)) {
|
|
|
|
if (aIsNew) {
|
2015-02-23 14:26:06 +03:00
|
|
|
CacheFileUtils::DetailedCacheHitTelemetry::AddRecord(
|
|
|
|
CacheFileUtils::DetailedCacheHitTelemetry::MISS, mLoadStart);
|
|
|
|
} else {
|
|
|
|
CacheFileUtils::DetailedCacheHitTelemetry::AddRecord(
|
|
|
|
CacheFileUtils::DetailedCacheHitTelemetry::HIT, mLoadStart);
|
2013-09-26 23:37:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// OnFileReady, that is the only code that can transit from LOADING
|
2015-10-22 13:11:00 +03:00
|
|
|
// to any follow-on state and can only be invoked ones on an entry.
|
|
|
|
// Until this moment there is no consumer that could manipulate
|
|
|
|
// the entry state.
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == LOADING);
|
|
|
|
|
|
|
|
mState = (aIsNew || NS_FAILED(aResult))
|
|
|
|
? EMPTY
|
|
|
|
: READY;
|
|
|
|
|
|
|
|
mFileStatus = aResult;
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
mPinned = mFile->IsPinned();;
|
|
|
|
mPinningKnown = true;
|
|
|
|
LOG((" pinning=%d", mPinned));
|
|
|
|
|
2014-01-09 03:27:33 +04:00
|
|
|
if (mState == READY) {
|
2013-09-20 13:11:25 +04:00
|
|
|
mHasData = true;
|
|
|
|
|
2014-01-09 03:27:33 +04:00
|
|
|
uint32_t frecency;
|
|
|
|
mFile->GetFrecency(&frecency);
|
|
|
|
// mFrecency is held in a double to increase computance precision.
|
|
|
|
// It is ok to persist frecency only as a uint32 with some math involved.
|
|
|
|
mFrecency = INT2FRECENCY(frecency);
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
InvokeCallbacks();
|
2015-10-22 13:11:00 +03:00
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::OnFileDoomed(nsresult aResult)
|
|
|
|
{
|
|
|
|
if (mDoomCallback) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DoomCallbackRunnable> event =
|
2013-09-20 13:11:25 +04:00
|
|
|
new DoomCallbackRunnable(this, aResult);
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-04 04:52:59 +04:00
|
|
|
already_AddRefed<CacheEntryHandle> CacheEntry::ReopenTruncated(bool aMemoryOnly,
|
|
|
|
nsICacheEntryOpenCallback* aCallback)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
LOG(("CacheEntry::ReopenTruncated [this=%p]", this));
|
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
// Hold callbacks invocation, AddStorageEntry would invoke from doom prematurly
|
|
|
|
mPreventCallbacks = true;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntryHandle> handle;
|
|
|
|
RefPtr<CacheEntry> newEntry;
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
2015-10-22 13:11:00 +03:00
|
|
|
if (mPinned) {
|
|
|
|
MOZ_ASSERT(mUseDisk);
|
|
|
|
// We want to pin even no-store entries (the case we recreate a disk entry as
|
|
|
|
// a memory-only entry.)
|
|
|
|
aMemoryOnly = false;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
mozilla::MutexAutoUnlock unlock(mLock);
|
|
|
|
|
|
|
|
// The following call dooms this entry (calls DoomAlreadyRemoved on us)
|
|
|
|
nsresult rv = CacheStorageService::Self()->AddStorageEntry(
|
|
|
|
GetStorageID(), GetURI(), GetEnhanceID(),
|
2014-02-04 04:52:59 +04:00
|
|
|
mUseDisk && !aMemoryOnly,
|
2015-09-04 02:05:42 +03:00
|
|
|
mSkipSizeCheck,
|
2015-10-22 13:11:00 +03:00
|
|
|
mPinned,
|
2013-09-20 13:11:25 +04:00
|
|
|
true, // truncate existing (this one)
|
2014-01-07 00:24:41 +04:00
|
|
|
getter_AddRefs(handle));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-01-07 00:24:41 +04:00
|
|
|
newEntry = handle->Entry();
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" exchanged entry %p by entry %p, rv=0x%08" PRIx32,
|
|
|
|
this, newEntry.get(), static_cast<uint32_t>(rv)));
|
2013-09-20 13:11:25 +04:00
|
|
|
newEntry->AsyncOpen(aCallback, nsICacheStorage::OPEN_TRUNCATE);
|
2014-03-17 21:21:39 +04:00
|
|
|
} else {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" exchanged of entry %p failed, rv=0x%08" PRIx32,
|
|
|
|
this, static_cast<uint32_t>(rv)));
|
2013-09-20 13:11:25 +04:00
|
|
|
AsyncDoom(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mPreventCallbacks = false;
|
|
|
|
|
|
|
|
if (!newEntry)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
newEntry->TransferCallbacks(*this);
|
|
|
|
mCallbacks.Clear();
|
|
|
|
|
2014-05-06 22:55:50 +04:00
|
|
|
// Must return a new write handle, since the consumer is expected to
|
|
|
|
// write to this newly recreated entry. The |handle| is only a common
|
|
|
|
// reference counter and doesn't revert entry state back when write
|
|
|
|
// fails and also doesn't update the entry frecency. Not updating
|
|
|
|
// frecency causes entries to not be purged from our memory pools.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntryHandle> writeHandle =
|
2014-05-06 22:55:50 +04:00
|
|
|
newEntry->NewWriteHandle();
|
|
|
|
return writeHandle.forget();
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
void CacheEntry::TransferCallbacks(CacheEntry & aFromEntry)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
LOG(("CacheEntry::TransferCallbacks [entry=%p, from=%p]",
|
|
|
|
this, &aFromEntry));
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (!mCallbacks.Length())
|
|
|
|
mCallbacks.SwapElements(aFromEntry.mCallbacks);
|
|
|
|
else
|
|
|
|
mCallbacks.AppendElements(aFromEntry.mCallbacks);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
uint32_t callbacksLength = mCallbacks.Length();
|
|
|
|
if (callbacksLength) {
|
2017-04-03 19:23:00 +03:00
|
|
|
// Carry the entry reference (unfortunately, needs to be done manually...)
|
2014-01-07 00:24:41 +04:00
|
|
|
for (uint32_t i = 0; i < callbacksLength; ++i)
|
|
|
|
mCallbacks[i].ExchangeEntry(this);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
BackgroundOp(Ops::CALLBACKS, true);
|
2014-01-07 00:24:41 +04:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-07-02 20:16:21 +04:00
|
|
|
void CacheEntry::RememberCallback(Callback & aCallback)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
2014-05-15 01:12:38 +04:00
|
|
|
LOG(("CacheEntry::RememberCallback [this=%p, cb=%p, state=%s]",
|
|
|
|
this, aCallback.mCallback.get(), StateString(mState)));
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
mCallbacks.AppendElement(aCallback);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
void CacheEntry::InvokeCallbacksLock()
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
InvokeCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheEntry::InvokeCallbacks()
|
|
|
|
{
|
2013-11-21 02:20:17 +04:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG(("CacheEntry::InvokeCallbacks BEGIN [this=%p]", this));
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
// Invoke first all r/w callbacks, then all r/o callbacks.
|
|
|
|
if (InvokeCallbacks(false))
|
|
|
|
InvokeCallbacks(true);
|
|
|
|
|
|
|
|
LOG(("CacheEntry::InvokeCallbacks END [this=%p]", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CacheEntry::InvokeCallbacks(bool aReadOnly)
|
|
|
|
{
|
2013-09-20 13:11:25 +04:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
RefPtr<CacheEntryHandle> recreatedHandle;
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
uint32_t i = 0;
|
|
|
|
while (i < mCallbacks.Length()) {
|
2013-09-20 13:11:25 +04:00
|
|
|
if (mPreventCallbacks) {
|
2013-11-21 02:20:17 +04:00
|
|
|
LOG((" callbacks prevented!"));
|
|
|
|
return false;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (!mIsDoomed && (mState == WRITING || mState == REVALIDATING)) {
|
|
|
|
LOG((" entry is being written/revalidated"));
|
|
|
|
return false;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
bool recreate;
|
|
|
|
if (mCallbacks[i].DeferDoom(&recreate)) {
|
|
|
|
mCallbacks.RemoveElementAt(i);
|
|
|
|
if (!recreate) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG((" defer doom marker callback hit positive, recreating"));
|
|
|
|
recreatedHandle = ReopenTruncated(!mUseDisk, nullptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (mCallbacks[i].mReadOnly != aReadOnly) {
|
|
|
|
// Callback is not r/w or r/o, go to another one in line
|
|
|
|
++i;
|
|
|
|
continue;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
bool onCheckThread;
|
|
|
|
nsresult rv = mCallbacks[i].OnCheckThread(&onCheckThread);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !onCheckThread) {
|
|
|
|
// Redispatch to the target thread
|
2017-06-01 23:43:44 +03:00
|
|
|
rv = mCallbacks[i].mTarget->Dispatch(NewRunnableMethod(this,
|
|
|
|
&CacheEntry::InvokeCallbacksLock),
|
|
|
|
nsIEventTarget::DISPATCH_NORMAL);
|
2013-11-21 02:20:17 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
LOG((" re-dispatching to target thread"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
Callback callback = mCallbacks[i];
|
|
|
|
mCallbacks.RemoveElementAt(i);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && !InvokeCallback(callback)) {
|
|
|
|
// Callback didn't fire, put it back and go to another one in line.
|
|
|
|
// Only reason InvokeCallback returns false is that onCacheEntryCheck
|
|
|
|
// returns RECHECK_AFTER_WRITE_FINISHED. If we would stop the loop, other
|
|
|
|
// readers or potential writers would be unnecessarily kept from being
|
|
|
|
// invoked.
|
2015-01-22 15:51:00 +03:00
|
|
|
size_t pos = std::min(mCallbacks.Length(), static_cast<size_t>(i));
|
|
|
|
mCallbacks.InsertElementAt(pos, callback);
|
2013-11-21 02:20:17 +04:00
|
|
|
++i;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
if (recreatedHandle) {
|
|
|
|
// Must be released outside of the lock, enters InvokeCallback on the new entry
|
|
|
|
mozilla::MutexAutoUnlock unlock(mLock);
|
|
|
|
recreatedHandle = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
return true;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
bool CacheEntry::InvokeCallback(Callback & aCallback)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
LOG(("CacheEntry::InvokeCallback [this=%p, state=%s, cb=%p]",
|
2013-11-21 02:20:17 +04:00
|
|
|
this, StateString(mState), aCallback.mCallback.get()));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
// When this entry is doomed we want to notify the callback any time
|
2013-09-20 13:11:25 +04:00
|
|
|
if (!mIsDoomed) {
|
|
|
|
// When we are here, the entry must be loaded from disk
|
|
|
|
MOZ_ASSERT(mState > LOADING);
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (mState == WRITING || mState == REVALIDATING) {
|
2013-09-20 13:11:25 +04:00
|
|
|
// Prevent invoking other callbacks since one of them is now writing
|
|
|
|
// or revalidating this entry. No consumers should get this entry
|
|
|
|
// until metadata are filled with values downloaded from the server
|
|
|
|
// or the entry revalidated and output stream has been opened.
|
|
|
|
LOG((" entry is being written/revalidated, callback bypassed"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
// mRecheckAfterWrite flag already set means the callback has already passed
|
|
|
|
// the onCacheEntryCheck call. Until the current write is not finished this
|
|
|
|
// callback will be bypassed.
|
2014-01-08 23:36:57 +04:00
|
|
|
if (!aCallback.mRecheckAfterWrite) {
|
|
|
|
|
|
|
|
if (!aCallback.mReadOnly) {
|
|
|
|
if (mState == EMPTY) {
|
|
|
|
// Advance to writing state, we expect to invoke the callback and let
|
|
|
|
// it fill content of this entry. Must set and check the state here
|
|
|
|
// to prevent more then one
|
|
|
|
mState = WRITING;
|
|
|
|
LOG((" advancing to WRITING state"));
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-01-08 23:36:57 +04:00
|
|
|
if (!aCallback.mCallback) {
|
|
|
|
// We can be given no callback only in case of recreate, it is ok
|
|
|
|
// to advance to WRITING state since the caller of recreate is expected
|
|
|
|
// to write this entry now.
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == READY) {
|
|
|
|
// Metadata present, validate the entry
|
|
|
|
uint32_t checkResult;
|
|
|
|
{
|
|
|
|
// mayhemer: TODO check and solve any potential races of concurent OnCacheEntryCheck
|
|
|
|
mozilla::MutexAutoUnlock unlock(mLock);
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
nsresult rv = aCallback.mCallback->OnCacheEntryCheck(
|
|
|
|
this, nullptr, &checkResult);
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" OnCacheEntryCheck: rv=0x%08" PRIx32 ", result=%" PRId32,
|
|
|
|
static_cast<uint32_t>(rv), static_cast<uint32_t>(checkResult)));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
2013-11-21 02:20:19 +04:00
|
|
|
checkResult = ENTRY_NOT_WANTED;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2015-02-20 14:50:00 +03:00
|
|
|
aCallback.mRevalidating = checkResult == ENTRY_NEEDS_REVALIDATION;
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
switch (checkResult) {
|
|
|
|
case ENTRY_WANTED:
|
|
|
|
// Nothing more to do here, the consumer is responsible to handle
|
|
|
|
// the result of OnCacheEntryCheck it self.
|
|
|
|
// Proceed to callback...
|
|
|
|
break;
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
case RECHECK_AFTER_WRITE_FINISHED:
|
|
|
|
LOG((" consumer will check on the entry again after write is done"));
|
|
|
|
// The consumer wants the entry to complete first.
|
|
|
|
aCallback.mRecheckAfterWrite = true;
|
|
|
|
break;
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
case ENTRY_NEEDS_REVALIDATION:
|
|
|
|
LOG((" will be holding callbacks until entry is revalidated"));
|
|
|
|
// State is READY now and from that state entry cannot transit to any other
|
|
|
|
// state then REVALIDATING for which cocurrency is not an issue. Potentially
|
|
|
|
// no need to lock here.
|
|
|
|
mState = REVALIDATING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENTRY_NOT_WANTED:
|
|
|
|
LOG((" consumer not interested in the entry"));
|
|
|
|
// Do not give this entry to the consumer, it is not interested in us.
|
2013-11-21 02:20:17 +04:00
|
|
|
aCallback.mNotWanted = true;
|
2013-09-20 13:11:25 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (aCallback.mCallback) {
|
|
|
|
if (!mIsDoomed && aCallback.mRecheckAfterWrite) {
|
|
|
|
// If we don't have data and the callback wants a complete entry,
|
|
|
|
// don't invoke now.
|
|
|
|
bool bypass = !mHasData;
|
2014-06-24 20:08:36 +04:00
|
|
|
if (!bypass && NS_SUCCEEDED(mFileStatus)) {
|
2013-11-21 02:20:17 +04:00
|
|
|
int64_t _unused;
|
|
|
|
bypass = !mFile->DataSize(&_unused);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bypass) {
|
|
|
|
LOG((" bypassing, entry data still being written"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry is complete now, do the check+avail call again
|
|
|
|
aCallback.mRecheckAfterWrite = false;
|
|
|
|
return InvokeCallback(aCallback);
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
mozilla::MutexAutoUnlock unlock(mLock);
|
2013-11-21 02:20:17 +04:00
|
|
|
InvokeAvailableCallback(aCallback);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-05 21:32:04 +04:00
|
|
|
void CacheEntry::InvokeAvailableCallback(Callback const & aCallback)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
LOG(("CacheEntry::InvokeAvailableCallback [this=%p, state=%s, cb=%p, r/o=%d, n/w=%d]",
|
2013-11-21 02:20:17 +04:00
|
|
|
this, StateString(mState), aCallback.mCallback.get(), aCallback.mReadOnly, aCallback.mNotWanted));
|
|
|
|
|
|
|
|
nsresult rv;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
uint32_t const state = mState;
|
|
|
|
|
|
|
|
// When we are here, the entry must be loaded from disk
|
|
|
|
MOZ_ASSERT(state > LOADING || mIsDoomed);
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
bool onAvailThread;
|
|
|
|
rv = aCallback.OnAvailThread(&onAvailThread);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG((" target thread dead?"));
|
2013-09-20 13:11:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (!onAvailThread) {
|
|
|
|
// Dispatch to the right thread
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AvailableCallbackRunnable> event =
|
2013-11-21 02:20:17 +04:00
|
|
|
new AvailableCallbackRunnable(this, aCallback);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2017-06-01 23:43:44 +03:00
|
|
|
rv = aCallback.mTarget->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" redispatched, (rv = 0x%08" PRIx32 ")", static_cast<uint32_t>(rv)));
|
2013-11-21 02:20:17 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsDoomed || aCallback.mNotWanted) {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG((" doomed or not wanted, notifying OCEA with NS_ERROR_CACHE_KEY_NOT_FOUND"));
|
2013-11-21 02:20:17 +04:00
|
|
|
aCallback.mCallback->OnCacheEntryAvailable(
|
|
|
|
nullptr, false, nullptr, NS_ERROR_CACHE_KEY_NOT_FOUND);
|
2013-09-20 13:11:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == READY) {
|
|
|
|
LOG((" ready/has-meta, notifying OCEA with entry and NS_OK"));
|
2014-09-16 17:51:50 +04:00
|
|
|
|
|
|
|
if (!aCallback.mSecret)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
BackgroundOp(Ops::FRECENCYUPDATE);
|
|
|
|
}
|
|
|
|
|
2016-08-09 18:19:00 +03:00
|
|
|
OnFetched(aCallback);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntryHandle> handle = NewHandle();
|
2013-11-21 02:20:17 +04:00
|
|
|
aCallback.mCallback->OnCacheEntryAvailable(
|
2014-01-07 00:24:41 +04:00
|
|
|
handle, false, nullptr, NS_OK);
|
2013-09-20 13:11:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-20 14:50:00 +03:00
|
|
|
// R/O callbacks may do revalidation, let them fall through
|
|
|
|
if (aCallback.mReadOnly && !aCallback.mRevalidating) {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG((" r/o and not ready, notifying OCEA with NS_ERROR_CACHE_KEY_NOT_FOUND"));
|
2013-11-21 02:20:17 +04:00
|
|
|
aCallback.mCallback->OnCacheEntryAvailable(
|
|
|
|
nullptr, false, nullptr, NS_ERROR_CACHE_KEY_NOT_FOUND);
|
2013-09-20 13:11:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a new or potentially non-valid entry and needs to be fetched first.
|
2014-01-07 00:24:41 +04:00
|
|
|
// The CacheEntryHandle blocks other consumers until the channel
|
2013-09-20 13:11:25 +04:00
|
|
|
// either releases the entry or marks metadata as filled or whole entry valid,
|
|
|
|
// i.e. until MetaDataReady() or SetValid() on the entry is called respectively.
|
|
|
|
|
|
|
|
// Consumer will be responsible to fill or validate the entry metadata and data.
|
|
|
|
|
2016-08-09 18:19:00 +03:00
|
|
|
OnFetched(aCallback);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntryHandle> handle = NewWriteHandle();
|
2013-11-21 02:20:17 +04:00
|
|
|
rv = aCallback.mCallback->OnCacheEntryAvailable(
|
|
|
|
handle, state == WRITING, nullptr, NS_OK);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" writing/revalidating failed (0x%08" PRIx32 ")", static_cast<uint32_t>(rv)));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
// Consumer given a new entry failed to take care of the entry.
|
2014-01-07 00:24:41 +04:00
|
|
|
OnHandleClosed(handle);
|
2013-09-20 13:11:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG((" writing/revalidating"));
|
|
|
|
}
|
|
|
|
|
2016-08-09 18:19:00 +03:00
|
|
|
void CacheEntry::OnFetched(Callback const & aCallback)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(mFileStatus) && !aCallback.mSecret) {
|
|
|
|
// Let the last-fetched and fetch-count properties be updated.
|
|
|
|
mFile->OnFetched();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
CacheEntryHandle* CacheEntry::NewHandle()
|
|
|
|
{
|
|
|
|
return new CacheEntryHandle(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheEntryHandle* CacheEntry::NewWriteHandle()
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2014-09-16 17:51:50 +04:00
|
|
|
// Ignore the OPEN_SECRETLY flag on purpose here, which should actually be
|
|
|
|
// used only along with OPEN_READONLY, but there is no need to enforce that.
|
2013-09-20 13:11:25 +04:00
|
|
|
BackgroundOp(Ops::FRECENCYUPDATE);
|
2014-09-16 17:51:50 +04:00
|
|
|
|
2014-05-06 22:55:50 +04:00
|
|
|
return (mWriter = NewHandle());
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
void CacheEntry::OnHandleClosed(CacheEntryHandle const* aHandle)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
2014-01-07 00:24:41 +04:00
|
|
|
LOG(("CacheEntry::OnHandleClosed [this=%p, state=%s, handle=%p]", this, StateString(mState), aHandle));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-01-14 23:16:06 +03:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-08-05 12:05:00 +03:00
|
|
|
if (IsDoomed() && NS_SUCCEEDED(mFileStatus) &&
|
|
|
|
// Note: mHandlesCount is dropped before this method is called
|
|
|
|
(mHandlesCount == 0 ||
|
|
|
|
(mHandlesCount == 1 && mWriter && mWriter != aHandle))
|
|
|
|
) {
|
2016-02-12 16:45:00 +03:00
|
|
|
// This entry is no longer referenced from outside and is doomed.
|
2016-08-05 12:05:00 +03:00
|
|
|
// We can do this also when there is just reference from the writer,
|
|
|
|
// no one else could ever reach the written data.
|
2016-02-12 16:45:00 +03:00
|
|
|
// Tell the file to kill the handle, i.e. bypass any I/O operations
|
|
|
|
// on it except removing the file.
|
|
|
|
mFile->Kill();
|
|
|
|
}
|
|
|
|
|
2016-01-14 23:16:06 +03:00
|
|
|
if (mWriter != aHandle) {
|
|
|
|
LOG((" not the writer"));
|
|
|
|
return;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-01-14 23:16:06 +03:00
|
|
|
if (mOutputStream) {
|
|
|
|
LOG((" abandoning phantom output stream"));
|
|
|
|
// No one took our internal output stream, so there are no data
|
|
|
|
// and output stream has to be open symultaneously with input stream
|
|
|
|
// on this entry again.
|
|
|
|
mHasData = false;
|
|
|
|
// This asynchronously ends up invoking callbacks on this entry
|
|
|
|
// through OnOutputClosed() call.
|
|
|
|
mOutputStream->Close();
|
|
|
|
mOutputStream = nullptr;
|
|
|
|
} else {
|
|
|
|
// We must always redispatch, otherwise there is a risk of stack
|
|
|
|
// overflow. This code can recurse deeply. It won't execute sooner
|
|
|
|
// than we release mLock.
|
|
|
|
BackgroundOp(Ops::CALLBACKS, true);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-01-14 23:16:06 +03:00
|
|
|
mWriter = nullptr;
|
2014-07-07 22:58:26 +04:00
|
|
|
|
2016-01-14 23:16:06 +03:00
|
|
|
if (mState == WRITING) {
|
|
|
|
LOG((" reverting to state EMPTY - write failed"));
|
|
|
|
mState = EMPTY;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
2016-01-14 23:16:06 +03:00
|
|
|
else if (mState == REVALIDATING) {
|
|
|
|
LOG((" reverting to state READY - reval failed"));
|
|
|
|
mState = READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == READY && !mHasData) {
|
|
|
|
// We may get to this state when following steps happen:
|
|
|
|
// 1. a new entry is given to a consumer
|
|
|
|
// 2. the consumer calls MetaDataReady(), we transit to READY
|
|
|
|
// 3. abandons the entry w/o opening the output stream, mHasData left false
|
|
|
|
//
|
|
|
|
// In this case any following consumer will get a ready entry (with metadata)
|
|
|
|
// but in state like the entry data write was still happening (was in progress)
|
|
|
|
// and will indefinitely wait for the entry data or even the entry itself when
|
|
|
|
// RECHECK_AFTER_WRITE is returned from onCacheEntryCheck.
|
|
|
|
LOG((" we are in READY state, pretend we have data regardless it"
|
|
|
|
" has actully been never touched"));
|
|
|
|
mHasData = true;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
void CacheEntry::OnOutputClosed()
|
|
|
|
{
|
|
|
|
// Called when the file's output stream is closed. Invoke any callbacks
|
|
|
|
// waiting for complete entry.
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
InvokeCallbacks();
|
|
|
|
}
|
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
bool CacheEntry::IsReferenced() const
|
|
|
|
{
|
|
|
|
CacheStorageService::Self()->Lock().AssertCurrentThreadOwns();
|
|
|
|
|
2014-01-09 02:18:06 +04:00
|
|
|
// Increasing this counter from 0 to non-null and this check both happen only
|
|
|
|
// under the service lock.
|
2014-04-06 22:45:18 +04:00
|
|
|
return mHandlesCount > 0;
|
2014-01-07 00:24:41 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
bool CacheEntry::IsFileDoomed()
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(mFileStatus)) {
|
|
|
|
return mFile->IsDoomed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
uint32_t CacheEntry::GetMetadataMemoryConsumption()
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, 0);
|
|
|
|
|
|
|
|
uint32_t size;
|
|
|
|
if (NS_FAILED(mFile->ElementsSize(&size)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsICacheEntry
|
|
|
|
|
2014-02-04 04:52:59 +04:00
|
|
|
NS_IMETHODIMP CacheEntry::GetPersistent(bool *aPersistToDisk)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
// No need to sync when only reading.
|
|
|
|
// When consumer needs to be consistent with state of the memory storage entries
|
|
|
|
// table, then let it use GetUseDisk getter that must be called under the service lock.
|
|
|
|
*aPersistToDisk = mUseDisk;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetKey(nsACString & aKey)
|
|
|
|
{
|
2016-05-12 13:38:00 +03:00
|
|
|
aKey.Assign(mURI);
|
|
|
|
return NS_OK;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetFetchCount(int32_t *aFetchCount)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
return mFile->GetFetchCount(reinterpret_cast<uint32_t*>(aFetchCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetLastFetched(uint32_t *aLastFetched)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
return mFile->GetLastFetched(aLastFetched);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetLastModified(uint32_t *aLastModified)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
return mFile->GetLastModified(aLastModified);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetExpirationTime(uint32_t *aExpirationTime)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
return mFile->GetExpirationTime(aExpirationTime);
|
|
|
|
}
|
|
|
|
|
2017-03-28 07:02:45 +03:00
|
|
|
nsresult CacheEntry::GetOnStartTime(uint64_t *aTime)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return mFile->GetOnStartTime(aTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheEntry::GetOnStopTime(uint64_t *aTime)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return mFile->GetOnStopTime(aTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheEntry::SetNetworkTimes(uint64_t aOnStartTime, uint64_t aOnStopTime)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(mFileStatus)) {
|
|
|
|
return mFile->SetNetworkTimes(aOnStartTime, aOnStopTime);
|
|
|
|
}
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2014-07-21 22:34:17 +04:00
|
|
|
NS_IMETHODIMP CacheEntry::GetIsForcedValid(bool *aIsForcedValid)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aIsForcedValid);
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
MOZ_ASSERT(mState > LOADING);
|
|
|
|
|
|
|
|
if (mPinned) {
|
|
|
|
*aIsForcedValid = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-21 22:34:17 +04:00
|
|
|
nsAutoCString key;
|
2016-02-19 13:56:00 +03:00
|
|
|
nsresult rv = HashingKey(key);
|
2014-07-21 22:34:17 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
*aIsForcedValid = CacheStorageService::Self()->IsForcedValidEntry(mStorageID, key);
|
2014-07-21 22:34:17 +04:00
|
|
|
LOG(("CacheEntry::GetIsForcedValid [this=%p, IsForcedValid=%d]", this, *aIsForcedValid));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::ForceValidFor(uint32_t aSecondsToTheFuture)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::ForceValidFor [this=%p, aSecondsToTheFuture=%d]", this, aSecondsToTheFuture));
|
|
|
|
|
|
|
|
nsAutoCString key;
|
2016-02-19 13:56:00 +03:00
|
|
|
nsresult rv = HashingKey(key);
|
2014-07-21 22:34:17 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
CacheStorageService::Self()->ForceEntryValidFor(mStorageID, key, aSecondsToTheFuture);
|
2014-07-21 22:34:17 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_IMETHODIMP CacheEntry::SetExpirationTime(uint32_t aExpirationTime)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsresult rv = mFile->SetExpirationTime(aExpirationTime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Aligned assignment, thus atomic.
|
|
|
|
mSortingExpirationTime = aExpirationTime;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::OpenInputStream(int64_t offset, nsIInputStream * *_retval)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::OpenInputStream [this=%p]", this));
|
2016-04-11 06:17:02 +03:00
|
|
|
return OpenInputStreamInternal(offset, nullptr, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::OpenAlternativeInputStream(const nsACString & type, nsIInputStream * *_retval)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::OpenAlternativeInputStream [this=%p, type=%s]", this,
|
|
|
|
PromiseFlatCString(type).get()));
|
|
|
|
return OpenInputStreamInternal(0, PromiseFlatCString(type).get(), _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheEntry::OpenInputStreamInternal(int64_t offset, const char *aAltDataType, nsIInputStream * *_retval)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::OpenInputStreamInternal [this=%p]", this));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2016-02-12 16:45:00 +03:00
|
|
|
RefPtr<CacheEntryHandle> selfHandle = NewHandle();
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2016-04-11 06:17:02 +03:00
|
|
|
if (aAltDataType) {
|
|
|
|
rv = mFile->OpenAlternativeInputStream(selfHandle, aAltDataType,
|
|
|
|
getter_AddRefs(stream));
|
2016-09-29 14:26:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Failure of this method may be legal when the alternative data requested
|
|
|
|
// is not avaialble or of a different type. Console error logs are ensured
|
|
|
|
// by CacheFile::OpenAlternativeInputStream.
|
|
|
|
return rv;
|
|
|
|
}
|
2016-04-11 06:17:02 +03:00
|
|
|
} else {
|
|
|
|
rv = mFile->OpenInputStream(selfHandle, getter_AddRefs(stream));
|
2016-09-29 14:26:00 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-04-11 06:17:02 +03:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable =
|
|
|
|
do_QueryInterface(stream, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, offset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (!mHasData) {
|
|
|
|
// So far output stream on this new entry not opened, do it now.
|
|
|
|
LOG((" creating phantom output stream"));
|
|
|
|
rv = OpenOutputStreamInternal(0, getter_AddRefs(mOutputStream));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::OpenOutputStream(int64_t offset, nsIOutputStream * *_retval)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::OpenOutputStream [this=%p]", this));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState > EMPTY);
|
|
|
|
|
2014-02-04 04:53:00 +04:00
|
|
|
if (mOutputStream && !mIsDoomed) {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG((" giving phantom output stream"));
|
|
|
|
mOutputStream.forget(_retval);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = OpenOutputStreamInternal(offset, _retval);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry considered ready when writer opens output stream.
|
|
|
|
if (mState < READY)
|
|
|
|
mState = READY;
|
|
|
|
|
|
|
|
// Invoke any pending readers now.
|
|
|
|
InvokeCallbacks();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:17:02 +03:00
|
|
|
NS_IMETHODIMP CacheEntry::OpenAlternativeOutputStream(const nsACString & type, nsIOutputStream * *_retval)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::OpenAlternativeOutputStream [this=%p, type=%s]", this,
|
|
|
|
PromiseFlatCString(type).get()));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (!mHasData || mState < READY || mOutputStream || mIsDoomed) {
|
|
|
|
LOG((" entry not in state to write alt-data"));
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> stream;
|
|
|
|
rv = mFile->OpenAlternativeOutputStream(nullptr,
|
|
|
|
PromiseFlatCString(type).get(),
|
|
|
|
getter_AddRefs(stream));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
stream.swap(*_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsresult CacheEntry::OpenOutputStreamInternal(int64_t offset, nsIOutputStream * *_retval)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::OpenOutputStreamInternal [this=%p]", this));
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (mIsDoomed) {
|
|
|
|
LOG((" doomed..."));
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState > LOADING);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// No need to sync on mUseDisk here, we don't need to be consistent
|
|
|
|
// with content of the memory storage entries hash table.
|
|
|
|
if (!mUseDisk) {
|
|
|
|
rv = mFile->SetMemoryOnly();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheOutputCloseListener> listener =
|
2013-11-21 02:20:17 +04:00
|
|
|
new CacheOutputCloseListener(this);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsIOutputStream> stream;
|
2013-11-21 02:20:17 +04:00
|
|
|
rv = mFile->OpenOutputStream(listener, getter_AddRefs(stream));
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable =
|
|
|
|
do_QueryInterface(stream, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, offset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Prevent opening output stream again.
|
|
|
|
mHasData = true;
|
|
|
|
|
|
|
|
stream.swap(*_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetPredictedDataSize(int64_t *aPredictedDataSize)
|
|
|
|
{
|
|
|
|
*aPredictedDataSize = mPredictedDataSize;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP CacheEntry::SetPredictedDataSize(int64_t aPredictedDataSize)
|
|
|
|
{
|
|
|
|
mPredictedDataSize = aPredictedDataSize;
|
2014-02-04 04:53:00 +04:00
|
|
|
|
2015-09-04 02:05:42 +03:00
|
|
|
if (!mSkipSizeCheck && CacheObserver::EntryIsTooBig(mPredictedDataSize, mUseDisk)) {
|
2014-02-04 04:53:00 +04:00
|
|
|
LOG(("CacheEntry::SetPredictedDataSize [this=%p] too big, dooming", this));
|
|
|
|
AsyncDoom(nullptr);
|
|
|
|
|
|
|
|
return NS_ERROR_FILE_TOO_BIG;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetSecurityInfo(nsISupports * *aSecurityInfo)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
if (mSecurityInfoLoaded) {
|
|
|
|
NS_IF_ADDREF(*aSecurityInfo = mSecurityInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
2014-03-05 21:14:19 +04:00
|
|
|
nsXPIDLCString info;
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsISupports> secInfo;
|
|
|
|
nsresult rv;
|
|
|
|
|
2014-03-05 21:14:19 +04:00
|
|
|
rv = mFile->GetElement("security-info", getter_Copies(info));
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (info) {
|
2014-03-05 21:14:19 +04:00
|
|
|
rv = NS_DeserializeObject(info, getter_AddRefs(secInfo));
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
mSecurityInfo.swap(secInfo);
|
|
|
|
mSecurityInfoLoaded = true;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aSecurityInfo = mSecurityInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP CacheEntry::SetSecurityInfo(nsISupports *aSecurityInfo)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, mFileStatus);
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
mSecurityInfo = aSecurityInfo;
|
|
|
|
mSecurityInfoLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISerializable> serializable =
|
|
|
|
do_QueryInterface(aSecurityInfo);
|
|
|
|
if (aSecurityInfo && !serializable)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
nsCString info;
|
|
|
|
if (serializable) {
|
|
|
|
rv = NS_SerializeToString(serializable, info);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mFile->SetElement("security-info", info.Length() ? info.get() : nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetStorageDataSize(uint32_t *aStorageDataSize)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aStorageDataSize);
|
|
|
|
|
|
|
|
int64_t dataSize;
|
|
|
|
nsresult rv = GetDataSize(&dataSize);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
*aStorageDataSize = (uint32_t)std::min(int64_t(uint32_t(-1)), dataSize);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::AsyncDoom(nsICacheEntryDoomCallback *aCallback)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::AsyncDoom [this=%p]", this));
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (mIsDoomed || mDoomCallback)
|
|
|
|
return NS_ERROR_IN_PROGRESS; // to aggregate have DOOMING state
|
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
RemoveForcedValidity();
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
mIsDoomed = true;
|
|
|
|
mDoomCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2014-01-22 21:54:51 +04:00
|
|
|
// This immediately removes the entry from the master hashtable and also
|
|
|
|
// immediately dooms the file. This way we make sure that any consumer
|
|
|
|
// after this point asking for the same entry won't get
|
|
|
|
// a) this entry
|
|
|
|
// b) a new entry with the same file
|
|
|
|
PurgeAndDoom();
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetMetaDataElement(const char * aKey, char * *aRetval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
2014-03-05 21:14:19 +04:00
|
|
|
return mFile->GetElement(aKey, aRetval);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::SetMetaDataElement(const char * aKey, const char * aValue)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
return mFile->SetElement(aKey, aValue);
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
NS_IMETHODIMP CacheEntry::VisitMetaData(nsICacheEntryMetaDataVisitor *aVisitor)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
return mFile->VisitMetaData(aVisitor);
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_IMETHODIMP CacheEntry::MetaDataReady()
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
LOG(("CacheEntry::MetaDataReady [this=%p, state=%s]", this, StateString(mState)));
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState > EMPTY);
|
|
|
|
|
|
|
|
if (mState == WRITING)
|
|
|
|
mState = READY;
|
|
|
|
|
|
|
|
InvokeCallbacks();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::SetValid()
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::SetValid [this=%p, state=%s]", this, StateString(mState)));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> outputStream;
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState > EMPTY);
|
|
|
|
|
|
|
|
mState = READY;
|
|
|
|
mHasData = true;
|
|
|
|
|
|
|
|
InvokeCallbacks();
|
|
|
|
|
|
|
|
outputStream.swap(mOutputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outputStream) {
|
|
|
|
LOG((" abandoning phantom output stream"));
|
|
|
|
outputStream->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-04 04:52:59 +04:00
|
|
|
NS_IMETHODIMP CacheEntry::Recreate(bool aMemoryOnly,
|
|
|
|
nsICacheEntry **_retval)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
LOG(("CacheEntry::Recreate [this=%p, state=%s]", this, StateString(mState)));
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntryHandle> handle = ReopenTruncated(aMemoryOnly, nullptr);
|
2014-01-09 02:18:06 +04:00
|
|
|
if (handle) {
|
2013-09-20 13:11:25 +04:00
|
|
|
handle.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundOp(Ops::CALLBACKS, true);
|
2017-01-17 15:36:00 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetDataSize(int64_t *aDataSize)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::GetDataSize [this=%p]", this));
|
|
|
|
*aDataSize = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (!mHasData) {
|
|
|
|
LOG((" write in progress (no data)"));
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, mFileStatus);
|
|
|
|
|
|
|
|
// mayhemer: TODO Problem with compression?
|
|
|
|
if (!mFile->DataSize(aDataSize)) {
|
|
|
|
LOG((" write in progress (stream active)"));
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" size=%" PRId64, *aDataSize));
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-27 11:26:00 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::GetAltDataSize(int64_t *aDataSize)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::GetAltDataSize [this=%p]", this));
|
|
|
|
if (NS_FAILED(mFileStatus)) {
|
|
|
|
return mFileStatus;
|
|
|
|
}
|
|
|
|
return mFile->GetAltDataSize(aDataSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_IMETHODIMP CacheEntry::MarkValid()
|
|
|
|
{
|
|
|
|
// NOT IMPLEMENTED ACTUALLY
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::MaybeMarkValid()
|
|
|
|
{
|
|
|
|
// NOT IMPLEMENTED ACTUALLY
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::HasWriteAccess(bool aWriteAllowed, bool *aWriteAccess)
|
|
|
|
{
|
|
|
|
*aWriteAccess = aWriteAllowed;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::Close()
|
|
|
|
{
|
|
|
|
// NOT IMPLEMENTED ACTUALLY
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-10 18:14:34 +03:00
|
|
|
NS_IMETHODIMP CacheEntry::GetDiskStorageSizeInKB(uint32_t *aDiskStorageSize)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(mFileStatus)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mFile->GetDiskStorageSizeInKB(aDiskStorageSize);
|
|
|
|
}
|
|
|
|
|
2017-02-03 06:15:00 +03:00
|
|
|
NS_IMETHODIMP CacheEntry::GetLoadContextInfo(nsILoadContextInfo** aInfo)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info = CacheFileUtils::ParseKey(mStorageID);
|
|
|
|
if (!info) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.forget(aInfo);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// nsIRunnable
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntry::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
BackgroundOp(mBackgroundOperations.Grab());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Management methods
|
|
|
|
|
|
|
|
double CacheEntry::GetFrecency() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
|
|
|
return mFrecency;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CacheEntry::GetExpirationTime() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
|
|
|
return mSortingExpirationTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CacheEntry::IsRegistered() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
2013-10-02 13:30:42 +04:00
|
|
|
return mRegistration == REGISTERED;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CacheEntry::CanRegister() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
2013-10-02 13:30:42 +04:00
|
|
|
return mRegistration == NEVERREGISTERED;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheEntry::SetRegistered(bool aRegistered)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
|
|
|
|
2013-10-02 13:30:42 +04:00
|
|
|
if (aRegistered) {
|
|
|
|
MOZ_ASSERT(mRegistration == NEVERREGISTERED);
|
|
|
|
mRegistration = REGISTERED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(mRegistration == REGISTERED);
|
|
|
|
mRegistration = DEREGISTERED;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
bool CacheEntry::DeferOrBypassRemovalOnPinStatus(bool aPinned)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::DeferOrBypassRemovalOnPinStatus [this=%p]", this));
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (mPinningKnown) {
|
|
|
|
LOG((" pinned=%d, caller=%d", mPinned, aPinned));
|
|
|
|
// Bypass when the pin status of this entry doesn't match the pin status
|
|
|
|
// caller wants to remove
|
|
|
|
return mPinned != aPinned;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG((" pinning unknown, caller=%d", aPinned));
|
|
|
|
// Oterwise, remember to doom after the status is determined for any
|
|
|
|
// callback opening the entry after this point...
|
|
|
|
Callback c(this, aPinned);
|
|
|
|
RememberCallback(c);
|
|
|
|
// ...and always bypass
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
bool CacheEntry::Purge(uint32_t aWhat)
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::Purge [this=%p, what=%d]", this, aWhat));
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
|
|
|
|
|
|
|
switch (aWhat) {
|
|
|
|
case PURGE_DATA_ONLY_DISK_BACKED:
|
|
|
|
case PURGE_WHOLE_ONLY_DISK_BACKED:
|
|
|
|
// This is an in-memory only entry, don't purge it
|
|
|
|
if (!mUseDisk) {
|
|
|
|
LOG((" not using disk"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == WRITING || mState == LOADING || mFrecency == 0) {
|
|
|
|
// In-progress (write or load) entries should (at least for consistency and from
|
|
|
|
// the logical point of view) stay in memory.
|
|
|
|
// Zero-frecency entries are those which have never been given to any consumer, those
|
|
|
|
// are actually very fresh and should not go just because frecency had not been set
|
|
|
|
// so far.
|
|
|
|
LOG((" state=%s, frecency=%1.10f", StateString(mState), mFrecency));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
if (NS_SUCCEEDED(mFileStatus) && mFile->IsWriteInProgress()) {
|
|
|
|
// The file is used when there are open streams or chunks/metadata still waiting for
|
|
|
|
// write. In this case, this entry cannot be purged, otherwise reopenned entry
|
|
|
|
// would may not even find the data on disk - CacheFile is not shared and cannot be
|
|
|
|
// left orphan when its job is not done, hence keep the whole entry.
|
|
|
|
LOG((" file still under use"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
switch (aWhat) {
|
|
|
|
case PURGE_WHOLE_ONLY_DISK_BACKED:
|
|
|
|
case PURGE_WHOLE:
|
|
|
|
{
|
2014-01-07 00:24:41 +04:00
|
|
|
if (!CacheStorageService::Self()->RemoveEntry(this, true)) {
|
|
|
|
LOG((" not purging, still referenced"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
CacheStorageService::Self()->UnregisterEntry(this);
|
|
|
|
|
|
|
|
// Entry removed it self from control arrays, return true
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PURGE_DATA_ONLY_DISK_BACKED:
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mFileStatus, false);
|
|
|
|
|
|
|
|
mFile->ThrowMemoryCachedData();
|
|
|
|
|
|
|
|
// Entry has been left in control arrays, return false (not purged)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG((" ?"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheEntry::PurgeAndDoom()
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::PurgeAndDoom [this=%p]", this));
|
|
|
|
|
|
|
|
CacheStorageService::Self()->RemoveEntry(this);
|
|
|
|
DoomAlreadyRemoved();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheEntry::DoomAlreadyRemoved()
|
|
|
|
{
|
|
|
|
LOG(("CacheEntry::DoomAlreadyRemoved [this=%p]", this));
|
|
|
|
|
2014-01-22 21:54:51 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
RemoveForcedValidity();
|
|
|
|
|
2014-01-22 21:54:51 +04:00
|
|
|
mIsDoomed = true;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
// Pretend pinning is know. This entry is now doomed for good, so don't
|
|
|
|
// bother with defering doom because of unknown pinning state any more.
|
|
|
|
mPinningKnown = true;
|
|
|
|
|
2014-01-22 21:54:51 +04:00
|
|
|
// This schedules dooming of the file, dooming is ensured to happen
|
|
|
|
// sooner than demand to open the same file made after this point
|
|
|
|
// so that we don't get this file for any newer opened entry(s).
|
|
|
|
DoomFile();
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-01-22 21:54:51 +04:00
|
|
|
// Must force post here since may be indirectly called from
|
|
|
|
// InvokeCallbacks of this entry and we don't want reentrancy here.
|
|
|
|
BackgroundOp(Ops::CALLBACKS, true);
|
|
|
|
// Process immediately when on the management thread.
|
|
|
|
BackgroundOp(Ops::UNREGISTER);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-01-22 21:54:51 +04:00
|
|
|
void CacheEntry::DoomFile()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mFileStatus)) {
|
2016-08-05 12:05:00 +03:00
|
|
|
if (mHandlesCount == 0 ||
|
|
|
|
(mHandlesCount == 1 && mWriter)) {
|
|
|
|
// We kill the file also when there is just reference from the writer,
|
|
|
|
// no one else could ever reach the written data. Obvisouly also
|
|
|
|
// when there is no reference at all (should we ever end up here
|
|
|
|
// in that case.)
|
|
|
|
// Tell the file to kill the handle, i.e. bypass any I/O operations
|
|
|
|
// on it except removing the file.
|
|
|
|
mFile->Kill();
|
|
|
|
}
|
|
|
|
|
2014-01-22 21:54:51 +04:00
|
|
|
// Always calls the callback asynchronously.
|
|
|
|
rv = mFile->Doom(mDoomCallback ? this : nullptr);
|
2013-09-20 13:11:25 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
LOG((" file doomed"));
|
|
|
|
return;
|
|
|
|
}
|
2014-01-22 21:54:51 +04:00
|
|
|
|
|
|
|
if (NS_ERROR_FILE_NOT_FOUND == rv) {
|
|
|
|
// File is set to be just memory-only, notify the callbacks
|
|
|
|
// and pretend dooming has succeeded. From point of view of
|
|
|
|
// the entry it actually did - the data is gone and cannot be
|
|
|
|
// reused.
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-01-22 21:54:51 +04:00
|
|
|
// Always posts to the main thread.
|
|
|
|
OnFileDoomed(rv);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
void CacheEntry::RemoveForcedValidity()
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mIsDoomed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString entryKey;
|
|
|
|
rv = HashingKey(entryKey);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheStorageService::Self()->RemoveEntryForceValid(mStorageID, entryKey);
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
void CacheEntry::BackgroundOp(uint32_t aOperations, bool aForceAsync)
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (!CacheStorageService::IsOnManagementThread() || aForceAsync) {
|
|
|
|
if (mBackgroundOperations.Set(aOperations))
|
|
|
|
CacheStorageService::Self()->Dispatch(this);
|
|
|
|
|
|
|
|
LOG(("CacheEntry::BackgroundOp this=%p dipatch of %x", this, aOperations));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoUnlock unlock(mLock);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
if (aOperations & Ops::FRECENCYUPDATE) {
|
2014-07-08 18:48:00 +04:00
|
|
|
++mUseCount;
|
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
#ifndef M_LN2
|
|
|
|
#define M_LN2 0.69314718055994530942
|
|
|
|
#endif
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
// Half-life is dynamic, in seconds.
|
|
|
|
static double half_life = CacheObserver::HalfLifeSeconds();
|
|
|
|
// Must convert from seconds to milliseconds since PR_Now() gives usecs.
|
|
|
|
static double const decay = (M_LN2 / half_life) / static_cast<double>(PR_USEC_PER_SEC);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
double now_decay = static_cast<double>(PR_Now()) * decay;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
if (mFrecency == 0) {
|
|
|
|
mFrecency = now_decay;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO: when C++11 enabled, use std::log1p(n) which is equal to log(n + 1) but
|
|
|
|
// more precise.
|
|
|
|
mFrecency = log(exp(mFrecency - now_decay) + 1) + now_decay;
|
|
|
|
}
|
|
|
|
LOG(("CacheEntry FRECENCYUPDATE [this=%p, frecency=%1.10f]", this, mFrecency));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
// Because CacheFile::Set*() are not thread-safe to use (uses WeakReference that
|
|
|
|
// is not thread-safe) we must post to the main thread...
|
2016-05-05 11:45:00 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod<double>(this, &CacheEntry::StoreFrecency, mFrecency));
|
2014-06-23 19:48:52 +04:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
if (aOperations & Ops::REGISTER) {
|
|
|
|
LOG(("CacheEntry REGISTER [this=%p]", this));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
CacheStorageService::Self()->RegisterEntry(this);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
if (aOperations & Ops::UNREGISTER) {
|
|
|
|
LOG(("CacheEntry UNREGISTER [this=%p]", this));
|
2014-06-23 15:07:18 +04:00
|
|
|
|
2014-06-23 19:48:52 +04:00
|
|
|
CacheStorageService::Self()->UnregisterEntry(this);
|
|
|
|
}
|
|
|
|
} // unlock
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (aOperations & Ops::CALLBACKS) {
|
|
|
|
LOG(("CacheEntry CALLBACKS (invoke) [this=%p]", this));
|
|
|
|
|
|
|
|
InvokeCallbacks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 21:55:00 +03:00
|
|
|
void CacheEntry::StoreFrecency(double aFrecency)
|
2014-01-09 03:27:33 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-06-24 20:08:36 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mFileStatus)) {
|
2015-03-25 21:55:00 +03:00
|
|
|
mFile->SetFrecency(FRECENCY2INT(aFrecency));
|
2014-06-24 20:08:36 +04:00
|
|
|
}
|
2014-01-09 03:27:33 +04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
// CacheOutputCloseListener
|
|
|
|
|
|
|
|
CacheOutputCloseListener::CacheOutputCloseListener(CacheEntry* aEntry)
|
|
|
|
: mEntry(aEntry)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheOutputCloseListener::~CacheOutputCloseListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheOutputCloseListener::OnOutputClosed()
|
|
|
|
{
|
|
|
|
// We need this class and to redispatch since this callback is invoked
|
|
|
|
// under the file's lock and to do the job we need to enter the entry's
|
|
|
|
// lock too. That would lead to potential deadlocks.
|
|
|
|
NS_DispatchToCurrentThread(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheOutputCloseListener::Run()
|
|
|
|
{
|
|
|
|
mEntry->OnOutputClosed();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
// Memory reporting
|
|
|
|
|
|
|
|
size_t CacheEntry::SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
|
2015-07-29 09:24:24 +03:00
|
|
|
n += mCallbacks.ShallowSizeOfExcludingThis(mallocSizeOf);
|
2014-02-27 03:11:40 +04:00
|
|
|
if (mFile) {
|
|
|
|
n += mFile->SizeOfIncludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2016-05-12 13:38:00 +03:00
|
|
|
n += mURI.SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
2014-02-27 03:11:40 +04:00
|
|
|
n += mEnhanceID.SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
|
|
|
n += mStorageID.SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
|
|
|
|
|
|
|
// mDoomCallback is an arbitrary class that is probably reported elsewhere.
|
|
|
|
// mOutputStream is reported in mFile.
|
|
|
|
// mWriter is one of many handles we create, but (intentionally) not keep
|
2017-04-03 19:23:00 +03:00
|
|
|
// any reference to, so those unfortunately cannot be reported. Handles are
|
2014-02-27 03:11:40 +04:00
|
|
|
// small, though.
|
|
|
|
// mSecurityInfo doesn't impl nsISizeOf.
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CacheEntry::SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const
|
|
|
|
{
|
|
|
|
return mallocSizeOf(this) + SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|