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

Side by Side Diff: webkit/blob/blob_url_request_job.cc

Issue 10827414: Factor out common Element struct from BlobData and ResourceRequestBody (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: webkit/common -> webkit/base Created 8 years, 4 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/blob/blob_storage_controller.cc ('k') | webkit/blob/view_blob_internals_job.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/blob/blob_url_request_job.h" 5 #include "webkit/blob/blob_url_request_job.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.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"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 } 169 }
170 170
171 void BlobURLRequestJob::CountSize() { 171 void BlobURLRequestJob::CountSize() {
172 error_ = false; 172 error_ = false;
173 pending_get_file_info_count_ = 0; 173 pending_get_file_info_count_ = 0;
174 total_size_ = 0; 174 total_size_ = 0;
175 item_length_list_.resize(blob_data_->items().size()); 175 item_length_list_.resize(blob_data_->items().size());
176 176
177 for (size_t i = 0; i < blob_data_->items().size(); ++i) { 177 for (size_t i = 0; i < blob_data_->items().size(); ++i) {
178 const BlobData::Item& item = blob_data_->items().at(i); 178 const BlobData::Item& item = blob_data_->items().at(i);
179 if (item.type == BlobData::TYPE_FILE) { 179 if (item.type() == BlobData::Item::TYPE_FILE) {
180 ++pending_get_file_info_count_; 180 ++pending_get_file_info_count_;
181 GetFileStreamReader(i)->GetLength( 181 GetFileStreamReader(i)->GetLength(
182 base::Bind(&BlobURLRequestJob::DidGetFileItemLength, 182 base::Bind(&BlobURLRequestJob::DidGetFileItemLength,
183 weak_factory_.GetWeakPtr(), i)); 183 weak_factory_.GetWeakPtr(), i));
184 continue; 184 continue;
185 } 185 }
186 // Cache the size and add it to the total size. 186 // Cache the size and add it to the total size.
187 int64 item_length = static_cast<int64>(item.length); 187 int64 item_length = static_cast<int64>(item.length());
188 item_length_list_[i] = item_length; 188 item_length_list_[i] = item_length;
189 total_size_ += item_length; 189 total_size_ += item_length;
190 } 190 }
191 191
192 if (pending_get_file_info_count_ == 0) 192 if (pending_get_file_info_count_ == 0)
193 DidCountSize(net::OK); 193 DidCountSize(net::OK);
194 } 194 }
195 195
196 void BlobURLRequestJob::DidCountSize(int error) { 196 void BlobURLRequestJob::DidCountSize(int error) {
197 DCHECK(!error_); 197 DCHECK(!error_);
(...skipping 29 matching lines...) Expand all
227 if (result == net::ERR_UPLOAD_FILE_CHANGED) { 227 if (result == net::ERR_UPLOAD_FILE_CHANGED) {
228 NotifyFailure(net::ERR_FILE_NOT_FOUND); 228 NotifyFailure(net::ERR_FILE_NOT_FOUND);
229 return; 229 return;
230 } else if (result < 0) { 230 } else if (result < 0) {
231 NotifyFailure(result); 231 NotifyFailure(result);
232 return; 232 return;
233 } 233 }
234 234
235 DCHECK_LT(index, blob_data_->items().size()); 235 DCHECK_LT(index, blob_data_->items().size());
236 const BlobData::Item& item = blob_data_->items().at(index); 236 const BlobData::Item& item = blob_data_->items().at(index);
237 DCHECK(item.type == BlobData::TYPE_FILE); 237 DCHECK(item.type() == BlobData::Item::TYPE_FILE);
238 238
239 // If item length is -1, we need to use the file size being resolved 239 // If item length is -1, we need to use the file size being resolved
240 // in the real time. 240 // in the real time.
241 int64 item_length = static_cast<int64>(item.length); 241 int64 item_length = static_cast<int64>(item.length());
242 if (item_length == -1) 242 if (item_length == -1)
243 item_length = result - item.offset; 243 item_length = result - item.offset();
244 244
245 // Cache the size and add it to the total size. 245 // Cache the size and add it to the total size.
246 DCHECK_LT(index, item_length_list_.size()); 246 DCHECK_LT(index, item_length_list_.size());
247 item_length_list_[index] = item_length; 247 item_length_list_[index] = item_length;
248 total_size_ += item_length; 248 total_size_ += item_length;
249 249
250 if (--pending_get_file_info_count_ == 0) 250 if (--pending_get_file_info_count_ == 0)
251 DidCountSize(net::OK); 251 DidCountSize(net::OK);
252 } 252 }
253 253
254 void BlobURLRequestJob::Seek(int64 offset) { 254 void BlobURLRequestJob::Seek(int64 offset) {
255 // Skip the initial items that are not in the range. 255 // Skip the initial items that are not in the range.
256 for (current_item_index_ = 0; 256 for (current_item_index_ = 0;
257 current_item_index_ < blob_data_->items().size() && 257 current_item_index_ < blob_data_->items().size() &&
258 offset >= item_length_list_[current_item_index_]; 258 offset >= item_length_list_[current_item_index_];
259 ++current_item_index_) { 259 ++current_item_index_) {
260 offset -= item_length_list_[current_item_index_]; 260 offset -= item_length_list_[current_item_index_];
261 } 261 }
262 262
263 // Set the offset that need to jump to for the first item in the range. 263 // Set the offset that need to jump to for the first item in the range.
264 current_item_offset_ = offset; 264 current_item_offset_ = offset;
265 265
266 if (offset == 0) 266 if (offset == 0)
267 return; 267 return;
268 268
269 // Adjust the offset of the first stream if it is of file type. 269 // Adjust the offset of the first stream if it is of file type.
270 const BlobData::Item& item = blob_data_->items().at(current_item_index_); 270 const BlobData::Item& item = blob_data_->items().at(current_item_index_);
271 if (item.type == BlobData::TYPE_FILE) { 271 if (item.type() == BlobData::Item::TYPE_FILE) {
272 DeleteCurrentFileReader(); 272 DeleteCurrentFileReader();
273 index_to_reader_[current_item_index_] = new LocalFileStreamReader( 273 index_to_reader_[current_item_index_] = new LocalFileStreamReader(
274 file_thread_proxy_, 274 file_thread_proxy_,
275 item.file_path, 275 item.path(),
276 item.offset + offset, 276 item.offset() + offset,
277 item.expected_modification_time); 277 item.expected_modification_time());
278 } 278 }
279 } 279 }
280 280
281 bool BlobURLRequestJob::ReadItem() { 281 bool BlobURLRequestJob::ReadItem() {
282 // Are we done with reading all the blob data? 282 // Are we done with reading all the blob data?
283 if (remaining_bytes_ == 0) 283 if (remaining_bytes_ == 0)
284 return true; 284 return true;
285 285
286 // If we get to the last item but still expect something to read, bail out 286 // If we get to the last item but still expect something to read, bail out
287 // since something is wrong. 287 // since something is wrong.
288 if (current_item_index_ >= blob_data_->items().size()) { 288 if (current_item_index_ >= blob_data_->items().size()) {
289 NotifyFailure(net::ERR_FAILED); 289 NotifyFailure(net::ERR_FAILED);
290 return false; 290 return false;
291 } 291 }
292 292
293 // Compute the bytes to read for current item. 293 // Compute the bytes to read for current item.
294 int bytes_to_read = ComputeBytesToRead(); 294 int bytes_to_read = ComputeBytesToRead();
295 295
296 // If nothing to read for current item, advance to next item. 296 // If nothing to read for current item, advance to next item.
297 if (bytes_to_read == 0) { 297 if (bytes_to_read == 0) {
298 AdvanceItem(); 298 AdvanceItem();
299 return ReadItem(); 299 return ReadItem();
300 } 300 }
301 301
302 // Do the reading. 302 // Do the reading.
303 const BlobData::Item& item = blob_data_->items().at(current_item_index_); 303 const BlobData::Item& item = blob_data_->items().at(current_item_index_);
304 switch (item.type) { 304 switch (item.type()) {
305 case BlobData::TYPE_DATA: 305 case BlobData::Item::TYPE_BYTES:
306 return ReadBytesItem(item, bytes_to_read); 306 return ReadBytesItem(item, bytes_to_read);
307 case BlobData::TYPE_FILE: 307 case BlobData::Item::TYPE_FILE:
308 return ReadFileItem(GetFileStreamReader(current_item_index_), 308 return ReadFileItem(GetFileStreamReader(current_item_index_),
309 bytes_to_read); 309 bytes_to_read);
310 default: 310 default:
311 DCHECK(false); 311 DCHECK(false);
312 return false; 312 return false;
313 } 313 }
314 } 314 }
315 315
316 void BlobURLRequestJob::AdvanceItem() { 316 void BlobURLRequestJob::AdvanceItem() {
317 // Close the file if the current item is a file. 317 // Close the file if the current item is a file.
(...skipping 19 matching lines...) Expand all
337 // Adjust the read buffer. 337 // Adjust the read buffer.
338 read_buf_->DidConsume(result); 338 read_buf_->DidConsume(result);
339 DCHECK_GE(read_buf_->BytesRemaining(), 0); 339 DCHECK_GE(read_buf_->BytesRemaining(), 0);
340 } 340 }
341 341
342 bool BlobURLRequestJob::ReadBytesItem(const BlobData::Item& item, 342 bool BlobURLRequestJob::ReadBytesItem(const BlobData::Item& item,
343 int bytes_to_read) { 343 int bytes_to_read) {
344 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); 344 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read);
345 345
346 memcpy(read_buf_->data(), 346 memcpy(read_buf_->data(),
347 &item.data.at(0) + item.offset + current_item_offset_, 347 item.bytes() + item.offset() + current_item_offset_,
348 bytes_to_read); 348 bytes_to_read);
349 349
350 AdvanceBytesRead(bytes_to_read); 350 AdvanceBytesRead(bytes_to_read);
351 return true; 351 return true;
352 } 352 }
353 353
354 bool BlobURLRequestJob::ReadFileItem(LocalFileStreamReader* reader, 354 bool BlobURLRequestJob::ReadFileItem(LocalFileStreamReader* reader,
355 int bytes_to_read) { 355 int bytes_to_read) {
356 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); 356 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read);
357 DCHECK(reader); 357 DCHECK(reader);
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 520
521 set_expected_content_size(remaining_bytes_); 521 set_expected_content_size(remaining_bytes_);
522 headers_set_ = true; 522 headers_set_ = true;
523 523
524 NotifyHeadersComplete(); 524 NotifyHeadersComplete();
525 } 525 }
526 526
527 LocalFileStreamReader* BlobURLRequestJob::GetFileStreamReader(size_t index) { 527 LocalFileStreamReader* BlobURLRequestJob::GetFileStreamReader(size_t index) {
528 DCHECK_LT(index, blob_data_->items().size()); 528 DCHECK_LT(index, blob_data_->items().size());
529 const BlobData::Item& item = blob_data_->items().at(index); 529 const BlobData::Item& item = blob_data_->items().at(index);
530 if (item.type != BlobData::TYPE_FILE) 530 if (item.type() != BlobData::Item::TYPE_FILE)
531 return NULL; 531 return NULL;
532 if (index_to_reader_.find(index) == index_to_reader_.end()) { 532 if (index_to_reader_.find(index) == index_to_reader_.end()) {
533 index_to_reader_[index] = new LocalFileStreamReader( 533 index_to_reader_[index] = new LocalFileStreamReader(
534 file_thread_proxy_, 534 file_thread_proxy_,
535 item.file_path, 535 item.path(),
536 item.offset, 536 item.offset(),
537 item.expected_modification_time); 537 item.expected_modification_time());
538 } 538 }
539 DCHECK(index_to_reader_[index]); 539 DCHECK(index_to_reader_[index]);
540 return index_to_reader_[index]; 540 return index_to_reader_[index];
541 } 541 }
542 542
543 } // namespace webkit_blob 543 } // namespace webkit_blob
OLDNEW
« no previous file with comments | « webkit/blob/blob_storage_controller.cc ('k') | webkit/blob/view_blob_internals_job.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698