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

[FileSystem] Hook up new FileWriter API to backend.

Implements method to create a FileWriter for a FileSystemFileHandle, as
well as fixes a bug in the FileSystemWriter implementation.

Bug: 872460, 872465
Change-Id: Idfd7d2b486a7f2e1b0ec3f996ba940e10236b764
Reviewed-on: https://chromium-review.googlesource.com/1185624Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Commit-Queue: Marijn Kruisselbrink <mek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#586335}
parent 6d44793d
......@@ -27,6 +27,7 @@
#include "content/common/fileapi/webblob_messages.h"
#include "content/public/browser/browser_thread.h"
#include "ipc/ipc_platform_file.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "net/base/mime_util.h"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_storage_context.h"
......@@ -34,6 +35,7 @@
#include "storage/browser/fileapi/file_observers.h"
#include "storage/browser/fileapi/file_permission_policy.h"
#include "storage/browser/fileapi/file_system_context.h"
#include "storage/browser/fileapi/file_writer_impl.h"
#include "storage/browser/fileapi/isolated_context.h"
#include "storage/common/fileapi/file_system_info.h"
#include "storage/common/fileapi/file_system_type_converters.h"
......@@ -513,26 +515,6 @@ void FileSystemManagerImpl::TouchFile(const GURL& path,
base::Passed(&callback)));
}
void FileSystemManagerImpl::Cancel(
OperationID op_id,
FileSystemCancellableOperationImpl::CancelCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
operation_runner()->Cancel(
op_id, base::BindRepeating(&FileSystemManagerImpl::DidFinish,
GetWeakPtr(), base::Passed(&callback)));
}
void FileSystemManagerImpl::GetPlatformPath(const GURL& path,
GetPlatformPathCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::FilePath platform_path;
context_->default_file_task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&FileSystemManagerImpl::GetPlatformPathOnFileThread, path,
process_id_, base::Unretained(context_), GetWeakPtr(),
std::move(callback)));
}
void FileSystemManagerImpl::CreateSnapshotFile(
const GURL& file_path,
CreateSnapshotFileCallback callback) {
......@@ -570,6 +552,49 @@ void FileSystemManagerImpl::CreateSnapshotFile(
}
}
void FileSystemManagerImpl::GetPlatformPath(const GURL& path,
GetPlatformPathCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::FilePath platform_path;
context_->default_file_task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&FileSystemManagerImpl::GetPlatformPathOnFileThread, path,
process_id_, base::Unretained(context_), GetWeakPtr(),
std::move(callback)));
}
void FileSystemManagerImpl::CreateWriter(const GURL& file_path,
CreateWriterCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
FileSystemURL url(context_->CrackURL(file_path));
base::Optional<base::File::Error> opt_error = ValidateFileSystemURL(url);
if (opt_error) {
std::move(callback).Run(opt_error.value(), nullptr);
return;
}
if (!security_policy_->CanWriteFileSystemFile(process_id_, url)) {
std::move(callback).Run(base::File::FILE_ERROR_SECURITY, nullptr);
return;
}
blink::mojom::FileWriterPtr writer;
mojo::MakeStrongBinding(std::make_unique<storage::FileWriterImpl>(
url, context_->CreateFileSystemOperationRunner(),
blob_storage_context_->context()->AsWeakPtr()),
MakeRequest(&writer));
std::move(callback).Run(base::File::FILE_OK, std::move(writer));
}
void FileSystemManagerImpl::Cancel(
OperationID op_id,
FileSystemCancellableOperationImpl::CancelCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
operation_runner()->Cancel(
op_id, base::BindRepeating(&FileSystemManagerImpl::DidFinish,
GetWeakPtr(), base::Passed(&callback)));
}
void FileSystemManagerImpl::DidReceiveSnapshotFile(int snapshot_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
in_transit_snapshot_files_.Remove(snapshot_id);
......
......@@ -120,6 +120,8 @@ class CONTENT_EXPORT FileSystemManagerImpl
CreateSnapshotFileCallback callback) override;
void GetPlatformPath(const GURL& file_path,
GetPlatformPathCallback callback) override;
void CreateWriter(const GURL& file_path,
CreateWriterCallback callback) override;
private:
class FileSystemCancellableOperationImpl;
......
......@@ -548,6 +548,23 @@ void FileSystemDispatcher::CreateSnapshotFileSync(
std::move(listener));
}
void FileSystemDispatcher::CreateFileWriter(
const GURL& file_path,
std::unique_ptr<CreateFileWriterCallbacks> callbacks) {
GetFileSystemManager().CreateWriter(
file_path,
base::BindOnce(
[](std::unique_ptr<CreateFileWriterCallbacks> callbacks,
base::File::Error result, blink::mojom::FileWriterPtr writer) {
if (result != base::File::FILE_OK) {
callbacks->OnError(result);
} else {
callbacks->OnSuccess(writer.PassInterface().PassHandle());
}
},
std::move(callbacks)));
}
void FileSystemDispatcher::DidOpenFileSystem(int request_id,
const std::string& name,
const GURL& root,
......
......@@ -22,6 +22,7 @@
#include "storage/common/fileapi/file_system_types.h"
#include "storage/common/quota/quota_limit_type.h"
#include "third_party/blink/public/mojom/filesystem/file_system.mojom.h"
#include "third_party/blink/public/platform/web_callbacks.h"
namespace base {
class FilePath;
......@@ -178,6 +179,11 @@ class FileSystemDispatcher {
const CreateSnapshotFileCallback& success_callback,
const StatusCallback& error_callback);
using CreateFileWriterCallbacks =
blink::WebCallbacks<mojo::ScopedMessagePipeHandle, base::File::Error>;
void CreateFileWriter(const GURL& file_path,
std::unique_ptr<CreateFileWriterCallbacks> callbacks);
private:
class CallbackDispatcher;
class FileSystemOperationListenerImpl;
......
......@@ -430,6 +430,12 @@ void WebFileSystemImpl::CreateFileWriter(const WebURL& path,
base::BindRepeating(&StatusCallbackAdapter, callbacks_id)));
}
void WebFileSystemImpl::CreateFileWriter(
const blink::WebURL& path,
std::unique_ptr<CreateFileWriterCallbacks> callbacks) {
file_system_dispatcher_.CreateFileWriter(path, std::move(callbacks));
}
void WebFileSystemImpl::CreateSnapshotFileAndReadMetadata(
const blink::WebURL& path,
WebFileSystemCallbacks callbacks) {
......
......@@ -80,6 +80,9 @@ class WebFileSystemImpl : public blink::WebFileSystem,
void CreateFileWriter(const blink::WebURL& path,
blink::WebFileWriterClient*,
blink::WebFileSystemCallbacks) override;
void CreateFileWriter(
const blink::WebURL& path,
std::unique_ptr<CreateFileWriterCallbacks> callbacks) override;
void CreateSnapshotFileAndReadMetadata(
const blink::WebURL& path,
blink::WebFileSystemCallbacks) override;
......
......@@ -10,6 +10,7 @@ import "mojo/public/mojom/base/file_error.mojom";
import "mojo/public/mojom/base/file_path.mojom";
import "mojo/public/mojom/base/file_info.mojom";
import "mojo/public/mojom/base/time.mojom";
import "third_party/blink/public/mojom/filesystem/file_writer.mojom";
enum FileSystemType {
kTemporary,
......@@ -199,4 +200,9 @@ interface FileSystemManager {
[Sync]
GetPlatformPath(url.mojom.Url file_path) =>
(mojo_base.mojom.FilePath platform_path);
// Creates a writer for the given file at |file_path|.
CreateWriter(url.mojom.Url file_path) =>
(mojo_base.mojom.FileError result,
blink.mojom.FileWriter? writer);
};
......@@ -3,6 +3,7 @@ include_rules = [
"+base/callback_forward.h",
"+base/containers/flat_set.h",
"+base/containers/span.h",
"+base/files/file.h",
"+base/location.h",
"+base/logging.h",
"+base/memory/ref_counted.h",
......
......@@ -31,6 +31,9 @@
#ifndef THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_FILE_SYSTEM_H_
#define THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_FILE_SYSTEM_H_
#include "base/files/file.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "third_party/blink/public/platform/web_callbacks.h"
#include "third_party/blink/public/platform/web_common.h"
#include "third_party/blink/public/platform/web_file_system_callbacks.h"
#include "third_party/blink/public/platform/web_file_system_type.h"
......@@ -171,6 +174,14 @@ class WebFileSystem {
WebFileWriterClient*,
WebFileSystemCallbacks) = 0;
// Creates a blink::mojom::FileWriter that can be used to write to the given
// file. The resulting FileWriter is passed as a ScopedMessagePipeHandle to
// deal with converting between non-blink and blink mojom bindings variants.
using CreateFileWriterCallbacks =
WebCallbacks<mojo::ScopedMessagePipeHandle, base::File::Error>;
virtual void CreateFileWriter(const WebURL& path,
std::unique_ptr<CreateFileWriterCallbacks>) = 0;
// Creates a snapshot file for a given file specified by |path|. It returns
// the metadata of the created snapshot file. The returned metadata should
// include a local platform path to the snapshot image. In local filesystem
......
......@@ -4,8 +4,10 @@
#include "third_party/blink/renderer/modules/filesystem/file_system_file_handle.h"
#include "third_party/blink/public/mojom/filesystem/file_writer.mojom-blink.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/dom/dom_exception.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"
......@@ -15,6 +17,26 @@ namespace blink {
namespace {
class CreateWriterCallbacks
: public WebCallbacks<mojo::ScopedMessagePipeHandle, base::File::Error> {
public:
explicit CreateWriterCallbacks(ScriptPromiseResolver* resolver)
: resolver_(resolver) {}
void OnSuccess(mojo::ScopedMessagePipeHandle handle) override {
mojom::blink::FileWriterPtr mojo_writer(mojom::blink::FileWriterPtrInfo(
std::move(handle), mojom::blink::FileWriter::Version_));
resolver_->Resolve(new FileSystemWriter(std::move(mojo_writer)));
}
void OnError(base::File::Error error) override {
resolver_->Reject(FileError::CreateDOMException(error));
}
private:
Persistent<ScriptPromiseResolver> resolver_;
};
class OnDidCreateSnapshotFilePromise
: public SnapshotFileCallback::OnDidCreateSnapshotFileCallback {
public:
......@@ -37,9 +59,17 @@ FileSystemFileHandle::FileSystemFileHandle(DOMFileSystemBase* file_system,
: FileSystemBaseHandle(file_system, full_path) {}
ScriptPromise FileSystemFileHandle::createWriter(ScriptState* script_state) {
// TODO(mek): Implement this.
return ScriptPromise::RejectWithDOMException(
script_state, FileError::CreateDOMException(FileError::kAbortErr));
if (!filesystem()->FileSystem()) {
return ScriptPromise::RejectWithDOMException(
script_state, FileError::CreateDOMException(FileError::kAbortErr));
}
auto* resolver = ScriptPromiseResolver::Create(script_state);
ScriptPromise result = resolver->Promise();
filesystem()->FileSystem()->CreateFileWriter(
filesystem()->CreateFileSystemURL(this),
std::make_unique<CreateWriterCallbacks>(resolver));
return result;
}
ScriptPromise FileSystemFileHandle::getFile(ScriptState* script_state) {
......
......@@ -70,6 +70,7 @@ void FileSystemWriter::WriteComplete(base::File::Error result,
} else {
pending_operation_->Reject(FileError::CreateDOMException(result));
}
pending_operation_ = nullptr;
}
void FileSystemWriter::TruncateComplete(base::File::Error result) {
......@@ -79,6 +80,7 @@ void FileSystemWriter::TruncateComplete(base::File::Error result) {
} else {
pending_operation_->Reject(FileError::CreateDOMException(result));
}
pending_operation_ = nullptr;
}
} // namespace blink
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