summaryrefslogtreecommitdiffstats
path: root/dom/fs/api/FileSystemSyncAccessHandle.h
blob: ec2ca9bd767ae473ef0eb8d397e1ca5dbaf45b1e (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
/* -*- 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/. */

#ifndef DOM_FS_FILESYSTEMSYNCACCESSHANDLE_H_
#define DOM_FS_FILESYSTEMSYNCACCESSHANDLE_H_

#include "mozilla/dom/PFileSystemManager.h"
#include "mozilla/dom/quota/ForwardDecls.h"
#include "nsCOMPtr.h"
#include "nsISupports.h"
#include "nsWrapperCache.h"

class nsIGlobalObject;

namespace mozilla {

class ErrorResult;
class TaskQueue;

namespace dom {

class FileSystemAccessHandleChild;
struct FileSystemReadWriteOptions;
class FileSystemManager;
class MaybeSharedArrayBufferViewOrMaybeSharedArrayBuffer;
class Promise;
class StrongWorkerRef;

class FileSystemSyncAccessHandle final : public nsISupports,
                                         public nsWrapperCache {
 public:
  enum struct State : uint8_t { Initial = 0, Open, Closing, Closed };

  static Result<RefPtr<FileSystemSyncAccessHandle>, nsresult> Create(
      nsIGlobalObject* aGlobal, RefPtr<FileSystemManager>& aManager,
      RefPtr<FileSystemAccessHandleChild> aActor,
      mozilla::ipc::RandomAccessStreamParams&& aStreamParams,
      const fs::FileSystemEntryMetadata& aMetadata);

  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(FileSystemSyncAccessHandle)

  void LastRelease();

  void ClearActor();

  bool IsOpen() const;

  bool IsClosing() const;

  bool IsClosed() const;

  [[nodiscard]] RefPtr<BoolPromise> BeginClose();

  [[nodiscard]] RefPtr<BoolPromise> OnClose();

  // WebIDL Boilerplate
  nsIGlobalObject* GetParentObject() const;

  JSObject* WrapObject(JSContext* aCx,
                       JS::Handle<JSObject*> aGivenProto) override;

  // WebIDL Interface
  uint64_t Read(
      const MaybeSharedArrayBufferViewOrMaybeSharedArrayBuffer& aBuffer,
      const FileSystemReadWriteOptions& aOptions, ErrorResult& aRv);

  uint64_t Write(
      const MaybeSharedArrayBufferViewOrMaybeSharedArrayBuffer& aBuffer,
      const FileSystemReadWriteOptions& aOptions, ErrorResult& aRv);

  void Truncate(uint64_t aSize, ErrorResult& aError);

  uint64_t GetSize(ErrorResult& aError);

  void Flush(ErrorResult& aError);

  void Close();

 private:
  FileSystemSyncAccessHandle(
      nsIGlobalObject* aGlobal, RefPtr<FileSystemManager>& aManager,
      RefPtr<FileSystemAccessHandleChild> aActor,
      RefPtr<TaskQueue> aIOTaskQueue,
      mozilla::ipc::RandomAccessStreamParams&& aStreamParams,
      const fs::FileSystemEntryMetadata& aMetadata);

  virtual ~FileSystemSyncAccessHandle();

  uint64_t ReadOrWrite(
      const MaybeSharedArrayBufferViewOrMaybeSharedArrayBuffer& aBuffer,
      const FileSystemReadWriteOptions& aOptions, const bool aRead,
      ErrorResult& aRv);

  nsresult EnsureStream();

  nsCOMPtr<nsIGlobalObject> mGlobal;

  RefPtr<FileSystemManager> mManager;

  RefPtr<FileSystemAccessHandleChild> mActor;

  RefPtr<TaskQueue> mIOTaskQueue;

  nsCOMPtr<nsIRandomAccessStream> mStream;

  RefPtr<StrongWorkerRef> mWorkerRef;

  MozPromiseHolder<BoolPromise> mClosePromiseHolder;

  mozilla::ipc::RandomAccessStreamParams mStreamParams;

  const fs::FileSystemEntryMetadata mMetadata;

  State mState;
};

}  // namespace dom
}  // namespace mozilla

#endif  // DOM_FS_FILESYSTEMSYNCACCESSHANDLE_H_