tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

FileSystemFileHandle.cpp (3815B)


      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/. */
      6 
      7 #include "FileSystemFileHandle.h"
      8 
      9 #include "fs/FileSystemRequestHandler.h"
     10 #include "js/StructuredClone.h"
     11 #include "js/TypeDecls.h"
     12 #include "mozilla/ErrorResult.h"
     13 #include "mozilla/dom/FileSystemFileHandleBinding.h"
     14 #include "mozilla/dom/FileSystemHandleBinding.h"
     15 #include "mozilla/dom/FileSystemManager.h"
     16 #include "mozilla/dom/PFileSystemManager.h"
     17 #include "mozilla/dom/Promise.h"
     18 
     19 namespace mozilla::dom {
     20 
     21 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(FileSystemFileHandle,
     22                                               FileSystemHandle)
     23 NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemFileHandle, FileSystemHandle)
     24 
     25 FileSystemFileHandle::FileSystemFileHandle(
     26    nsIGlobalObject* aGlobal, RefPtr<FileSystemManager>& aManager,
     27    const fs::FileSystemEntryMetadata& aMetadata,
     28    fs::FileSystemRequestHandler* aRequestHandler)
     29    : FileSystemHandle(aGlobal, aManager, aMetadata, aRequestHandler) {}
     30 
     31 FileSystemFileHandle::FileSystemFileHandle(
     32    nsIGlobalObject* aGlobal, RefPtr<FileSystemManager>& aManager,
     33    const fs::FileSystemEntryMetadata& aMetadata)
     34    : FileSystemFileHandle(aGlobal, aManager, aMetadata,
     35                           new fs::FileSystemRequestHandler()) {}
     36 
     37 // WebIDL Boilerplate
     38 
     39 JSObject* FileSystemFileHandle::WrapObject(JSContext* aCx,
     40                                           JS::Handle<JSObject*> aGivenProto) {
     41  return FileSystemFileHandle_Binding::Wrap(aCx, this, aGivenProto);
     42 }
     43 
     44 // WebIDL Interface
     45 
     46 FileSystemHandleKind FileSystemFileHandle::Kind() const {
     47  return FileSystemHandleKind::File;
     48 }
     49 
     50 already_AddRefed<Promise> FileSystemFileHandle::GetFile(ErrorResult& aError) {
     51  RefPtr<Promise> promise = Promise::Create(GetParentObject(), aError);
     52  if (aError.Failed()) {
     53    return nullptr;
     54  }
     55 
     56  mRequestHandler->GetFile(mManager, mMetadata, promise, aError);
     57  if (aError.Failed()) {
     58    return nullptr;
     59  }
     60 
     61  return promise.forget();
     62 }
     63 
     64 already_AddRefed<Promise> FileSystemFileHandle::CreateWritable(
     65    const FileSystemCreateWritableOptions& aOptions, ErrorResult& aError) {
     66  RefPtr<Promise> promise = Promise::Create(GetParentObject(), aError);
     67  if (aError.Failed()) {
     68    return nullptr;
     69  }
     70 
     71  mRequestHandler->GetWritable(mManager, mMetadata, aOptions.mKeepExistingData,
     72                               promise, aError);
     73  if (aError.Failed()) {
     74    return nullptr;
     75  }
     76 
     77  return promise.forget();
     78 }
     79 
     80 already_AddRefed<Promise> FileSystemFileHandle::CreateSyncAccessHandle(
     81    ErrorResult& aError) {
     82  RefPtr<Promise> promise = Promise::Create(GetParentObject(), aError);
     83  if (aError.Failed()) {
     84    return nullptr;
     85  }
     86 
     87  mRequestHandler->GetAccessHandle(mManager, mMetadata, promise, aError);
     88  if (aError.Failed()) {
     89    return nullptr;
     90  }
     91 
     92  return promise.forget();
     93 }
     94 
     95 // [Serializable] implementation
     96 
     97 // static
     98 already_AddRefed<FileSystemFileHandle>
     99 FileSystemFileHandle::ReadStructuredClone(JSContext* aCx,
    100                                          nsIGlobalObject* aGlobal,
    101                                          JSStructuredCloneReader* aReader) {
    102  uint32_t kind = UINT32_MAX;
    103 
    104  if (!JS_ReadBytes(aReader, reinterpret_cast<void*>(&kind),
    105                    sizeof(uint32_t))) {
    106    return nullptr;
    107  }
    108 
    109  if (kind != static_cast<uint32_t>(FileSystemHandleKind::File)) {
    110    return nullptr;
    111  }
    112 
    113  RefPtr<FileSystemFileHandle> result =
    114      FileSystemHandle::ConstructFileHandle(aCx, aGlobal, aReader);
    115  if (!result) {
    116    return nullptr;
    117  }
    118 
    119  return result.forget();
    120 }
    121 
    122 }  // namespace mozilla::dom