1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_cache_QuotaClientImpl_h
8 #define mozilla_dom_cache_QuotaClientImpl_h
10 #include "CacheCipherKeyManager.h"
11 #include "mozilla/RefPtr.h"
12 #include "mozilla/dom/QMResult.h"
13 #include "mozilla/dom/cache/QuotaClient.h"
14 #include "mozilla/dom/cache/FileUtils.h"
15 #include "mozilla/dom/cache/Types.h"
16 #include "mozilla/dom/quota/ResultExtensions.h"
18 namespace mozilla::dom::cache
{
20 class CacheQuotaClient final
: public quota::Client
{
21 static CacheQuotaClient
* sInstance
;
24 using OriginMetadata
= quota::OriginMetadata
;
25 using PersistenceType
= quota::PersistenceType
;
26 using UsageInfo
= quota::UsageInfo
;
30 static CacheQuotaClient
* Get();
32 virtual Type
GetType() override
;
34 virtual Result
<UsageInfo
, nsresult
> InitOrigin(
35 PersistenceType aPersistenceType
, const OriginMetadata
& aOriginMetadata
,
36 const AtomicBool
& aCanceled
) override
;
38 virtual nsresult
InitOriginWithoutTracking(
39 PersistenceType aPersistenceType
, const OriginMetadata
& aOriginMetadata
,
40 const AtomicBool
& aCanceled
) override
;
42 virtual Result
<UsageInfo
, nsresult
> GetUsageForOrigin(
43 PersistenceType aPersistenceType
, const OriginMetadata
& aOriginMetadata
,
44 const AtomicBool
& aCanceled
) override
;
46 virtual void OnOriginClearCompleted(
47 const OriginMetadata
& aOriginMetadata
) override
;
49 void OnRepositoryClearCompleted(PersistenceType aPersistenceType
) override
;
51 virtual void ReleaseIOThreadObjects() override
;
53 void AbortOperationsForLocks(
54 const DirectoryLockIdTable
& aDirectoryLockIds
) override
;
56 virtual void AbortOperationsForProcess(
57 ContentParentId aContentParentId
) override
;
59 virtual void AbortAllOperations() override
;
61 virtual void StartIdleMaintenance() override
;
63 virtual void StopIdleMaintenance() override
;
65 nsresult
UpgradeStorageFrom2_0To2_1(nsIFile
* aDirectory
) override
;
67 template <typename Callable
>
68 nsresult
MaybeUpdatePaddingFileInternal(nsIFile
& aBaseDir
,
69 mozIStorageConnection
& aConn
,
70 const int64_t aIncreaseSize
,
71 const int64_t aDecreaseSize
,
72 Callable
&& aCommitHook
) {
73 MOZ_ASSERT(!NS_IsMainThread());
74 MOZ_DIAGNOSTIC_ASSERT(aIncreaseSize
>= 0);
75 MOZ_DIAGNOSTIC_ASSERT(aDecreaseSize
>= 0);
77 // Temporary should be removed at the end of each action. If not, it means
78 // the failure happened.
79 const bool temporaryPaddingFileExist
=
80 DirectoryPaddingFileExists(aBaseDir
, DirPaddingFile::TMP_FILE
);
82 if (aIncreaseSize
== aDecreaseSize
&& !temporaryPaddingFileExist
) {
83 // Early return here, since most cache actions won't modify padding size.
84 QM_TRY(MOZ_TO_RESULT(aCommitHook()));
89 // Don't delete the temporary padding file in case of an error to force the
90 // next action recalculate the padding size.
92 UpdateDirectoryPaddingFile(aBaseDir
, aConn
, aIncreaseSize
,
93 aDecreaseSize
, temporaryPaddingFileExist
)));
95 // Don't delete the temporary padding file in case of an error to force the
96 // next action recalculate the padding size.
97 QM_TRY(MOZ_TO_RESULT(aCommitHook()));
99 QM_WARNONLY_TRY(MOZ_TO_RESULT(DirectoryPaddingFinalizeWrite(aBaseDir
)),
100 ([&aBaseDir
](const nsresult
) {
101 // Force restore file next time.
102 QM_WARNONLY_TRY(QM_TO_RESULT(DirectoryPaddingDeleteFile(
103 aBaseDir
, DirPaddingFile::FILE)));
105 // Ensure that we are able to force the padding file to
107 MOZ_ASSERT(DirectoryPaddingFileExists(
108 aBaseDir
, DirPaddingFile::TMP_FILE
));
110 // Since both the body file and header have been stored
111 // in the file-system, just make the action be resolve
112 // and let the padding file be restored in the next
119 nsresult
RestorePaddingFileInternal(nsIFile
* aBaseDir
,
120 mozIStorageConnection
* aConn
);
122 nsresult
WipePaddingFileInternal(
123 const CacheDirectoryMetadata
& aDirectoryMetadata
, nsIFile
* aBaseDir
);
125 RefPtr
<CipherKeyManager
> GetOrCreateCipherKeyManager(
126 const quota::PrincipalMetadata
& aMetadata
);
131 void InitiateShutdown() override
;
132 bool IsShutdownCompleted() const override
;
133 nsCString
GetShutdownStatus() const override
;
134 void ForceKillActors() override
;
135 void FinalizeShutdown() override
;
137 // Should always be accessed from QM IO thread.
138 nsTHashMap
<nsCStringHashKey
, RefPtr
<CipherKeyManager
>> mCipherKeyManagers
;
140 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheQuotaClient
, override
)
143 } // namespace mozilla::dom::cache
145 #endif // mozilla_dom_cache_QuotaClientImpl_h