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
|
// 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_stream_writer.h"
#include "base/callback.h"
#include "base/message_loop.h"
#include "net/base/file_stream.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
namespace fileapi {
namespace {
const int kOpenFlagsForWrite = base::PLATFORM_FILE_OPEN |
base::PLATFORM_FILE_WRITE |
base::PLATFORM_FILE_ASYNC;
} // namespace
LocalFileStreamWriter::LocalFileStreamWriter(const base::FilePath& file_path,
int64 initial_offset)
: file_path_(file_path),
initial_offset_(initial_offset),
has_pending_operation_(false),
weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
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,
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(callback,
base::Bind(&LocalFileStreamWriter::ReadyToWrite,
weak_factory_.GetWeakPtr(),
make_scoped_refptr(buf), buf_len, callback));
}
int LocalFileStreamWriter::Cancel(const net::CompletionCallback& callback) {
if (!has_pending_operation_)
return net::ERR_UNEXPECTED;
DCHECK(!callback.is_null());
cancel_callback_ = callback;
return net::ERR_IO_PENDING;
}
int LocalFileStreamWriter::Flush(const net::CompletionCallback& callback) {
DCHECK(!has_pending_operation_);
DCHECK(cancel_callback_.is_null());
// Write() is not called yet, so there's nothing to flush.
if (!stream_impl_.get())
return net::OK;
has_pending_operation_ = true;
int result = InitiateFlush(callback);
if (result != net::ERR_IO_PENDING)
has_pending_operation_ = false;
return result;
}
int LocalFileStreamWriter::InitiateOpen(
const net::CompletionCallback& error_callback,
const base::Closure& 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(&LocalFileStreamWriter::DidOpen,
weak_factory_.GetWeakPtr(),
error_callback,
main_operation));
}
void LocalFileStreamWriter::DidOpen(
const net::CompletionCallback& error_callback,
const base::Closure& 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(NULL);
error_callback.Run(result);
return;
}
InitiateSeek(error_callback, main_operation);
}
void LocalFileStreamWriter::InitiateSeek(
const net::CompletionCallback& error_callback,
const base::Closure& main_operation) {
DCHECK(has_pending_operation_);
DCHECK(stream_impl_.get());
if (initial_offset_ == 0) {
// No need to seek.
main_operation.Run();
return;
}
int result = stream_impl_->Seek(net::FROM_BEGIN, initial_offset_,
base::Bind(&LocalFileStreamWriter::DidSeek,
weak_factory_.GetWeakPtr(),
error_callback,
main_operation));
if (result != net::ERR_IO_PENDING) {
has_pending_operation_ = false;
error_callback.Run(result);
}
}
void LocalFileStreamWriter::DidSeek(
const net::CompletionCallback& error_callback,
const base::Closure& main_operation,
int64 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;
error_callback.Run(static_cast<int>(result));
return;
}
main_operation.Run();
}
void LocalFileStreamWriter::ReadyToWrite(
net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) {
DCHECK(has_pending_operation_);
int result = InitiateWrite(buf, buf_len, callback);
if (result != net::ERR_IO_PENDING) {
has_pending_operation_ = false;
callback.Run(result);
}
}
int LocalFileStreamWriter::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(&LocalFileStreamWriter::DidWrite,
weak_factory_.GetWeakPtr(),
callback));
}
void LocalFileStreamWriter::DidWrite(const net::CompletionCallback& callback,
int result) {
DCHECK(has_pending_operation_);
if (CancelIfRequested())
return;
has_pending_operation_ = false;
callback.Run(result);
}
int LocalFileStreamWriter::InitiateFlush(
const net::CompletionCallback& callback) {
DCHECK(has_pending_operation_);
DCHECK(stream_impl_.get());
return stream_impl_->Flush(base::Bind(&LocalFileStreamWriter::DidFlush,
weak_factory_.GetWeakPtr(),
callback));
}
void LocalFileStreamWriter::DidFlush(const net::CompletionCallback& callback,
int result) {
DCHECK(has_pending_operation_);
if (CancelIfRequested())
return;
has_pending_operation_ = false;
callback.Run(result);
}
bool LocalFileStreamWriter::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
|