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