1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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"
#include "mozilla/dom/cache/Manager.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/dom/quota/UsageInfo.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "nsIFile.h"
#include "nsISimpleEnumerator.h"
#include "nsThreadUtils.h"
namespace {
using mozilla::Atomic;
using mozilla::dom::ContentParentId;
using mozilla::dom::cache::Manager;
using mozilla::dom::quota::Client;
using mozilla::dom::quota::PersistenceType;
using mozilla::dom::quota::QuotaManager;
using mozilla::dom::quota::UsageInfo;
using mozilla::ipc::AssertIsOnBackgroundThread;
static nsresult
GetBodyUsage(nsIFile* aDir, const Atomic<bool>& aCanceled,
UsageInfo* aUsageInfo)
{
nsCOMPtr<nsISimpleEnumerator> entries;
nsresult rv = aDir->GetDirectoryEntries(getter_AddRefs(entries));
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
bool hasMore;
while (NS_SUCCEEDED(rv = entries->HasMoreElements(&hasMore)) && hasMore &&
!aCanceled) {
nsCOMPtr<nsISupports> entry;
rv = entries->GetNext(getter_AddRefs(entry));
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
nsCOMPtr<nsIFile> file = do_QueryInterface(entry);
bool isDir;
rv = file->IsDirectory(&isDir);
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
if (isDir) {
rv = GetBodyUsage(file, aCanceled, aUsageInfo);
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
continue;
}
int64_t fileSize;
rv = file->GetFileSize(&fileSize);
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
MOZ_DIAGNOSTIC_ASSERT(fileSize >= 0);
aUsageInfo->AppendToFileUsage(fileSize);
}
return NS_OK;
}
class CacheQuotaClient final : public Client
{
public:
virtual Type
GetType() override
{
return DOMCACHE;
}
virtual nsresult
InitOrigin(PersistenceType aPersistenceType, const nsACString& aGroup,
const nsACString& aOrigin, const AtomicBool& aCanceled,
UsageInfo* aUsageInfo) override
{
// The QuotaManager passes a nullptr UsageInfo if there is no quota being
// enforced against the origin.
if (!aUsageInfo) {
return NS_OK;
}
return GetUsageForOrigin(aPersistenceType, aGroup, aOrigin, aCanceled,
aUsageInfo);
}
virtual nsresult
GetUsageForOrigin(PersistenceType aPersistenceType, const nsACString& aGroup,
const nsACString& aOrigin, const AtomicBool& aCanceled,
UsageInfo* aUsageInfo) override
{
MOZ_DIAGNOSTIC_ASSERT(aUsageInfo);
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))) { return rv; }
rv = dir->Append(NS_LITERAL_STRING(DOMCACHE_DIRECTORY_NAME));
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
nsCOMPtr<nsISimpleEnumerator> entries;
rv = dir->GetDirectoryEntries(getter_AddRefs(entries));
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
bool hasMore;
while (NS_SUCCEEDED(rv = entries->HasMoreElements(&hasMore)) && hasMore &&
!aCanceled) {
nsCOMPtr<nsISupports> entry;
rv = entries->GetNext(getter_AddRefs(entry));
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
nsCOMPtr<nsIFile> file = do_QueryInterface(entry);
nsAutoString leafName;
rv = file->GetLeafName(leafName);
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
bool isDir;
rv = file->IsDirectory(&isDir);
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
if (isDir) {
if (leafName.EqualsLiteral("morgue")) {
rv = GetBodyUsage(file, aCanceled, aUsageInfo);
if (NS_WARN_IF(NS_FAILED(rv))) { 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))) { return rv; }
MOZ_DIAGNOSTIC_ASSERT(fileSize >= 0);
aUsageInfo->AppendToDatabaseUsage(fileSize);
continue;
}
NS_WARNING("Unknown Cache file found!");
}
return NS_OK;
}
virtual void
OnOriginClearCompleted(PersistenceType aPersistenceType,
const nsACString& aOrigin) override
{
// Nothing to do here.
}
virtual void
ReleaseIOThreadObjects() override
{
// Nothing to do here as the Context handles cleaning everything up
// automatically.
}
virtual void
AbortOperations(const nsACString& aOrigin) override
{
AssertIsOnBackgroundThread();
Manager::Abort(aOrigin);
}
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.
}
virtual void
StartIdleMaintenance() override
{ }
virtual void
StopIdleMaintenance() override
{ }
virtual void
ShutdownWorkThreads() override
{
AssertIsOnBackgroundThread();
// spins the event loop and synchronously shuts down all Managers
Manager::ShutdownAll();
}
private:
~CacheQuotaClient()
{
AssertIsOnBackgroundThread();
}
NS_INLINE_DECL_REFCOUNTING(CacheQuotaClient, override)
};
} // namespace
namespace mozilla {
namespace dom {
namespace cache {
already_AddRefed<quota::Client> CreateQuotaClient()
{
AssertIsOnBackgroundThread();
RefPtr<CacheQuotaClient> ref = new CacheQuotaClient();
return ref.forget();
}
} // namespace cache
} // namespace dom
} // namespace mozilla
|