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

Side by Side Diff: webkit/fileapi/file_writer_delegate.cc

Issue 10387054: Implement SandboxFileWriter and rewrite FileWriterDelegate to use it (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test fix Created 8 years, 7 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/file_writer_delegate.h ('k') | webkit/fileapi/file_writer_delegate_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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "webkit/fileapi/file_writer_delegate.h" 5 #include "webkit/fileapi/file_writer_delegate.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/file_util_proxy.h" 9 #include "base/file_util_proxy.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/message_loop_proxy.h" 11 #include "base/message_loop_proxy.h"
12 #include "base/sequenced_task_runner.h" 12 #include "base/sequenced_task_runner.h"
13 #include "base/threading/thread_restrictions.h" 13 #include "base/threading/thread_restrictions.h"
14 #include "net/base/net_errors.h" 14 #include "net/base/net_errors.h"
15 #include "webkit/fileapi/file_system_context.h" 15 #include "webkit/fileapi/file_system_context.h"
16 #include "webkit/fileapi/file_system_operation.h" 16 #include "webkit/fileapi/file_writer.h"
17 #include "webkit/fileapi/file_system_operation_context.h"
18 #include "webkit/fileapi/file_system_quota_util.h"
19 17
20 namespace fileapi { 18 namespace fileapi {
21 19
22 static const int kReadBufSize = 32768; 20 static const int kReadBufSize = 32768;
23 21
24 namespace { 22 namespace {
25 23
26 typedef base::Callback<void(base::PlatformFileError /* error code */, 24 base::PlatformFileError NetErrorToPlatformFileError(int error) {
27 const base::PlatformFileInfo& /* file_info */)> 25 // TODO(kinuko): Move this static method to more convenient place.
28 InitializeTaskCallback; 26 switch (error) {
27 case net::ERR_FILE_NO_SPACE:
28 return base::PLATFORM_FILE_ERROR_NO_SPACE;
29 case net::ERR_FILE_NOT_FOUND:
30 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
31 case net::ERR_ACCESS_DENIED:
32 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED;
33 default:
34 return base::PLATFORM_FILE_ERROR_FAILED;
35 }
36 }
29 37
30 class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { 38 } // namespace
31 public:
32 InitializeTask(
33 base::PlatformFile file,
34 const InitializeTaskCallback& callback)
35 : original_loop_(base::MessageLoopProxy::current()),
36 error_code_(base::PLATFORM_FILE_OK),
37 file_(file),
38 callback_(callback) {
39 DCHECK_EQ(false, callback.is_null());
40 }
41
42 bool Start(base::SequencedTaskRunner* task_runner,
43 const tracked_objects::Location& from_here) {
44 return task_runner->PostTask(
45 from_here,
46 base::Bind(&InitializeTask::ProcessOnTargetThread, this));
47 }
48
49 private:
50 friend class base::RefCountedThreadSafe<InitializeTask>;
51 ~InitializeTask() {}
52
53 void RunCallback() {
54 callback_.Run(error_code_, file_info_);
55 }
56
57 void ProcessOnTargetThread() {
58 if (!base::GetPlatformFileInfo(file_, &file_info_))
59 error_code_ = base::PLATFORM_FILE_ERROR_FAILED;
60 original_loop_->PostTask(
61 FROM_HERE,
62 base::Bind(&InitializeTask::RunCallback, this));
63 }
64
65 scoped_refptr<base::MessageLoopProxy> original_loop_;
66 base::PlatformFileError error_code_;
67
68 base::PlatformFile file_;
69 InitializeTaskCallback callback_;
70
71 base::PlatformFileInfo file_info_;
72 };
73
74 } // namespace (anonymous)
75 39
76 FileWriterDelegate::FileWriterDelegate( 40 FileWriterDelegate::FileWriterDelegate(
77 FileSystemOperation* file_system_operation, 41 const FileSystemOperationInterface::WriteCallback& write_callback,
78 const FileSystemPath& path, 42 scoped_ptr<FileWriter> file_writer)
79 int64 offset) 43 : write_callback_(write_callback),
80 : file_system_operation_(file_system_operation), 44 file_writer_(file_writer.Pass()),
81 file_(base::kInvalidPlatformFileValue),
82 path_(path),
83 offset_(offset),
84 has_pending_write_(false),
85 bytes_written_backlog_(0), 45 bytes_written_backlog_(0),
86 bytes_written_(0), 46 bytes_written_(0),
87 bytes_read_(0), 47 bytes_read_(0),
88 total_bytes_written_(0),
89 allowed_bytes_to_write_(0),
90 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), 48 io_buffer_(new net::IOBufferWithSize(kReadBufSize)),
91 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { 49 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
92 } 50 }
93 51
94 FileWriterDelegate::~FileWriterDelegate() { 52 FileWriterDelegate::~FileWriterDelegate() {
95 } 53 }
96 54
97 void FileWriterDelegate::OnGetFileInfoAndStartRequest( 55 void FileWriterDelegate::Start(scoped_ptr<net::URLRequest> request) {
98 scoped_ptr<net::URLRequest> request,
99 base::PlatformFileError error,
100 const base::PlatformFileInfo& file_info) {
101 if (error != base::PLATFORM_FILE_OK) {
102 OnError(error);
103 return;
104 }
105 int64 allowed_bytes_growth =
106 file_system_operation_context()->allowed_bytes_growth();
107 if (allowed_bytes_growth < 0)
108 allowed_bytes_growth = 0;
109 int64 overlap = file_info.size - offset_;
110 allowed_bytes_to_write_ = allowed_bytes_growth;
111 if (kint64max - overlap > allowed_bytes_growth)
112 allowed_bytes_to_write_ += overlap;
113 size_ = file_info.size;
114 file_stream_.reset(new net::FileStream(
115 file_,
116 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE |
117 base::PLATFORM_FILE_ASYNC,
118 NULL));
119 DCHECK(!request_.get());
120 request_ = request.Pass(); 56 request_ = request.Pass();
121 request_->Start(); 57 request_->Start();
122 } 58 }
123 59
124 void FileWriterDelegate::Start(base::PlatformFile file,
125 scoped_ptr<net::URLRequest> request) {
126 file_ = file;
127
128 scoped_refptr<InitializeTask> relay = new InitializeTask(
129 file_,
130 base::Bind(&FileWriterDelegate::OnGetFileInfoAndStartRequest,
131 weak_factory_.GetWeakPtr(), base::Passed(&request)));
132 relay->Start(file_system_operation_context()->file_task_runner(), FROM_HERE);
133 }
134
135 bool FileWriterDelegate::Cancel() { 60 bool FileWriterDelegate::Cancel() {
136 if (request_.get()) { 61 if (request_.get()) {
137 // This halts any callbacks on this delegate. 62 // This halts any callbacks on this delegate.
138 request_->set_delegate(NULL); 63 request_->set_delegate(NULL);
139 request_->Cancel(); 64 request_->Cancel();
140 } 65 }
141 66
142 // Return true to finish immediately if we're not writing. 67 const int status = file_writer_->Cancel(
143 // Otherwise we'll do the final cleanup in the write callback. 68 base::Bind(&FileWriterDelegate::OnWriteCancelled,
144 return !has_pending_write_; 69 weak_factory_.GetWeakPtr()));
70 // Return true to finish immediately if we have no pending writes.
71 // Otherwise we'll do the final cleanup in the Cancel callback.
72 return (status != net::ERR_IO_PENDING);
145 } 73 }
146 74
147 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, 75 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request,
148 const GURL& new_url, 76 const GURL& new_url,
149 bool* defer_redirect) { 77 bool* defer_redirect) {
150 NOTREACHED(); 78 NOTREACHED();
151 OnError(base::PLATFORM_FILE_ERROR_SECURITY); 79 OnError(base::PLATFORM_FILE_ERROR_SECURITY);
152 } 80 }
153 81
154 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request, 82 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request,
(...skipping 11 matching lines...) Expand all
166 94
167 void FileWriterDelegate::OnSSLCertificateError(net::URLRequest* request, 95 void FileWriterDelegate::OnSSLCertificateError(net::URLRequest* request,
168 const net::SSLInfo& ssl_info, 96 const net::SSLInfo& ssl_info,
169 bool fatal) { 97 bool fatal) {
170 NOTREACHED(); 98 NOTREACHED();
171 OnError(base::PLATFORM_FILE_ERROR_SECURITY); 99 OnError(base::PLATFORM_FILE_ERROR_SECURITY);
172 } 100 }
173 101
174 void FileWriterDelegate::OnResponseStarted(net::URLRequest* request) { 102 void FileWriterDelegate::OnResponseStarted(net::URLRequest* request) {
175 DCHECK_EQ(request_.get(), request); 103 DCHECK_EQ(request_.get(), request);
176 // file_stream_->Seek() blocks the IO thread.
177 // See http://crbug.com/75548.
178 base::ThreadRestrictions::ScopedAllowIO allow_io;
179 if (!request->status().is_success() || request->GetResponseCode() != 200) { 104 if (!request->status().is_success() || request->GetResponseCode() != 200) {
180 OnError(base::PLATFORM_FILE_ERROR_FAILED); 105 OnError(base::PLATFORM_FILE_ERROR_FAILED);
181 return; 106 return;
182 } 107 }
183 int64 error = file_stream_->SeekSync(net::FROM_BEGIN, offset_);
184 if (error != offset_) {
185 OnError(base::PLATFORM_FILE_ERROR_FAILED);
186 return;
187 }
188 Read(); 108 Read();
189 } 109 }
190 110
191 void FileWriterDelegate::OnReadCompleted(net::URLRequest* request, 111 void FileWriterDelegate::OnReadCompleted(net::URLRequest* request,
192 int bytes_read) { 112 int bytes_read) {
193 DCHECK_EQ(request_.get(), request); 113 DCHECK_EQ(request_.get(), request);
194 if (!request->status().is_success()) { 114 if (!request->status().is_success()) {
195 OnError(base::PLATFORM_FILE_ERROR_FAILED); 115 OnError(base::PLATFORM_FILE_ERROR_FAILED);
196 return; 116 return;
197 } 117 }
198 OnDataReceived(bytes_read); 118 OnDataReceived(bytes_read);
199 } 119 }
200 120
201 void FileWriterDelegate::Read() { 121 void FileWriterDelegate::Read() {
202 bytes_written_ = 0; 122 bytes_written_ = 0;
203 bytes_read_ = 0; 123 bytes_read_ = 0;
204 if (request_->Read(io_buffer_.get(), io_buffer_->size(), 124 if (request_->Read(io_buffer_.get(), io_buffer_->size(), &bytes_read_)) {
205 &bytes_read_)) {
206 MessageLoop::current()->PostTask( 125 MessageLoop::current()->PostTask(
207 FROM_HERE, 126 FROM_HERE,
208 base::Bind(&FileWriterDelegate::OnDataReceived, 127 base::Bind(&FileWriterDelegate::OnDataReceived,
209 weak_factory_.GetWeakPtr(), bytes_read_)); 128 weak_factory_.GetWeakPtr(), bytes_read_));
210 } else if (!request_->status().is_io_pending()) { 129 } else if (!request_->status().is_io_pending()) {
211 OnError(base::PLATFORM_FILE_ERROR_FAILED); 130 OnError(base::PLATFORM_FILE_ERROR_FAILED);
212 } 131 }
213 } 132 }
214 133
215 void FileWriterDelegate::OnDataReceived(int bytes_read) { 134 void FileWriterDelegate::OnDataReceived(int bytes_read) {
216 bytes_read_ = bytes_read; 135 bytes_read_ = bytes_read;
217 if (!bytes_read_) { // We're done. 136 if (!bytes_read_) { // We're done.
218 OnProgress(0, true); 137 OnProgress(0, true);
219 } else { 138 } else {
220 // This could easily be optimized to rotate between a pool of buffers, so 139 // This could easily be optimized to rotate between a pool of buffers, so
221 // that we could read and write at the same time. It's not yet clear that 140 // that we could read and write at the same time. It's not yet clear that
222 // it's necessary. 141 // it's necessary.
223 cursor_ = new net::DrainableIOBuffer(io_buffer_, bytes_read_); 142 cursor_ = new net::DrainableIOBuffer(io_buffer_, bytes_read_);
224 Write(); 143 Write();
225 } 144 }
226 } 145 }
227 146
228 void FileWriterDelegate::Write() { 147 void FileWriterDelegate::Write() {
229 // allowed_bytes_to_write could be negative if the file size is
230 // greater than the current (possibly new) quota.
231 // (The UI should clear the entire origin data if the smaller quota size
232 // is set in general, though the UI/deletion code is not there yet.)
233 DCHECK(total_bytes_written_ <= allowed_bytes_to_write_ ||
234 allowed_bytes_to_write_ < 0);
235 if (total_bytes_written_ >= allowed_bytes_to_write_) {
236 OnError(base::PLATFORM_FILE_ERROR_NO_SPACE);
237 return;
238 }
239
240 int64 bytes_to_write = bytes_read_ - bytes_written_; 148 int64 bytes_to_write = bytes_read_ - bytes_written_;
241 if (bytes_to_write > allowed_bytes_to_write_ - total_bytes_written_)
242 bytes_to_write = allowed_bytes_to_write_ - total_bytes_written_;
243
244 has_pending_write_ = true;
245 int write_response = 149 int write_response =
246 file_stream_->Write(cursor_, 150 file_writer_->Write(cursor_,
247 static_cast<int>(bytes_to_write), 151 static_cast<int>(bytes_to_write),
248 base::Bind(&FileWriterDelegate::OnDataWritten, 152 base::Bind(&FileWriterDelegate::OnDataWritten,
249 weak_factory_.GetWeakPtr())); 153 weak_factory_.GetWeakPtr()));
250 if (write_response > 0) 154 if (write_response > 0)
251 MessageLoop::current()->PostTask( 155 MessageLoop::current()->PostTask(
252 FROM_HERE, 156 FROM_HERE,
253 base::Bind(&FileWriterDelegate::OnDataWritten, 157 base::Bind(&FileWriterDelegate::OnDataWritten,
254 weak_factory_.GetWeakPtr(), write_response)); 158 weak_factory_.GetWeakPtr(), write_response));
255 else if (net::ERR_IO_PENDING != write_response) 159 else if (net::ERR_IO_PENDING != write_response)
256 OnError(base::PLATFORM_FILE_ERROR_FAILED); 160 OnError(NetErrorToPlatformFileError(write_response));
257 } 161 }
258 162
259 void FileWriterDelegate::OnDataWritten(int write_response) { 163 void FileWriterDelegate::OnDataWritten(int write_response) {
260 has_pending_write_ = false;
261 if (write_response > 0) { 164 if (write_response > 0) {
262 if (request_->status().status() == net::URLRequestStatus::CANCELED) {
263 OnProgress(write_response, true);
264 return;
265 }
266 OnProgress(write_response, false); 165 OnProgress(write_response, false);
267 cursor_->DidConsume(write_response); 166 cursor_->DidConsume(write_response);
268 bytes_written_ += write_response; 167 bytes_written_ += write_response;
269 total_bytes_written_ += write_response;
270 if (bytes_written_ == bytes_read_) 168 if (bytes_written_ == bytes_read_)
271 Read(); 169 Read();
272 else 170 else
273 Write(); 171 Write();
274 } else { 172 } else {
275 OnError(base::PLATFORM_FILE_ERROR_FAILED); 173 OnError(NetErrorToPlatformFileError(write_response));
276 } 174 }
277 } 175 }
278 176
279 void FileWriterDelegate::OnError(base::PlatformFileError error) { 177 void FileWriterDelegate::OnError(base::PlatformFileError error) {
280 if (request_.get()) { 178 if (request_.get()) {
281 request_->set_delegate(NULL); 179 request_->set_delegate(NULL);
282 request_->Cancel(); 180 request_->Cancel();
283 } 181 }
284 182
285 file_system_operation_->DidWrite(error, 0, true); 183 write_callback_.Run(error, 0, true);
286 } 184 }
287 185
288 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { 186 void FileWriterDelegate::OnProgress(int bytes_written, bool done) {
289 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); 187 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_);
290 if (quota_util() &&
291 bytes_written > 0 &&
292 total_bytes_written_ + bytes_written + offset_ > size_) {
293 int overlapped = 0;
294 if (total_bytes_written_ + offset_ < size_)
295 overlapped = size_ - total_bytes_written_ - offset_;
296 quota_util()->proxy()->UpdateOriginUsage(
297 file_system_operation_->file_system_context()->quota_manager_proxy(),
298 path_.origin(), path_.type(),
299 bytes_written - overlapped);
300 }
301 static const int kMinProgressDelayMS = 200; 188 static const int kMinProgressDelayMS = 200;
302 base::Time currentTime = base::Time::Now(); 189 base::Time currentTime = base::Time::Now();
303 if (done || last_progress_event_time_.is_null() || 190 if (done || last_progress_event_time_.is_null() ||
304 (currentTime - last_progress_event_time_).InMilliseconds() > 191 (currentTime - last_progress_event_time_).InMilliseconds() >
305 kMinProgressDelayMS) { 192 kMinProgressDelayMS) {
306 bytes_written += bytes_written_backlog_; 193 bytes_written += bytes_written_backlog_;
307 last_progress_event_time_ = currentTime; 194 last_progress_event_time_ = currentTime;
308 bytes_written_backlog_ = 0; 195 bytes_written_backlog_ = 0;
309 if (done && quota_util()) 196 write_callback_.Run(
310 quota_util()->proxy()->EndUpdateOrigin(path_.origin(), path_.type());
311 file_system_operation_->DidWrite(
312 base::PLATFORM_FILE_OK, bytes_written, done); 197 base::PLATFORM_FILE_OK, bytes_written, done);
313 return; 198 return;
314 } 199 }
315 bytes_written_backlog_ += bytes_written; 200 bytes_written_backlog_ += bytes_written;
316 } 201 }
317 202
318 FileSystemOperationContext* 203 void FileWriterDelegate::OnWriteCancelled(int status) {
319 FileWriterDelegate::file_system_operation_context() const { 204 write_callback_.Run(base::PLATFORM_FILE_ERROR_ABORT, 0, true);
320 DCHECK(file_system_operation_);
321 DCHECK(file_system_operation_->file_system_operation_context());
322 return file_system_operation_->file_system_operation_context();
323 }
324
325 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const {
326 DCHECK(file_system_operation_);
327 DCHECK(file_system_operation_->file_system_context());
328 DCHECK(file_system_operation_->file_system_operation_context());
329 return file_system_operation_->file_system_context()->GetQuotaUtil(
330 path_.type());
331 } 205 }
332 206
333 } // namespace fileapi 207 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/file_writer_delegate.h ('k') | webkit/fileapi/file_writer_delegate_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698