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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/* -*- 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 "FileSystemHandle.h"
#include "FileSystemDirectoryHandle.h"
#include "FileSystemFileHandle.h"
#include "fs/FileSystemRequestHandler.h"
#include "js/StructuredClone.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/FileSystemHandleBinding.h"
#include "mozilla/dom/FileSystemLog.h"
#include "mozilla/dom/FileSystemManager.h"
#include "mozilla/dom/Promise-inl.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/StorageManager.h"
#include "mozilla/dom/StructuredCloneHolder.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"
#include "nsJSPrincipals.h"
#include "nsString.h"
#include "prio.h"
#include "private/pprio.h"
#include "xpcpublic.h"
namespace mozilla::dom {
namespace {
bool ConstructHandleMetadata(JSContext* aCx, nsIGlobalObject* aGlobal,
JSStructuredCloneReader* aReader,
const bool aDirectory,
fs::FileSystemEntryMetadata& aMetadata) {
using namespace mozilla::dom::fs;
EntryId entryId;
if (!entryId.SetLength(32u, fallible)) {
return false;
}
if (!JS_ReadBytes(aReader, static_cast<void*>(entryId.BeginWriting()), 32u)) {
return false;
}
Name name;
if (!StructuredCloneHolder::ReadString(aReader, name)) {
return false;
}
mozilla::ipc::PrincipalInfo storageKey;
if (!nsJSPrincipals::ReadPrincipalInfo(aReader, storageKey)) {
return false;
}
QM_TRY_UNWRAP(auto hasEqualStorageKey,
aGlobal->HasEqualStorageKey(storageKey), false);
if (!hasEqualStorageKey) {
LOG(("Blocking deserialization of %s due to cross-origin",
NS_ConvertUTF16toUTF8(name).get()));
return false;
}
LOG_VERBOSE(("Deserializing %s", NS_ConvertUTF16toUTF8(name).get()));
aMetadata = fs::FileSystemEntryMetadata(entryId, name, aDirectory);
return true;
}
} // namespace
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystemHandle)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(FileSystemHandle)
NS_IMPL_CYCLE_COLLECTING_RELEASE(FileSystemHandle)
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(FileSystemHandle)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(FileSystemHandle)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal)
// Don't unlink mManager!
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(FileSystemHandle)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
FileSystemHandle::FileSystemHandle(
nsIGlobalObject* aGlobal, RefPtr<FileSystemManager>& aManager,
const fs::FileSystemEntryMetadata& aMetadata,
fs::FileSystemRequestHandler* aRequestHandler)
: mGlobal(aGlobal),
mManager(aManager),
mMetadata(aMetadata),
mRequestHandler(aRequestHandler) {
MOZ_ASSERT(!mMetadata.entryId().IsEmpty());
}
// WebIDL Boilerplate
nsIGlobalObject* FileSystemHandle::GetParentObject() const { return mGlobal; }
JSObject* FileSystemHandle::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return FileSystemHandle_Binding::Wrap(aCx, this, aGivenProto);
}
// WebIDL Interface
void FileSystemHandle::GetName(nsAString& aResult) {
aResult = mMetadata.entryName();
}
already_AddRefed<Promise> FileSystemHandle::IsSameEntry(
FileSystemHandle& aOther, ErrorResult& aError) const {
RefPtr<Promise> promise = Promise::Create(GetParentObject(), aError);
if (aError.Failed()) {
return nullptr;
}
// Handles the case of "dir = createdir foo; removeEntry(foo); file =
// createfile foo; issameentry(dir, file)"
const bool result = mMetadata.entryId().Equals(aOther.mMetadata.entryId()) &&
Kind() == aOther.Kind();
promise->MaybeResolve(result);
return promise.forget();
}
already_AddRefed<Promise> FileSystemHandle::Move(const nsAString& aName,
ErrorResult& aError) {
LOG(("Move %s to %s", NS_ConvertUTF16toUTF8(mMetadata.entryName()).get(),
NS_ConvertUTF16toUTF8(aName).get()));
fs::EntryId parent; // empty means same directory
return Move(parent, aName, aError);
}
already_AddRefed<Promise> FileSystemHandle::Move(
FileSystemDirectoryHandle& aParent, ErrorResult& aError) {
LOG(("Move %s to %s/%s", NS_ConvertUTF16toUTF8(mMetadata.entryName()).get(),
NS_ConvertUTF16toUTF8(aParent.mMetadata.entryName()).get(),
NS_ConvertUTF16toUTF8(mMetadata.entryName()).get()));
return Move(aParent, mMetadata.entryName(), aError);
}
already_AddRefed<Promise> FileSystemHandle::Move(
FileSystemDirectoryHandle& aParent, const nsAString& aName,
ErrorResult& aError) {
LOG(("Move %s to %s/%s", NS_ConvertUTF16toUTF8(mMetadata.entryName()).get(),
NS_ConvertUTF16toUTF8(aParent.mMetadata.entryName()).get(),
NS_ConvertUTF16toUTF8(aName).get()));
return Move(aParent.mMetadata.entryId(), aName, aError);
}
already_AddRefed<Promise> FileSystemHandle::Move(const fs::EntryId& aParentId,
const nsAString& aName,
ErrorResult& aError) {
RefPtr<Promise> promise = Promise::Create(GetParentObject(), aError);
if (aError.Failed()) {
return nullptr;
}
fs::FileSystemChildMetadata newMetadata;
newMetadata.parentId() = aParentId;
newMetadata.childName() = aName;
if (!aParentId.IsEmpty()) {
mRequestHandler->MoveEntry(mManager, this, &mMetadata, newMetadata, promise,
aError);
} else {
mRequestHandler->RenameEntry(mManager, this, &mMetadata,
newMetadata.childName(), promise, aError);
}
if (aError.Failed()) {
return nullptr;
}
// Other handles to this will be broken, and the spec is ok with this, but we
// need to update our EntryId and name
promise->AddCallbacksWithCycleCollectedArgs(
[newMetadata](JSContext* aCx, JS::Handle<JS::Value> aValue,
ErrorResult& aRv, FileSystemHandle* aHandle) {
// XXX Fix entryId!
LOG(("Changing FileSystemHandle name from %s to %s",
NS_ConvertUTF16toUTF8(aHandle->mMetadata.entryName()).get(),
NS_ConvertUTF16toUTF8(newMetadata.childName()).get()));
aHandle->mMetadata.entryName() = newMetadata.childName();
},
[](JSContext* aCx, JS::Handle<JS::Value> aValue, ErrorResult& aRv,
FileSystemHandle* aHandle) {
LOG(("reject of move for %s",
NS_ConvertUTF16toUTF8(aHandle->mMetadata.entryName()).get()));
},
RefPtr(this));
return promise.forget();
}
// [Serializable] implementation
// static
already_AddRefed<FileSystemHandle> FileSystemHandle::ReadStructuredClone(
JSContext* aCx, nsIGlobalObject* aGlobal,
JSStructuredCloneReader* aReader) {
LOG_VERBOSE(("Reading File/DirectoryHandle"));
uint32_t kind = static_cast<uint32_t>(FileSystemHandleKind::EndGuard_);
if (!JS_ReadBytes(aReader, reinterpret_cast<void*>(&kind),
sizeof(uint32_t))) {
return nullptr;
}
if (kind == static_cast<uint32_t>(FileSystemHandleKind::Directory)) {
RefPtr<FileSystemHandle> result =
FileSystemHandle::ConstructDirectoryHandle(aCx, aGlobal, aReader);
return result.forget();
}
if (kind == static_cast<uint32_t>(FileSystemHandleKind::File)) {
RefPtr<FileSystemHandle> result =
FileSystemHandle::ConstructFileHandle(aCx, aGlobal, aReader);
return result.forget();
}
return nullptr;
}
bool FileSystemHandle::WriteStructuredClone(
JSContext* aCx, JSStructuredCloneWriter* aWriter) const {
LOG_VERBOSE(("Writing File/DirectoryHandle"));
MOZ_ASSERT(mMetadata.entryId().Length() == 32);
auto kind = static_cast<uint32_t>(Kind());
if (NS_WARN_IF(!JS_WriteBytes(aWriter, static_cast<void*>(&kind),
sizeof(uint32_t)))) {
return false;
}
if (NS_WARN_IF(!JS_WriteBytes(
aWriter, static_cast<const void*>(mMetadata.entryId().get()),
mMetadata.entryId().Length()))) {
return false;
}
if (!StructuredCloneHolder::WriteString(aWriter, mMetadata.entryName())) {
return false;
}
// Needed to make sure the destination nsIGlobalObject is from the same
// origin/principal
QM_TRY_INSPECT(const auto& storageKey, mGlobal->GetStorageKey(), false);
return nsJSPrincipals::WritePrincipalInfo(aWriter, storageKey);
}
// static
already_AddRefed<FileSystemFileHandle> FileSystemHandle::ConstructFileHandle(
JSContext* aCx, nsIGlobalObject* aGlobal,
JSStructuredCloneReader* aReader) {
LOG(("Reading FileHandle"));
fs::FileSystemEntryMetadata metadata;
if (!ConstructHandleMetadata(aCx, aGlobal, aReader, /* aDirectory */ false,
metadata)) {
return nullptr;
}
RefPtr<StorageManager> storageManager = aGlobal->GetStorageManager();
if (!storageManager) {
return nullptr;
}
// Note that the actor may not exist or may not be connected yet.
RefPtr<FileSystemManager> fileSystemManager =
storageManager->GetFileSystemManager();
RefPtr<FileSystemFileHandle> fsHandle =
new FileSystemFileHandle(aGlobal, fileSystemManager, metadata);
return fsHandle.forget();
}
// static
already_AddRefed<FileSystemDirectoryHandle>
FileSystemHandle::ConstructDirectoryHandle(JSContext* aCx,
nsIGlobalObject* aGlobal,
JSStructuredCloneReader* aReader) {
LOG(("Reading DirectoryHandle"));
fs::FileSystemEntryMetadata metadata;
if (!ConstructHandleMetadata(aCx, aGlobal, aReader, /* aDirectory */ true,
metadata)) {
return nullptr;
}
RefPtr<StorageManager> storageManager = aGlobal->GetStorageManager();
if (!storageManager) {
return nullptr;
}
// Note that the actor may not exist or may not be connected yet.
RefPtr<FileSystemManager> fileSystemManager =
storageManager->GetFileSystemManager();
RefPtr<FileSystemDirectoryHandle> fsHandle =
new FileSystemDirectoryHandle(aGlobal, fileSystemManager, metadata);
return fsHandle.forget();
}
} // namespace mozilla::dom
|