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

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

Issue 22314003: NavigationController prototype Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: NavController prototype - chrome side Created 7 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 | Annotate | Revision Log
« no previous file with comments | « webkit/browser/blob/blob_reader.h ('k') | webkit/common/appcache/appcache_interfaces.h » ('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/browser/blob/blob_reader.h"
6
7 #include "net/base/io_buffer.h"
8 #include "net/base/net_errors.h"
9 #include "webkit/browser/blob/local_file_stream_reader.h"
10 #include "webkit/browser/fileapi/file_system_context.h"
11
12 namespace webkit_blob {
13
14 namespace {
15 bool IsFileType(BlobData::Item::Type type) {
16 return type == BlobData::Item::TYPE_FILE ||
17 type == BlobData::Item::TYPE_FILE_FILESYSTEM;
18 }
19 } // namespace
20
21 BlobReader::BlobReader(
22 BlobData* blob_data,
23 fileapi::FileSystemContext* file_system_context)
24 : weak_factory_(this),
25 blob_data_(blob_data),
26 file_system_context_(file_system_context),
27 total_size_(0),
28 remaining_bytes_(0),
29 pending_get_file_info_count_(0),
30 current_item_index_(0),
31 current_item_offset_(0),
32 error_(false) {
33 is_counting_size_ = false;
34 has_started_reading_ = false;
35 has_total_size_ = false;
36 initial_offset_ = 0;
37 file_task_runner_ = file_system_context_->default_file_task_runner();
38 }
39
40 BlobReader::~BlobReader() {
41 STLDeleteValues(&index_to_reader_);
42 }
43
44 void BlobReader::SetInitialOffset(int64 offset) {
45 DCHECK(!has_started_reading_);
46 initial_offset_ = offset;
47 }
48
49 int BlobReader::Read(net::IOBuffer* buf, int buf_len,
50 const net::CompletionCallback& callback) {
51 if (callback.is_null() || !pending_read_callback_.is_null())
52 return net::ERR_UNEXPECTED;
53 if (error_)
54 return error_;
55
56 // Keep track of the buffer.
57 DCHECK(!pending_read_buf_.get());
58 pending_read_buf_ = buf;
59 pending_read_length_ = buf_len;
60
61 // Before reading, we need to compute the size of blob data elements.
62 if (!has_total_size_) {
63 CountSize();
64 if (!has_total_size_) {
65 pending_read_callback_ = callback;
66 return net::ERR_IO_PENDING;
67 }
68 }
69
70 int rv = DoRead();
71 if (rv == net::ERR_IO_PENDING)
72 pending_read_callback_ = callback;
73 return rv;
74 }
75
76 int64 BlobReader::GetLength(const net::Int64CompletionCallback& callback) {
77 if (callback.is_null() || pending_size_callback_.is_null())
78 return net::ERR_UNEXPECTED;
79 if (error_)
80 return error_;
81 if (has_total_size_)
82 return total_size_;
83
84 CountSize();
85 if (!has_total_size_) {
86 // We'll complete the callback when we know the total size.
87 pending_size_callback_ = callback;
88 return net::ERR_IO_PENDING;
89 }
90 return total_size_;
91 }
92
93 void BlobReader::CountSize() {
94 DCHECK(!has_total_size_);
95 if (is_counting_size_)
96 return;
97 pending_get_file_info_count_ = 0;
98 total_size_ = 0;
99 item_length_list_.resize(blob_data_->items().size());
100
101 for (size_t i = 0; i < blob_data_->items().size(); ++i) {
102 const BlobData::Item& item = blob_data_->items().at(i);
103 if (IsFileType(item.type())) {
104 ++pending_get_file_info_count_;
105 GetFileStreamReader(i)->GetLength(
106 base::Bind(&BlobReader::DidGetFileItemLength,
107 weak_factory_.GetWeakPtr(), i));
108 continue;
109 }
110
111 if (!AddItemLength(i, item.length()))
112 return;
113 }
114
115 if (pending_get_file_info_count_ == 0)
116 OnSizeCounted();
117 }
118
119 bool BlobReader::AddItemLength(size_t index, int64 item_length) {
120 if (item_length > kint64max - total_size_) {
121 OnError(net::ERR_FAILED);
122 return false;
123 }
124
125 // Cache the size and add it to the total size.
126 DCHECK_LT(index, item_length_list_.size());
127 item_length_list_[index] = item_length;
128 total_size_ += item_length;
129 return true;
130 }
131
132 void BlobReader::DidGetFileItemLength(size_t index, int64 result) {
133 // Do nothing if we have already encountered an error.
134 if (error_)
135 return;
136
137 if (result == net::ERR_UPLOAD_FILE_CHANGED) {
138 OnError(net::ERR_FILE_NOT_FOUND);
139 return;
140 }
141 if (result < 0) {
142 OnError(result);
143 return;
144 }
145
146 DCHECK_LT(index, blob_data_->items().size());
147 const BlobData::Item& item = blob_data_->items().at(index);
148 DCHECK(IsFileType(item.type()));
149
150 uint64 file_length = result;
151 uint64 item_offset = item.offset();
152 uint64 item_length = item.length();
153
154 if (item_offset > file_length) {
155 OnError(net::ERR_FILE_NOT_FOUND);
156 return;
157 }
158
159 uint64 max_length = file_length - item_offset;
160
161 // If item length is -1, we need to use the file size being resolved
162 // in the real time.
163 if (item_length == static_cast<uint64>(-1)) {
164 item_length = max_length;
165 } else if (item_length > max_length) {
166 OnError(net::ERR_FILE_NOT_FOUND);
167 return;
168 }
169
170 if (!AddItemLength(index, item_length))
171 return;
172
173 if (--pending_get_file_info_count_ == 0)
174 OnSizeCounted();
175 }
176
177 int BlobReader::DoRead() {
178 DCHECK(!error_);
179 DCHECK(pending_read_buf_.get());
180
181 if (!has_started_reading_) {
182 has_started_reading_ = true;
183 if (initial_offset_)
184 PerformInitialSeek();
185 }
186
187 int desired_read_size = pending_read_length_;
188
189 if (remaining_bytes_ < desired_read_size)
190 desired_read_size = static_cast<int>(remaining_bytes_);
191
192 // If we should copy zero bytes because |remaining_bytes_| is zero, short
193 // circuit here.
194 if (!desired_read_size) {
195 pending_read_buf_ = NULL;
196 return 0;
197 }
198
199 // Keep track of the buffer.
200 DCHECK(!read_buf_.get());
201 read_buf_ = new net::DrainableIOBuffer(pending_read_buf_, desired_read_size);
202 pending_read_buf_ = NULL;
203
204 return ReadLoop();
205 }
206
207 void BlobReader::PerformInitialSeek() {
208 remaining_bytes_ -= initial_offset_;
209 if (remaining_bytes_ < 0)
210 remaining_bytes_ = 0;
211
212 int64 offset = initial_offset_;
213
214 // Private method to skip the initial items when reading from
215 // an offset.
216 for (current_item_index_ = 0;
217 current_item_index_ < blob_data_->items().size() &&
218 offset >= item_length_list_[current_item_index_];
219 ++current_item_index_) {
220 offset -= item_length_list_[current_item_index_];
221 }
222
223 // Set the offset that need to jump to for the first item in the range.
224 current_item_offset_ = offset;
225
226 if (offset == 0)
227 return;
228
229 // Adjust the offset of the first stream if it is of file type.
230 const BlobData::Item& item = blob_data_->items().at(current_item_index_);
231 if (IsFileType(item.type())) {
232 DeleteCurrentFileReader();
233 CreateFileStreamReader(current_item_index_, offset);
234 }
235 }
236
237
238 int BlobReader::ReadLoop() {
239 // Read until we encounter an error or could not get the data immediately.
240 while (remaining_bytes_ > 0 && read_buf_->BytesRemaining() > 0) {
241 if (!ReadItem())
242 return error_ ? error_ : net::ERR_IO_PENDING;
243 }
244 return OnReadComplete();
245 }
246
247 bool BlobReader::ReadItem() {
248 // Are we done with reading all the blob data?
249 if (remaining_bytes_ == 0)
250 return true;
251
252 // If we get to the last item but still expect something to read, bail out
253 // since something is wrong.
254 if (current_item_index_ >= blob_data_->items().size()) {
255 OnError(net::ERR_FAILED);
256 return false;
257 }
258
259 // Compute the bytes to read for current item.
260 int bytes_to_read = ComputeBytesToRead();
261
262 // If nothing to read for current item, advance to next item.
263 if (bytes_to_read == 0) {
264 AdvanceItem();
265 return ReadItem();
266 }
267
268 // Do the reading.
269 const BlobData::Item& item = blob_data_->items().at(current_item_index_);
270 if (item.type() == BlobData::Item::TYPE_BYTES)
271 return ReadBytesItem(item, bytes_to_read);
272 if (IsFileType(item.type())) {
273 return ReadFileItem(GetFileStreamReader(current_item_index_),
274 bytes_to_read);
275 }
276 NOTREACHED();
277 return false;
278 }
279
280 void BlobReader::AdvanceItem() {
281 // Close the file if the current item is a file.
282 DeleteCurrentFileReader();
283
284 // Advance to the next item.
285 current_item_index_++;
286 current_item_offset_ = 0;
287 }
288
289 void BlobReader::AdvanceBytesRead(int result) {
290 DCHECK_GT(result, 0);
291
292 // Do we finish reading the current item?
293 current_item_offset_ += result;
294 if (current_item_offset_ == item_length_list_[current_item_index_])
295 AdvanceItem();
296
297 // Subtract the remaining bytes.
298 remaining_bytes_ -= result;
299 DCHECK_GE(remaining_bytes_, 0);
300
301 // Adjust the read buffer.
302 read_buf_->DidConsume(result);
303 DCHECK_GE(read_buf_->BytesRemaining(), 0);
304 }
305
306 bool BlobReader::ReadBytesItem(const BlobData::Item& item,
307 int bytes_to_read) {
308 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read);
309
310 memcpy(read_buf_->data(),
311 item.bytes() + item.offset() + current_item_offset_,
312 bytes_to_read);
313
314 AdvanceBytesRead(bytes_to_read);
315 return true;
316 }
317
318 bool BlobReader::ReadFileItem(FileStreamReader* reader, int bytes_to_read) {
319 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read);
320 DCHECK_GT(bytes_to_read, 0);
321 DCHECK(reader);
322 const int result = reader->Read(
323 read_buf_.get(),
324 bytes_to_read,
325 base::Bind(&BlobReader::DidReadFile, base::Unretained(this)));
326 if (result >= 0) {
327 DidReadFile(result);
328 return true;
329 }
330 if (result == net::ERR_IO_PENDING)
331 return false;
332 OnError(result);
333 return false;
334 }
335
336 void BlobReader::DidReadFile(int result) {
337 if (result <= 0) {
338 OnError(net::ERR_FAILED);
339 return;
340 }
341
342 AdvanceBytesRead(result);
343
344 // If the read buffer is completely filled, we're done.
345 if (!read_buf_->BytesRemaining()) {
346 OnReadComplete();
347 return;
348 }
349
350 // Otherwise, continue the reading.
351 ReadLoop();
352 }
353
354 int BlobReader::ComputeBytesToRead() const {
355 int64 current_item_length = item_length_list_[current_item_index_];
356 int64 item_remaining = current_item_length - current_item_offset_;
357 int64 buf_remaining = read_buf_->BytesRemaining();
358 int64 max_remaining = std::numeric_limits<int>::max();
359 int64 min = std::min(std::min(std::min(item_remaining,
360 buf_remaining),
361 remaining_bytes_),
362 max_remaining);
363 return static_cast<int>(min);
364 }
365
366 void BlobReader::OnSizeCounted() {
367 DCHECK(!error_);
368 has_total_size_ = true;
369 remaining_bytes_ = total_size_;
370 if (!pending_read_callback_.is_null()) {
371 DoRead();
372 }
373 if (!pending_size_callback_.is_null()) {
374 net::Int64CompletionCallback callback = pending_size_callback_;
375 pending_size_callback_.Reset();
376 callback.Run(total_size_);
377 }
378 }
379
380 int BlobReader::OnReadComplete() {
381 int bytes_read = read_buf_->BytesConsumed();
382 read_buf_ = NULL;
383 if (!pending_read_callback_.is_null()) {
384 net::CompletionCallback callback = pending_read_callback_;
385 pending_read_callback_.Reset();
386 callback.Run(bytes_read);
387 }
388 return bytes_read;
389 }
390
391 void BlobReader::OnError(int error_code) {
392 DCHECK(error_code);
393 if (error_)
394 return;
395 error_ = error_code;
396 if (!pending_read_callback_.is_null()) {
397 pending_read_callback_.Run(error_code);
398 pending_read_callback_.Reset();
399 } else if (!pending_size_callback_.is_null()) {
400 pending_size_callback_.Run(error_code);
401 pending_size_callback_.Reset();
402 }
403 }
404
405 FileStreamReader* BlobReader::GetFileStreamReader(size_t index) {
406 DCHECK_LT(index, blob_data_->items().size());
407 const BlobData::Item& item = blob_data_->items().at(index);
408 if (!IsFileType(item.type()))
409 return NULL;
410 if (index_to_reader_.find(index) == index_to_reader_.end())
411 CreateFileStreamReader(index, 0);
412 DCHECK(index_to_reader_[index]);
413 return index_to_reader_[index];
414 }
415
416 void BlobReader::CreateFileStreamReader(size_t index,
417 int64 additional_offset) {
418 DCHECK_LT(index, blob_data_->items().size());
419 const BlobData::Item& item = blob_data_->items().at(index);
420 DCHECK(IsFileType(item.type()));
421 DCHECK_EQ(0U, index_to_reader_.count(index));
422
423 FileStreamReader* reader = NULL;
424 switch (item.type()) {
425 case BlobData::Item::TYPE_FILE:
426 reader = new LocalFileStreamReader(file_task_runner_.get(),
427 item.path(),
428 item.offset() + additional_offset,
429 item.expected_modification_time());
430 break;
431 case BlobData::Item::TYPE_FILE_FILESYSTEM:
432 reader = file_system_context_->CreateFileStreamReader(
433 fileapi::FileSystemURL(file_system_context_->CrackURL(item.url())),
434 item.offset() + additional_offset,
435 item.expected_modification_time()).release();
436 break;
437 default:
438 NOTREACHED();
439 }
440 DCHECK(reader);
441 index_to_reader_[index] = reader;
442 }
443
444 void BlobReader::DeleteCurrentFileReader() {
445 IndexToReaderMap::iterator found = index_to_reader_.find(current_item_index_);
446 if (found != index_to_reader_.end() && found->second) {
447 delete found->second;
448 index_to_reader_.erase(found);
449 }
450 }
451
452 } // namespace webkit_blob
OLDNEW
« no previous file with comments | « webkit/browser/blob/blob_reader.h ('k') | webkit/common/appcache/appcache_interfaces.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698