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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "storage/browser/file_system/local_file_stream_writer.h"
#include <stdint.h>
#include <memory>
#include "base/functional/bind.h"
#include "base/types/pass_key.h"
#include "net/base/file_stream.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
namespace storage {
namespace {
const int kOpenFlagsForWrite =
base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
const int kCreateFlagsForWrite =
base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
const int kCreateFlagsForWriteAlways = base::File::FLAG_CREATE_ALWAYS |
base::File::FLAG_WRITE |
base::File::FLAG_ASYNC;
#if BUILDFLAG(IS_ANDROID)
// Android always opens Content-URI files for write with truncate for security.
const int kOpenFlagsForWriteContentUri = base::File::FLAG_CREATE_ALWAYS |
base::File::FLAG_WRITE |
base::File::FLAG_ASYNC;
#endif
} // namespace
std::unique_ptr<FileStreamWriter> FileStreamWriter::CreateForLocalFile(
base::TaskRunner* task_runner,
const base::FilePath& file_path,
int64_t initial_offset,
OpenOrCreate open_or_create) {
return std::make_unique<LocalFileStreamWriter>(
task_runner, file_path, initial_offset, open_or_create,
base::PassKey<FileStreamWriter>());
}
LocalFileStreamWriter::~LocalFileStreamWriter() {
// Invalidate weak pointers so that we won't receive any callbacks from
// in-flight stream operations, which might be triggered during the file close
// in the FileStream destructor.
weak_factory_.InvalidateWeakPtrs();
// FileStream's destructor closes the file safely, since we opened the file
// by its Open() method.
}
int LocalFileStreamWriter::Write(net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
DCHECK(!has_pending_operation_);
DCHECK(write_callback_.is_null());
DCHECK(cancel_callback_.is_null());
has_pending_operation_ = true;
write_callback_ = std::move(callback);
if (stream_impl_) {
int result = InitiateWrite(buf, buf_len);
if (result != net::ERR_IO_PENDING)
has_pending_operation_ = false;
return result;
}
return InitiateOpen(base::BindOnce(&LocalFileStreamWriter::ReadyToWrite,
weak_factory_.GetWeakPtr(),
base::RetainedRef(buf), buf_len));
}
int LocalFileStreamWriter::Cancel(net::CompletionOnceCallback callback) {
if (!has_pending_operation_)
return net::ERR_UNEXPECTED;
DCHECK(!callback.is_null());
cancel_callback_ = std::move(callback);
return net::ERR_IO_PENDING;
}
int LocalFileStreamWriter::Flush(FlushMode /*flush_mode*/,
net::CompletionOnceCallback callback) {
DCHECK(!has_pending_operation_);
DCHECK(cancel_callback_.is_null());
// Write() is not called yet, so there's nothing to flush.
if (!stream_impl_)
return net::OK;
has_pending_operation_ = true;
int result = InitiateFlush(std::move(callback));
if (result != net::ERR_IO_PENDING)
has_pending_operation_ = false;
return result;
}
LocalFileStreamWriter::LocalFileStreamWriter(
base::TaskRunner* task_runner,
const base::FilePath& file_path,
int64_t initial_offset,
OpenOrCreate open_or_create,
base::PassKey<FileStreamWriter> /*pass_key*/)
: file_path_(file_path),
open_or_create_(open_or_create),
initial_offset_(initial_offset),
task_runner_(task_runner),
has_pending_operation_(false) {}
int LocalFileStreamWriter::InitiateOpen(base::OnceClosure main_operation) {
DCHECK(has_pending_operation_);
DCHECK(!stream_impl_.get());
stream_impl_ = std::make_unique<net::FileStream>(task_runner_);
int open_flags = 0;
switch (open_or_create_) {
case OPEN_EXISTING_FILE:
open_flags = kOpenFlagsForWrite;
#if BUILDFLAG(IS_ANDROID)
if (file_path_.IsContentUri()) {
open_flags = kOpenFlagsForWriteContentUri;
}
#endif
break;
case CREATE_NEW_FILE:
open_flags = kCreateFlagsForWrite;
break;
case CREATE_NEW_FILE_ALWAYS:
open_flags = kCreateFlagsForWriteAlways;
break;
}
return stream_impl_->Open(
file_path_, open_flags,
base::BindOnce(&LocalFileStreamWriter::DidOpen,
weak_factory_.GetWeakPtr(), std::move(main_operation)));
}
void LocalFileStreamWriter::DidOpen(base::OnceClosure main_operation,
int result) {
DCHECK(has_pending_operation_);
DCHECK(stream_impl_.get());
if (CancelIfRequested())
return;
if (result != net::OK) {
has_pending_operation_ = false;
stream_impl_.reset(nullptr);
std::move(write_callback_).Run(result);
return;
}
InitiateSeek(std::move(main_operation));
}
void LocalFileStreamWriter::InitiateSeek(base::OnceClosure main_operation) {
DCHECK(has_pending_operation_);
DCHECK(stream_impl_.get());
if (initial_offset_ == 0) {
// No need to seek.
std::move(main_operation).Run();
return;
}
int result = stream_impl_->Seek(
initial_offset_,
base::BindOnce(&LocalFileStreamWriter::DidSeek,
weak_factory_.GetWeakPtr(), std::move(main_operation)));
if (result != net::ERR_IO_PENDING) {
has_pending_operation_ = false;
std::move(write_callback_).Run(result);
}
}
void LocalFileStreamWriter::DidSeek(base::OnceClosure main_operation,
int64_t result) {
DCHECK(has_pending_operation_);
if (CancelIfRequested())
return;
if (result != initial_offset_) {
// TODO(kinaba) add a more specific error code.
result = net::ERR_FAILED;
}
if (result < 0) {
has_pending_operation_ = false;
std::move(write_callback_).Run(static_cast<int>(result));
return;
}
std::move(main_operation).Run();
}
void LocalFileStreamWriter::ReadyToWrite(net::IOBuffer* buf, int buf_len) {
DCHECK(has_pending_operation_);
int result = InitiateWrite(buf, buf_len);
if (result != net::ERR_IO_PENDING) {
has_pending_operation_ = false;
std::move(write_callback_).Run(result);
}
}
int LocalFileStreamWriter::InitiateWrite(net::IOBuffer* buf, int buf_len) {
DCHECK(has_pending_operation_);
DCHECK(stream_impl_.get());
return stream_impl_->Write(buf, buf_len,
base::BindOnce(&LocalFileStreamWriter::DidWrite,
weak_factory_.GetWeakPtr()));
}
void LocalFileStreamWriter::DidWrite(int result) {
DCHECK(has_pending_operation_);
if (CancelIfRequested())
return;
has_pending_operation_ = false;
std::move(write_callback_).Run(result);
}
int LocalFileStreamWriter::InitiateFlush(net::CompletionOnceCallback callback) {
DCHECK(has_pending_operation_);
DCHECK(stream_impl_.get());
return stream_impl_->Flush(base::BindOnce(&LocalFileStreamWriter::DidFlush,
weak_factory_.GetWeakPtr(),
std::move(callback)));
}
void LocalFileStreamWriter::DidFlush(net::CompletionOnceCallback callback,
int result) {
DCHECK(has_pending_operation_);
if (CancelIfRequested())
return;
has_pending_operation_ = false;
std::move(callback).Run(result);
}
bool LocalFileStreamWriter::CancelIfRequested() {
DCHECK(has_pending_operation_);
if (cancel_callback_.is_null())
return false;
has_pending_operation_ = false;
std::move(cancel_callback_).Run(net::OK);
return true;
}
} // namespace storage
|