summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/native_io/native_io_file.h
blob: db236c543bf06d6eeabe8ab0c1b8bae81db8805e (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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_NATIVE_IO_NATIVE_IO_FILE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_NATIVE_IO_NATIVE_IO_FILE_H_

#include <memory>

#include "base/files/file.h"
#include "base/memory/scoped_refptr.h"
#include "third_party/blink/public/mojom/native_io/native_io.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/typed_arrays/array_buffer_view_helpers.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer_view.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace base {
class SequencedTaskRunner;
}  // namespace base

namespace blink {

class DOMSharedArrayBuffer;
class ExecutionContext;
class ScriptPromiseResolver;
class ScriptState;

class NativeIOFile final : public ScriptWrappable {
  DEFINE_WRAPPERTYPEINFO();

 public:
  NativeIOFile(base::File backing_file,
               HeapMojoRemote<mojom::blink::NativeIOFileHost> backend_file,
               ExecutionContext*);

  NativeIOFile(const NativeIOFile&) = delete;
  NativeIOFile& operator=(const NativeIOFile&) = delete;

  // Needed because of the mojo::Remote<mojom::blink::NativeIOFile>.
  ~NativeIOFile() override;

  ScriptPromise close(ScriptState*);
  ScriptPromise read(ScriptState*,
                     MaybeShared<DOMArrayBufferView> buffer,
                     uint64_t file_offset,
                     ExceptionState&);
  ScriptPromise write(ScriptState*,
                      MaybeShared<DOMArrayBufferView> buffer,
                      uint64_t file_offset,
                      ExceptionState&);

  // GarbageCollected
  void Trace(Visitor* visitor) const override;

 private:
  // Data accessed on the threads that do file I/O.
  //
  // Instances are allocated on the PartitionAlloc heap. Instances are initially
  // constructed on Blink's main thread, or on a worker thread. Afterwards,
  // instances are only accessed on dedicated threads that do blocking file I/O.
  struct FileState;

  // Called when the mojo backend disconnects.
  void OnBackendDisconnect();

  // Runs any queued close operation.
  void DispatchQueuedClose();

  // Performs the file I/O part of close().
  static void DoClose(
      CrossThreadPersistent<NativeIOFile> native_io_file,
      CrossThreadPersistent<ScriptPromiseResolver> resolver,
      NativeIOFile::FileState* file_state,
      scoped_refptr<base::SequencedTaskRunner> file_task_runner);
  // Performs the post file-I/O part of close(), on the main thread.
  void DidClose(CrossThreadPersistent<ScriptPromiseResolver> resolver);

  // Performs the file I/O part of read().
  static void DoRead(
      CrossThreadPersistent<NativeIOFile> native_io_file,
      CrossThreadPersistent<ScriptPromiseResolver> resolver,
      CrossThreadPersistent<DOMSharedArrayBuffer> read_buffer_keepalive,
      NativeIOFile::FileState* file_state,
      scoped_refptr<base::SequencedTaskRunner> file_task_runner,
      char* read_buffer,
      uint64_t file_offset,
      int read_size);
  // Performs the post file-I/O part of read(), on the main thread.
  void DidRead(CrossThreadPersistent<ScriptPromiseResolver> resolver,
               int read_bytes,
               base::File::Error read_error);

  // Performs the file I/O part of write().
  static void DoWrite(
      CrossThreadPersistent<NativeIOFile> native_io_file,
      CrossThreadPersistent<ScriptPromiseResolver> resolver,
      CrossThreadPersistent<DOMSharedArrayBuffer> write_data_keepalive,
      NativeIOFile::FileState* file_state,
      scoped_refptr<base::SequencedTaskRunner> file_task_runner,
      const char* write_data,
      uint64_t file_offset,
      int write_size);
  // Performs the post file-I/O part of write(), on the main thread.
  void DidWrite(CrossThreadPersistent<ScriptPromiseResolver> resolver,
                int written_bytes,
                base::File::Error write_error);

  // Kicks off closing the file from the main thread.
  void CloseBackingFile();

  // True when an I/O operation other than close is underway.
  //
  // Checked before kicking off any I/O operation except for close(). This
  // ensures that at most one I/O operation is underway at any given time.
  bool io_pending_ = false;

  // Non-null when a close() I/O is queued behind another I/O operation.
  //
  // Set when close() is called while another I/O operation is underway. Cleared
  // when the queued close() operation is queued.
  Member<ScriptPromiseResolver> queued_close_resolver_;

  // Set to true when close() is called, or when the backend goes away.
  bool closed_ = false;

  // See NativeIO::FileState, declared above.
  const std::unique_ptr<FileState> file_state_;

  // Schedules resolving Promises with file I/O results.
  const scoped_refptr<base::SequencedTaskRunner> resolver_task_runner_;

  // Mojo pipe that holds the renderer's lock on the file.
  HeapMojoRemote<mojom::blink::NativeIOFileHost> backend_file_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_NATIVE_IO_NATIVE_IO_FILE_H_