| OLD | NEW |
| 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/media/buffered_resource_loader.h" | 5 #include "webkit/media/buffered_resource_loader.h" |
| 6 | 6 |
| 7 #include "base/bits.h" | 7 #include "base/bits.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 137 |
| 138 // Set the initial capacity of |buffer_| based on |bitrate_| and | 138 // Set the initial capacity of |buffer_| based on |bitrate_| and |
| 139 // |playback_rate_|. | 139 // |playback_rate_|. |
| 140 UpdateBufferWindow(); | 140 UpdateBufferWindow(); |
| 141 } | 141 } |
| 142 | 142 |
| 143 BufferedResourceLoader::~BufferedResourceLoader() {} | 143 BufferedResourceLoader::~BufferedResourceLoader() {} |
| 144 | 144 |
| 145 void BufferedResourceLoader::Start( | 145 void BufferedResourceLoader::Start( |
| 146 const StartCB& start_cb, | 146 const StartCB& start_cb, |
| 147 const base::Closure& event_cb, | 147 const LoadingStateChangedCB& loading_cb, |
| 148 const ProgressCB& progress_cb, |
| 148 WebFrame* frame) { | 149 WebFrame* frame) { |
| 149 // Make sure we have not started. | 150 // Make sure we have not started. |
| 150 DCHECK(start_cb_.is_null()); | 151 DCHECK(start_cb_.is_null()); |
| 151 DCHECK(event_cb_.is_null()); | 152 DCHECK(loading_cb_.is_null()); |
| 153 DCHECK(progress_cb_.is_null()); |
| 152 DCHECK(!start_cb.is_null()); | 154 DCHECK(!start_cb.is_null()); |
| 153 DCHECK(!event_cb.is_null()); | 155 DCHECK(!loading_cb.is_null()); |
| 156 DCHECK(!progress_cb.is_null()); |
| 154 CHECK(frame); | 157 CHECK(frame); |
| 155 | 158 |
| 156 start_cb_ = start_cb; | 159 start_cb_ = start_cb; |
| 157 event_cb_ = event_cb; | 160 loading_cb_ = loading_cb; |
| 161 progress_cb_ = progress_cb; |
| 158 | 162 |
| 159 if (first_byte_position_ != kPositionNotSpecified) { | 163 if (first_byte_position_ != kPositionNotSpecified) { |
| 160 // TODO(hclam): server may not support range request so |offset_| may not | 164 // TODO(hclam): server may not support range request so |offset_| may not |
| 161 // equal to |first_byte_position_|. | 165 // equal to |first_byte_position_|. |
| 162 offset_ = first_byte_position_; | 166 offset_ = first_byte_position_; |
| 163 } | 167 } |
| 164 | 168 |
| 165 // Prepare the request. | 169 // Prepare the request. |
| 166 WebURLRequest request(url_); | 170 WebURLRequest request(url_); |
| 167 request.setTargetType(WebURLRequest::TargetIsMedia); | 171 request.setTargetType(WebURLRequest::TargetIsMedia); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 196 WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; | 200 WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; |
| 197 if (cors_mode_ == kUseCredentials) | 201 if (cors_mode_ == kUseCredentials) |
| 198 options.allowCredentials = true; | 202 options.allowCredentials = true; |
| 199 } | 203 } |
| 200 loader.reset(frame->createAssociatedURLLoader(options)); | 204 loader.reset(frame->createAssociatedURLLoader(options)); |
| 201 } | 205 } |
| 202 | 206 |
| 203 // Start the resource loading. | 207 // Start the resource loading. |
| 204 loader->loadAsynchronously(request, this); | 208 loader->loadAsynchronously(request, this); |
| 205 active_loader_.reset(new ActiveLoader(loader.Pass())); | 209 active_loader_.reset(new ActiveLoader(loader.Pass())); |
| 210 loading_cb_.Run(kLoading); |
| 206 } | 211 } |
| 207 | 212 |
| 208 void BufferedResourceLoader::Stop() { | 213 void BufferedResourceLoader::Stop() { |
| 209 // Reset callbacks. | 214 // Reset callbacks. |
| 210 start_cb_.Reset(); | 215 start_cb_.Reset(); |
| 211 event_cb_.Reset(); | 216 loading_cb_.Reset(); |
| 217 progress_cb_.Reset(); |
| 212 read_cb_.Reset(); | 218 read_cb_.Reset(); |
| 213 | 219 |
| 214 // Cancel and reset any active loaders. | 220 // Cancel and reset any active loaders. |
| 215 active_loader_.reset(); | 221 active_loader_.reset(); |
| 216 } | 222 } |
| 217 | 223 |
| 218 void BufferedResourceLoader::Read( | 224 void BufferedResourceLoader::Read( |
| 219 int64 position, | 225 int64 position, |
| 220 int read_size, | 226 int read_size, |
| 221 uint8* buffer, | 227 uint8* buffer, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 DCHECK(!ShouldEnableDefer()) | 312 DCHECK(!ShouldEnableDefer()) |
| 307 << "Capacity was not adjusted properly to prevent deferring."; | 313 << "Capacity was not adjusted properly to prevent deferring."; |
| 308 | 314 |
| 309 return; | 315 return; |
| 310 } | 316 } |
| 311 | 317 |
| 312 // Make a callback to report failure. | 318 // Make a callback to report failure. |
| 313 DoneRead(kCacheMiss, 0); | 319 DoneRead(kCacheMiss, 0); |
| 314 } | 320 } |
| 315 | 321 |
| 316 int64 BufferedResourceLoader::GetBufferedPosition() { | |
| 317 return offset_ + buffer_.forward_bytes() - 1; | |
| 318 } | |
| 319 | |
| 320 int64 BufferedResourceLoader::content_length() { | 322 int64 BufferedResourceLoader::content_length() { |
| 321 return content_length_; | 323 return content_length_; |
| 322 } | 324 } |
| 323 | 325 |
| 324 int64 BufferedResourceLoader::instance_size() { | 326 int64 BufferedResourceLoader::instance_size() { |
| 325 return instance_size_; | 327 return instance_size_; |
| 326 } | 328 } |
| 327 | 329 |
| 328 bool BufferedResourceLoader::range_supported() { | 330 bool BufferedResourceLoader::range_supported() { |
| 329 return range_supported_; | 331 return range_supported_; |
| 330 } | 332 } |
| 331 | 333 |
| 332 bool BufferedResourceLoader::is_downloading_data() { | |
| 333 return active_loader_.get() && !active_loader_->deferred(); | |
| 334 } | |
| 335 | |
| 336 const GURL& BufferedResourceLoader::url() { | 334 const GURL& BufferedResourceLoader::url() { |
| 337 return url_; | 335 return url_; |
| 338 } | 336 } |
| 339 | 337 |
| 340 ///////////////////////////////////////////////////////////////////////////// | 338 ///////////////////////////////////////////////////////////////////////////// |
| 341 // WebKit::WebURLLoaderClient implementation. | 339 // WebKit::WebURLLoaderClient implementation. |
| 342 void BufferedResourceLoader::willSendRequest( | 340 void BufferedResourceLoader::willSendRequest( |
| 343 WebURLLoader* loader, | 341 WebURLLoader* loader, |
| 344 WebURLRequest& newRequest, | 342 WebURLRequest& newRequest, |
| 345 const WebURLResponse& redirectResponse) { | 343 const WebURLResponse& redirectResponse) { |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 UpdateDeferBehavior(); | 468 UpdateDeferBehavior(); |
| 471 | 469 |
| 472 // Consume excess bytes from our in-memory buffer if necessary. | 470 // Consume excess bytes from our in-memory buffer if necessary. |
| 473 if (buffer_.forward_bytes() > buffer_.forward_capacity()) { | 471 if (buffer_.forward_bytes() > buffer_.forward_capacity()) { |
| 474 int excess = buffer_.forward_bytes() - buffer_.forward_capacity(); | 472 int excess = buffer_.forward_bytes() - buffer_.forward_capacity(); |
| 475 bool success = buffer_.Seek(excess); | 473 bool success = buffer_.Seek(excess); |
| 476 DCHECK(success); | 474 DCHECK(success); |
| 477 offset_ += first_offset_ + excess; | 475 offset_ += first_offset_ + excess; |
| 478 } | 476 } |
| 479 | 477 |
| 480 // Notify that we have received some data. | 478 // Notify latest progress and buffered offset. |
| 481 NotifyNetworkEvent(); | 479 progress_cb_.Run(offset_ + buffer_.forward_bytes() - 1); |
| 482 Log(); | 480 Log(); |
| 483 } | 481 } |
| 484 | 482 |
| 485 void BufferedResourceLoader::didDownloadData( | 483 void BufferedResourceLoader::didDownloadData( |
| 486 WebKit::WebURLLoader* loader, | 484 WebKit::WebURLLoader* loader, |
| 487 int dataLength) { | 485 int dataLength) { |
| 488 NOTIMPLEMENTED(); | 486 NOTIMPLEMENTED(); |
| 489 } | 487 } |
| 490 | 488 |
| 491 void BufferedResourceLoader::didReceiveCachedMetadata( | 489 void BufferedResourceLoader::didReceiveCachedMetadata( |
| 492 WebURLLoader* loader, | 490 WebURLLoader* loader, |
| 493 const char* data, | 491 const char* data, |
| 494 int data_length) { | 492 int data_length) { |
| 495 NOTIMPLEMENTED(); | 493 NOTIMPLEMENTED(); |
| 496 } | 494 } |
| 497 | 495 |
| 498 void BufferedResourceLoader::didFinishLoading( | 496 void BufferedResourceLoader::didFinishLoading( |
| 499 WebURLLoader* loader, | 497 WebURLLoader* loader, |
| 500 double finishTime) { | 498 double finishTime) { |
| 501 DVLOG(1) << "didFinishLoading"; | 499 DVLOG(1) << "didFinishLoading"; |
| 502 DCHECK(active_loader_.get()); | 500 DCHECK(active_loader_.get()); |
| 503 | 501 |
| 504 // We're done with the loader. | 502 // We're done with the loader. |
| 505 active_loader_.reset(); | 503 active_loader_.reset(); |
| 506 NotifyNetworkEvent(); | 504 loading_cb_.Run(kLoadingFinished); |
| 507 | 505 |
| 508 // If we didn't know the |instance_size_| we do now. | 506 // If we didn't know the |instance_size_| we do now. |
| 509 if (instance_size_ == kPositionNotSpecified) { | 507 if (instance_size_ == kPositionNotSpecified) { |
| 510 instance_size_ = offset_ + buffer_.forward_bytes(); | 508 instance_size_ = offset_ + buffer_.forward_bytes(); |
| 511 } | 509 } |
| 512 | 510 |
| 513 // If there is a start callback, run it. | 511 // If there is a start callback, run it. |
| 514 if (!start_cb_.is_null()) { | 512 if (!start_cb_.is_null()) { |
| 515 DCHECK(read_cb_.is_null()) | 513 DCHECK(read_cb_.is_null()) |
| 516 << "Shouldn't have a read callback during start"; | 514 << "Shouldn't have a read callback during start"; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 540 << ", domain=" << error.domain.utf8().data() | 538 << ", domain=" << error.domain.utf8().data() |
| 541 << ", localizedDescription=" | 539 << ", localizedDescription=" |
| 542 << error.localizedDescription.utf8().data(); | 540 << error.localizedDescription.utf8().data(); |
| 543 DCHECK(active_loader_.get()); | 541 DCHECK(active_loader_.get()); |
| 544 | 542 |
| 545 // We don't need to continue loading after failure. | 543 // We don't need to continue loading after failure. |
| 546 // | 544 // |
| 547 // Keep it alive until we exit this method so that |error| remains valid. | 545 // Keep it alive until we exit this method so that |error| remains valid. |
| 548 scoped_ptr<ActiveLoader> active_loader = active_loader_.Pass(); | 546 scoped_ptr<ActiveLoader> active_loader = active_loader_.Pass(); |
| 549 loader_failed_ = true; | 547 loader_failed_ = true; |
| 550 NotifyNetworkEvent(); | 548 loading_cb_.Run(kLoadingFailed); |
| 551 | 549 |
| 552 // Don't leave start callbacks hanging around. | 550 // Don't leave start callbacks hanging around. |
| 553 if (!start_cb_.is_null()) { | 551 if (!start_cb_.is_null()) { |
| 554 DCHECK(read_cb_.is_null()) | 552 DCHECK(read_cb_.is_null()) |
| 555 << "Shouldn't have a read callback during start"; | 553 << "Shouldn't have a read callback during start"; |
| 556 DoneStart(kFailed); | 554 DoneStart(kFailed); |
| 557 return; | 555 return; |
| 558 } | 556 } |
| 559 | 557 |
| 560 // Don't leave read callbacks hanging around. | 558 // Don't leave read callbacks hanging around. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 return; | 618 return; |
| 621 | 619 |
| 622 // If necessary, toggle defer state and continue/pause downloading data | 620 // If necessary, toggle defer state and continue/pause downloading data |
| 623 // accordingly. | 621 // accordingly. |
| 624 if (ShouldEnableDefer() || ShouldDisableDefer()) | 622 if (ShouldEnableDefer() || ShouldDisableDefer()) |
| 625 SetDeferred(!active_loader_->deferred()); | 623 SetDeferred(!active_loader_->deferred()); |
| 626 } | 624 } |
| 627 | 625 |
| 628 void BufferedResourceLoader::SetDeferred(bool deferred) { | 626 void BufferedResourceLoader::SetDeferred(bool deferred) { |
| 629 active_loader_->SetDeferred(deferred); | 627 active_loader_->SetDeferred(deferred); |
| 630 NotifyNetworkEvent(); | 628 loading_cb_.Run(deferred ? kLoadingDeferred : kLoading); |
| 631 } | 629 } |
| 632 | 630 |
| 633 bool BufferedResourceLoader::ShouldEnableDefer() const { | 631 bool BufferedResourceLoader::ShouldEnableDefer() const { |
| 634 // If we're already deferring, then enabling makes no sense. | 632 // If we're already deferring, then enabling makes no sense. |
| 635 if (active_loader_->deferred()) | 633 if (active_loader_->deferred()) |
| 636 return false; | 634 return false; |
| 637 | 635 |
| 638 switch(defer_strategy_) { | 636 switch(defer_strategy_) { |
| 639 // Never defer at all, so never enable defer. | 637 // Never defer at all, so never enable defer. |
| 640 case kNeverDefer: | 638 case kNeverDefer: |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 Log(); | 835 Log(); |
| 838 | 836 |
| 839 base::ResetAndReturn(&read_cb_).Run(status, bytes_read); | 837 base::ResetAndReturn(&read_cb_).Run(status, bytes_read); |
| 840 } | 838 } |
| 841 | 839 |
| 842 | 840 |
| 843 void BufferedResourceLoader::DoneStart(Status status) { | 841 void BufferedResourceLoader::DoneStart(Status status) { |
| 844 base::ResetAndReturn(&start_cb_).Run(status); | 842 base::ResetAndReturn(&start_cb_).Run(status); |
| 845 } | 843 } |
| 846 | 844 |
| 847 void BufferedResourceLoader::NotifyNetworkEvent() { | |
| 848 if (!event_cb_.is_null()) | |
| 849 event_cb_.Run(); | |
| 850 } | |
| 851 | |
| 852 bool BufferedResourceLoader::IsRangeRequest() const { | 845 bool BufferedResourceLoader::IsRangeRequest() const { |
| 853 return first_byte_position_ != kPositionNotSpecified; | 846 return first_byte_position_ != kPositionNotSpecified; |
| 854 } | 847 } |
| 855 | 848 |
| 856 void BufferedResourceLoader::Log() { | 849 void BufferedResourceLoader::Log() { |
| 857 media_log_->AddEvent( | 850 media_log_->AddEvent( |
| 858 media_log_->CreateBufferedExtentsChangedEvent( | 851 media_log_->CreateBufferedExtentsChangedEvent( |
| 859 offset_ - buffer_.backward_bytes(), | 852 offset_ - buffer_.backward_bytes(), |
| 860 offset_, | 853 offset_, |
| 861 offset_ + buffer_.forward_bytes())); | 854 offset_ + buffer_.forward_bytes())); |
| 862 } | 855 } |
| 863 | 856 |
| 864 } // namespace webkit_media | 857 } // namespace webkit_media |
| OLD | NEW |