summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/native_file_system/native_file_system_directory_handle.cc
blob: 8fd7e9c296ecd8736b60f144eca79ed5fa34cb2f (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
// Copyright 2019 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.

#include "third_party/blink/renderer/modules/native_file_system/native_file_system_directory_handle.h"

#include "mojo/public/cpp/bindings/remote.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/mojom/native_file_system/native_file_system_error.mojom-blink.h"
#include "third_party/blink/public/mojom/native_file_system/native_file_system_manager.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/fileapi/file_error.h"
#include "third_party/blink/renderer/modules/native_file_system/file_system_get_directory_options.h"
#include "third_party/blink/renderer/modules/native_file_system/file_system_get_file_options.h"
#include "third_party/blink/renderer/modules/native_file_system/file_system_remove_options.h"
#include "third_party/blink/renderer/modules/native_file_system/native_file_system_directory_iterator.h"
#include "third_party/blink/renderer/modules/native_file_system/native_file_system_error.h"
#include "third_party/blink/renderer/modules/native_file_system/native_file_system_file_handle.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"

namespace blink {
namespace {
// The name to use for the root directory of a sandboxed file system.
constexpr const char kSandboxRootDirectoryName[] = "";
}  // namespace

using mojom::blink::NativeFileSystemErrorPtr;

NativeFileSystemDirectoryHandle::NativeFileSystemDirectoryHandle(
    const String& name,
    RevocableInterfacePtr<mojom::blink::NativeFileSystemDirectoryHandle>
        mojo_ptr)
    : NativeFileSystemHandle(name), mojo_ptr_(std::move(mojo_ptr)) {
  DCHECK(mojo_ptr_);
}

ScriptPromise NativeFileSystemDirectoryHandle::getFile(
    ScriptState* script_state,
    const String& name,
    const FileSystemGetFileOptions* options) {
  auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
  ScriptPromise result = resolver->Promise();

  mojo_ptr_->GetFile(
      name, options->create(),
      WTF::Bind(
          [](ScriptPromiseResolver* resolver, const String& name,
             NativeFileSystemErrorPtr result,
             mojo::PendingRemote<mojom::blink::NativeFileSystemFileHandle>
                 handle) {
            ExecutionContext* context = resolver->GetExecutionContext();
            if (!context)
              return;
            if (result->status != mojom::blink::NativeFileSystemStatus::kOk) {
              native_file_system_error::Reject(resolver, *result);
              return;
            }
            resolver->Resolve(MakeGarbageCollected<NativeFileSystemFileHandle>(
                name,
                RevocableInterfacePtr<mojom::blink::NativeFileSystemFileHandle>(
                    std::move(handle), context->GetInterfaceInvalidator(),
                    context->GetTaskRunner(TaskType::kMiscPlatformAPI))));
          },
          WrapPersistent(resolver), name));

  return result;
}

ScriptPromise NativeFileSystemDirectoryHandle::getDirectory(
    ScriptState* script_state,
    const String& name,
    const FileSystemGetDirectoryOptions* options) {
  auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
  ScriptPromise result = resolver->Promise();

  mojo_ptr_->GetDirectory(
      name, options->create(),
      WTF::Bind(
          [](ScriptPromiseResolver* resolver, const String& name,
             NativeFileSystemErrorPtr result,
             mojom::blink::NativeFileSystemDirectoryHandlePtr handle) {
            ExecutionContext* context = resolver->GetExecutionContext();
            if (!context)
              return;
            if (result->status != mojom::blink::NativeFileSystemStatus::kOk) {
              native_file_system_error::Reject(resolver, *result);
              return;
            }
            resolver->Resolve(
                MakeGarbageCollected<NativeFileSystemDirectoryHandle>(
                    name,
                    RevocableInterfacePtr<
                        mojom::blink::NativeFileSystemDirectoryHandle>(
                        handle.PassInterface(),
                        context->GetInterfaceInvalidator(),
                        context->GetTaskRunner(TaskType::kMiscPlatformAPI))));
          },
          WrapPersistent(resolver), name));

  return result;
}

namespace {

void ReturnDataFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
  V8SetReturnValue(info, info.Data());
}

}  // namespace

ScriptValue NativeFileSystemDirectoryHandle::getEntries(
    ScriptState* script_state) {
  auto* iterator = MakeGarbageCollected<NativeFileSystemDirectoryIterator>(
      this, ExecutionContext::From(script_state));
  auto* isolate = script_state->GetIsolate();
  auto context = script_state->GetContext();
  v8::Local<v8::Object> result = v8::Object::New(isolate);
  if (!result
           ->Set(context, v8::Symbol::GetAsyncIterator(isolate),
                 v8::Function::New(context, &ReturnDataFunction,
                                   ToV8(iterator, script_state))
                     .ToLocalChecked())
           .ToChecked()) {
    return ScriptValue();
  }
  return ScriptValue(script_state, result);
}

ScriptPromise NativeFileSystemDirectoryHandle::removeEntry(
    ScriptState* script_state,
    const String& name,
    const FileSystemRemoveOptions* options) {
  auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
  ScriptPromise result = resolver->Promise();

  mojo_ptr_->RemoveEntry(
      name, options->recursive(),
      WTF::Bind(
          [](ScriptPromiseResolver* resolver, NativeFileSystemErrorPtr result) {
            native_file_system_error::ResolveOrReject(resolver, *result);
          },
          WrapPersistent(resolver)));

  return result;
}

// static
ScriptPromise NativeFileSystemDirectoryHandle::getSystemDirectory(
    ScriptState* script_state,
    const GetSystemDirectoryOptions* options) {
  auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
  ScriptPromise result = resolver->Promise();

  // TODO(mek): Cache mojo::Remote<mojom::blink::NativeFileSystemManager>
  // associated with an ExecutionContext, so we don't have to request a new one
  // for each operation, and can avoid code duplication between here and other
  // uses.
  mojo::Remote<mojom::blink::NativeFileSystemManager> manager;
  auto* provider = ExecutionContext::From(script_state)->GetInterfaceProvider();
  if (!provider) {
    resolver->Reject(file_error::CreateDOMException(
        base::File::FILE_ERROR_INVALID_OPERATION));
    return result;
  }

  provider->GetInterface(manager.BindNewPipeAndPassReceiver());
  auto* raw_manager = manager.get();
  raw_manager->GetSandboxedFileSystem(WTF::Bind(
      [](ScriptPromiseResolver* resolver,
         mojo::Remote<mojom::blink::NativeFileSystemManager>,
         NativeFileSystemErrorPtr result,
         mojom::blink::NativeFileSystemDirectoryHandlePtr handle) {
        ExecutionContext* context = resolver->GetExecutionContext();
        if (!context)
          return;
        if (result->status != mojom::blink::NativeFileSystemStatus::kOk) {
          native_file_system_error::Reject(resolver, *result);
          return;
        }
        resolver->Resolve(MakeGarbageCollected<NativeFileSystemDirectoryHandle>(
            kSandboxRootDirectoryName,
            RevocableInterfacePtr<
                mojom::blink::NativeFileSystemDirectoryHandle>(
                handle.PassInterface(), context->GetInterfaceInvalidator(),
                context->GetTaskRunner(TaskType::kMiscPlatformAPI))));
      },
      WrapPersistent(resolver), std::move(manager)));

  return result;
}

mojom::blink::NativeFileSystemTransferTokenPtr
NativeFileSystemDirectoryHandle::Transfer() {
  mojom::blink::NativeFileSystemTransferTokenPtr result;
  mojo_ptr_->Transfer(mojo::MakeRequest(&result));
  return result;
}

void NativeFileSystemDirectoryHandle::QueryPermissionImpl(
    bool writable,
    base::OnceCallback<void(mojom::blink::PermissionStatus)> callback) {
  mojo_ptr_->GetPermissionStatus(writable, std::move(callback));
}

void NativeFileSystemDirectoryHandle::RequestPermissionImpl(
    bool writable,
    base::OnceCallback<void(mojom::blink::NativeFileSystemErrorPtr,
                            mojom::blink::PermissionStatus)> callback) {
  mojo_ptr_->RequestPermission(writable, std::move(callback));
}

}  // namespace blink