Commit 033240b1 authored by Olivier Yiptong's avatar Olivier Yiptong Committed by Commit Bot

[Native File System] Refactor write methods from NFSFileHandle to NFSFileWriter

This CL moves write-related operations to NFSFileWriter. This makes
the backend and interface closer to the current shape of the spec.

Bug: 968550
Change-Id: Ia2ccd97416b478eddf210118132519eef2b16040
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1672026Reviewed-by: default avatarMarijn Kruisselbrink <mek@chromium.org>
Reviewed-by: default avatarOliver Chang <ochang@chromium.org>
Commit-Queue: Olivier Yiptong <oyiptong@chromium.org>
Auto-Submit: Olivier Yiptong <oyiptong@chromium.org>
Cr-Commit-Position: refs/heads/master@{#672105}
parent 1c3cf120
......@@ -21,11 +21,6 @@ using storage::FileSystemOperation;
namespace content {
struct NativeFileSystemFileHandleImpl::WriteState {
WriteCallback callback;
uint64_t bytes_written = 0;
};
NativeFileSystemFileHandleImpl::NativeFileSystemFileHandleImpl(
NativeFileSystemManagerImpl* manager,
const BindingContext& context,
......@@ -80,53 +75,6 @@ void NativeFileSystemFileHandleImpl::Remove(RemoveCallback callback) {
std::move(callback));
}
void NativeFileSystemFileHandleImpl::Write(uint64_t offset,
blink::mojom::BlobPtr data,
WriteCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
RunWithWritePermission(
base::BindOnce(&NativeFileSystemFileHandleImpl::WriteImpl,
weak_factory_.GetWeakPtr(), offset, std::move(data)),
base::BindOnce([](WriteCallback callback) {
std::move(callback).Run(
NativeFileSystemError::New(base::File::FILE_ERROR_ACCESS_DENIED),
0);
}),
std::move(callback));
}
void NativeFileSystemFileHandleImpl::WriteStream(
uint64_t offset,
mojo::ScopedDataPipeConsumerHandle stream,
WriteStreamCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
RunWithWritePermission(
base::BindOnce(&NativeFileSystemFileHandleImpl::WriteStreamImpl,
weak_factory_.GetWeakPtr(), offset, std::move(stream)),
base::BindOnce([](WriteStreamCallback callback) {
std::move(callback).Run(
NativeFileSystemError::New(base::File::FILE_ERROR_ACCESS_DENIED),
0);
}),
std::move(callback));
}
void NativeFileSystemFileHandleImpl::Truncate(uint64_t length,
TruncateCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
RunWithWritePermission(
base::BindOnce(&NativeFileSystemFileHandleImpl::TruncateImpl,
weak_factory_.GetWeakPtr(), length),
base::BindOnce([](TruncateCallback callback) {
std::move(callback).Run(
NativeFileSystemError::New(base::File::FILE_ERROR_ACCESS_DENIED));
}),
std::move(callback));
}
void NativeFileSystemFileHandleImpl::CreateFileWriter(
CreateFileWriterCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
......@@ -186,6 +134,7 @@ void NativeFileSystemFileHandleImpl::DidGetMetaDataForBlob(
blink::mojom::SerializedBlob::New(uuid, "application/octet-stream",
info.size, blob_ptr.PassInterface()));
}
void NativeFileSystemFileHandleImpl::RemoveImpl(RemoveCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_EQ(GetWritePermissionStatus(),
......@@ -199,82 +148,6 @@ void NativeFileSystemFileHandleImpl::RemoveImpl(RemoveCallback callback) {
std::move(callback)));
}
void NativeFileSystemFileHandleImpl::WriteImpl(uint64_t offset,
blink::mojom::BlobPtr data,
WriteCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_EQ(GetWritePermissionStatus(),
blink::mojom::PermissionStatus::GRANTED);
blob_context()->GetBlobDataFromBlobPtr(
std::move(data),
base::BindOnce(&NativeFileSystemFileHandleImpl::DoWriteBlob,
weak_factory_.GetWeakPtr(), std::move(callback), offset));
}
void NativeFileSystemFileHandleImpl::DoWriteBlob(
WriteCallback callback,
uint64_t position,
std::unique_ptr<BlobDataHandle> blob) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!blob) {
std::move(callback).Run(
NativeFileSystemError::New(base::File::FILE_ERROR_FAILED), 0);
return;
}
operation_runner()->Write(
url(), std::move(blob), position,
base::BindRepeating(&NativeFileSystemFileHandleImpl::DidWrite,
weak_factory_.GetWeakPtr(),
base::Owned(new WriteState{std::move(callback)})));
}
void NativeFileSystemFileHandleImpl::WriteStreamImpl(
uint64_t offset,
mojo::ScopedDataPipeConsumerHandle stream,
WriteStreamCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_EQ(GetWritePermissionStatus(),
blink::mojom::PermissionStatus::GRANTED);
operation_runner()->Write(
url(), std::move(stream), offset,
base::BindRepeating(&NativeFileSystemFileHandleImpl::DidWrite,
weak_factory_.GetWeakPtr(),
base::Owned(new WriteState{std::move(callback)})));
}
void NativeFileSystemFileHandleImpl::DidWrite(WriteState* state,
base::File::Error result,
int64_t bytes,
bool complete) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(state);
state->bytes_written += bytes;
if (complete) {
std::move(state->callback)
.Run(NativeFileSystemError::New(result), state->bytes_written);
}
}
void NativeFileSystemFileHandleImpl::TruncateImpl(uint64_t length,
TruncateCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_EQ(GetWritePermissionStatus(),
blink::mojom::PermissionStatus::GRANTED);
operation_runner()->Truncate(
url(), length,
base::BindOnce(
[](TruncateCallback callback, base::File::Error result) {
std::move(callback).Run(NativeFileSystemError::New(result));
},
std::move(callback)));
}
void NativeFileSystemFileHandleImpl::CreateFileWriterImpl(
CreateFileWriterCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
......
......@@ -13,10 +13,6 @@
#include "storage/browser/fileapi/file_system_url.h"
#include "third_party/blink/public/mojom/native_file_system/native_file_system_file_handle.mojom.h"
namespace storage {
class BlobDataHandle;
} // namespace storage
namespace content {
// This is the browser side implementation of the
......@@ -48,43 +44,16 @@ class CONTENT_EXPORT NativeFileSystemFileHandleImpl
RequestPermissionCallback callback) override;
void AsBlob(AsBlobCallback callback) override;
void Remove(RemoveCallback callback) override;
void Write(uint64_t offset,
blink::mojom::BlobPtr data,
WriteCallback callback) override;
void WriteStream(uint64_t offset,
mojo::ScopedDataPipeConsumerHandle stream,
WriteStreamCallback callback) override;
void Truncate(uint64_t length, TruncateCallback callback) override;
void CreateFileWriter(CreateFileWriterCallback callback) override;
void Transfer(
blink::mojom::NativeFileSystemTransferTokenRequest token) override;
private:
// State that is kept for the duration of a write operation, to keep track of
// progress until the write completes.
struct WriteState;
void DidGetMetaDataForBlob(AsBlobCallback callback,
base::File::Error result,
const base::File::Info& info);
void RemoveImpl(RemoveCallback callback);
void WriteImpl(uint64_t offset,
blink::mojom::BlobPtr data,
WriteCallback callback);
void DoWriteBlob(WriteCallback callback,
uint64_t position,
std::unique_ptr<storage::BlobDataHandle> blob);
void WriteStreamImpl(uint64_t offset,
mojo::ScopedDataPipeConsumerHandle stream,
WriteStreamCallback callback);
void DidWrite(WriteState* state,
base::File::Error result,
int64_t bytes,
bool complete);
void TruncateImpl(uint64_t length, TruncateCallback callback);
void CreateFileWriterImpl(CreateFileWriterCallback callback);
base::WeakPtr<NativeFileSystemHandleBase> AsWeakPtr() override;
......
......@@ -4,11 +4,22 @@
#include "content/browser/native_file_system/native_file_system_file_writer_impl.h"
#include "content/browser/native_file_system/native_file_system_manager_impl.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/fileapi/file_system_operation_runner.h"
#include "third_party/blink/public/mojom/blob/blob.mojom.h"
#include "third_party/blink/public/mojom/native_file_system/native_file_system_error.mojom.h"
using blink::mojom::NativeFileSystemError;
using storage::BlobDataHandle;
using storage::FileSystemOperation;
namespace content {
struct NativeFileSystemFileWriterImpl::WriteState {
WriteCallback callback;
uint64_t bytes_written = 0;
};
NativeFileSystemFileWriterImpl::NativeFileSystemFileWriterImpl(
NativeFileSystemManagerImpl* manager,
const BindingContext& context,
......@@ -18,6 +29,53 @@ NativeFileSystemFileWriterImpl::NativeFileSystemFileWriterImpl(
NativeFileSystemFileWriterImpl::~NativeFileSystemFileWriterImpl() = default;
void NativeFileSystemFileWriterImpl::Write(uint64_t offset,
blink::mojom::BlobPtr data,
WriteCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
RunWithWritePermission(
base::BindOnce(&NativeFileSystemFileWriterImpl::WriteImpl,
weak_factory_.GetWeakPtr(), offset, std::move(data)),
base::BindOnce([](WriteCallback callback) {
std::move(callback).Run(
NativeFileSystemError::New(base::File::FILE_ERROR_ACCESS_DENIED),
0);
}),
std::move(callback));
}
void NativeFileSystemFileWriterImpl::WriteStream(
uint64_t offset,
mojo::ScopedDataPipeConsumerHandle stream,
WriteStreamCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
RunWithWritePermission(
base::BindOnce(&NativeFileSystemFileWriterImpl::WriteStreamImpl,
weak_factory_.GetWeakPtr(), offset, std::move(stream)),
base::BindOnce([](WriteStreamCallback callback) {
std::move(callback).Run(
NativeFileSystemError::New(base::File::FILE_ERROR_ACCESS_DENIED),
0);
}),
std::move(callback));
}
void NativeFileSystemFileWriterImpl::Truncate(uint64_t length,
TruncateCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
RunWithWritePermission(
base::BindOnce(&NativeFileSystemFileWriterImpl::TruncateImpl,
weak_factory_.GetWeakPtr(), length),
base::BindOnce([](TruncateCallback callback) {
std::move(callback).Run(
NativeFileSystemError::New(base::File::FILE_ERROR_ACCESS_DENIED));
}),
std::move(callback));
}
void NativeFileSystemFileWriterImpl::Close(CloseCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
......@@ -31,6 +89,82 @@ void NativeFileSystemFileWriterImpl::Close(CloseCallback callback) {
std::move(callback));
}
void NativeFileSystemFileWriterImpl::WriteImpl(uint64_t offset,
blink::mojom::BlobPtr data,
WriteCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_EQ(GetWritePermissionStatus(),
blink::mojom::PermissionStatus::GRANTED);
blob_context()->GetBlobDataFromBlobPtr(
std::move(data),
base::BindOnce(&NativeFileSystemFileWriterImpl::DoWriteBlob,
weak_factory_.GetWeakPtr(), std::move(callback), offset));
}
void NativeFileSystemFileWriterImpl::DoWriteBlob(
WriteCallback callback,
uint64_t position,
std::unique_ptr<BlobDataHandle> blob) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!blob) {
std::move(callback).Run(
NativeFileSystemError::New(base::File::FILE_ERROR_FAILED), 0);
return;
}
operation_runner()->Write(
url(), std::move(blob), position,
base::BindRepeating(&NativeFileSystemFileWriterImpl::DidWrite,
weak_factory_.GetWeakPtr(),
base::Owned(new WriteState{std::move(callback)})));
}
void NativeFileSystemFileWriterImpl::WriteStreamImpl(
uint64_t offset,
mojo::ScopedDataPipeConsumerHandle stream,
WriteStreamCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_EQ(GetWritePermissionStatus(),
blink::mojom::PermissionStatus::GRANTED);
operation_runner()->Write(
url(), std::move(stream), offset,
base::BindRepeating(&NativeFileSystemFileWriterImpl::DidWrite,
weak_factory_.GetWeakPtr(),
base::Owned(new WriteState{std::move(callback)})));
}
void NativeFileSystemFileWriterImpl::DidWrite(WriteState* state,
base::File::Error result,
int64_t bytes,
bool complete) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(state);
state->bytes_written += bytes;
if (complete) {
std::move(state->callback)
.Run(NativeFileSystemError::New(result), state->bytes_written);
}
}
void NativeFileSystemFileWriterImpl::TruncateImpl(uint64_t length,
TruncateCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_EQ(GetWritePermissionStatus(),
blink::mojom::PermissionStatus::GRANTED);
operation_runner()->Truncate(
url(), length,
base::BindOnce(
[](TruncateCallback callback, base::File::Error result) {
std::move(callback).Run(NativeFileSystemError::New(result));
},
std::move(callback)));
}
void NativeFileSystemFileWriterImpl::CloseImpl(CloseCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_EQ(GetWritePermissionStatus(),
......
......@@ -36,10 +36,35 @@ class CONTENT_EXPORT NativeFileSystemFileWriterImpl
const storage::FileSystemURL& url,
const SharedHandleState& handle_state);
~NativeFileSystemFileWriterImpl() override;
void Write(uint64_t offset,
blink::mojom::BlobPtr data,
WriteCallback callback) override;
void WriteStream(uint64_t offset,
mojo::ScopedDataPipeConsumerHandle stream,
WriteStreamCallback callback) override;
void Truncate(uint64_t length, TruncateCallback callback) override;
void Close(CloseCallback callback) override;
private:
// State that is kept for the duration of a write operation, to keep track of
// progress until the write completes.
struct WriteState;
void WriteImpl(uint64_t offset,
blink::mojom::BlobPtr data,
WriteCallback callback);
void DoWriteBlob(WriteCallback callback,
uint64_t position,
std::unique_ptr<storage::BlobDataHandle> blob);
void WriteStreamImpl(uint64_t offset,
mojo::ScopedDataPipeConsumerHandle stream,
WriteStreamCallback callback);
void DidWrite(WriteState* state,
base::File::Error result,
int64_t bytes,
bool complete);
void TruncateImpl(uint64_t length, TruncateCallback callback);
void CloseImpl(CloseCallback callback);
base::WeakPtr<NativeFileSystemHandleBase> AsWeakPtr() override;
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/native_file_system/native_file_system_file_handle_impl.h"
#include "content/browser/native_file_system/native_file_system_file_writer_impl.h"
#include <limits>
#include "base/bind.h"
......@@ -31,9 +31,9 @@ using storage::FileSystemURL;
namespace content {
class NativeFileSystemFileHandleImplTest : public testing::Test {
class NativeFileSystemFileWriterImplTest : public testing::Test {
public:
NativeFileSystemFileHandleImplTest()
NativeFileSystemFileWriterImplTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::IO) {
scoped_feature_list_.InitAndEnableFeature(
......@@ -60,7 +60,7 @@ class NativeFileSystemFileHandleImplTest : public testing::Test {
file_system_context_, chrome_blob_context_,
/*permission_context=*/nullptr);
handle_ = std::make_unique<NativeFileSystemFileHandleImpl>(
handle_ = std::make_unique<NativeFileSystemFileWriterImpl>(
manager_.get(),
NativeFileSystemManagerImpl::BindingContext(
test_url_.origin(), /*process_id=*/1,
......@@ -174,6 +174,18 @@ class NativeFileSystemFileHandleImplTest : public testing::Test {
return result_out;
}
base::File::Error CloseSync() {
base::RunLoop loop;
base::File::Error result_out;
handle_->Close(base::BindLambdaForTesting(
[&](blink::mojom::NativeFileSystemErrorPtr result) {
result_out = result->error_code;
loop.Quit();
}));
loop.Run();
return result_out;
}
virtual bool WriteUsingBlobs() { return true; }
base::File::Error WriteSync(uint64_t position,
......@@ -199,21 +211,21 @@ class NativeFileSystemFileHandleImplTest : public testing::Test {
scoped_refptr<FixedNativeFileSystemPermissionGrant> permission_grant_ =
base::MakeRefCounted<FixedNativeFileSystemPermissionGrant>(
FixedNativeFileSystemPermissionGrant::PermissionStatus::GRANTED);
std::unique_ptr<NativeFileSystemFileHandleImpl> handle_;
std::unique_ptr<NativeFileSystemFileWriterImpl> handle_;
};
class NativeFileSystemFileHandleImplWriteTest
: public NativeFileSystemFileHandleImplTest,
class NativeFileSystemFileWriterImplWriteTest
: public NativeFileSystemFileWriterImplTest,
public testing::WithParamInterface<bool> {
public:
bool WriteUsingBlobs() override { return GetParam(); }
};
INSTANTIATE_TEST_SUITE_P(NativeFileSystemFileHandleImplTest,
NativeFileSystemFileHandleImplWriteTest,
INSTANTIATE_TEST_SUITE_P(NativeFileSystemFileWriterImplTest,
NativeFileSystemFileWriterImplWriteTest,
::testing::Bool());
TEST_F(NativeFileSystemFileHandleImplTest, WriteInvalidBlob) {
TEST_F(NativeFileSystemFileWriterImplTest, WriteInvalidBlob) {
blink::mojom::BlobPtr blob;
MakeRequest(&blob);
......@@ -225,7 +237,7 @@ TEST_F(NativeFileSystemFileHandleImplTest, WriteInvalidBlob) {
EXPECT_EQ("", ReadFile(test_url_));
}
TEST_P(NativeFileSystemFileHandleImplWriteTest, WriteValidEmptyString) {
TEST_P(NativeFileSystemFileWriterImplWriteTest, WriteValidEmptyString) {
uint64_t bytes_written;
base::File::Error result = WriteSync(0, "", &bytes_written);
EXPECT_EQ(result, base::File::FILE_OK);
......@@ -234,7 +246,7 @@ TEST_P(NativeFileSystemFileHandleImplWriteTest, WriteValidEmptyString) {
EXPECT_EQ("", ReadFile(test_url_));
}
TEST_P(NativeFileSystemFileHandleImplWriteTest, WriteValidNonEmpty) {
TEST_P(NativeFileSystemFileWriterImplWriteTest, WriteValidNonEmpty) {
std::string test_data("abcdefghijklmnopqrstuvwxyz");
uint64_t bytes_written;
base::File::Error result = WriteSync(0, test_data, &bytes_written);
......@@ -244,7 +256,7 @@ TEST_P(NativeFileSystemFileHandleImplWriteTest, WriteValidNonEmpty) {
EXPECT_EQ(test_data, ReadFile(test_url_));
}
TEST_P(NativeFileSystemFileHandleImplWriteTest, WriteWithOffsetInFile) {
TEST_P(NativeFileSystemFileWriterImplWriteTest, WriteWithOffsetInFile) {
uint64_t bytes_written;
base::File::Error result;
......@@ -259,7 +271,7 @@ TEST_P(NativeFileSystemFileHandleImplWriteTest, WriteWithOffsetInFile) {
EXPECT_EQ("1234abc890", ReadFile(test_url_));
}
TEST_P(NativeFileSystemFileHandleImplWriteTest, WriteWithOffsetPastFile) {
TEST_P(NativeFileSystemFileWriterImplWriteTest, WriteWithOffsetPastFile) {
uint64_t bytes_written;
base::File::Error result = WriteSync(4, "abc", &bytes_written);
EXPECT_EQ(result, base::File::FILE_ERROR_FAILED);
......@@ -268,7 +280,18 @@ TEST_P(NativeFileSystemFileHandleImplWriteTest, WriteWithOffsetPastFile) {
EXPECT_EQ("", ReadFile(test_url_));
}
TEST_F(NativeFileSystemFileHandleImplTest, TruncateShrink) {
TEST_F(NativeFileSystemFileWriterImplTest, CloseOK) {
uint64_t bytes_written;
base::File::Error result = WriteSync(0, "abc", &bytes_written);
EXPECT_EQ(result, base::File::FILE_OK);
EXPECT_EQ(bytes_written, 3u);
result = CloseSync();
EXPECT_EQ(result, base::File::FILE_OK);
EXPECT_EQ("abc", ReadFile(test_url_));
}
TEST_F(NativeFileSystemFileWriterImplTest, TruncateShrink) {
uint64_t bytes_written;
base::File::Error result;
......@@ -282,7 +305,7 @@ TEST_F(NativeFileSystemFileHandleImplTest, TruncateShrink) {
EXPECT_EQ("12345", ReadFile(test_url_));
}
TEST_F(NativeFileSystemFileHandleImplTest, TruncateGrow) {
TEST_F(NativeFileSystemFileWriterImplTest, TruncateGrow) {
uint64_t bytes_written;
base::File::Error result;
......
......@@ -1626,7 +1626,7 @@ test("content_unittests") {
"../browser/memory/test_memory_monitor.cc",
"../browser/memory/test_memory_monitor.h",
"../browser/native_file_system/file_system_chooser_unittest.cc",
"../browser/native_file_system/native_file_system_file_handle_impl_unittest.cc",
"../browser/native_file_system/native_file_system_file_writer_impl_unittest.cc",
"../browser/native_file_system/native_file_system_handle_base_unittest.cc",
"../browser/native_file_system/native_file_system_manager_impl_unittest.cc",
"../browser/net/network_quality_observer_impl_unittest.cc",
......
......@@ -28,30 +28,6 @@ interface NativeFileSystemFileHandle {
// TODO(oyiptong): Remove this method. It isn't part of the spec anymore.
Remove() => (NativeFileSystemError result);
// Write data from |data| to the given |position| in the file being written
// to. Returns whether the operation succeeded and if so how many bytes were
// written.
// TODO(mek): This might need some way of reporting progress events back to
// the renderer.
// TODO(oyiptong): Refactor into NFSFileWriter.
Write(uint64 offset, Blob data) => (NativeFileSystemError result,
uint64 bytes_written);
// Write data from |stream| to the given |position| in the file being written
// to. Returns whether the operation succeeded and if so how many bytes were
// written.
// TODO(mek): This might need some way of reporting progress events back to
// the renderer.
// TODO(oyiptong): Refactor into NFSFileWriter.
WriteStream(uint64 offset, handle<data_pipe_consumer> stream) =>
(NativeFileSystemError result, uint64 bytes_written);
// Changes the length of the file to be |length|. If |length| is larger than
// the current size of the file, the file will be extended, and the extended
// part is filled with null bytes.
// TODO(oyiptong): Refactor into NFSFileWriter.
Truncate(uint64 length) => (NativeFileSystemError result);
// Returns a FileWriter object. The FileWriter provides write operations on a file.
CreateFileWriter() => (NativeFileSystemError result, NativeFileSystemFileWriter? writer);
......
......@@ -4,10 +4,32 @@
module blink.mojom;
import "third_party/blink/public/mojom/blob/blob.mojom";
import "third_party/blink/public/mojom/native_file_system/native_file_system_error.mojom";
// Represents an object to modify a file.
interface NativeFileSystemFileWriter {
// Write data from |data| to the given |position| in the file being written
// to. Returns whether the operation succeeded and if so how many bytes were
// written.
// TODO(mek): This might need some way of reporting progress events back to
// the renderer.
Write(uint64 offset, Blob data) => (NativeFileSystemError result,
uint64 bytes_written);
// Write data from |stream| to the given |position| in the file being written
// to. Returns whether the operation succeeded and if so how many bytes were
// written.
// TODO(mek): This might need some way of reporting progress events back to
// the renderer.
WriteStream(uint64 offset, handle<data_pipe_consumer> stream) =>
(NativeFileSystemError result, uint64 bytes_written);
// Changes the length of the file to be |length|. If |length| is larger than
// the current size of the file, the file will be extended, and the extended
// part is filled with null bytes.
Truncate(uint64 length) => (NativeFileSystemError result);
// Closes the file writer. This will materialize the writes operations on the
// intended file target in the case of atomic writes.
// Close() will close the mojo pipe after completion.
......
......@@ -32,17 +32,17 @@ ScriptPromise NativeFileSystemFileHandle::createWriter(
ScriptPromise result = resolver->Promise();
mojo_ptr_->CreateFileWriter(WTF::Bind(
[](ScriptPromiseResolver* resolver, NativeFileSystemFileHandle* handle,
[](ScriptPromiseResolver* resolver,
mojom::blink::NativeFileSystemErrorPtr result,
mojom::blink::NativeFileSystemFileWriterPtr writer) {
if (result->error_code == base::File::FILE_OK) {
resolver->Resolve(MakeGarbageCollected<NativeFileSystemWriter>(
handle, std::move(writer)));
resolver->Resolve(
MakeGarbageCollected<NativeFileSystemWriter>(std::move(writer)));
} else {
resolver->Reject(file_error::CreateDOMException(result->error_code));
}
},
WrapPersistent(resolver), WrapPersistent(this)));
WrapPersistent(resolver)));
return result;
}
......@@ -52,8 +52,19 @@ ScriptPromise NativeFileSystemFileHandle::createWritable(
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise result = resolver->Promise();
resolver->Resolve(
MakeGarbageCollected<NativeFileSystemWritableFileStream>(this));
mojo_ptr_->CreateFileWriter(WTF::Bind(
[](ScriptPromiseResolver* resolver,
mojom::blink::NativeFileSystemErrorPtr result,
mojom::blink::NativeFileSystemFileWriterPtr writer) {
if (result->error_code == base::File::FILE_OK) {
resolver->Resolve(
MakeGarbageCollected<NativeFileSystemWritableFileStream>(
std::move(writer)));
} else {
resolver->Reject(file_error::CreateDOMException(result->error_code));
}
},
WrapPersistent(resolver)));
return result;
}
......
......@@ -16,7 +16,6 @@
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
class NativeFileSystemDirectoryHandle;
class FileSystemHandlePermissionDescriptor;
class NativeFileSystemHandle : public ScriptWrappable {
......@@ -31,14 +30,7 @@ class NativeFileSystemHandle : public ScriptWrappable {
virtual bool isDirectory() const { return false; }
const String& name() const { return name_; }
ScriptPromise moveTo(ScriptState*,
NativeFileSystemDirectoryHandle* parent,
const String& new_name = String());
ScriptPromise copyTo(ScriptState*,
NativeFileSystemDirectoryHandle* parent,
const String& new_name = String());
ScriptPromise remove(ScriptState*);
ScriptPromise queryPermission(ScriptState*,
const FileSystemHandlePermissionDescriptor*);
ScriptPromise requestPermission(ScriptState*,
......
......@@ -17,9 +17,9 @@
namespace blink {
NativeFileSystemWritableFileStream::NativeFileSystemWritableFileStream(
NativeFileSystemFileHandle* file)
: file_(file) {
DCHECK(file_);
mojom::blink::NativeFileSystemFileWriterPtr mojo_ptr)
: mojo_ptr_(std::move(mojo_ptr)) {
DCHECK(mojo_ptr_);
}
ScriptPromise NativeFileSystemWritableFileStream::write(
......@@ -57,7 +57,7 @@ ScriptPromise NativeFileSystemWritableFileStream::write(
ScriptPromise NativeFileSystemWritableFileStream::truncate(
ScriptState* script_state,
uint64_t size) {
if (!file_ || pending_operation_) {
if (!mojo_ptr_ || pending_operation_) {
return ScriptPromise::RejectWithDOMException(
script_state, MakeGarbageCollected<DOMException>(
DOMExceptionCode::kInvalidStateError));
......@@ -65,7 +65,7 @@ ScriptPromise NativeFileSystemWritableFileStream::truncate(
pending_operation_ =
MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise result = pending_operation_->Promise();
file_->MojoHandle()->Truncate(
mojo_ptr_->Truncate(
size, WTF::Bind(&NativeFileSystemWritableFileStream::TruncateComplete,
WrapPersistent(this)));
return result;
......@@ -100,7 +100,7 @@ ScriptPromise NativeFileSystemWritableFileStream::WriteBlob(
ScriptState* script_state,
uint64_t position,
Blob* blob) {
if (!file_ || pending_operation_) {
if (!mojo_ptr_ || pending_operation_) {
return ScriptPromise::RejectWithDOMException(
script_state, MakeGarbageCollected<DOMException>(
DOMExceptionCode::kInvalidStateError));
......@@ -108,10 +108,9 @@ ScriptPromise NativeFileSystemWritableFileStream::WriteBlob(
pending_operation_ =
MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise result = pending_operation_->Promise();
file_->MojoHandle()->Write(
position, blob->AsMojoBlob(),
WTF::Bind(&NativeFileSystemWritableFileStream::WriteComplete,
WrapPersistent(this)));
mojo_ptr_->Write(position, blob->AsMojoBlob(),
WTF::Bind(&NativeFileSystemWritableFileStream::WriteComplete,
WrapPersistent(this)));
return result;
}
......@@ -170,7 +169,6 @@ void NativeFileSystemWritableFileStream::Serialize(
void NativeFileSystemWritableFileStream::Trace(Visitor* visitor) {
ScriptWrappable::Trace(visitor);
visitor->Trace(file_);
visitor->Trace(pending_operation_);
}
......
......@@ -6,6 +6,7 @@
#define THIRD_PARTY_BLINK_RENDERER_MODULES_NATIVE_FILE_SYSTEM_NATIVE_FILE_SYSTEM_WRITABLE_FILE_STREAM_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_file_writer.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/array_buffer_or_array_buffer_view_or_blob_or_usv_string.h"
#include "third_party/blink/renderer/core/streams/writable_stream.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
......@@ -17,13 +18,13 @@ class ExceptionState;
class ScriptPromise;
class ScriptPromiseResolver;
class ScriptState;
class NativeFileSystemFileHandle;
class NativeFileSystemWritableFileStream final : public WritableStream {
DEFINE_WRAPPERTYPEINFO();
public:
explicit NativeFileSystemWritableFileStream(NativeFileSystemFileHandle*);
explicit NativeFileSystemWritableFileStream(
mojom::blink::NativeFileSystemFileWriterPtr);
void Trace(Visitor* visitor) override;
......@@ -51,7 +52,7 @@ class NativeFileSystemWritableFileStream final : public WritableStream {
uint64_t bytes_written);
void TruncateComplete(mojom::blink::NativeFileSystemErrorPtr result);
Member<NativeFileSystemFileHandle> file_;
mojom::blink::NativeFileSystemFileWriterPtr mojo_ptr_;
Member<ScriptPromiseResolver> pending_operation_;
};
......
......@@ -25,10 +25,8 @@
namespace blink {
NativeFileSystemWriter::NativeFileSystemWriter(
NativeFileSystemFileHandle* file,
mojom::blink::NativeFileSystemFileWriterPtr mojo_ptr)
: mojo_ptr_(std::move(mojo_ptr)), file_(file) {
DCHECK(file_);
: mojo_ptr_(std::move(mojo_ptr)) {
DCHECK(mojo_ptr_);
}
......@@ -67,7 +65,7 @@ ScriptPromise NativeFileSystemWriter::write(
ScriptPromise NativeFileSystemWriter::WriteBlob(ScriptState* script_state,
uint64_t position,
Blob* blob) {
if (!file_ || pending_operation_) {
if (!mojo_ptr_ || pending_operation_) {
return ScriptPromise::RejectWithDOMException(
script_state, MakeGarbageCollected<DOMException>(
DOMExceptionCode::kInvalidStateError));
......@@ -75,7 +73,7 @@ ScriptPromise NativeFileSystemWriter::WriteBlob(ScriptState* script_state,
pending_operation_ =
MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise result = pending_operation_->Promise();
file_->MojoHandle()->Write(
mojo_ptr_->Write(
position, blob->AsMojoBlob(),
WTF::Bind(&NativeFileSystemWriter::WriteComplete, WrapPersistent(this)));
return result;
......@@ -173,7 +171,7 @@ ScriptPromise NativeFileSystemWriter::WriteStream(
uint64_t position,
ReadableStream* stream,
ExceptionState& exception_state) {
if (!file_ || pending_operation_) {
if (!mojo_ptr_ || pending_operation_) {
return ScriptPromise::RejectWithDOMException(
script_state, MakeGarbageCollected<DOMException>(
DOMExceptionCode::kInvalidStateError));
......@@ -193,7 +191,7 @@ ScriptPromise NativeFileSystemWriter::WriteStream(
ScriptPromise result = pending_operation_->Promise();
auto* client = MakeGarbageCollected<StreamWriterClient>(this);
stream_loader_->Start(consumer, client);
file_->MojoHandle()->WriteStream(
mojo_ptr_->WriteStream(
position, client->TakeDataPipe(),
WTF::Bind(&StreamWriterClient::WriteComplete, WrapPersistent(client)));
return result;
......@@ -201,7 +199,7 @@ ScriptPromise NativeFileSystemWriter::WriteStream(
ScriptPromise NativeFileSystemWriter::truncate(ScriptState* script_state,
uint64_t size) {
if (!file_ || pending_operation_) {
if (!mojo_ptr_ || pending_operation_) {
return ScriptPromise::RejectWithDOMException(
script_state, MakeGarbageCollected<DOMException>(
DOMExceptionCode::kInvalidStateError));
......@@ -209,14 +207,13 @@ ScriptPromise NativeFileSystemWriter::truncate(ScriptState* script_state,
pending_operation_ =
MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise result = pending_operation_->Promise();
file_->MojoHandle()->Truncate(
size, WTF::Bind(&NativeFileSystemWriter::TruncateComplete,
WrapPersistent(this)));
mojo_ptr_->Truncate(size, WTF::Bind(&NativeFileSystemWriter::TruncateComplete,
WrapPersistent(this)));
return result;
}
ScriptPromise NativeFileSystemWriter::close(ScriptState* script_state) {
if (!file_ || pending_operation_) {
if (!mojo_ptr_ || pending_operation_) {
return ScriptPromise::RejectWithDOMException(
script_state, MakeGarbageCollected<DOMException>(
DOMExceptionCode::kInvalidStateError));
......
......@@ -25,8 +25,7 @@ class NativeFileSystemWriter final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
public:
explicit NativeFileSystemWriter(NativeFileSystemFileHandle*,
mojom::blink::NativeFileSystemFileWriterPtr);
explicit NativeFileSystemWriter(mojom::blink::NativeFileSystemFileWriterPtr);
ScriptPromise write(ScriptState*,
uint64_t position,
......
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