Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/media/buffered_resource_loader.h" | 5 #include "webkit/media/buffered_resource_loader.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "media/base/media_log.h" | 10 #include "media/base/media_log.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 *out_backward_capacity = std::max( | 86 *out_backward_capacity = std::max( |
| 87 kTargetSecondsBufferedBehind * bytes_per_second, kMinBufferCapacity); | 87 kTargetSecondsBufferedBehind * bytes_per_second, kMinBufferCapacity); |
| 88 | 88 |
| 89 *out_forward_capacity = std::min(*out_forward_capacity, kMaxBufferCapacity); | 89 *out_forward_capacity = std::min(*out_forward_capacity, kMaxBufferCapacity); |
| 90 *out_backward_capacity = std::min(*out_backward_capacity, kMaxBufferCapacity); | 90 *out_backward_capacity = std::min(*out_backward_capacity, kMaxBufferCapacity); |
| 91 | 91 |
| 92 if (backward_playback) | 92 if (backward_playback) |
| 93 std::swap(*out_forward_capacity, *out_backward_capacity); | 93 std::swap(*out_forward_capacity, *out_backward_capacity); |
| 94 } | 94 } |
| 95 | 95 |
| 96 | |
| 97 BufferedResourceLoader::BufferedResourceLoader( | 96 BufferedResourceLoader::BufferedResourceLoader( |
| 98 const GURL& url, | 97 const GURL& url, |
| 99 int64 first_byte_position, | 98 int64 first_byte_position, |
| 100 int64 last_byte_position, | 99 int64 last_byte_position, |
| 101 DeferStrategy strategy, | 100 DeferStrategy strategy, |
| 102 int bitrate, | 101 int bitrate, |
| 103 float playback_rate, | 102 float playback_rate, |
| 104 media::MediaLog* media_log) | 103 media::MediaLog* media_log) |
| 105 : deferred_(false), | 104 : defer_strategy_(strategy), |
| 106 defer_strategy_(strategy), | |
| 107 completed_(false), | |
| 108 range_requested_(false), | 105 range_requested_(false), |
| 109 range_supported_(false), | 106 range_supported_(false), |
| 110 saved_forward_capacity_(0), | 107 saved_forward_capacity_(0), |
| 111 url_(url), | 108 url_(url), |
| 112 first_byte_position_(first_byte_position), | 109 first_byte_position_(first_byte_position), |
| 113 last_byte_position_(last_byte_position), | 110 last_byte_position_(last_byte_position), |
| 114 single_origin_(true), | 111 single_origin_(true), |
| 115 start_callback_(NULL), | 112 start_callback_(NULL), |
| 116 offset_(0), | 113 offset_(0), |
| 117 content_length_(kPositionNotSpecified), | 114 content_length_(kPositionNotSpecified), |
| 118 instance_size_(kPositionNotSpecified), | 115 instance_size_(kPositionNotSpecified), |
| 119 read_callback_(NULL), | 116 read_callback_(NULL), |
| 120 read_position_(0), | 117 read_position_(0), |
| 121 read_size_(0), | 118 read_size_(0), |
| 122 read_buffer_(NULL), | 119 read_buffer_(NULL), |
| 123 first_offset_(0), | 120 first_offset_(0), |
| 124 last_offset_(0), | 121 last_offset_(0), |
| 125 keep_test_loader_(false), | |
| 126 bitrate_(bitrate), | 122 bitrate_(bitrate), |
| 127 playback_rate_(playback_rate), | 123 playback_rate_(playback_rate), |
| 128 media_log_(media_log) { | 124 media_log_(media_log) { |
| 129 | 125 |
| 130 size_t backward_capacity; | 126 size_t backward_capacity; |
| 131 size_t forward_capacity; | 127 size_t forward_capacity; |
| 132 ComputeTargetBufferWindow( | 128 ComputeTargetBufferWindow( |
| 133 playback_rate_, bitrate_, &backward_capacity, &forward_capacity); | 129 playback_rate_, bitrate_, &backward_capacity, &forward_capacity); |
| 134 buffer_.reset(new media::SeekableBuffer(backward_capacity, forward_capacity)); | 130 buffer_.reset(new media::SeekableBuffer(backward_capacity, forward_capacity)); |
| 135 } | 131 } |
| 136 | 132 |
| 137 BufferedResourceLoader::~BufferedResourceLoader() { | 133 BufferedResourceLoader::~BufferedResourceLoader() {} |
| 138 if (!completed_ && url_loader_.get()) | |
| 139 url_loader_->cancel(); | |
| 140 } | |
| 141 | 134 |
| 142 void BufferedResourceLoader::Start(net::OldCompletionCallback* start_callback, | 135 void BufferedResourceLoader::Start(net::OldCompletionCallback* start_callback, |
| 143 const base::Closure& event_callback, | 136 const base::Closure& event_callback, |
| 144 WebFrame* frame) { | 137 WebFrame* frame) { |
| 145 // Make sure we have not started. | 138 // Make sure we have not started. |
| 146 DCHECK(!start_callback_.get()); | 139 DCHECK(!start_callback_.get()); |
| 147 DCHECK(event_callback_.is_null()); | 140 DCHECK(event_callback_.is_null()); |
| 148 DCHECK(start_callback); | 141 DCHECK(start_callback); |
| 149 DCHECK(!event_callback.is_null()); | 142 DCHECK(!event_callback.is_null()); |
| 150 CHECK(frame); | 143 CHECK(frame); |
| 151 | 144 |
| 152 start_callback_.reset(start_callback); | 145 start_callback_.reset(start_callback); |
| 153 event_callback_ = event_callback; | 146 event_callback_ = event_callback; |
| 154 | 147 |
| 155 if (first_byte_position_ != kPositionNotSpecified) { | 148 if (first_byte_position_ != kPositionNotSpecified) { |
| 156 // TODO(hclam): server may not support range request so |offset_| may not | 149 // TODO(hclam): server may not support range request so |offset_| may not |
| 157 // equal to |first_byte_position_|. | 150 // equal to |first_byte_position_|. |
| 158 offset_ = first_byte_position_; | 151 offset_ = first_byte_position_; |
| 159 } | 152 } |
| 160 | 153 |
| 161 // Increment the reference count right before we start the request. This | |
| 162 // reference will be release when this request has ended. | |
| 163 AddRef(); | |
| 164 | |
| 165 // Prepare the request. | 154 // Prepare the request. |
| 166 WebURLRequest request(url_); | 155 WebURLRequest request(url_); |
| 167 request.setTargetType(WebURLRequest::TargetIsMedia); | 156 request.setTargetType(WebURLRequest::TargetIsMedia); |
| 168 | 157 |
| 169 if (IsRangeRequest()) { | 158 if (IsRangeRequest()) { |
| 170 range_requested_ = true; | 159 range_requested_ = true; |
| 171 request.setHTTPHeaderField( | 160 request.setHTTPHeaderField( |
| 172 WebString::fromUTF8(net::HttpRequestHeaders::kRange), | 161 WebString::fromUTF8(net::HttpRequestHeaders::kRange), |
| 173 WebString::fromUTF8(GenerateHeaders(first_byte_position_, | 162 WebString::fromUTF8(GenerateHeaders(first_byte_position_, |
| 174 last_byte_position_))); | 163 last_byte_position_))); |
| 175 } | 164 } |
| 176 frame->setReferrerForRequest(request, WebKit::WebURL()); | 165 frame->setReferrerForRequest(request, WebKit::WebURL()); |
| 177 | 166 |
| 178 // Disable compression, compression for audio/video doesn't make sense... | 167 // Disable compression, compression for audio/video doesn't make sense... |
| 179 request.setHTTPHeaderField( | 168 request.setHTTPHeaderField( |
| 180 WebString::fromUTF8(net::HttpRequestHeaders::kAcceptEncoding), | 169 WebString::fromUTF8(net::HttpRequestHeaders::kAcceptEncoding), |
| 181 WebString::fromUTF8("identity;q=1, *;q=0")); | 170 WebString::fromUTF8("identity;q=1, *;q=0")); |
| 182 | 171 |
| 183 // This flag is for unittests as we don't want to reset |url_loader| | 172 // Check for our test WebURLLoader. |
| 184 if (!keep_test_loader_) { | 173 WebURLLoader* loader = NULL; |
| 174 if (test_loader_.get()) { | |
| 175 loader = test_loader_.release(); | |
| 176 } else { | |
| 185 WebURLLoaderOptions options; | 177 WebURLLoaderOptions options; |
| 186 options.allowCredentials = true; | 178 options.allowCredentials = true; |
| 187 options.crossOriginRequestPolicy = | 179 options.crossOriginRequestPolicy = |
| 188 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; | 180 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; |
| 189 url_loader_.reset(frame->createAssociatedURLLoader(options)); | 181 loader = frame->createAssociatedURLLoader(options); |
| 190 } | 182 } |
| 191 | 183 |
| 192 // Start the resource loading. | 184 // Start the resource loading. |
| 193 url_loader_->loadAsynchronously(request, this); | 185 loader->loadAsynchronously(request, this); |
| 186 active_loader_.reset(new ActiveLoader(this, loader)); | |
| 194 } | 187 } |
| 195 | 188 |
| 196 void BufferedResourceLoader::Stop() { | 189 void BufferedResourceLoader::Stop() { |
| 197 // Reset callbacks. | 190 // Reset callbacks. |
| 198 start_callback_.reset(); | 191 start_callback_.reset(); |
| 199 event_callback_.Reset(); | 192 event_callback_.Reset(); |
| 200 read_callback_.reset(); | 193 read_callback_.reset(); |
| 201 | 194 |
| 202 // Use the internal buffer to signal that we have been stopped. | 195 // Use the internal buffer to signal that we have been stopped. |
| 203 // TODO(hclam): Not so pretty to do this. | 196 // TODO(hclam): Not so pretty to do this. |
| 204 if (!buffer_.get()) | 197 if (!buffer_.get()) |
| 205 return; | 198 return; |
| 206 | 199 |
| 207 // Destroy internal buffer. | 200 // Destroy internal buffer. |
| 208 buffer_.reset(); | 201 buffer_.reset(); |
| 209 | 202 |
| 210 if (url_loader_.get()) { | 203 // Cancel and reset any active loaders. |
| 211 if (deferred_) | 204 active_loader_.reset(); |
| 212 url_loader_->setDefersLoading(false); | |
| 213 deferred_ = false; | |
| 214 | |
| 215 if (!completed_) { | |
| 216 url_loader_->cancel(); | |
| 217 completed_ = true; | |
| 218 } | |
| 219 } | |
| 220 } | 205 } |
| 221 | 206 |
| 222 void BufferedResourceLoader::Read(int64 position, | 207 void BufferedResourceLoader::Read(int64 position, |
| 223 int read_size, | 208 int read_size, |
| 224 uint8* buffer, | 209 uint8* buffer, |
| 225 net::OldCompletionCallback* read_callback) { | 210 net::OldCompletionCallback* read_callback) { |
| 226 DCHECK(!read_callback_.get()); | 211 DCHECK(!read_callback_.get()); |
| 227 DCHECK(buffer_.get()); | 212 DCHECK(buffer_.get()); |
| 228 DCHECK(read_callback); | 213 DCHECK(read_callback); |
| 229 DCHECK(buffer); | 214 DCHECK(buffer); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 285 // capacity. | 270 // capacity. |
| 286 // | 271 // |
| 287 // This can happen when reading in a large seek index or when the | 272 // This can happen when reading in a large seek index or when the |
| 288 // first byte of a read request falls within kForwardWaitThreshold. | 273 // first byte of a read request falls within kForwardWaitThreshold. |
| 289 if (last_offset_ > static_cast<int>(buffer_->forward_capacity())) { | 274 if (last_offset_ > static_cast<int>(buffer_->forward_capacity())) { |
| 290 saved_forward_capacity_ = buffer_->forward_capacity(); | 275 saved_forward_capacity_ = buffer_->forward_capacity(); |
| 291 buffer_->set_forward_capacity(last_offset_); | 276 buffer_->set_forward_capacity(last_offset_); |
| 292 } | 277 } |
| 293 | 278 |
| 294 // Make sure we stop deferring now that there's additional capacity. | 279 // Make sure we stop deferring now that there's additional capacity. |
| 295 // | 280 if (active_loader_->deferred()) |
| 296 // XXX: can we DCHECK(url_loader_.get()) at this point in time? | |
| 297 if (deferred_) | |
| 298 SetDeferred(false); | 281 SetDeferred(false); |
| 299 | 282 |
| 300 DCHECK(!ShouldEnableDefer()) | 283 DCHECK(!ShouldEnableDefer()) |
| 301 << "Capacity was not adjusted properly to prevent deferring."; | 284 << "Capacity was not adjusted properly to prevent deferring."; |
| 302 | 285 |
| 303 return; | 286 return; |
| 304 } | 287 } |
| 305 | 288 |
| 306 // Make a callback to report failure. | 289 // Make a callback to report failure. |
| 307 DoneRead(net::ERR_CACHE_MISS); | 290 DoneRead(net::ERR_CACHE_MISS); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 319 | 302 |
| 320 int64 BufferedResourceLoader::instance_size() { | 303 int64 BufferedResourceLoader::instance_size() { |
| 321 return instance_size_; | 304 return instance_size_; |
| 322 } | 305 } |
| 323 | 306 |
| 324 bool BufferedResourceLoader::range_supported() { | 307 bool BufferedResourceLoader::range_supported() { |
| 325 return range_supported_; | 308 return range_supported_; |
| 326 } | 309 } |
| 327 | 310 |
| 328 bool BufferedResourceLoader::is_downloading_data() { | 311 bool BufferedResourceLoader::is_downloading_data() { |
| 329 return !completed_ && !deferred_; | 312 return active_loader_.get() && !active_loader_->deferred(); |
| 330 } | 313 } |
| 331 | 314 |
| 332 const GURL& BufferedResourceLoader::url() { | 315 const GURL& BufferedResourceLoader::url() { |
| 333 return url_; | 316 return url_; |
| 334 } | 317 } |
| 335 | 318 |
| 336 void BufferedResourceLoader::SetURLLoaderForTest(WebURLLoader* mock_loader) { | 319 void BufferedResourceLoader::SetURLLoaderForTest(WebURLLoader* test_loader) { |
| 337 url_loader_.reset(mock_loader); | 320 test_loader_.reset(test_loader); |
| 338 keep_test_loader_ = true; | |
| 339 } | 321 } |
| 340 | 322 |
| 341 ///////////////////////////////////////////////////////////////////////////// | 323 ///////////////////////////////////////////////////////////////////////////// |
| 342 // WebKit::WebURLLoaderClient implementation. | 324 // WebKit::WebURLLoaderClient implementation. |
| 343 void BufferedResourceLoader::willSendRequest( | 325 void BufferedResourceLoader::willSendRequest( |
| 344 WebURLLoader* loader, | 326 WebURLLoader* loader, |
| 345 WebURLRequest& newRequest, | 327 WebURLRequest& newRequest, |
| 346 const WebURLResponse& redirectResponse) { | 328 const WebURLResponse& redirectResponse) { |
| 347 | 329 |
| 348 // The load may have been stopped and |start_callback| is destroyed. | 330 // The load may have been stopped and |start_callback| is destroyed. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 364 WebURLLoader* loader, | 346 WebURLLoader* loader, |
| 365 unsigned long long bytes_sent, | 347 unsigned long long bytes_sent, |
| 366 unsigned long long total_bytes_to_be_sent) { | 348 unsigned long long total_bytes_to_be_sent) { |
| 367 NOTIMPLEMENTED(); | 349 NOTIMPLEMENTED(); |
| 368 } | 350 } |
| 369 | 351 |
| 370 void BufferedResourceLoader::didReceiveResponse( | 352 void BufferedResourceLoader::didReceiveResponse( |
| 371 WebURLLoader* loader, | 353 WebURLLoader* loader, |
| 372 const WebURLResponse& response) { | 354 const WebURLResponse& response) { |
| 373 VLOG(1) << "didReceiveResponse: " << response.httpStatusCode(); | 355 VLOG(1) << "didReceiveResponse: " << response.httpStatusCode(); |
| 356 DCHECK(active_loader_.get()); | |
| 374 | 357 |
| 375 // The loader may have been stopped and |start_callback| is destroyed. | 358 // The loader may have been stopped and |start_callback| is destroyed. |
| 376 // In this case we shouldn't do anything. | 359 // In this case we shouldn't do anything. |
| 377 if (!start_callback_.get()) | 360 if (!start_callback_.get()) |
| 378 return; | 361 return; |
| 379 | 362 |
| 380 bool partial_response = false; | 363 bool partial_response = false; |
| 381 | 364 |
| 382 // We make a strong assumption that when we reach here we have either | 365 // We make a strong assumption that when we reach here we have either |
| 383 // received a response from HTTP/HTTPS protocol or the request was | 366 // received a response from HTTP/HTTPS protocol or the request was |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 401 range_supported_ = true; | 384 range_supported_ = true; |
| 402 else | 385 else |
| 403 error = net::ERR_INVALID_RESPONSE; | 386 error = net::ERR_INVALID_RESPONSE; |
| 404 } else if (response.httpStatusCode() != kHttpOK) { | 387 } else if (response.httpStatusCode() != kHttpOK) { |
| 405 // We didn't request a range but server didn't reply with "200 OK". | 388 // We didn't request a range but server didn't reply with "200 OK". |
| 406 error = net::ERR_FAILED; | 389 error = net::ERR_FAILED; |
| 407 } | 390 } |
| 408 | 391 |
| 409 if (error != net::OK) { | 392 if (error != net::OK) { |
| 410 DoneStart(error); | 393 DoneStart(error); |
| 411 Stop(); | |
| 412 return; | 394 return; |
| 413 } | 395 } |
| 414 } else { | 396 } else { |
| 415 // For any protocol other than HTTP and HTTPS, assume range request is | 397 // For any protocol other than HTTP and HTTPS, assume range request is |
| 416 // always fulfilled. | 398 // always fulfilled. |
| 417 partial_response = range_requested_; | 399 partial_response = range_requested_; |
| 418 } | 400 } |
| 419 | 401 |
| 420 // Expected content length can be |kPositionNotSpecified|, in that case | 402 // Expected content length can be |kPositionNotSpecified|, in that case |
| 421 // |content_length_| is not specified and this is a streaming response. | 403 // |content_length_| is not specified and this is a streaming response. |
| 422 content_length_ = response.expectedContentLength(); | 404 content_length_ = response.expectedContentLength(); |
| 423 | 405 |
| 424 // If we have not requested a range, then the size of the instance is equal | 406 // If we have not requested a range, then the size of the instance is equal |
| 425 // to the content length. | 407 // to the content length. |
| 426 if (!partial_response) | 408 if (!partial_response) |
| 427 instance_size_ = content_length_; | 409 instance_size_ = content_length_; |
| 428 | 410 |
| 429 // Calls with a successful response. | 411 // Calls with a successful response. |
| 430 DoneStart(net::OK); | 412 DoneStart(net::OK); |
| 431 } | 413 } |
| 432 | 414 |
| 433 void BufferedResourceLoader::didReceiveData( | 415 void BufferedResourceLoader::didReceiveData( |
| 434 WebURLLoader* loader, | 416 WebURLLoader* loader, |
| 435 const char* data, | 417 const char* data, |
| 436 int data_length, | 418 int data_length, |
| 437 int encoded_data_length) { | 419 int encoded_data_length) { |
| 438 VLOG(1) << "didReceiveData: " << data_length << " bytes"; | 420 VLOG(1) << "didReceiveData: " << data_length << " bytes"; |
| 439 | 421 DCHECK(active_loader_.get()); |
| 440 DCHECK(!completed_); | |
| 441 DCHECK_GT(data_length, 0); | 422 DCHECK_GT(data_length, 0); |
| 442 | 423 |
| 443 // If this loader has been stopped, |buffer_| would be destroyed. | 424 // If this loader has been stopped, |buffer_| would be destroyed. |
| 444 // In this case we shouldn't do anything. | 425 // In this case we shouldn't do anything. |
| 445 if (!buffer_.get()) | 426 if (!buffer_.get()) |
| 446 return; | 427 return; |
| 447 | 428 |
| 448 // Writes more data to |buffer_|. | 429 // Writes more data to |buffer_|. |
| 449 buffer_->Append(reinterpret_cast<const uint8*>(data), data_length); | 430 buffer_->Append(reinterpret_cast<const uint8*>(data), data_length); |
| 450 | 431 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 478 WebURLLoader* loader, | 459 WebURLLoader* loader, |
| 479 const char* data, | 460 const char* data, |
| 480 int data_length) { | 461 int data_length) { |
| 481 NOTIMPLEMENTED(); | 462 NOTIMPLEMENTED(); |
| 482 } | 463 } |
| 483 | 464 |
| 484 void BufferedResourceLoader::didFinishLoading( | 465 void BufferedResourceLoader::didFinishLoading( |
| 485 WebURLLoader* loader, | 466 WebURLLoader* loader, |
| 486 double finishTime) { | 467 double finishTime) { |
| 487 VLOG(1) << "didFinishLoading"; | 468 VLOG(1) << "didFinishLoading"; |
| 488 | 469 DCHECK(active_loader_.get()); |
| 489 DCHECK(!completed_); | 470 active_loader_.reset(); |
| 490 completed_ = true; | |
| 491 | 471 |
| 492 // If we didn't know the |instance_size_| we do now. | 472 // If we didn't know the |instance_size_| we do now. |
| 493 if (instance_size_ == kPositionNotSpecified) { | 473 if (instance_size_ == kPositionNotSpecified) { |
| 494 instance_size_ = offset_ + buffer_->forward_bytes(); | 474 instance_size_ = offset_ + buffer_->forward_bytes(); |
| 495 } | 475 } |
| 496 | 476 |
| 497 // If there is a start callback, calls it. | 477 // If there is a start callback, calls it. |
| 498 if (start_callback_.get()) { | 478 if (start_callback_.get()) { |
| 499 DoneStart(net::OK); | 479 DoneStart(net::OK); |
|
vrk (LEFT CHROMIUM)
2011/12/01 00:22:28
When would there be a start_callback_ at this poin
scherkus (not reviewing)
2011/12/02 18:24:24
I *think* so -- I'll add in a DCHECK for now
| |
| 500 } | 480 } |
| 501 | 481 |
| 502 // If there is a pending read but the request has ended, returns with what | 482 // If there is a pending read but the request has ended, returns with what |
| 503 // we have. | 483 // we have. |
| 504 if (HasPendingRead()) { | 484 if (HasPendingRead()) { |
| 505 // Make sure we have a valid buffer before we satisfy a read request. | 485 // Make sure we have a valid buffer before we satisfy a read request. |
| 506 DCHECK(buffer_.get()); | 486 DCHECK(buffer_.get()); |
| 507 | 487 |
| 508 // Try to fulfill with what is in the buffer. | 488 // Try to fulfill with what is in the buffer. |
| 509 if (CanFulfillRead()) | 489 if (CanFulfillRead()) |
| 510 ReadInternal(); | 490 ReadInternal(); |
| 511 else | 491 else |
| 512 DoneRead(net::ERR_CACHE_MISS); | 492 DoneRead(net::ERR_CACHE_MISS); |
| 513 } | 493 } |
| 514 | 494 |
| 515 // There must not be any outstanding read request. | 495 // There must not be any outstanding read request. |
| 516 DCHECK(!HasPendingRead()); | 496 DCHECK(!HasPendingRead()); |
| 517 | 497 |
| 518 // Notify that network response is completed. | 498 // Notify that network response is completed. |
| 519 NotifyNetworkEvent(); | 499 NotifyNetworkEvent(); |
| 520 | |
| 521 url_loader_.reset(); | |
| 522 Release(); | |
| 523 } | 500 } |
| 524 | 501 |
| 525 void BufferedResourceLoader::didFail( | 502 void BufferedResourceLoader::didFail( |
| 526 WebURLLoader* loader, | 503 WebURLLoader* loader, |
| 527 const WebURLError& error) { | 504 const WebURLError& error) { |
| 528 VLOG(1) << "didFail: " << error.reason; | 505 VLOG(1) << "didFail: " << error.reason; |
| 506 DCHECK(active_loader_.get()); | |
| 507 active_loader_.reset(); | |
| 529 | 508 |
| 530 DCHECK(!completed_); | 509 NotifyNetworkEvent(); |
| 531 completed_ = true; | |
| 532 | 510 |
| 533 // If there is a start callback, calls it. | 511 // Don't leave start callbacks hanging around. |
| 534 if (start_callback_.get()) { | 512 if (start_callback_.get()) { |
| 535 DoneStart(error.reason); | 513 DoneStart(error.reason); |
| 514 return; | |
|
vrk (LEFT CHROMIUM)
2011/12/01 00:22:28
As mentioned offline: DCHECK that you don't have b
scherkus (not reviewing)
2011/12/02 18:24:24
Done.
| |
| 536 } | 515 } |
| 537 | 516 |
| 538 // If there is a pending read but the request failed, return with the | 517 // Don't leave read callbacks hanging around. |
| 539 // reason for the error. | |
| 540 if (HasPendingRead()) { | 518 if (HasPendingRead()) { |
| 541 DoneRead(error.reason); | 519 DoneRead(error.reason); |
| 542 } | 520 } |
| 543 | |
| 544 // Notify that network response is completed. | |
| 545 NotifyNetworkEvent(); | |
| 546 | |
| 547 url_loader_.reset(); | |
| 548 Release(); | |
| 549 } | 521 } |
| 550 | 522 |
| 551 bool BufferedResourceLoader::HasSingleOrigin() const { | 523 bool BufferedResourceLoader::HasSingleOrigin() const { |
| 552 return single_origin_; | 524 return single_origin_; |
| 553 } | 525 } |
| 554 | 526 |
| 555 void BufferedResourceLoader::UpdateDeferStrategy(DeferStrategy strategy) { | 527 void BufferedResourceLoader::UpdateDeferStrategy(DeferStrategy strategy) { |
| 556 defer_strategy_ = strategy; | 528 defer_strategy_ = strategy; |
| 557 UpdateDeferBehavior(); | 529 UpdateDeferBehavior(); |
| 558 } | 530 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 587 playback_rate_, bitrate_, &backward_capacity, &forward_capacity); | 559 playback_rate_, bitrate_, &backward_capacity, &forward_capacity); |
| 588 | 560 |
| 589 // This does not evict data from the buffer if the new capacities are less | 561 // This does not evict data from the buffer if the new capacities are less |
| 590 // than the current capacities; the new limits will be enforced after the | 562 // than the current capacities; the new limits will be enforced after the |
| 591 // existing excess buffered data is consumed. | 563 // existing excess buffered data is consumed. |
| 592 buffer_->set_backward_capacity(backward_capacity); | 564 buffer_->set_backward_capacity(backward_capacity); |
| 593 buffer_->set_forward_capacity(forward_capacity); | 565 buffer_->set_forward_capacity(forward_capacity); |
| 594 } | 566 } |
| 595 | 567 |
| 596 void BufferedResourceLoader::UpdateDeferBehavior() { | 568 void BufferedResourceLoader::UpdateDeferBehavior() { |
| 597 if (!url_loader_.get() || !buffer_.get()) | 569 if (!active_loader_.get() || !buffer_.get()) |
| 598 return; | 570 return; |
| 599 | 571 |
| 600 // If necessary, toggle defer state and continue/pause downloading data | 572 // If necessary, toggle defer state and continue/pause downloading data |
| 601 // accordingly. | 573 // accordingly. |
| 602 if (ShouldEnableDefer() || ShouldDisableDefer()) | 574 if (ShouldEnableDefer() || ShouldDisableDefer()) |
| 603 SetDeferred(!deferred_); | 575 SetDeferred(!active_loader_->deferred()); |
| 604 } | 576 } |
| 605 | 577 |
| 606 void BufferedResourceLoader::SetDeferred(bool deferred) { | 578 void BufferedResourceLoader::SetDeferred(bool deferred) { |
| 607 deferred_ = deferred; | 579 active_loader_->SetDeferred(deferred); |
| 608 if (url_loader_.get()) { | 580 NotifyNetworkEvent(); |
| 609 url_loader_->setDefersLoading(deferred); | |
| 610 NotifyNetworkEvent(); | |
| 611 } | |
| 612 } | 581 } |
| 613 | 582 |
| 614 bool BufferedResourceLoader::ShouldEnableDefer() { | 583 bool BufferedResourceLoader::ShouldEnableDefer() const { |
| 615 // If we're already deferring, then enabling makes no sense. | 584 // If we're already deferring, then enabling makes no sense. |
| 616 if (deferred_) | 585 if (active_loader_->deferred()) |
| 617 return false; | 586 return false; |
| 618 | 587 |
| 619 switch(defer_strategy_) { | 588 switch(defer_strategy_) { |
| 620 // Never defer at all, so never enable defer. | 589 // Never defer at all, so never enable defer. |
| 621 case kNeverDefer: | 590 case kNeverDefer: |
| 622 return false; | 591 return false; |
| 623 | 592 |
| 624 // Defer if nothing is being requested. | 593 // Defer if nothing is being requested. |
| 625 case kReadThenDefer: | 594 case kReadThenDefer: |
| 626 return !read_callback_.get(); | 595 return !read_callback_.get(); |
| 627 | 596 |
| 628 // Defer if we've reached the max capacity of the threshold. | 597 // Defer if we've reached the max capacity of the threshold. |
| 629 case kThresholdDefer: | 598 case kThresholdDefer: |
| 630 return buffer_->forward_bytes() >= buffer_->forward_capacity(); | 599 return buffer_->forward_bytes() >= buffer_->forward_capacity(); |
| 631 } | 600 } |
| 632 // Otherwise don't enable defer. | 601 // Otherwise don't enable defer. |
| 633 return false; | 602 return false; |
| 634 } | 603 } |
| 635 | 604 |
| 636 bool BufferedResourceLoader::ShouldDisableDefer() { | 605 bool BufferedResourceLoader::ShouldDisableDefer() const { |
| 637 // If we're not deferring, then disabling makes no sense. | 606 // If we're not deferring, then disabling makes no sense. |
| 638 if (!deferred_) | 607 if (!active_loader_->deferred()) |
| 639 return false; | 608 return false; |
| 640 | 609 |
| 641 switch(defer_strategy_) { | 610 switch(defer_strategy_) { |
| 642 // Always disable deferring. | 611 // Always disable deferring. |
| 643 case kNeverDefer: | 612 case kNeverDefer: |
| 644 return true; | 613 return true; |
| 645 | 614 |
| 646 // We have an outstanding read request, and we have not buffered enough | 615 // We have an outstanding read request, and we have not buffered enough |
| 647 // yet to fulfill the request; disable defer to get more data. | 616 // yet to fulfill the request; disable defer to get more data. |
| 648 case kReadThenDefer: | 617 case kReadThenDefer: |
| 649 return read_callback_.get() && | 618 return read_callback_.get() && |
| 650 last_offset_ > static_cast<int>(buffer_->forward_bytes()); | 619 last_offset_ > static_cast<int>(buffer_->forward_bytes()); |
| 651 | 620 |
| 652 // We have less than half the capacity of our threshold, so | 621 // We have less than half the capacity of our threshold, so |
| 653 // disable defer to get more data. | 622 // disable defer to get more data. |
| 654 case kThresholdDefer: { | 623 case kThresholdDefer: { |
| 655 size_t amount_buffered = buffer_->forward_bytes(); | 624 size_t amount_buffered = buffer_->forward_bytes(); |
| 656 size_t half_capacity = buffer_->forward_capacity() / 2; | 625 size_t half_capacity = buffer_->forward_capacity() / 2; |
| 657 return amount_buffered < half_capacity; | 626 return amount_buffered < half_capacity; |
| 658 } | 627 } |
| 659 } | 628 } |
| 660 | 629 |
| 661 // Otherwise keep deferring. | 630 // Otherwise keep deferring. |
| 662 return false; | 631 return false; |
| 663 } | 632 } |
| 664 | 633 |
| 665 bool BufferedResourceLoader::CanFulfillRead() { | 634 bool BufferedResourceLoader::CanFulfillRead() const { |
| 666 // If we are reading too far in the backward direction. | 635 // If we are reading too far in the backward direction. |
| 667 if (first_offset_ < 0 && | 636 if (first_offset_ < 0 && |
| 668 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0) | 637 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0) |
| 669 return false; | 638 return false; |
| 670 | 639 |
| 671 // If the start offset is too far ahead. | 640 // If the start offset is too far ahead. |
| 672 if (first_offset_ >= static_cast<int>(buffer_->forward_bytes())) | 641 if (first_offset_ >= static_cast<int>(buffer_->forward_bytes())) |
| 673 return false; | 642 return false; |
| 674 | 643 |
| 675 // At the point, we verified that first byte requested is within the buffer. | 644 // At the point, we verified that first byte requested is within the buffer. |
| 676 // If the request has completed, then just returns with what we have now. | 645 // If the request has completed, then just returns with what we have now. |
| 677 if (completed_) | 646 if (!active_loader_.get()) |
| 678 return true; | 647 return true; |
| 679 | 648 |
| 680 // If the resource request is still active, make sure the whole requested | 649 // If the resource request is still active, make sure the whole requested |
| 681 // range is covered. | 650 // range is covered. |
| 682 if (last_offset_ > static_cast<int>(buffer_->forward_bytes())) | 651 if (last_offset_ > static_cast<int>(buffer_->forward_bytes())) |
| 683 return false; | 652 return false; |
| 684 | 653 |
| 685 return true; | 654 return true; |
| 686 } | 655 } |
| 687 | 656 |
| 688 bool BufferedResourceLoader::WillFulfillRead() { | 657 bool BufferedResourceLoader::WillFulfillRead() const { |
| 689 // Trying to read too far behind. | 658 // Trying to read too far behind. |
| 690 if (first_offset_ < 0 && | 659 if (first_offset_ < 0 && |
| 691 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0) | 660 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0) |
| 692 return false; | 661 return false; |
| 693 | 662 |
| 694 // Trying to read too far ahead. | 663 // Trying to read too far ahead. |
| 695 if (first_offset_ - static_cast<int>(buffer_->forward_bytes()) >= | 664 if (first_offset_ - static_cast<int>(buffer_->forward_bytes()) >= |
| 696 kForwardWaitThreshold) | 665 kForwardWaitThreshold) |
| 697 return false; | 666 return false; |
| 698 | 667 |
| 699 // The resource request has completed, there's no way we can fulfill the | 668 // The resource request has completed, there's no way we can fulfill the |
| 700 // read request. | 669 // read request. |
| 701 if (completed_) | 670 if (!active_loader_.get()) |
| 702 return false; | 671 return false; |
| 703 | 672 |
| 704 return true; | 673 return true; |
| 705 } | 674 } |
| 706 | 675 |
| 707 void BufferedResourceLoader::ReadInternal() { | 676 void BufferedResourceLoader::ReadInternal() { |
| 708 // Seek to the first byte requested. | 677 // Seek to the first byte requested. |
| 709 bool ret = buffer_->Seek(first_offset_); | 678 bool ret = buffer_->Seek(first_offset_); |
| 710 DCHECK(ret); | 679 DCHECK(ret); |
| 711 | 680 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 757 } else if (first_byte_position > kPositionNotSpecified) { | 726 } else if (first_byte_position > kPositionNotSpecified) { |
| 758 header = base::StringPrintf("bytes=%" PRId64 "-", | 727 header = base::StringPrintf("bytes=%" PRId64 "-", |
| 759 first_byte_position); | 728 first_byte_position); |
| 760 } else if (last_byte_position > kPositionNotSpecified) { | 729 } else if (last_byte_position > kPositionNotSpecified) { |
| 761 NOTIMPLEMENTED() << "Suffix range not implemented"; | 730 NOTIMPLEMENTED() << "Suffix range not implemented"; |
| 762 } | 731 } |
| 763 return header; | 732 return header; |
| 764 } | 733 } |
| 765 | 734 |
| 766 void BufferedResourceLoader::DoneRead(int error) { | 735 void BufferedResourceLoader::DoneRead(int error) { |
| 767 read_callback_->RunWithParams(Tuple1<int>(error)); | |
| 768 read_callback_.reset(); | |
| 769 if (buffer_.get() && saved_forward_capacity_) { | 736 if (buffer_.get() && saved_forward_capacity_) { |
| 770 buffer_->set_forward_capacity(saved_forward_capacity_); | 737 buffer_->set_forward_capacity(saved_forward_capacity_); |
| 771 saved_forward_capacity_ = 0; | 738 saved_forward_capacity_ = 0; |
| 772 } | 739 } |
| 773 read_position_ = 0; | 740 read_position_ = 0; |
| 774 read_size_ = 0; | 741 read_size_ = 0; |
| 775 read_buffer_ = NULL; | 742 read_buffer_ = NULL; |
| 776 first_offset_ = 0; | 743 first_offset_ = 0; |
| 777 last_offset_ = 0; | 744 last_offset_ = 0; |
| 778 Log(); | 745 Log(); |
| 746 | |
| 747 scoped_ptr<net::OldCompletionCallback> read_callback; | |
| 748 read_callback.swap(read_callback_); | |
| 749 read_callback->RunWithParams(Tuple1<int>(error)); | |
| 779 } | 750 } |
| 780 | 751 |
| 781 void BufferedResourceLoader::DoneStart(int error) { | 752 void BufferedResourceLoader::DoneStart(int error) { |
| 782 start_callback_->RunWithParams(Tuple1<int>(error)); | 753 scoped_ptr<net::OldCompletionCallback> start_callback; |
| 783 start_callback_.reset(); | 754 start_callback.swap(start_callback_); |
| 755 start_callback->RunWithParams(Tuple1<int>(error)); | |
| 784 } | 756 } |
| 785 | 757 |
| 786 void BufferedResourceLoader::NotifyNetworkEvent() { | 758 void BufferedResourceLoader::NotifyNetworkEvent() { |
| 787 if (!event_callback_.is_null()) | 759 if (!event_callback_.is_null()) |
| 788 event_callback_.Run(); | 760 event_callback_.Run(); |
| 789 } | 761 } |
| 790 | 762 |
| 791 bool BufferedResourceLoader::IsRangeRequest() const { | 763 bool BufferedResourceLoader::IsRangeRequest() const { |
| 792 return first_byte_position_ != kPositionNotSpecified; | 764 return first_byte_position_ != kPositionNotSpecified; |
| 793 } | 765 } |
| 794 | 766 |
| 795 void BufferedResourceLoader::Log() { | 767 void BufferedResourceLoader::Log() { |
| 796 if (buffer_.get()) { | 768 if (buffer_.get()) { |
| 797 media_log_->AddEvent( | 769 media_log_->AddEvent( |
| 798 media_log_->CreateBufferedExtentsChangedEvent( | 770 media_log_->CreateBufferedExtentsChangedEvent( |
| 799 offset_ - buffer_->backward_bytes(), | 771 offset_ - buffer_->backward_bytes(), |
| 800 offset_, | 772 offset_, |
| 801 offset_ + buffer_->forward_bytes())); | 773 offset_ + buffer_->forward_bytes())); |
| 802 } | 774 } |
| 803 } | 775 } |
| 804 | 776 |
| 805 } // namespace webkit_media | 777 } // namespace webkit_media |
| OLD | NEW |