blob: b77ac7b1fd709ac4d1ca50e9e5c438990a7ef64b [file] [log] [blame]
// 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 STORAGE_BROWSER_FILEAPI_FILE_WRITER_IMPL_H_
#define STORAGE_BROWSER_FILEAPI_FILE_WRITER_IMPL_H_
#include "base/component_export.h"
#include "base/memory/weak_ptr.h"
#include "storage/browser/fileapi/file_system_operation_runner.h"
#include "storage/browser/fileapi/file_system_url.h"
#include "third_party/blink/public/mojom/filesystem/file_writer.mojom.h"
namespace storage {
// This class itself is stateless, but it uses the passed in
// FileSystemOperationRunner and BlobStorageContext so all methods should be
// called on the sequence those instances live on. In chromium that means all
// usage has to be on the IO thread.
class COMPONENT_EXPORT(STORAGE_BROWSER) FileWriterImpl
: public blink::mojom::FileWriter {
public:
FileWriterImpl(FileSystemURL url,
std::unique_ptr<FileSystemOperationRunner> operation_runner,
base::WeakPtr<BlobStorageContext> blob_context);
~FileWriterImpl() override;
void Write(uint64_t position,
blink::mojom::BlobPtr blob,
WriteCallback callback) override;
void WriteStream(uint64_t position,
mojo::ScopedDataPipeConsumerHandle stream,
WriteStreamCallback callback) override;
void Truncate(uint64_t length, TruncateCallback callback) override;
private:
void DoWrite(WriteCallback callback,
uint64_t position,
std::unique_ptr<BlobDataHandle> blob);
void DoWriteWithFileInfo(WriteCallback callback,
uint64_t position,
std::unique_ptr<BlobDataHandle> blob,
base::File::Error result,
const base::File::Info& file_info);
void DoWriteStreamWithFileInfo(WriteCallback callback,
uint64_t position,
mojo::ScopedDataPipeConsumerHandle data_pipe,
base::File::Error result,
const base::File::Info& file_info);
struct WriteState {
uint64_t bytes_written = 0;
};
void DidWrite(WriteCallback callback,
WriteState* state,
base::File::Error result,
int64_t bytes,
bool complete);
void DidTruncate(TruncateCallback callback, base::File::Error result);
const std::unique_ptr<FileSystemOperationRunner> operation_runner_;
const base::WeakPtr<BlobStorageContext> blob_context_;
const FileSystemURL url_;
base::WeakPtrFactory<FileWriterImpl> weak_ptr_factory_;
};
} // namespace storage
#endif // STORAGE_BROWSER_FILEAPI_FILE_WRITER_IMPL_H_