Commit e711fc3c authored by Marijn Kruisselbrink's avatar Marijn Kruisselbrink Committed by Commit Bot

[FileSystem] Start adding new FileSystem*Handle interfaces.

These are more or less promisified versions of the existing FileEntry
and DirectoryEntry interfaces.

Tbr: jbroman@chromium.org
Bug: 872465
Change-Id: Idccd40c4ef3e94bbf12be2d5a1bb6e44019929ab
Reviewed-on: https://chromium-review.googlesource.com/1176555
Commit-Queue: Marijn Kruisselbrink <mek@chromium.org>
Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585244}
parent bf5a6e04
...@@ -42,9 +42,15 @@ blink_modules_sources("filesystem") { ...@@ -42,9 +42,15 @@ blink_modules_sources("filesystem") {
"file_entry.h", "file_entry.h",
"file_entry_sync.cc", "file_entry_sync.cc",
"file_entry_sync.h", "file_entry_sync.h",
"file_system_base_handle.cc",
"file_system_base_handle.h",
"file_system_callbacks.cc", "file_system_callbacks.cc",
"file_system_callbacks.h", "file_system_callbacks.h",
"file_system_client.h", "file_system_client.h",
"file_system_directory_handle.cc",
"file_system_directory_handle.h",
"file_system_file_handle.cc",
"file_system_file_handle.h",
"file_system_writer.cc", "file_system_writer.cc",
"file_system_writer.h", "file_system_writer.h",
"file_writer.cc", "file_writer.cc",
......
...@@ -52,4 +52,11 @@ ...@@ -52,4 +52,11 @@
// TODO(crbug.com/841185): |successCallback| and |errorCallback| are not // TODO(crbug.com/841185): |successCallback| and |errorCallback| are not
// nullable in the spec. // nullable in the spec.
[CallWith=ScriptState] void getParent(optional EntryCallback? successCallback, optional ErrorCallback? errorCallback); [CallWith=ScriptState] void getParent(optional EntryCallback? successCallback, optional ErrorCallback? errorCallback);
// Primarily exposed for testing, to give script some way of getting new
// style handles.
// TODO(mek): Remove this method once API to get new-style file handles
// directly has been implemented.
[RuntimeEnabled=WritableFiles]
FileSystemBaseHandle asFileSystemHandle();
}; };
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
#include "third_party/blink/renderer/modules/filesystem/dom_file_path.h" #include "third_party/blink/renderer/modules/filesystem/dom_file_path.h"
#include "third_party/blink/renderer/modules/filesystem/dom_file_system_base.h" #include "third_party/blink/renderer/modules/filesystem/dom_file_system_base.h"
#include "third_party/blink/renderer/modules/filesystem/file_system_directory_handle.h"
#include "third_party/blink/renderer/modules/filesystem/file_system_file_handle.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h" #include "third_party/blink/renderer/platform/weborigin/security_origin.h"
namespace blink { namespace blink {
...@@ -55,6 +57,13 @@ String EntryBase::toURL() const { ...@@ -55,6 +57,13 @@ String EntryBase::toURL() const {
return cached_url_; return cached_url_;
} }
FileSystemBaseHandle* EntryBase::asFileSystemHandle() const {
if (isFile())
return new FileSystemFileHandle(filesystem(), fullPath());
DCHECK(isDirectory());
return new FileSystemDirectoryHandle(filesystem(), fullPath());
}
void EntryBase::Trace(blink::Visitor* visitor) { void EntryBase::Trace(blink::Visitor* visitor) {
visitor->Trace(file_system_); visitor->Trace(file_system_);
ScriptWrappable::Trace(visitor); ScriptWrappable::Trace(visitor);
......
...@@ -40,6 +40,7 @@ namespace blink { ...@@ -40,6 +40,7 @@ namespace blink {
class DOMFileSystemBase; class DOMFileSystemBase;
class EntrySync; class EntrySync;
class FileSystemBaseHandle;
// A common base class for Entry and EntrySync. // A common base class for Entry and EntrySync.
class MODULES_EXPORT EntryBase : public ScriptWrappable { class MODULES_EXPORT EntryBase : public ScriptWrappable {
...@@ -56,6 +57,8 @@ class MODULES_EXPORT EntryBase : public ScriptWrappable { ...@@ -56,6 +57,8 @@ class MODULES_EXPORT EntryBase : public ScriptWrappable {
String toURL() const; String toURL() const;
FileSystemBaseHandle* asFileSystemHandle() const;
void Trace(blink::Visitor*) override; void Trace(blink::Visitor*) override;
protected: protected:
......
// Copyright 2018 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/filesystem/file_system_base_handle.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/modules/filesystem/dom_file_system_base.h"
#include "third_party/blink/renderer/modules/filesystem/file_system_callbacks.h"
namespace blink {
FileSystemBaseHandle::FileSystemBaseHandle(DOMFileSystemBase* file_system,
const String& full_path)
: EntryBase(file_system, full_path) {}
ScriptPromise FileSystemBaseHandle::getParent(ScriptState* script_state) {
auto* resolver = ScriptPromiseResolver::Create(script_state);
ScriptPromise result = resolver->Promise();
filesystem()->GetParent(
this, new EntryCallbacks::OnDidGetEntryPromiseImpl(resolver),
new PromiseErrorCallback(resolver));
return result;
}
} // namespace blink
// Copyright 2018 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_FILESYSTEM_FILE_SYSTEM_BASE_HANDLE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_FILESYSTEM_FILE_SYSTEM_BASE_HANDLE_H_
#include "third_party/blink/renderer/modules/filesystem/entry_base.h"
namespace blink {
class ScriptPromise;
class ScriptState;
class FileSystemBaseHandle : public EntryBase {
DEFINE_WRAPPERTYPEINFO();
public:
explicit FileSystemBaseHandle(DOMFileSystemBase*, const String& full_path);
ScriptPromise getParent(ScriptState*);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_FILESYSTEM_FILE_SYSTEM_BASE_HANDLE_H_
// Copyright 2018 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.
// https://github.com/WICG/writable-files/blob/master/EXPLAINER.md
[
RuntimeEnabled=WritableFiles,
NoInterfaceObject
] interface FileSystemBaseHandle {
// Brand checking APIs because javascript makes it otherwise really hard to
// figure out what type an object is when you don't know in which global
// (i.e. iframe) the object was created.
readonly attribute boolean isFile;
readonly attribute boolean isDirectory;
readonly attribute USVString name;
[CallWith=ScriptState] Promise<FileSystemDirectoryHandle> getParent();
// TODO(mek): Other methods to move/copy/delete entries.
};
...@@ -34,6 +34,8 @@ ...@@ -34,6 +34,8 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "third_party/blink/public/platform/web_file_writer.h" #include "third_party/blink/public/platform/web_file_writer.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/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/fileapi/file.h" #include "third_party/blink/renderer/core/fileapi/file.h"
#include "third_party/blink/renderer/core/fileapi/file_error.h" #include "third_party/blink/renderer/core/fileapi/file_error.h"
...@@ -44,6 +46,7 @@ ...@@ -44,6 +46,7 @@
#include "third_party/blink/renderer/modules/filesystem/dom_file_system_base.h" #include "third_party/blink/renderer/modules/filesystem/dom_file_system_base.h"
#include "third_party/blink/renderer/modules/filesystem/entry.h" #include "third_party/blink/renderer/modules/filesystem/entry.h"
#include "third_party/blink/renderer/modules/filesystem/file_entry.h" #include "third_party/blink/renderer/modules/filesystem/file_entry.h"
#include "third_party/blink/renderer/modules/filesystem/file_system_base_handle.h"
#include "third_party/blink/renderer/modules/filesystem/file_writer.h" #include "third_party/blink/renderer/modules/filesystem/file_writer.h"
#include "third_party/blink/renderer/modules/filesystem/metadata.h" #include "third_party/blink/renderer/modules/filesystem/metadata.h"
#include "third_party/blink/renderer/platform/file_metadata.h" #include "third_party/blink/renderer/platform/file_metadata.h"
...@@ -127,6 +130,20 @@ void ScriptErrorCallback::Invoke(FileError::ErrorCode error) { ...@@ -127,6 +130,20 @@ void ScriptErrorCallback::Invoke(FileError::ErrorCode error) {
ScriptErrorCallback::ScriptErrorCallback(V8ErrorCallback* callback) ScriptErrorCallback::ScriptErrorCallback(V8ErrorCallback* callback)
: callback_(ToV8PersistentCallbackInterface(callback)) {} : callback_(ToV8PersistentCallbackInterface(callback)) {}
// PromiseErrorCallback -------------------------------------------------------
PromiseErrorCallback::PromiseErrorCallback(ScriptPromiseResolver* resolver)
: resolver_(resolver) {}
void PromiseErrorCallback::Trace(Visitor* visitor) {
ErrorCallbackBase::Trace(visitor);
visitor->Trace(resolver_);
}
void PromiseErrorCallback::Invoke(FileError::ErrorCode error) {
resolver_->Reject(FileError::CreateDOMException(error));
}
// EntryCallbacks ------------------------------------------------------------- // EntryCallbacks -------------------------------------------------------------
void EntryCallbacks::OnDidGetEntryV8Impl::Trace(blink::Visitor* visitor) { void EntryCallbacks::OnDidGetEntryV8Impl::Trace(blink::Visitor* visitor) {
...@@ -138,6 +155,19 @@ void EntryCallbacks::OnDidGetEntryV8Impl::OnSuccess(Entry* entry) { ...@@ -138,6 +155,19 @@ void EntryCallbacks::OnDidGetEntryV8Impl::OnSuccess(Entry* entry) {
callback_->InvokeAndReportException(nullptr, entry); callback_->InvokeAndReportException(nullptr, entry);
} }
EntryCallbacks::OnDidGetEntryPromiseImpl::OnDidGetEntryPromiseImpl(
ScriptPromiseResolver* resolver)
: resolver_(resolver) {}
void EntryCallbacks::OnDidGetEntryPromiseImpl::Trace(Visitor* visitor) {
OnDidGetEntryCallback::Trace(visitor);
visitor->Trace(resolver_);
}
void EntryCallbacks::OnDidGetEntryPromiseImpl::OnSuccess(Entry* entry) {
resolver_->Resolve(entry->asFileSystemHandle());
}
std::unique_ptr<AsyncFileSystemCallbacks> EntryCallbacks::Create( std::unique_ptr<AsyncFileSystemCallbacks> EntryCallbacks::Create(
OnDidGetEntryCallback* success_callback, OnDidGetEntryCallback* success_callback,
ErrorCallbackBase* error_callback, ErrorCallbackBase* error_callback,
......
...@@ -59,6 +59,7 @@ class File; ...@@ -59,6 +59,7 @@ class File;
class FileMetadata; class FileMetadata;
class FileWriterBase; class FileWriterBase;
class Metadata; class Metadata;
class ScriptPromiseResolver;
// Passed to DOMFileSystem implementations that may report errors. Subclasses // Passed to DOMFileSystem implementations that may report errors. Subclasses
// may capture the error for throwing on return to script (for synchronous APIs) // may capture the error for throwing on return to script (for synchronous APIs)
...@@ -117,6 +118,16 @@ class ScriptErrorCallback final : public ErrorCallbackBase { ...@@ -117,6 +118,16 @@ class ScriptErrorCallback final : public ErrorCallbackBase {
Member<V8PersistentCallbackInterface<V8ErrorCallback>> callback_; Member<V8PersistentCallbackInterface<V8ErrorCallback>> callback_;
}; };
class PromiseErrorCallback final : public ErrorCallbackBase {
public:
explicit PromiseErrorCallback(ScriptPromiseResolver*);
void Trace(Visitor*) override;
void Invoke(FileError::ErrorCode) override;
private:
Member<ScriptPromiseResolver> resolver_;
};
class EntryCallbacks final : public FileSystemCallbacksBase { class EntryCallbacks final : public FileSystemCallbacksBase {
public: public:
class OnDidGetEntryCallback class OnDidGetEntryCallback
...@@ -145,6 +156,16 @@ class EntryCallbacks final : public FileSystemCallbacksBase { ...@@ -145,6 +156,16 @@ class EntryCallbacks final : public FileSystemCallbacksBase {
Member<V8PersistentCallbackInterface<V8EntryCallback>> callback_; Member<V8PersistentCallbackInterface<V8EntryCallback>> callback_;
}; };
class OnDidGetEntryPromiseImpl : public OnDidGetEntryCallback {
public:
explicit OnDidGetEntryPromiseImpl(ScriptPromiseResolver*);
void Trace(Visitor*) override;
void OnSuccess(Entry*) override;
private:
Member<ScriptPromiseResolver> resolver_;
};
static std::unique_ptr<AsyncFileSystemCallbacks> Create( static std::unique_ptr<AsyncFileSystemCallbacks> Create(
OnDidGetEntryCallback*, OnDidGetEntryCallback*,
ErrorCallbackBase*, ErrorCallbackBase*,
......
// Copyright 2018 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/filesystem/file_system_directory_handle.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/modules/filesystem/dom_file_system_base.h"
#include "third_party/blink/renderer/modules/filesystem/file_system_callbacks.h"
namespace blink {
FileSystemDirectoryHandle::FileSystemDirectoryHandle(
DOMFileSystemBase* file_system,
const String& full_path)
: FileSystemBaseHandle(file_system, full_path) {}
ScriptPromise FileSystemDirectoryHandle::getFile(ScriptState* script_state,
const String& name) {
auto* resolver = ScriptPromiseResolver::Create(script_state);
ScriptPromise result = resolver->Promise();
filesystem()->GetFile(this, name, FileSystemFlags(),
new EntryCallbacks::OnDidGetEntryPromiseImpl(resolver),
new PromiseErrorCallback(resolver));
return result;
}
ScriptPromise FileSystemDirectoryHandle::getDirectory(ScriptState* script_state,
const String& name) {
auto* resolver = ScriptPromiseResolver::Create(script_state);
ScriptPromise result = resolver->Promise();
filesystem()->GetDirectory(
this, name, FileSystemFlags(),
new EntryCallbacks::OnDidGetEntryPromiseImpl(resolver),
new PromiseErrorCallback(resolver));
return result;
}
} // namespace blink
// Copyright 2018 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_FILESYSTEM_FILE_SYSTEM_DIRECTORY_HANDLE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_FILESYSTEM_FILE_SYSTEM_DIRECTORY_HANDLE_H_
#include "third_party/blink/renderer/modules/filesystem/file_system_base_handle.h"
namespace blink {
class FileSystemDirectoryHandle : public FileSystemBaseHandle {
DEFINE_WRAPPERTYPEINFO();
public:
FileSystemDirectoryHandle(DOMFileSystemBase*, const String& full_path);
bool isDirectory() const override { return true; }
ScriptPromise getFile(ScriptState*, const String& name);
ScriptPromise getDirectory(ScriptState*, const String& name);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_FILESYSTEM_FILE_SYSTEM_DIRECTORY_HANDLE_H_
// Copyright 2018 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.
// https://github.com/WICG/writable-files/blob/master/EXPLAINER.md
[
RuntimeEnabled=WritableFiles
] interface FileSystemDirectoryHandle : FileSystemBaseHandle {
[CallWith=ScriptState] Promise<FileSystemFileHandle> getFile(USVString name);
[CallWith=ScriptState] Promise<FileSystemDirectoryHandle> getDirectory(USVString name);
// TODO(mek): Other methods such as getEntries etc.
};
// Copyright 2018 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/filesystem/file_system_file_handle.h"
#include "third_party/blink/public/platform/web_file_system.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/fileapi/file.h"
#include "third_party/blink/renderer/modules/filesystem/dom_file_system.h"
#include "third_party/blink/renderer/modules/filesystem/file_system_callbacks.h"
#include "third_party/blink/renderer/modules/filesystem/file_system_writer.h"
namespace blink {
namespace {
class OnDidCreateSnapshotFilePromise
: public SnapshotFileCallback::OnDidCreateSnapshotFileCallback {
public:
explicit OnDidCreateSnapshotFilePromise(ScriptPromiseResolver* resolver)
: resolver_(resolver) {}
void Trace(Visitor* visitor) override {
OnDidCreateSnapshotFileCallback::Trace(visitor);
visitor->Trace(resolver_);
}
void OnSuccess(File* file) override { resolver_->Resolve(file); }
private:
Member<ScriptPromiseResolver> resolver_;
};
} // namespace
FileSystemFileHandle::FileSystemFileHandle(DOMFileSystemBase* file_system,
const String& full_path)
: FileSystemBaseHandle(file_system, full_path) {}
ScriptPromise FileSystemFileHandle::createWriter(ScriptState* script_state) {
// TODO(mek): Implement this.
return ScriptPromise::RejectWithDOMException(
script_state, FileError::CreateDOMException(FileError::kAbortErr));
}
ScriptPromise FileSystemFileHandle::getFile(ScriptState* script_state) {
if (!filesystem()->FileSystem()) {
return ScriptPromise::RejectWithDOMException(
script_state, FileError::CreateDOMException(FileError::kAbortErr));
}
auto* resolver = ScriptPromiseResolver::Create(script_state);
ScriptPromise result = resolver->Promise();
KURL file_system_url = filesystem()->CreateFileSystemURL(this);
filesystem()->FileSystem()->CreateSnapshotFileAndReadMetadata(
file_system_url,
SnapshotFileCallback::Create(filesystem(), name(), file_system_url,
new OnDidCreateSnapshotFilePromise(resolver),
new PromiseErrorCallback(resolver),
ExecutionContext::From(script_state)));
return result;
}
} // namespace blink
// Copyright 2018 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_FILESYSTEM_FILE_SYSTEM_FILE_HANDLE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_FILESYSTEM_FILE_SYSTEM_FILE_HANDLE_H_
#include "third_party/blink/renderer/modules/filesystem/file_system_base_handle.h"
namespace blink {
class FileSystemFileHandle : public FileSystemBaseHandle {
DEFINE_WRAPPERTYPEINFO();
public:
FileSystemFileHandle(DOMFileSystemBase*, const String& full_path);
bool isFile() const override { return true; }
ScriptPromise createWriter(ScriptState*);
ScriptPromise getFile(ScriptState*);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_FILESYSTEM_FILE_SYSTEM_FILE_HANDLE_H_
// Copyright 2018 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.
// https://github.com/WICG/writable-files/blob/master/EXPLAINER.md
[
RuntimeEnabled=WritableFiles
] interface FileSystemFileHandle : FileSystemBaseHandle {
[CallWith=ScriptState] Promise<FileSystemWriter> createWriter();
[CallWith=ScriptState] Promise<File> getFile();
};
...@@ -135,6 +135,9 @@ modules_idl_files = ...@@ -135,6 +135,9 @@ modules_idl_files =
"filesystem/file_callback.idl", "filesystem/file_callback.idl",
"filesystem/file_entry.idl", "filesystem/file_entry.idl",
"filesystem/file_entry_sync.idl", "filesystem/file_entry_sync.idl",
"filesystem/file_system_base_handle.idl",
"filesystem/file_system_directory_handle.idl",
"filesystem/file_system_file_handle.idl",
"filesystem/file_system_callback.idl", "filesystem/file_system_callback.idl",
"filesystem/file_system_writer.idl", "filesystem/file_system_writer.idl",
"filesystem/file_writer.idl", "filesystem/file_writer.idl",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment