Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Unified Diff: webkit/fileapi/local_file_writer.cc

Issue 10126004: fileapi: FileWriter and LocalFileWriter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: First working version. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webkit/fileapi/local_file_writer.cc
diff --git a/webkit/fileapi/local_file_writer.cc b/webkit/fileapi/local_file_writer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1d4904ca96fbb3c9f5e46898634a94693ed26a8b
--- /dev/null
+++ b/webkit/fileapi/local_file_writer.cc
@@ -0,0 +1,192 @@
+// Copyright (c) 2012 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 "webkit/fileapi/local_file_writer.h"
+
+#include "base/callback.h"
+#include "base/location.h"
+#include "net/base/file_stream.h"
+#include "net/base/io_buffer.h"
+
+namespace fileapi {
+
+namespace {
+
+const int kOpenFlagsForWrite = base::PLATFORM_FILE_OPEN |
+ base::PLATFORM_FILE_WRITE |
+ base::PLATFORM_FILE_ASYNC;
+
+void EmptyCompletionCallback(int) {}
+
+} // anonymous namespace
kinuko 2012/04/20 11:26:37 nit: just 'namespace' would be ok (and more common
kinaba 2012/04/23 08:56:41 Done.
+
+LocalFileWriter::LocalFileWriter(const FilePath& file_path)
+ : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
+ file_path_(file_path),
+ has_pending_operation_(false) {}
+
+LocalFileWriter::~LocalFileWriter() {
+ DCHECK(!has_pending_operation_);
+ DCHECK(cancel_callback_.is_null());
+
+ weak_factory_.InvalidateWeakPtrs();
+}
+
+int LocalFileWriter::Write(net::IOBuffer* buf, int buf_len,
+ const net::CompletionCallback& callback) {
+ DCHECK(!has_pending_operation_);
+ DCHECK(cancel_callback_.is_null());
+
+ has_pending_operation_ = true;
+ if (stream_impl_.get()) {
+ int result = InitiateWrite(buf, buf_len, callback);
+ if (result != net::ERR_IO_PENDING)
+ has_pending_operation_ = false;
+ return result;
+ }
+ return InitiateOpen(base::Bind(&LocalFileWriter::ReadyToWrite,
+ weak_factory_.GetWeakPtr(),
+ make_scoped_refptr(buf), buf_len, callback));
+}
+
+int LocalFileWriter::Seek(int64 offset,
+ const net::Int64CompletionCallback& callback) {
+ DCHECK(!has_pending_operation_);
+ DCHECK(cancel_callback_.is_null());
+
+ has_pending_operation_ = true;
+ if (stream_impl_.get()) {
+ int result = InitiateSeek(offset, callback);
+ if (result != net::ERR_IO_PENDING)
+ has_pending_operation_ = false;
+ return result;
+ }
+ return InitiateOpen(base::Bind(&LocalFileWriter::ReadyToSeek,
+ weak_factory_.GetWeakPtr(), offset, callback));
+}
+
+void LocalFileWriter::Cancel(const net::CompletionCallback& callback) {
+ DCHECK(has_pending_operation_);
kinuko 2012/04/20 11:26:37 Could we simply run the callback (asynchronously)
kinaba 2012/04/23 08:56:41 I agree. Done.
+ DCHECK(cancel_callback_.is_null());
+
+ DCHECK(!callback.is_null());
+ cancel_callback_ = callback;
+}
+
+int LocalFileWriter::InitiateOpen(
+ const net::CompletionCallback& main_operation) {
+ DCHECK(has_pending_operation_);
+ DCHECK(!stream_impl_.get());
+
+ stream_impl_.reset(new net::FileStream(NULL));
+ return stream_impl_->Open(file_path_, kOpenFlagsForWrite,
+ base::Bind(&LocalFileWriter::DidOpen,
+ weak_factory_.GetWeakPtr(),
+ main_operation));
+}
+
+void LocalFileWriter::DidOpen(const net::CompletionCallback& main_operation,
+ int result) {
+ DCHECK(has_pending_operation_);
+ DCHECK(stream_impl_.get());
+
+ if (result != net::OK)
+ stream_impl_.reset(NULL);
+ if (CancelIfRequested())
+ return;
+ main_operation.Run(result);
+}
+
+void LocalFileWriter::ReadyToWrite(net::IOBuffer* buf, int buf_len,
+ const net::CompletionCallback& callback,
+ int file_open_result) {
+ DCHECK(has_pending_operation_);
+
+ if (file_open_result != net::OK) {
+ has_pending_operation_ = false;
+ callback.Run(file_open_result);
+ return;
+ }
+
+ int result = InitiateWrite(buf, buf_len, callback);
+ if (result != net::ERR_IO_PENDING) {
+ has_pending_operation_ = false;
+ callback.Run(result);
+ }
+}
+
+void LocalFileWriter::ReadyToSeek(int64 offset,
+ const net::Int64CompletionCallback& callback,
+ int file_open_result) {
+ DCHECK(has_pending_operation_);
+
+ if (file_open_result != net::OK) {
+ has_pending_operation_ = false;
+ callback.Run(file_open_result);
+ return;
+ }
+
+ int result = InitiateSeek(offset, callback);
+ if (result != net::ERR_IO_PENDING) {
+ has_pending_operation_ = false;
+ callback.Run(result);
+ }
+}
+
+int LocalFileWriter::InitiateWrite(net::IOBuffer* buf, int buf_len,
+ const net::CompletionCallback& callback) {
+ DCHECK(has_pending_operation_);
+ DCHECK(stream_impl_.get());
+
+ return stream_impl_->Write(buf, buf_len,
+ base::Bind(&LocalFileWriter::DidWrite,
+ weak_factory_.GetWeakPtr(),
+ callback));
+}
+
+int LocalFileWriter::InitiateSeek(
+ int64 offset, const net::Int64CompletionCallback& callback) {
+ DCHECK(has_pending_operation_);
+ DCHECK(stream_impl_.get());
+
+ return stream_impl_->Seek(net::FROM_BEGIN, offset,
+ base::Bind(&LocalFileWriter::DidSeek,
+ weak_factory_.GetWeakPtr(),
+ callback));
+}
+
+void LocalFileWriter::DidWrite(const net::CompletionCallback& callback,
+ int result) {
+ DCHECK(has_pending_operation_);
+
+ if (CancelIfRequested())
+ return;
+ has_pending_operation_ = false;
+ callback.Run(result);
+}
+
+void LocalFileWriter::DidSeek(const net::Int64CompletionCallback& callback,
+ int64 result) {
+ DCHECK(has_pending_operation_);
+
+ if (CancelIfRequested())
+ return;
+ has_pending_operation_ = false;
+ callback.Run(result);
+}
+
+bool LocalFileWriter::CancelIfRequested() {
+ DCHECK(has_pending_operation_);
+
+ if (cancel_callback_.is_null())
+ return false;
+
+ net::CompletionCallback pending_cancel = cancel_callback_;
+ has_pending_operation_ = false;
+ cancel_callback_.Reset();
+ pending_cancel.Run(net::OK);
+ return true;
+}
+
+} // namespace fileapi

Powered by Google App Engine
This is Rietveld 408576698