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

Side by Side Diff: storage/browser/blob/blob_reader.cc

Issue 1337153002: [Blob] BlobReader class & tests, and removal of all redundant reading. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: windows fix Created 5 years, 3 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
OLDNEW
(Empty)
1 // Copyright 2015 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 "storage/browser/blob/blob_reader.h"
6
7 #include <algorithm>
8 #include <limits>
9
10 #include "base/bind.h"
11 #include "base/stl_util.h"
12 #include "base/task_runner.h"
13 #include "base/time/time.h"
14 #include "base/trace_event/trace_event.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h"
17 #include "net/disk_cache/disk_cache.h"
18 #include "storage/browser/blob/blob_data_handle.h"
19 #include "storage/browser/blob/blob_data_snapshot.h"
20 #include "storage/browser/fileapi/file_stream_reader.h"
21 #include "storage/browser/fileapi/file_system_context.h"
22 #include "storage/browser/fileapi/file_system_url.h"
23 #include "storage/common/data_element.h"
24
25 namespace storage {
26 namespace {
27 bool IsFileType(DataElement::Type type) {
28 switch (type) {
29 case DataElement::TYPE_FILE:
30 case DataElement::TYPE_FILE_FILESYSTEM:
31 return true;
32 default:
33 return false;
34 }
35 }
36 } // namespace
37
38 BlobReader::FileStreamReaderProvider::~FileStreamReaderProvider() {}
39
40 BlobReader::BlobReader(
41 const BlobDataHandle* blob_handle,
42 scoped_ptr<FileStreamReaderProvider> file_stream_provider,
43 base::SingleThreadTaskRunner* file_task_runner)
44 : blob_handle_(blob_handle),
michaeln 2015/09/17 00:45:37 rawptrs data members make me look twice. i don't t
dmurph 2015/09/19 00:33:43 Sounds good.
45 file_stream_provider_(file_stream_provider.Pass()),
46 file_task_runner_(file_task_runner),
47 net_error_(net::OK),
48 item_list_populated_(false),
49 total_size_calculated_(false),
50 total_size_(0),
51 remaining_bytes_(0),
52 pending_get_file_info_count_(0),
53 current_item_index_(0),
54 current_item_offset_(0),
55 io_pending_(false),
56 weak_factory_(this) {
57 if (blob_handle_) {
58 blob_data_ = blob_handle_->CreateSnapshot().Pass();
59 }
60 }
61
62 BlobReader::~BlobReader() {
63 STLDeleteValues(&index_to_reader_);
64 }
65
66 BlobReader::Status BlobReader::CalculateSize(net::CompletionCallback done) {
67 DCHECK(!total_size_calculated_);
michaeln 2015/09/17 00:45:37 not sure this matters, its not safe to call Calcul
dmurph 2015/09/19 00:33:43 Done.
68 if (!blob_handle_) {
69 return ReportError(net::ERR_FILE_NOT_FOUND);
70 }
71
72 net_error_ = net::OK;
73 total_size_ = 0;
74 const auto& items = blob_data_->items();
75 item_length_list_.resize(items.size());
76 pending_get_file_info_count_ = 0;
77 for (size_t i = 0; i < items.size(); ++i) {
78 const BlobDataItem& item = *items.at(i);
79 if (IsFileType(item.type())) {
80 ++pending_get_file_info_count_;
81 storage::FileStreamReader* const reader = FileReaderAtIndex(i);
michaeln 2015/09/17 00:45:37 fyi: i was surprised that FileReaderAtIndex() crea
dmurph 2015/09/19 00:33:43 Replaced with GetOrCreateFileReaderAtIndex
82 if (!reader) {
83 return ReportError(net::ERR_FAILED);
84 }
85 int64_t length_output = reader->GetLength(base::Bind(
86 &BlobReader::DidGetFileItemLength, weak_factory_.GetWeakPtr(), i));
87 if (length_output == net::ERR_IO_PENDING) {
88 continue;
89 }
90 if (length_output < 0) {
91 return ReportError(length_output);
92 }
93 // We got the length right away
94 --pending_get_file_info_count_;
95 uint64_t resolved_length;
96 if (!ResolveFileItemLength(item, length_output, &resolved_length)) {
97 return ReportError(net::ERR_FILE_NOT_FOUND);
98 }
99 if (!AddItemLength(i, resolved_length)) {
100 return ReportError(net::ERR_FAILED);
101 }
102 continue;
103 }
104
105 if (!AddItemLength(i, item.length()))
106 return ReportError(net::ERR_FAILED);
107 }
108
109 if (pending_get_file_info_count_ == 0) {
110 DidCountSize();
111 return Status::DONE;
112 }
113
114 size_done_ = done;
michaeln 2015/09/17 00:45:37 nit: these names sound like bool values, maybe app
dmurph 2015/09/19 00:33:43 Done.
115 return Status::IO_PENDING;
116 }
117
118 BlobReader::Status BlobReader::SetReadRange(uint64_t offset, uint64_t length) {
119 if (!blob_handle_) {
120 return ReportError(net::ERR_FILE_NOT_FOUND);
121 }
122 if (!total_size_calculated_) {
123 return ReportError(net::ERR_FAILED);
124 }
125 if (offset + length > total_size_) {
126 return ReportError(net::ERR_FILE_NOT_FOUND);
127 }
128 // Skip the initial items that are not in the range.
129 remaining_bytes_ = length;
130 const auto& items = blob_data_->items();
131 for (current_item_index_ = 0;
132 current_item_index_ < items.size() &&
133 offset >= item_length_list_[current_item_index_];
134 ++current_item_index_) {
135 offset -= item_length_list_[current_item_index_];
136 }
137
138 // Set the offset that need to jump to for the first item in the range.
139 current_item_offset_ = offset;
140 if (current_item_offset_ == 0)
141 return Status::DONE;
142
143 // Adjust the offset of the first stream if it is of file type.
144 const BlobDataItem& item = *items.at(current_item_index_);
145 if (IsFileType(item.type())) {
146 SetFileReaderAtIndex(current_item_index_,
147 CreateFileStreamReader(item, offset));
148 }
149 return Status::DONE;
150 }
151
152 BlobReader::Status BlobReader::Read(net::IOBuffer* buffer,
153 size_t dest_size,
154 int* bytes_read,
155 net::CompletionCallback done) {
156 if (!blob_handle_) {
157 return ReportError(net::ERR_FILE_NOT_FOUND);
158 }
159 DCHECK(bytes_read);
160 DCHECK_GE(remaining_bytes_, 0ul);
161 DCHECK(read_done_.is_null());
162
163 if (!total_size_calculated_) {
164 net_error_ = net::ERR_FAILED;
165 }
166
167 // Bail out immediately if we encountered an error.
168 if (net_error_ != net::OK) {
169 *bytes_read = 0;
170 return Status::NET_ERROR;
171 }
172
173 read_done_ = done;
174
175 DCHECK_GE(dest_size, 0ul);
176 if (remaining_bytes_ < static_cast<uint64_t>(dest_size))
177 dest_size = static_cast<int>(remaining_bytes_);
178
179 // If we should copy zero bytes because |remaining_bytes_| is zero, short
180 // circuit here.
181 if (!dest_size) {
182 *bytes_read = 0;
183 return Status::DONE;
184 }
185
186 // Keep track of the buffer.
187 DCHECK(!read_buf_.get());
188 read_buf_ = new net::DrainableIOBuffer(buffer, dest_size);
189
190 Status status = ReadLoop(bytes_read);
191 if (status != Status::IO_PENDING) {
192 read_done_.Reset();
193 }
194 return status;
195 }
196
197 void BlobReader::Kill() {
198 DeleteCurrentFileReader();
199 weak_factory_.InvalidateWeakPtrs();
200 }
201
202 bool BlobReader::IsInMemory() const {
michaeln 2015/09/17 00:45:37 if (!blob_data_) return true? probably need to te
dmurph 2015/09/19 00:33:43 Done.
203 for (const auto& item : blob_data_->items()) {
204 if (item->type() != DataElement::TYPE_BYTES) {
205 return false;
206 }
207 }
208 return true;
209 }
210
211 void BlobReader::InvalidateCallbacksAndDone(int net_error,
212 net::CompletionCallback done) {
213 net_error_ = net_error;
214 weak_factory_.InvalidateWeakPtrs();
215 size_done_.Reset();
216 read_done_.Reset();
217 read_buf_ = nullptr;
218 done.Run(net_error);
219 }
220
221 BlobReader::Status BlobReader::ReportError(int net_error) {
222 net_error_ = net_error;
223 return Status::NET_ERROR;
224 }
225
226 bool BlobReader::AddItemLength(size_t index, uint64_t item_length) {
227 if (item_length > std::numeric_limits<uint64_t>::max() - total_size_) {
228 return false;
229 }
230
231 // Cache the size and add it to the total size.
232 DCHECK_LT(index, item_length_list_.size());
233 item_length_list_[index] = item_length;
234 total_size_ += item_length;
235 return true;
236 }
237
238 bool BlobReader::ResolveFileItemLength(const BlobDataItem& item,
239 int64_t total_length,
240 uint64_t* output_length) {
241 DCHECK(IsFileType(item.type()));
242 DCHECK(output_length);
243 uint64_t file_length = total_length;
244 uint64_t item_offset = item.offset();
245 uint64_t item_length = item.length();
246 if (item_offset > file_length) {
247 return false;
248 }
249
250 uint64 max_length = file_length - item_offset;
251
252 // If item length is undefined, then we need to use the file size being
253 // resolved in the real time.
254 if (item_length == std::numeric_limits<uint64>::max()) {
255 item_length = max_length;
256 } else if (item_length > max_length) {
257 return false;
258 }
259
260 *output_length = item_length;
261 return true;
262 }
263
264 void BlobReader::DidGetFileItemLength(size_t index, int64_t result) {
265 // Do nothing if we have encountered an error.
266 if (net_error_)
267 return;
268
269 if (result == net::ERR_UPLOAD_FILE_CHANGED) {
michaeln 2015/09/17 00:45:37 why translate this error code? nit: if (result =
dmurph 2015/09/19 00:33:43 Done.
270 InvalidateCallbacksAndDone(net::ERR_FILE_NOT_FOUND, size_done_);
271 return;
272 } else if (result < 0) {
273 InvalidateCallbacksAndDone(result, size_done_);
274 return;
275 }
276
277 const auto& items = blob_data_->items();
278 DCHECK_LT(index, items.size());
279 const BlobDataItem& item = *items.at(index);
280 uint64_t length;
281 if (!ResolveFileItemLength(item, result, &length)) {
282 InvalidateCallbacksAndDone(net::ERR_FILE_NOT_FOUND, size_done_);
283 return;
284 }
285 if (!AddItemLength(index, length)) {
286 InvalidateCallbacksAndDone(net::ERR_FAILED, size_done_);
287 return;
288 }
289
290 if (--pending_get_file_info_count_ == 0) {
291 DidCountSize();
292 net::CompletionCallback done = size_done_;
293 size_done_.Reset();
294 done.Run(total_size_);
295 }
296 }
297
298 void BlobReader::DidCountSize() {
michaeln 2015/09/17 00:45:37 might make sense to have this method be responsibl
dmurph 2015/09/19 00:33:43 This gets called at the end of both the async and
dmurph 2015/09/19 00:33:43 This gets called at the end of both the async and
299 DCHECK(!net_error_);
300 total_size_calculated_ = true;
301 remaining_bytes_ = total_size_;
302 }
303
304 BlobReader::Status BlobReader::ReadLoop(int* bytes_read) {
305 // Read until we encounter an error or could not get the data immediately.
306 while (remaining_bytes_ > 0 && read_buf_->BytesRemaining() > 0) {
307 Status read_status = ReadItem();
308 if (read_status == Status::DONE) {
309 continue;
310 }
311 return read_status;
312 }
313
314 *bytes_read = BytesReadCompleted();
315 return Status::DONE;
316 }
317
318 BlobReader::Status BlobReader::ReadItem() {
319 // Are we done with reading all the blob data?
320 if (remaining_bytes_ == 0)
321 return Status::DONE;
322
323 const auto& items = blob_data_->items();
324 // If we get to the last item but still expect something to read, bail out
325 // since something is wrong.
326 if (current_item_index_ >= items.size()) {
327 return ReportError(net::ERR_FAILED);
328 }
329
330 // Compute the bytes to read for current item.
331 int bytes_to_read = ComputeBytesToRead();
332
333 // If nothing to read for current item, advance to next item.
334 if (bytes_to_read == 0) {
335 AdvanceItem();
336 return Status::DONE;
337 }
338
339 // Do the reading.
340 const BlobDataItem& item = *items.at(current_item_index_);
341 if (item.type() == DataElement::TYPE_BYTES) {
342 ReadBytesItem(item, bytes_to_read);
343 return Status::DONE;
344 }
345 if (item.type() == DataElement::TYPE_DISK_CACHE_ENTRY)
346 return ReadDiskCacheEntryItem(item, bytes_to_read);
347 if (!IsFileType(item.type())) {
348 NOTREACHED();
349 return ReportError(net::ERR_FAILED);
350 }
351 storage::FileStreamReader* const reader =
352 FileReaderAtIndex(current_item_index_);
353 if (!reader) {
354 return ReportError(net::ERR_FAILED);
355 }
356
357 return ReadFileItem(reader, bytes_to_read);
358 }
359
360 void BlobReader::AdvanceItem() {
361 // Close the file if the current item is a file.
362 DeleteCurrentFileReader();
363
364 // Advance to the next item.
365 current_item_index_++;
366 current_item_offset_ = 0;
367 }
368
369 void BlobReader::AdvanceBytesRead(int result) {
370 DCHECK_GT(result, 0);
371
372 // Do we finish reading the current item?
373 current_item_offset_ += result;
374 if (current_item_offset_ == item_length_list_[current_item_index_])
375 AdvanceItem();
376
377 // Subtract the remaining bytes.
378 remaining_bytes_ -= result;
379 DCHECK_GE(remaining_bytes_, 0ul);
380
381 // Adjust the read buffer.
382 read_buf_->DidConsume(result);
383 DCHECK_GE(read_buf_->BytesRemaining(), 0);
384 }
385
386 void BlobReader::ReadBytesItem(const BlobDataItem& item, int bytes_to_read) {
387 TRACE_EVENT1("Blob", "BlobReader::ReadBytesItem", "uuid", blob_data_->uuid());
388 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read);
389
390 memcpy(read_buf_->data(), item.bytes() + item.offset() + current_item_offset_,
391 bytes_to_read);
392
393 AdvanceBytesRead(bytes_to_read);
394 }
395
396 BlobReader::Status BlobReader::ReadFileItem(FileStreamReader* reader,
397 int bytes_to_read) {
398 DCHECK(!io_pending_)
399 << "Can't begin IO while another IO operation is pending.";
400 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read);
401 DCHECK(reader);
402 TRACE_EVENT_ASYNC_BEGIN1("Blob", "BlobRequest::ReadFileItem", this, "uuid",
403 blob_data_->uuid());
404 const int result = reader->Read(
405 read_buf_.get(), bytes_to_read,
406 base::Bind(&BlobReader::DidReadFile, weak_factory_.GetWeakPtr()));
407 if (result >= 0) {
408 AdvanceBytesRead(result);
409 return Status::DONE;
410 }
411 if (result == net::ERR_IO_PENDING) {
412 io_pending_ = true;
413 return Status::IO_PENDING;
414 }
415 return ReportError(result);
416 }
417
418 void BlobReader::DidReadFile(int result) {
419 DCHECK(io_pending_) << "Asynchronous IO completed while IO wasn't pending?";
420 TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest::ReadFileItem", this, "uuid",
421 blob_data_->uuid());
422 io_pending_ = false;
423 if (result <= 0) {
424 InvalidateCallbacksAndDone(result, read_done_);
425 return;
426 }
427
428 AdvanceBytesRead(result);
429
430 // Otherwise, continue the reading.
431 ContinueAsyncReadLoop();
432 }
433
434 void BlobReader::ContinueAsyncReadLoop() {
435 int bytes_read = 0;
436 Status read_status = ReadLoop(&bytes_read);
437 switch (read_status) {
438 case Status::DONE: {
439 net::CompletionCallback done = read_done_;
440 read_done_.Reset();
441 done.Run(bytes_read);
442 return;
443 }
444 case Status::NET_ERROR:
445 InvalidateCallbacksAndDone(net_error_, read_done_);
446 return;
447 case Status::IO_PENDING:
448 return;
449 }
450 }
451
452 void BlobReader::DeleteCurrentFileReader() {
453 auto found = index_to_reader_.find(current_item_index_);
454 if (found != index_to_reader_.end() && found->second) {
455 delete found->second;
456 index_to_reader_.erase(found);
457 }
458 }
459
460 BlobReader::Status BlobReader::ReadDiskCacheEntryItem(const BlobDataItem& item,
461 int bytes_to_read) {
462 DCHECK(!io_pending_)
463 << "Can't begin IO while another IO operation is pending.";
464 TRACE_EVENT_ASYNC_BEGIN1("Blob", "BlobRequest::ReadDiskCacheItem", this,
465 "uuid", blob_data_->uuid());
466 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read);
467
468 const int result = item.disk_cache_entry()->ReadData(
469 item.disk_cache_stream_index(), current_item_offset_, read_buf_.get(),
470 bytes_to_read, base::Bind(&BlobReader::DidReadDiskCacheEntry,
471 weak_factory_.GetWeakPtr()));
472 if (result >= 0) {
473 AdvanceBytesRead(result);
474 return Status::DONE;
475 }
476 if (result == net::ERR_IO_PENDING) {
477 io_pending_ = true;
478 return Status::IO_PENDING;
479 }
480 return ReportError(result);
481 }
482
483 void BlobReader::DidReadDiskCacheEntry(int result) {
484 DCHECK(io_pending_) << "Asynchronous IO completed while IO wasn't pending?";
485 TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest::ReadDiskCacheItem", this, "uuid",
486 blob_data_->uuid());
487 io_pending_ = false;
488 if (result <= 0) {
489 InvalidateCallbacksAndDone(result, read_done_);
490 return;
491 }
492
493 AdvanceBytesRead(result);
494
495 ContinueAsyncReadLoop();
496 }
497
498 int BlobReader::BytesReadCompleted() {
499 int bytes_read = read_buf_->BytesConsumed();
500 read_buf_ = nullptr;
501 return bytes_read;
502 }
503
504 int BlobReader::ComputeBytesToRead() const {
505 uint64_t current_item_length = item_length_list_[current_item_index_];
506
507 uint64_t item_remaining = current_item_length - current_item_offset_;
508 uint64_t buf_remaining = read_buf_->BytesRemaining();
509 uint64_t max_remaining = std::numeric_limits<int>::max();
michaeln 2015/09/17 00:45:37 nit: maybe call this max_readable or better yet us
dmurph 2015/09/19 00:33:43 done.
510
511 uint64_t min = std::min(
512 std::min(std::min(item_remaining, buf_remaining), remaining_bytes_),
513 max_remaining);
514
515 return static_cast<int>(min);
516 }
517
518 FileStreamReader* BlobReader::FileReaderAtIndex(size_t index) {
519 const auto& items = blob_data_->items();
520 DCHECK_LT(index, items.size());
521 const BlobDataItem& item = *items.at(index);
522 if (!IsFileType(item.type()))
523 return nullptr;
524 FileStreamReader* reader;
525 auto it = index_to_reader_.find(index);
526 if (it != index_to_reader_.end()) {
527 DCHECK(it->second);
528 return it->second;
529 }
530 reader = CreateFileStreamReader(item, 0);
531 if (!reader)
532 return nullptr;
533 index_to_reader_[index] = reader;
534 return reader;
535 }
536
537 FileStreamReader* BlobReader::CreateFileStreamReader(
538 const BlobDataItem& item,
539 uint64_t additional_offset) {
540 DCHECK(IsFileType(item.type()));
541
542 FileStreamReader* reader = nullptr;
543 switch (item.type()) {
544 case DataElement::TYPE_FILE:
545 reader = file_stream_provider_->CreateForLocalFile(
546 file_task_runner_.get(), item.path(),
547 item.offset() + additional_offset, item.expected_modification_time());
548 DCHECK(reader);
549 return reader;
550 case DataElement::TYPE_FILE_FILESYSTEM:
551 reader = file_stream_provider_->CreateFileStreamReader(
552 item.filesystem_url(), item.offset() + additional_offset,
553 item.length() == std::numeric_limits<uint64_t>::max()
554 ? storage::kMaximumLength
555 : item.length() - additional_offset,
556 item.expected_modification_time());
557 return reader;
558 default:
559 break;
560 }
561
562 NOTREACHED();
563 return nullptr;
564 }
565
566 void BlobReader::SetFileReaderAtIndex(size_t index, FileStreamReader* reader) {
567 auto found = index_to_reader_.find(current_item_index_);
568 if (found != index_to_reader_.end()) {
569 if (found->second) {
570 delete found->second;
571 }
572 found->second = reader;
573 } else if (reader) {
michaeln 2015/09/17 00:45:37 looks possible to leave a nullptr in the map if we
dmurph 2015/09/19 00:33:43 It should be impossible. Edited so that it can nev
574 index_to_reader_[current_item_index_] = reader;
575 }
576 }
577
578 } // namespace storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698