2015-03-02 16:20:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/cache/QuotaClient.h"
|
|
|
|
|
2017-10-26 14:35:08 +03:00
|
|
|
#include "DBAction.h"
|
|
|
|
#include "FileUtils.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/cache/Manager.h"
|
2019-01-17 20:12:27 +03:00
|
|
|
#include "mozilla/dom/quota/QuotaCommon.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
|
|
|
#include "mozilla/dom/quota/UsageInfo.h"
|
2015-11-22 12:44:16 +03:00
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
2019-01-17 20:12:27 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2017-10-26 14:35:08 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-03-22 14:13:54 +03:00
|
|
|
using mozilla::Atomic;
|
2017-07-18 14:00:09 +03:00
|
|
|
using mozilla::MutexAutoLock;
|
2019-08-22 23:52:09 +03:00
|
|
|
using mozilla::Some;
|
2017-07-18 14:00:09 +03:00
|
|
|
using mozilla::Unused;
|
2015-06-30 15:59:27 +03:00
|
|
|
using mozilla::dom::ContentParentId;
|
2017-07-18 14:00:09 +03:00
|
|
|
using mozilla::dom::cache::DirPaddingFile;
|
2015-03-02 16:20:00 +03:00
|
|
|
using mozilla::dom::cache::Manager;
|
2017-07-18 14:00:09 +03:00
|
|
|
using mozilla::dom::cache::QuotaInfo;
|
2017-07-18 13:54:20 +03:00
|
|
|
using mozilla::dom::quota::AssertIsOnIOThread;
|
2015-03-02 16:20:00 +03:00
|
|
|
using mozilla::dom::quota::Client;
|
2019-10-26 11:50:43 +03:00
|
|
|
using mozilla::dom::quota::PERSISTENCE_TYPE_DEFAULT;
|
2015-03-02 16:20:00 +03:00
|
|
|
using mozilla::dom::quota::PersistenceType;
|
|
|
|
using mozilla::dom::quota::QuotaManager;
|
|
|
|
using mozilla::dom::quota::UsageInfo;
|
2015-11-22 12:44:16 +03:00
|
|
|
using mozilla::ipc::AssertIsOnBackgroundThread;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2019-09-27 19:54:22 +03:00
|
|
|
static nsresult GetBodyUsage(nsIFile* aMorgueDir, const Atomic<bool>& aCanceled,
|
2019-10-02 13:38:55 +03:00
|
|
|
UsageInfo* aUsageInfo, const bool aInitializing) {
|
2017-07-18 14:03:25 +03:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2018-05-20 06:17:45 +03:00
|
|
|
nsCOMPtr<nsIDirectoryEnumerator> entries;
|
2019-09-27 19:54:22 +03:00
|
|
|
nsresult rv = aMorgueDir->GetDirectoryEntries(getter_AddRefs(entries));
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-09-27 19:54:22 +03:00
|
|
|
nsCOMPtr<nsIFile> bodyDir;
|
|
|
|
while (NS_SUCCEEDED(rv = entries->GetNextFile(getter_AddRefs(bodyDir))) &&
|
|
|
|
bodyDir && !aCanceled) {
|
2019-09-13 19:43:30 +03:00
|
|
|
if (NS_WARN_IF(QuotaManager::IsShuttingDown())) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
bool isDir;
|
2019-09-27 19:54:22 +03:00
|
|
|
rv = bodyDir->IsDirectory(&isDir);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-09-27 19:54:22 +03:00
|
|
|
if (!isDir) {
|
|
|
|
QuotaInfo dummy;
|
|
|
|
mozilla::DebugOnly<nsresult> result =
|
|
|
|
RemoveNsIFile(dummy, bodyDir, /* aTrackQuota */ false);
|
|
|
|
// Try to remove the unexpected files, and keep moving on even if it fails
|
|
|
|
// because it might be created by virus or the operation system
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-27 19:54:45 +03:00
|
|
|
const QuotaInfo dummy;
|
|
|
|
const auto getUsage = [&aUsageInfo](nsIFile* bodyFile,
|
|
|
|
const nsACString& leafName,
|
|
|
|
bool& fileDeleted) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(bodyFile);
|
|
|
|
Unused << leafName;
|
2019-09-27 19:54:22 +03:00
|
|
|
|
|
|
|
int64_t fileSize;
|
2019-09-27 19:54:45 +03:00
|
|
|
nsresult rv = bodyFile->GetFileSize(&fileSize);
|
2019-09-27 19:54:22 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(fileSize >= 0);
|
|
|
|
aUsageInfo->AppendToFileUsage(Some(fileSize));
|
2019-09-27 19:54:45 +03:00
|
|
|
|
|
|
|
fileDeleted = false;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
rv = mozilla::dom::cache::BodyTraverseFiles(dummy, bodyDir, getUsage,
|
2019-10-02 13:38:55 +03:00
|
|
|
/* aCanRemoveFiles */
|
|
|
|
aInitializing,
|
2019-09-27 19:54:45 +03:00
|
|
|
/* aTrackQuota */ false);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2019-09-27 19:54:22 +03:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:30:40 +03:00
|
|
|
static nsresult LockedGetPaddingSizeFromDB(nsIFile* aDir,
|
|
|
|
const nsACString& aGroup,
|
|
|
|
const nsACString& aOrigin,
|
|
|
|
int64_t* aPaddingSizeOut) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aDir);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aPaddingSizeOut);
|
|
|
|
|
|
|
|
*aPaddingSizeOut = 0;
|
|
|
|
|
|
|
|
QuotaInfo quotaInfo;
|
|
|
|
quotaInfo.mGroup = aGroup;
|
|
|
|
quotaInfo.mOrigin = aOrigin;
|
2019-10-26 11:50:43 +03:00
|
|
|
// quotaInfo.mDirectoryLockId must be -1 (which is default for new QuotaInfo)
|
|
|
|
// because this method should only be called from QuotaClient::InitOrigin
|
|
|
|
// (via QuotaClient::GetUsageForOriginInternal) when the temporary storage
|
|
|
|
// hasn't been initialized yet. At that time, the in-memory objects (e.g.
|
|
|
|
// OriginInfo) are only being created so it doesn't make sense to tunnel
|
|
|
|
// quota information to TelemetryVFS to get corresponding QuotaObject instance
|
|
|
|
// for the SQLite file).
|
2019-10-25 15:02:10 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(quotaInfo.mDirectoryLockId == -1);
|
2019-10-26 11:50:43 +03:00
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageConnection> conn;
|
2017-10-05 12:30:40 +03:00
|
|
|
nsresult rv = mozilla::dom::cache::OpenDBConnection(quotaInfo, aDir,
|
|
|
|
getter_AddRefs(conn));
|
|
|
|
if (rv == NS_ERROR_FILE_NOT_FOUND ||
|
|
|
|
rv == NS_ERROR_FILE_TARGET_DOES_NOT_EXIST) {
|
|
|
|
// Return NS_OK with size = 0 if both the db and padding file don't exist.
|
|
|
|
// There is no other way to get the overall padding size of an origin.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-09-07 11:28:30 +03:00
|
|
|
// Make sure that the database has the latest schema before we try to read
|
|
|
|
// from it. We have to do this because LockedGetPaddingSizeFromDB is called
|
|
|
|
// by QuotaClient::GetUsageForOrigin which may run at any time (there's no
|
|
|
|
// guarantee that SetupAction::RunSyncWithDBOnTarget already checked the
|
|
|
|
// schema for the given origin).
|
|
|
|
rv = mozilla::dom::cache::db::CreateOrMigrateSchema(conn);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:30:40 +03:00
|
|
|
int64_t paddingSize = 0;
|
|
|
|
rv = mozilla::dom::cache::LockedDirectoryPaddingRestore(
|
|
|
|
aDir, conn, /* aMustRestore */ false, &paddingSize);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aPaddingSizeOut = paddingSize;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CacheQuotaClient final : public Client {
|
2017-07-18 14:00:09 +03:00
|
|
|
static CacheQuotaClient* sInstance;
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
public:
|
2017-07-20 06:33:39 +03:00
|
|
|
CacheQuotaClient()
|
|
|
|
: mDirPaddingFileMutex("DOMCacheQuotaClient.mDirPaddingFileMutex") {
|
2017-07-18 14:00:09 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!sInstance);
|
|
|
|
sInstance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CacheQuotaClient* Get() {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sInstance);
|
|
|
|
return sInstance;
|
|
|
|
}
|
2017-07-20 06:33:39 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual Type GetType() override { return DOMCACHE; }
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
virtual nsresult InitOrigin(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aGroup,
|
2017-03-22 14:13:54 +03:00
|
|
|
const nsACString& aOrigin,
|
|
|
|
const AtomicBool& aCanceled,
|
2019-04-15 14:35:38 +03:00
|
|
|
UsageInfo* aUsageInfo,
|
|
|
|
bool aForGetUsage) override {
|
2017-07-18 14:03:25 +03:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
|
2015-05-20 03:47:16 +03:00
|
|
|
// The QuotaManager passes a nullptr UsageInfo if there is no quota being
|
|
|
|
// enforced against the origin.
|
|
|
|
if (!aUsageInfo) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-01-17 20:12:27 +03:00
|
|
|
return GetUsageForOriginInternal(aPersistenceType, aGroup, aOrigin,
|
|
|
|
aCanceled, aUsageInfo,
|
|
|
|
/* aInitializing*/ true);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual nsresult GetUsageForOrigin(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aGroup,
|
2017-03-22 14:13:54 +03:00
|
|
|
const nsACString& aOrigin,
|
|
|
|
const AtomicBool& aCanceled,
|
2015-03-21 19:28:04 +03:00
|
|
|
UsageInfo* aUsageInfo) override {
|
2019-01-17 20:12:27 +03:00
|
|
|
return GetUsageForOriginInternal(aPersistenceType, aGroup, aOrigin,
|
|
|
|
aCanceled, aUsageInfo,
|
|
|
|
/* aInitializing*/ false);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnOriginClearCompleted(PersistenceType aPersistenceType,
|
2015-03-21 19:28:04 +03:00
|
|
|
const nsACString& aOrigin) override {
|
2015-03-16 17:10:36 +03:00
|
|
|
// Nothing to do here.
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void ReleaseIOThreadObjects() override {
|
2015-03-16 17:10:36 +03:00
|
|
|
// Nothing to do here as the Context handles cleaning everything up
|
|
|
|
// automatically.
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
virtual void AbortOperations(const nsACString& aOrigin) override {
|
2015-11-22 12:44:16 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
2015-03-16 17:10:36 +03:00
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
Manager::Abort(aOrigin);
|
2015-06-30 15:59:27 +03:00
|
|
|
}
|
2015-03-16 17:10:36 +03:00
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
virtual void AbortOperationsForProcess(
|
|
|
|
ContentParentId aContentParentId) override {
|
|
|
|
// The Cache and Context can be shared by multiple client processes. They
|
|
|
|
// are not exclusively owned by a single process.
|
|
|
|
//
|
|
|
|
// As far as I can tell this is used by QuotaManager to abort operations
|
|
|
|
// when a particular process goes away. We definitely don't want this
|
|
|
|
// since we are shared. Also, the Cache actor code already properly
|
|
|
|
// handles asynchronous actor destruction when the child process dies.
|
|
|
|
//
|
|
|
|
// Therefore, do nothing here.
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
virtual void StartIdleMaintenance() override {}
|
|
|
|
|
|
|
|
virtual void StopIdleMaintenance() override {}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-04-14 11:57:41 +03:00
|
|
|
virtual void ShutdownWorkThreads() override {
|
2015-11-22 12:44:16 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// spins the event loop and synchronously shuts down all Managers
|
2015-11-22 12:44:16 +03:00
|
|
|
Manager::ShutdownAll();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 17:02:49 +03:00
|
|
|
nsresult UpgradeStorageFrom2_0To2_1(nsIFile* aDirectory) override {
|
2017-07-18 13:54:20 +03:00
|
|
|
AssertIsOnIOThread();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aDirectory);
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
MutexAutoLock lock(mDirPaddingFileMutex);
|
|
|
|
|
|
|
|
nsresult rv = mozilla::dom::cache::LockedDirectoryPaddingInit(aDirectory);
|
2017-07-18 13:57:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2017-07-18 13:54:20 +03:00
|
|
|
|
2017-07-18 13:57:54 +03:00
|
|
|
return rv;
|
2017-07-18 13:54:20 +03:00
|
|
|
}
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
// static
|
|
|
|
template <typename Callable>
|
|
|
|
nsresult MaybeUpdatePaddingFileInternal(nsIFile* aBaseDir,
|
|
|
|
mozIStorageConnection* aConn,
|
|
|
|
const int64_t aIncreaseSize,
|
|
|
|
const int64_t aDecreaseSize,
|
|
|
|
Callable aCommitHook) {
|
2017-07-18 14:03:25 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-07-18 14:00:09 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBaseDir);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aConn);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aIncreaseSize >= 0);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aDecreaseSize >= 0);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Temporary should be removed at the end of each action. If not, it means
|
|
|
|
// the failure happened.
|
|
|
|
bool temporaryPaddingFileExist =
|
|
|
|
mozilla::dom::cache::DirectoryPaddingFileExists(
|
|
|
|
aBaseDir, DirPaddingFile::TMP_FILE);
|
|
|
|
|
|
|
|
if (aIncreaseSize == aDecreaseSize && !temporaryPaddingFileExist) {
|
|
|
|
// Early return here, since most cache actions won't modify padding size.
|
|
|
|
rv = aCommitHook();
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mDirPaddingFileMutex);
|
|
|
|
rv = mozilla::dom::cache::LockedUpdateDirectoryPaddingFile(
|
|
|
|
aBaseDir, aConn, aIncreaseSize, aDecreaseSize,
|
|
|
|
temporaryPaddingFileExist);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-10-03 13:44:56 +03:00
|
|
|
// Don't delete the temporary padding file here to force the next action
|
|
|
|
// recalculate the padding size.
|
2017-07-18 14:00:09 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = aCommitHook();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-10-03 13:44:56 +03:00
|
|
|
// Don't delete the temporary padding file here to force the next action
|
|
|
|
// recalculate the padding size.
|
2017-07-18 14:00:09 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mozilla::dom::cache::LockedDirectoryPaddingFinalizeWrite(aBaseDir);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// Force restore file next time.
|
2017-10-03 13:44:56 +03:00
|
|
|
Unused << mozilla::dom::cache::LockedDirectoryPaddingDeleteFile(
|
|
|
|
aBaseDir, DirPaddingFile::FILE);
|
|
|
|
|
|
|
|
// Ensure that we are able to force the padding file to be restored.
|
|
|
|
MOZ_ASSERT(mozilla::dom::cache::DirectoryPaddingFileExists(
|
|
|
|
aBaseDir, DirPaddingFile::TMP_FILE));
|
|
|
|
|
|
|
|
// Since both the body file and header have been stored in the
|
|
|
|
// file-system, just make the action be resolve and let the padding file
|
|
|
|
// be restored in the next action.
|
|
|
|
rv = NS_OK;
|
2017-07-18 14:00:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult RestorePaddingFileInternal(nsIFile* aBaseDir,
|
|
|
|
mozIStorageConnection* aConn) {
|
2017-07-18 14:03:25 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-07-18 14:00:09 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBaseDir);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aConn);
|
|
|
|
|
2017-10-05 12:30:40 +03:00
|
|
|
int64_t dummyPaddingSize;
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
MutexAutoLock lock(mDirPaddingFileMutex);
|
|
|
|
|
2017-10-05 12:30:40 +03:00
|
|
|
nsresult rv = mozilla::dom::cache::LockedDirectoryPaddingRestore(
|
|
|
|
aBaseDir, aConn, /* aMustRestore */ true, &dummyPaddingSize);
|
2017-07-18 14:00:09 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult WipePaddingFileInternal(const QuotaInfo& aQuotaInfo,
|
|
|
|
nsIFile* aBaseDir) {
|
2017-07-18 14:03:25 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-07-18 14:00:09 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBaseDir);
|
|
|
|
|
|
|
|
MutexAutoLock lock(mDirPaddingFileMutex);
|
|
|
|
|
2017-10-05 12:30:40 +03:00
|
|
|
MOZ_ASSERT(mozilla::dom::cache::DirectoryPaddingFileExists(
|
|
|
|
aBaseDir, DirPaddingFile::FILE));
|
2017-07-18 14:00:09 +03:00
|
|
|
|
|
|
|
int64_t paddingSize = 0;
|
2017-10-05 12:30:40 +03:00
|
|
|
bool temporaryPaddingFileExist =
|
|
|
|
mozilla::dom::cache::DirectoryPaddingFileExists(
|
|
|
|
aBaseDir, DirPaddingFile::TMP_FILE);
|
|
|
|
|
|
|
|
if (temporaryPaddingFileExist ||
|
|
|
|
NS_WARN_IF(NS_FAILED(mozilla::dom::cache::LockedDirectoryPaddingGet(
|
|
|
|
aBaseDir, &paddingSize)))) {
|
|
|
|
// XXXtt: Maybe have a method in the QuotaManager to clean the usage under
|
|
|
|
// the quota client and the origin.
|
|
|
|
// There is nothing we can do to recover the file.
|
2017-07-18 14:00:09 +03:00
|
|
|
NS_WARNING("Cannnot read padding size from file!");
|
|
|
|
paddingSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paddingSize > 0) {
|
|
|
|
mozilla::dom::cache::DecreaseUsageForQuotaInfo(aQuotaInfo, paddingSize);
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:30:40 +03:00
|
|
|
nsresult rv = mozilla::dom::cache::LockedDirectoryPaddingDeleteFile(
|
|
|
|
aBaseDir, DirPaddingFile::FILE);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove temporary file if we have one.
|
|
|
|
rv = mozilla::dom::cache::LockedDirectoryPaddingDeleteFile(
|
|
|
|
aBaseDir, DirPaddingFile::TMP_FILE);
|
2017-07-18 14:00:09 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mozilla::dom::cache::LockedDirectoryPaddingInit(aBaseDir);
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
private:
|
2015-03-16 17:10:36 +03:00
|
|
|
~CacheQuotaClient() {
|
2015-11-22 12:44:16 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
2017-07-18 14:00:09 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sInstance == this);
|
|
|
|
|
|
|
|
sInstance = nullptr;
|
2015-03-16 17:10:36 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2019-01-17 20:12:27 +03:00
|
|
|
nsresult GetUsageForOriginInternal(PersistenceType aPersistenceType,
|
|
|
|
const nsACString& aGroup,
|
|
|
|
const nsACString& aOrigin,
|
|
|
|
const AtomicBool& aCanceled,
|
|
|
|
UsageInfo* aUsageInfo,
|
|
|
|
const bool aInitializing) {
|
|
|
|
AssertIsOnIOThread();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aUsageInfo);
|
|
|
|
#ifndef NIGHTLY_BUILD
|
|
|
|
Unused << aInitializing;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QuotaManager* qm = QuotaManager::Get();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(qm);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
nsresult rv = qm->GetDirectoryForOrigin(aPersistenceType, aOrigin,
|
|
|
|
getter_AddRefs(dir));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-07-20 18:56:31 +03:00
|
|
|
REPORT_TELEMETRY_ERR_IN_INIT(aInitializing, kQuotaExternalError,
|
2019-01-17 20:12:27 +03:00
|
|
|
Cache_GetDirForOri);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = dir->Append(NS_LITERAL_STRING(DOMCACHE_DIRECTORY_NAME));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-07-20 18:56:31 +03:00
|
|
|
REPORT_TELEMETRY_ERR_IN_INIT(aInitializing, kQuotaExternalError,
|
|
|
|
Cache_Append);
|
2019-01-17 20:12:27 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:50:43 +03:00
|
|
|
bool useCachedValue = false;
|
|
|
|
|
2019-01-17 20:12:27 +03:00
|
|
|
int64_t paddingSize = 0;
|
|
|
|
{
|
|
|
|
// If the tempoary file still exists after locking, it means the previous
|
2019-10-26 11:50:43 +03:00
|
|
|
// action failed, so restore the padding file.
|
2019-01-17 20:12:27 +03:00
|
|
|
MutexAutoLock lock(mDirPaddingFileMutex);
|
|
|
|
|
|
|
|
if (mozilla::dom::cache::DirectoryPaddingFileExists(
|
|
|
|
dir, DirPaddingFile::TMP_FILE) ||
|
|
|
|
NS_WARN_IF(NS_FAILED(mozilla::dom::cache::LockedDirectoryPaddingGet(
|
|
|
|
dir, &paddingSize)))) {
|
2019-10-25 15:02:10 +03:00
|
|
|
if (aInitializing) {
|
|
|
|
rv = LockedGetPaddingSizeFromDB(dir, aGroup, aOrigin, &paddingSize);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
REPORT_TELEMETRY_ERR_IN_INIT(aInitializing, kQuotaInternalError,
|
|
|
|
Cache_GetPaddingSize);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-26 11:50:43 +03:00
|
|
|
// We can't open the database at this point, since it can be already
|
|
|
|
// used by Cache IO thread. Use the cached value instead. (In theory,
|
|
|
|
// we could check if the database is actually used by Cache IO thread
|
|
|
|
// at this moment, but it's probably not worth additional complexity.)
|
|
|
|
|
|
|
|
useCachedValue = true;
|
2019-01-17 20:12:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:50:43 +03:00
|
|
|
if (useCachedValue) {
|
|
|
|
uint64_t usage;
|
|
|
|
if (qm->GetUsageForClient(PERSISTENCE_TYPE_DEFAULT, aGroup, aOrigin,
|
|
|
|
Client::DOMCACHE, usage)) {
|
|
|
|
aUsageInfo->AppendToDatabaseUsage(Some(usage));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-22 23:52:09 +03:00
|
|
|
aUsageInfo->AppendToFileUsage(Some(paddingSize));
|
2019-01-17 20:12:27 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDirectoryEnumerator> entries;
|
|
|
|
rv = dir->GetDirectoryEntries(getter_AddRefs(entries));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-07-20 18:56:31 +03:00
|
|
|
REPORT_TELEMETRY_ERR_IN_INIT(aInitializing, kQuotaExternalError,
|
2019-01-17 20:12:27 +03:00
|
|
|
Cache_GetDirEntries);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
while (NS_SUCCEEDED(rv = entries->GetNextFile(getter_AddRefs(file))) &&
|
|
|
|
file && !aCanceled) {
|
2019-09-13 19:43:30 +03:00
|
|
|
if (NS_WARN_IF(QuotaManager::IsShuttingDown())) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2019-01-17 20:12:27 +03:00
|
|
|
nsAutoString leafName;
|
|
|
|
rv = file->GetLeafName(leafName);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-07-20 18:56:31 +03:00
|
|
|
REPORT_TELEMETRY_ERR_IN_INIT(aInitializing, kQuotaExternalError,
|
2019-01-17 20:12:27 +03:00
|
|
|
Cache_GetLeafName);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDir;
|
|
|
|
rv = file->IsDirectory(&isDir);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-07-20 18:56:31 +03:00
|
|
|
REPORT_TELEMETRY_ERR_IN_INIT(aInitializing, kQuotaExternalError,
|
2019-01-17 20:12:27 +03:00
|
|
|
Cache_IsDirectory);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDir) {
|
|
|
|
if (leafName.EqualsLiteral("morgue")) {
|
2019-10-02 13:38:55 +03:00
|
|
|
rv = GetBodyUsage(file, aCanceled, aUsageInfo, aInitializing);
|
2019-01-17 20:12:27 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-09-13 19:43:30 +03:00
|
|
|
if (rv != NS_ERROR_ABORT) {
|
|
|
|
REPORT_TELEMETRY_ERR_IN_INIT(aInitializing, kQuotaExternalError,
|
|
|
|
Cache_GetBodyUsage);
|
|
|
|
}
|
2019-01-17 20:12:27 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Unknown Cache directory found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore transient sqlite files and marker files
|
|
|
|
if (leafName.EqualsLiteral("caches.sqlite-journal") ||
|
|
|
|
leafName.EqualsLiteral("caches.sqlite-shm") ||
|
|
|
|
leafName.Find(NS_LITERAL_CSTRING("caches.sqlite-mj"), false, 0, 0) ==
|
|
|
|
0 ||
|
|
|
|
leafName.EqualsLiteral("context_open.marker")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (leafName.EqualsLiteral("caches.sqlite") ||
|
|
|
|
leafName.EqualsLiteral("caches.sqlite-wal")) {
|
|
|
|
int64_t fileSize;
|
|
|
|
rv = file->GetFileSize(&fileSize);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2019-07-20 18:56:31 +03:00
|
|
|
REPORT_TELEMETRY_ERR_IN_INIT(aInitializing, kQuotaExternalError,
|
2019-01-17 20:12:27 +03:00
|
|
|
Cache_GetFileSize);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(fileSize >= 0);
|
|
|
|
|
2019-08-22 23:52:09 +03:00
|
|
|
aUsageInfo->AppendToDatabaseUsage(Some(fileSize));
|
2019-01-17 20:12:27 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore directory padding file
|
|
|
|
if (leafName.EqualsLiteral(PADDING_FILE_NAME) ||
|
|
|
|
leafName.EqualsLiteral(PADDING_TMP_FILE_NAME)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("Unknown Cache file found!");
|
|
|
|
}
|
2019-09-27 19:54:22 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2019-01-17 20:12:27 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheQuotaClient, override)
|
2017-07-20 06:33:39 +03:00
|
|
|
|
|
|
|
// Mutex lock to protect directroy padding files. It should only be acquired
|
|
|
|
// in DOM Cache IO threads and Quota IO thread.
|
|
|
|
mozilla::Mutex mDirPaddingFileMutex;
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
// static
|
|
|
|
CacheQuotaClient* CacheQuotaClient::sInstance = nullptr;
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
|
|
|
|
2017-07-18 13:57:54 +03:00
|
|
|
// static
|
2015-03-02 16:20:00 +03:00
|
|
|
already_AddRefed<quota::Client> CreateQuotaClient() {
|
2015-11-22 12:44:16 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheQuotaClient> ref = new CacheQuotaClient();
|
2015-03-02 16:20:00 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2017-07-18 13:57:54 +03:00
|
|
|
// static
|
|
|
|
template <typename Callable>
|
|
|
|
nsresult MaybeUpdatePaddingFile(nsIFile* aBaseDir, mozIStorageConnection* aConn,
|
|
|
|
const int64_t aIncreaseSize,
|
|
|
|
const int64_t aDecreaseSize,
|
|
|
|
Callable aCommitHook) {
|
2017-07-18 14:03:25 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-07-18 13:57:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBaseDir);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aConn);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aIncreaseSize >= 0);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aDecreaseSize >= 0);
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
RefPtr<CacheQuotaClient> cacheQuotaClient = CacheQuotaClient::Get();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(cacheQuotaClient);
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
nsresult rv = cacheQuotaClient->MaybeUpdatePaddingFileInternal(
|
|
|
|
aBaseDir, aConn, aIncreaseSize, aDecreaseSize, aCommitHook);
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult RestorePaddingFile(nsIFile* aBaseDir, mozIStorageConnection* aConn) {
|
2017-07-18 14:03:25 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-07-18 13:57:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBaseDir);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aConn);
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
RefPtr<CacheQuotaClient> cacheQuotaClient = CacheQuotaClient::Get();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(cacheQuotaClient);
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
nsresult rv = cacheQuotaClient->RestorePaddingFileInternal(aBaseDir, aConn);
|
2017-07-18 13:57:54 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult WipePaddingFile(const QuotaInfo& aQuotaInfo, nsIFile* aBaseDir) {
|
2017-07-18 14:03:25 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-07-18 13:57:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBaseDir);
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
RefPtr<CacheQuotaClient> cacheQuotaClient = CacheQuotaClient::Get();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(cacheQuotaClient);
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
nsresult rv = cacheQuotaClient->WipePaddingFileInternal(aQuotaInfo, aBaseDir);
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|