summaryrefslogtreecommitdiffstats
path: root/dom/fs/child/FileSystemRequestHandler.cpp
blob: 939bd41115ba7c7e348ed92755a2446569ba49ed (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/* -*- 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 "fs/FileSystemRequestHandler.h"

#include "FileSystemEntryMetadataArray.h"
#include "fs/FileSystemConstants.h"
#include "mozilla/ResultVariant.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/dom/BlobImpl.h"
#include "mozilla/dom/File.h"
#include "mozilla/dom/FileSystemAccessHandleChild.h"
#include "mozilla/dom/FileSystemDirectoryHandle.h"
#include "mozilla/dom/FileSystemFileHandle.h"
#include "mozilla/dom/FileSystemHandle.h"
#include "mozilla/dom/FileSystemHelpers.h"
#include "mozilla/dom/FileSystemLog.h"
#include "mozilla/dom/FileSystemManager.h"
#include "mozilla/dom/FileSystemManagerChild.h"
#include "mozilla/dom/FileSystemSyncAccessHandle.h"
#include "mozilla/dom/FileSystemWritableFileStream.h"
#include "mozilla/dom/FileSystemWritableFileStreamChild.h"
#include "mozilla/dom/IPCBlobUtils.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/dom/WorkerRef.h"
#include "mozilla/dom/fs/IPCRejectReporter.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "mozilla/ipc/RandomAccessStreamUtils.h"

namespace mozilla::dom::fs {

using mozilla::ipc::RejectCallback;

namespace {

void HandleFailedStatus(nsresult aError, const RefPtr<Promise>& aPromise) {
  switch (aError) {
    case NS_ERROR_FILE_ACCESS_DENIED:
      aPromise->MaybeRejectWithNotAllowedError("Permission denied");
      break;
    case NS_ERROR_FILE_NOT_FOUND:
      [[fallthrough]];
    case NS_ERROR_DOM_NOT_FOUND_ERR:
      aPromise->MaybeRejectWithNotFoundError("Entry not found");
      break;
    case NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR:
      aPromise->MaybeRejectWithInvalidModificationError("Disallowed by system");
      break;
    case NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR:
      aPromise->MaybeRejectWithNoModificationAllowedError(
          "No modification allowed");
      break;
    case NS_ERROR_DOM_TYPE_MISMATCH_ERR:
      aPromise->MaybeRejectWithTypeMismatchError("Wrong type");
      break;
    case NS_ERROR_DOM_INVALID_MODIFICATION_ERR:
      aPromise->MaybeRejectWithInvalidModificationError("Invalid modification");
      break;
    default:
      if (NS_FAILED(aError)) {
        aPromise->MaybeRejectWithUnknownError("Unknown failure");
      } else {
        aPromise->MaybeResolveWithUndefined();
      }
      break;
  }
}

bool MakeResolution(nsIGlobalObject* aGlobal,
                    FileSystemGetEntriesResponse&& aResponse,
                    const bool& /* aResult */,
                    RefPtr<FileSystemEntryMetadataArray>& aSink) {
  // TODO: Add page size to FileSystemConstants, preallocate and handle overflow
  const auto& listing = aResponse.get_FileSystemDirectoryListing();

  for (const auto& it : listing.files()) {
    aSink->AppendElement(it);
  }

  for (const auto& it : listing.directories()) {
    aSink->AppendElement(it);
  }

  return true;
}

RefPtr<FileSystemDirectoryHandle> MakeResolution(
    nsIGlobalObject* aGlobal, FileSystemGetHandleResponse&& aResponse,
    const RefPtr<FileSystemDirectoryHandle>& /* aResult */,
    RefPtr<FileSystemManager>& aManager) {
  RefPtr<FileSystemDirectoryHandle> result = new FileSystemDirectoryHandle(
      aGlobal, aManager,
      FileSystemEntryMetadata(aResponse.get_EntryId(), kRootName,
                              /* directory */ true));
  return result;
}

RefPtr<FileSystemDirectoryHandle> MakeResolution(
    nsIGlobalObject* aGlobal, FileSystemGetHandleResponse&& aResponse,
    const RefPtr<FileSystemDirectoryHandle>& /* aResult */, const Name& aName,
    RefPtr<FileSystemManager>& aManager) {
  RefPtr<FileSystemDirectoryHandle> result = new FileSystemDirectoryHandle(
      aGlobal, aManager,
      FileSystemEntryMetadata(aResponse.get_EntryId(), aName,
                              /* directory */ true));

  return result;
}

RefPtr<FileSystemFileHandle> MakeResolution(
    nsIGlobalObject* aGlobal, FileSystemGetHandleResponse&& aResponse,
    const RefPtr<FileSystemFileHandle>& /* aResult */, const Name& aName,
    RefPtr<FileSystemManager>& aManager) {
  RefPtr<FileSystemFileHandle> result = new FileSystemFileHandle(
      aGlobal, aManager,
      FileSystemEntryMetadata(aResponse.get_EntryId(), aName,
                              /* directory */ false));
  return result;
}

RefPtr<FileSystemSyncAccessHandle> MakeResolution(
    nsIGlobalObject* aGlobal, FileSystemGetAccessHandleResponse&& aResponse,
    const RefPtr<FileSystemSyncAccessHandle>& /* aReturns */,
    const FileSystemEntryMetadata& aMetadata,
    RefPtr<FileSystemManager>& aManager) {
  auto& properties = aResponse.get_FileSystemAccessHandleProperties();

  QM_TRY_UNWRAP(
      RefPtr<FileSystemSyncAccessHandle> result,
      FileSystemSyncAccessHandle::Create(
          aGlobal, aManager, std::move(properties.streamParams()),
          std::move(properties.accessHandleChildEndpoint()),
          std::move(properties.accessHandleControlChildEndpoint()), aMetadata),
      nullptr);

  return result;
}

RefPtr<FileSystemWritableFileStream> MakeResolution(
    nsIGlobalObject* aGlobal,
    FileSystemGetWritableFileStreamResponse&& aResponse,
    const RefPtr<FileSystemWritableFileStream>& /* aReturns */,
    const FileSystemEntryMetadata& aMetadata,
    RefPtr<FileSystemManager>& aManager) {
  auto& properties = aResponse.get_FileSystemWritableFileStreamProperties();

  auto* const actor = static_cast<FileSystemWritableFileStreamChild*>(
      properties.writableFileStream().AsChild().get());

  QM_TRY_UNWRAP(RefPtr<FileSystemWritableFileStream> result,
                FileSystemWritableFileStream::Create(
                    aGlobal, aManager, std::move(properties.streamParams()),
                    actor, aMetadata),
                nullptr);

  return result;
}

RefPtr<File> MakeResolution(nsIGlobalObject* aGlobal,
                            FileSystemGetFileResponse&& aResponse,
                            const RefPtr<File>& /* aResult */,
                            const Name& aName,
                            RefPtr<FileSystemManager>& aManager) {
  auto& fileProperties = aResponse.get_FileSystemFileProperties();

  RefPtr<BlobImpl> blobImpl = IPCBlobUtils::Deserialize(fileProperties.file());
  MOZ_ASSERT(blobImpl);
  RefPtr<File> result = File::Create(aGlobal, blobImpl);
  return result;
}

template <class TResponse, class... Args>
void ResolveCallback(
    TResponse&& aResponse,
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    Args&&... args) {
  MOZ_ASSERT(aPromise);
  QM_TRY(OkIf(Promise::PromiseState::Pending == aPromise->State()), QM_VOID);

  if (TResponse::Tnsresult == aResponse.type()) {
    HandleFailedStatus(aResponse.get_nsresult(), aPromise);
    return;
  }

  auto resolution = MakeResolution(aPromise->GetParentObject(),
                                   std::forward<TResponse>(aResponse),
                                   std::forward<Args>(args)...);
  if (!resolution) {
    aPromise->MaybeRejectWithUnknownError("Could not complete request");
    return;
  }

  aPromise->MaybeResolve(resolution);
}

template <>
void ResolveCallback(
    FileSystemRemoveEntryResponse&& aResponse,
    RefPtr<Promise> aPromise) {  // NOLINT(performance-unnecessary-value-param)
  MOZ_ASSERT(aPromise);
  QM_TRY(OkIf(Promise::PromiseState::Pending == aPromise->State()), QM_VOID);

  if (FileSystemRemoveEntryResponse::Tvoid_t == aResponse.type()) {
    aPromise->MaybeResolveWithUndefined();
    return;
  }

  MOZ_ASSERT(FileSystemRemoveEntryResponse::Tnsresult == aResponse.type());
  HandleFailedStatus(aResponse.get_nsresult(), aPromise);
}

template <>
void ResolveCallback(
    FileSystemMoveEntryResponse&& aResponse,
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    FileSystemEntryMetadata* const& aEntry, const Name& aName) {
  MOZ_ASSERT(aPromise);
  QM_TRY(OkIf(Promise::PromiseState::Pending == aPromise->State()), QM_VOID);

  if (FileSystemMoveEntryResponse::TEntryId == aResponse.type()) {
    if (aEntry) {
      aEntry->entryId() = std::move(aResponse.get_EntryId());
      aEntry->entryName() = aName;
    }

    aPromise->MaybeResolveWithUndefined();
    return;
  }
  MOZ_ASSERT(FileSystemMoveEntryResponse::Tnsresult == aResponse.type());
  const auto& status = aResponse.get_nsresult();
  MOZ_ASSERT(NS_FAILED(status));
  HandleFailedStatus(status, aPromise);
}

template <>
void ResolveCallback(FileSystemResolveResponse&& aResponse,
                     // NOLINTNEXTLINE(performance-unnecessary-value-param)
                     RefPtr<Promise> aPromise) {
  MOZ_ASSERT(aPromise);
  QM_TRY(OkIf(Promise::PromiseState::Pending == aPromise->State()), QM_VOID);

  if (FileSystemResolveResponse::Tnsresult == aResponse.type()) {
    HandleFailedStatus(aResponse.get_nsresult(), aPromise);
    return;
  }

  auto& maybePath = aResponse.get_MaybeFileSystemPath();
  if (maybePath.isSome()) {
    aPromise->MaybeResolve(maybePath.value().path());
    return;
  }

  // Spec says if there is no parent/child relationship, return null
  aPromise->MaybeResolve(JS::NullHandleValue);
}

template <class TResponse, class TReturns, class... Args,
          std::enable_if_t<std::is_same<TReturns, void>::value, bool> = true>
mozilla::ipc::ResolveCallback<TResponse> SelectResolveCallback(
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    Args&&... args) {
  using TOverload = void (*)(TResponse&&, RefPtr<Promise>, Args...);
  return static_cast<std::function<void(TResponse&&)>>(
      // NOLINTNEXTLINE(modernize-avoid-bind)
      std::bind(static_cast<TOverload>(ResolveCallback), std::placeholders::_1,
                aPromise, std::forward<Args>(args)...));
}

template <class TResponse, class TReturns, class... Args,
          std::enable_if_t<!std::is_same<TReturns, void>::value, bool> = true>
mozilla::ipc::ResolveCallback<TResponse> SelectResolveCallback(
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    Args&&... args) {
  using TOverload =
      void (*)(TResponse&&, RefPtr<Promise>, const TReturns&, Args...);
  return static_cast<std::function<void(TResponse&&)>>(
      // NOLINTNEXTLINE(modernize-avoid-bind)
      std::bind(static_cast<TOverload>(ResolveCallback), std::placeholders::_1,
                aPromise, TReturns(), std::forward<Args>(args)...));
}

void RejectCallback(
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    mozilla::ipc::ResponseRejectReason aReason) {
  IPCRejectReporter(aReason);
  QM_TRY(OkIf(Promise::PromiseState::Pending == aPromise->State()), QM_VOID);
  aPromise->MaybeRejectWithUndefined();
}

mozilla::ipc::RejectCallback GetRejectCallback(
    RefPtr<Promise> aPromise) {  // NOLINT(performance-unnecessary-value-param)
  return static_cast<mozilla::ipc::RejectCallback>(
      // NOLINTNEXTLINE(modernize-avoid-bind)
      std::bind(RejectCallback, aPromise, std::placeholders::_1));
}

struct BeginRequestFailureCallback {
  explicit BeginRequestFailureCallback(RefPtr<Promise> aPromise)
      : mPromise(std::move(aPromise)) {}

  void operator()(nsresult aRv) const {
    if (aRv == NS_ERROR_DOM_SECURITY_ERR) {
      mPromise->MaybeRejectWithSecurityError(
          "Security error when calling GetDirectory");
      return;
    }
    mPromise->MaybeRejectWithUnknownError("Could not create actor");
  }

  RefPtr<Promise> mPromise;
};

}  // namespace

void FileSystemRequestHandler::GetRootHandle(
    RefPtr<FileSystemManager>
        aManager,              // NOLINT(performance-unnecessary-value-param)
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    ErrorResult& aError) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(aPromise);
  LOG(("GetRootHandle"));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  aManager->BeginRequest(
      [onResolve = SelectResolveCallback<FileSystemGetHandleResponse,
                                         RefPtr<FileSystemDirectoryHandle>>(
           aPromise, aManager),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendGetRootHandle(std::move(onResolve), std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

void FileSystemRequestHandler::GetDirectoryHandle(
    RefPtr<FileSystemManager>& aManager,
    const FileSystemChildMetadata& aDirectory, bool aCreate,
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    ErrorResult& aError) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(!aDirectory.parentId().IsEmpty());
  MOZ_ASSERT(aPromise);
  LOG(("GetDirectoryHandle"));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  if (!IsValidName(aDirectory.childName())) {
    aPromise->MaybeRejectWithTypeError("Invalid directory name");
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemGetHandleRequest(aDirectory, aCreate),
       onResolve = SelectResolveCallback<FileSystemGetHandleResponse,
                                         RefPtr<FileSystemDirectoryHandle>>(
           aPromise, aDirectory.childName(), aManager),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendGetDirectoryHandle(request, std::move(onResolve),
                                      std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

void FileSystemRequestHandler::GetFileHandle(
    RefPtr<FileSystemManager>& aManager, const FileSystemChildMetadata& aFile,
    bool aCreate,
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    ErrorResult& aError) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(!aFile.parentId().IsEmpty());
  MOZ_ASSERT(aPromise);
  LOG(("GetFileHandle"));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  if (!IsValidName(aFile.childName())) {
    aPromise->MaybeRejectWithTypeError("Invalid filename");
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemGetHandleRequest(aFile, aCreate),
       onResolve = SelectResolveCallback<FileSystemGetHandleResponse,
                                         RefPtr<FileSystemFileHandle>>(
           aPromise, aFile.childName(), aManager),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendGetFileHandle(request, std::move(onResolve),
                                 std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

void FileSystemRequestHandler::GetAccessHandle(
    RefPtr<FileSystemManager>& aManager, const FileSystemEntryMetadata& aFile,
    const RefPtr<Promise>& aPromise, ErrorResult& aError) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(aPromise);
  LOG(("GetAccessHandle %s", NS_ConvertUTF16toUTF8(aFile.entryName()).get()));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemGetAccessHandleRequest(aFile.entryId()),
       onResolve = SelectResolveCallback<FileSystemGetAccessHandleResponse,
                                         RefPtr<FileSystemSyncAccessHandle>>(
           aPromise, aFile, aManager),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendGetAccessHandle(request, std::move(onResolve),
                                   std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

void FileSystemRequestHandler::GetWritable(RefPtr<FileSystemManager>& aManager,
                                           const FileSystemEntryMetadata& aFile,
                                           bool aKeepData,
                                           const RefPtr<Promise>& aPromise,
                                           ErrorResult& aError) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(aPromise);
  LOG(("GetWritable %s keep %d", NS_ConvertUTF16toUTF8(aFile.entryName()).get(),
       aKeepData));

  // XXX This should be removed once bug 1798513 is fixed.
  if (!StaticPrefs::dom_fs_writable_file_stream_enabled()) {
    aError.Throw(NS_ERROR_NOT_IMPLEMENTED);
    return;
  }

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemGetWritableRequest(aFile.entryId(), aKeepData),
       onResolve =
           SelectResolveCallback<FileSystemGetWritableFileStreamResponse,
                                 RefPtr<FileSystemWritableFileStream>>(
               aPromise, aFile, aManager),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendGetWritable(request, std::move(onResolve),
                               std::move(onReject));
      },
      [promise = aPromise](const auto&) {
        promise->MaybeRejectWithUnknownError("Could not create actor");
      });
}

void FileSystemRequestHandler::GetFile(
    RefPtr<FileSystemManager>& aManager, const FileSystemEntryMetadata& aFile,
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    ErrorResult& aError) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(!aFile.entryId().IsEmpty());
  MOZ_ASSERT(aPromise);
  LOG(("GetFile %s", NS_ConvertUTF16toUTF8(aFile.entryName()).get()));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemGetFileRequest(aFile.entryId()),
       onResolve =
           SelectResolveCallback<FileSystemGetFileResponse, RefPtr<File>>(
               aPromise, aFile.entryName(), aManager),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendGetFile(request, std::move(onResolve), std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

void FileSystemRequestHandler::GetEntries(
    RefPtr<FileSystemManager>& aManager, const EntryId& aDirectory,
    PageNumber aPage,
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    RefPtr<FileSystemEntryMetadataArray>& aSink, ErrorResult& aError) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(!aDirectory.IsEmpty());
  MOZ_ASSERT(aPromise);
  LOG(("GetEntries, page %u", aPage));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemGetEntriesRequest(aDirectory, aPage),
       onResolve = SelectResolveCallback<FileSystemGetEntriesResponse, bool>(
           aPromise, aSink),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendGetEntries(request, std::move(onResolve),
                              std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

void FileSystemRequestHandler::RemoveEntry(
    RefPtr<FileSystemManager>& aManager, const FileSystemChildMetadata& aEntry,
    bool aRecursive,
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    ErrorResult& aError) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(!aEntry.parentId().IsEmpty());
  MOZ_ASSERT(aPromise);
  LOG(("RemoveEntry"));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  if (!IsValidName(aEntry.childName())) {
    aPromise->MaybeRejectWithTypeError("Invalid name");
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemRemoveEntryRequest(aEntry, aRecursive),
       onResolve =
           SelectResolveCallback<FileSystemRemoveEntryResponse, void>(aPromise),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendRemoveEntry(request, std::move(onResolve),
                               std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

void FileSystemRequestHandler::MoveEntry(
    RefPtr<FileSystemManager>& aManager, FileSystemHandle* aHandle,
    FileSystemEntryMetadata* const aEntry,
    const FileSystemChildMetadata& aNewEntry,
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    ErrorResult& aError) {
  MOZ_ASSERT(aEntry);
  MOZ_ASSERT(!aEntry->entryId().IsEmpty());
  MOZ_ASSERT(aPromise);
  LOG(("MoveEntry"));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  // reject invalid names: empty, path separators, current & parent directories
  if (!IsValidName(aNewEntry.childName())) {
    aPromise->MaybeRejectWithTypeError("Invalid name");
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemMoveEntryRequest(*aEntry, aNewEntry),
       onResolve = SelectResolveCallback<FileSystemMoveEntryResponse, void>(
           aPromise, aEntry, aNewEntry.childName()),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendMoveEntry(request, std::move(onResolve),
                             std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

void FileSystemRequestHandler::RenameEntry(
    RefPtr<FileSystemManager>& aManager, FileSystemHandle* aHandle,
    FileSystemEntryMetadata* const aEntry, const Name& aName,
    RefPtr<Promise> aPromise,  // NOLINT(performance-unnecessary-value-param)
    ErrorResult& aError) {
  MOZ_ASSERT(aEntry);
  MOZ_ASSERT(!aEntry->entryId().IsEmpty());
  MOZ_ASSERT(aPromise);
  LOG(("RenameEntry"));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  // reject invalid names: empty, path separators, current & parent directories
  if (!IsValidName(aName)) {
    aPromise->MaybeRejectWithTypeError("Invalid name");
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemRenameEntryRequest(*aEntry, aName),
       onResolve = SelectResolveCallback<FileSystemMoveEntryResponse, void>(
           aPromise, aEntry, aName),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendRenameEntry(request, std::move(onResolve),
                               std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

void FileSystemRequestHandler::Resolve(
    RefPtr<FileSystemManager>& aManager,
    // NOLINTNEXTLINE(performance-unnecessary-value-param)
    const FileSystemEntryPair& aEndpoints, RefPtr<Promise> aPromise,
    ErrorResult& aError) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(!aEndpoints.parentId().IsEmpty());
  MOZ_ASSERT(!aEndpoints.childId().IsEmpty());
  MOZ_ASSERT(aPromise);
  LOG(("Resolve"));

  if (aManager->IsShutdown()) {
    aError.Throw(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
    return;
  }

  aManager->BeginRequest(
      [request = FileSystemResolveRequest(aEndpoints),
       onResolve =
           SelectResolveCallback<FileSystemResolveResponse, void>(aPromise),
       onReject = GetRejectCallback(aPromise)](const auto& actor) mutable {
        actor->SendResolve(request, std::move(onResolve), std::move(onReject));
      },
      BeginRequestFailureCallback(aPromise));
}

}  // namespace mozilla::dom::fs