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

Side by Side 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: #include 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/fileapi/local_file_writer.h ('k') | webkit/fileapi/local_file_writer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "webkit/fileapi/local_file_writer.h"
6
7 #include "base/callback.h"
8 #include "base/message_loop.h"
9 #include "net/base/file_stream.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12
13 namespace fileapi {
14
15 namespace {
16
17 const int kOpenFlagsForWrite = base::PLATFORM_FILE_OPEN |
18 base::PLATFORM_FILE_WRITE |
19 base::PLATFORM_FILE_ASYNC;
20
21 } // namespace
22
23 LocalFileWriter::LocalFileWriter(const FilePath& file_path,
24 int64 initial_offset)
25 : file_path_(file_path),
26 initial_offset_(initial_offset),
27 has_pending_operation_(false),
28 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
29
30 LocalFileWriter::~LocalFileWriter() {
31 // Invalidate weak pointers so that we won't receive any callbacks from
32 // in-flight stream operations, which might be triggered during the file close
33 // in the FileStream destructor.
34 weak_factory_.InvalidateWeakPtrs();
35
36 // FileStream's destructor closes the file safely, since we opened the file
37 // by its Open() method.
38 }
39
40 int LocalFileWriter::Write(net::IOBuffer* buf, int buf_len,
41 const net::CompletionCallback& callback) {
42 DCHECK(!has_pending_operation_);
43 DCHECK(cancel_callback_.is_null());
44
45 has_pending_operation_ = true;
46 if (stream_impl_.get()) {
47 int result = InitiateWrite(buf, buf_len, callback);
48 if (result != net::ERR_IO_PENDING)
49 has_pending_operation_ = false;
50 return result;
51 }
52 return InitiateOpen(callback,
53 base::Bind(&LocalFileWriter::ReadyToWrite,
54 weak_factory_.GetWeakPtr(),
55 make_scoped_refptr(buf), buf_len, callback));
56 }
57
58 int LocalFileWriter::Cancel(const net::CompletionCallback& callback) {
59 if (!has_pending_operation_)
60 return net::ERR_UNEXPECTED;
61
62 DCHECK(!callback.is_null());
63 cancel_callback_ = callback;
64 return net::ERR_IO_PENDING;
65 }
66
67 int LocalFileWriter::InitiateOpen(const net::CompletionCallback& error_callback,
68 const base::Closure& main_operation) {
69 DCHECK(has_pending_operation_);
70 DCHECK(!stream_impl_.get());
71
72 stream_impl_.reset(new net::FileStream(NULL));
73 return stream_impl_->Open(file_path_,
74 kOpenFlagsForWrite,
75 base::Bind(&LocalFileWriter::DidOpen,
76 weak_factory_.GetWeakPtr(),
77 error_callback,
78 main_operation));
79 }
80
81 void LocalFileWriter::DidOpen(const net::CompletionCallback& error_callback,
82 const base::Closure& main_operation,
83 int result) {
84 DCHECK(has_pending_operation_);
85 DCHECK(stream_impl_.get());
86
87 if (CancelIfRequested())
88 return;
89
90 if (result != net::OK) {
91 has_pending_operation_ = false;
92 stream_impl_.reset(NULL);
93 error_callback.Run(result);
94 return;
95 }
96
97 InitiateSeek(error_callback, main_operation);
98 }
99
100 void LocalFileWriter::InitiateSeek(
101 const net::CompletionCallback& error_callback,
102 const base::Closure& main_operation) {
103 DCHECK(has_pending_operation_);
104 DCHECK(stream_impl_.get());
105
106 if (initial_offset_ == 0) {
107 // No need to seek.
108 main_operation.Run();
109 return;
110 }
111
112 int result = stream_impl_->Seek(net::FROM_BEGIN, initial_offset_,
113 base::Bind(&LocalFileWriter::DidSeek,
114 weak_factory_.GetWeakPtr(),
115 error_callback,
116 main_operation));
117 if (result != net::ERR_IO_PENDING) {
118 has_pending_operation_ = false;
119 error_callback.Run(result);
120 }
121 }
122
123 void LocalFileWriter::DidSeek(const net::CompletionCallback& error_callback,
124 const base::Closure& main_operation,
125 int64 result) {
126 DCHECK(has_pending_operation_);
127
128 if (CancelIfRequested())
129 return;
130
131 if (result != initial_offset_) {
132 // TODO(kinaba) add a more specific error code.
133 result = net::ERR_FAILED;
134 }
135
136 if (result < 0) {
137 has_pending_operation_ = false;
138 error_callback.Run(static_cast<int>(result));
139 return;
140 }
141
142 main_operation.Run();
143 }
144
145 void LocalFileWriter::ReadyToWrite(net::IOBuffer* buf, int buf_len,
146 const net::CompletionCallback& callback) {
147 DCHECK(has_pending_operation_);
148
149 int result = InitiateWrite(buf, buf_len, callback);
150 if (result != net::ERR_IO_PENDING) {
151 has_pending_operation_ = false;
152 callback.Run(result);
153 }
154 }
155
156 int LocalFileWriter::InitiateWrite(net::IOBuffer* buf, int buf_len,
157 const net::CompletionCallback& callback) {
158 DCHECK(has_pending_operation_);
159 DCHECK(stream_impl_.get());
160
161 return stream_impl_->Write(buf, buf_len,
162 base::Bind(&LocalFileWriter::DidWrite,
163 weak_factory_.GetWeakPtr(),
164 callback));
165 }
166
167 void LocalFileWriter::DidWrite(const net::CompletionCallback& callback,
168 int result) {
169 DCHECK(has_pending_operation_);
170
171 if (CancelIfRequested())
172 return;
173 has_pending_operation_ = false;
174 callback.Run(result);
175 }
176
177 bool LocalFileWriter::CancelIfRequested() {
178 DCHECK(has_pending_operation_);
179
180 if (cancel_callback_.is_null())
181 return false;
182
183 net::CompletionCallback pending_cancel = cancel_callback_;
184 has_pending_operation_ = false;
185 cancel_callback_.Reset();
186 pending_cancel.Run(net::OK);
187 return true;
188 }
189
190 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/local_file_writer.h ('k') | webkit/fileapi/local_file_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698