| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 #include "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/process_util.h" | 7 #include "base/process_util.h" |
| 8 #include "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/common/extensions/url_pattern.h" | 10 #include "chrome/common/extensions/url_pattern.h" |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 if (resource_loader) | 531 if (resource_loader) |
| 532 resource_loader->Stop(); | 532 resource_loader->Stop(); |
| 533 } | 533 } |
| 534 | 534 |
| 535 bool BufferedDataSource::Initialize(const std::string& url) { | 535 bool BufferedDataSource::Initialize(const std::string& url) { |
| 536 // Save the url. | 536 // Save the url. |
| 537 url_ = GURL(url); | 537 url_ = GURL(url); |
| 538 | 538 |
| 539 // Make sure we support the scheme of the URL. | 539 // Make sure we support the scheme of the URL. |
| 540 if (!IsSchemeSupported(url_)) { | 540 if (!IsSchemeSupported(url_)) { |
| 541 host_->Error(media::PIPELINE_ERROR_NETWORK); | 541 host()->Error(media::PIPELINE_ERROR_NETWORK); |
| 542 return false; | 542 return false; |
| 543 } | 543 } |
| 544 | 544 |
| 545 media_format_.SetAsString(media::MediaFormat::kMimeType, | 545 media_format_.SetAsString(media::MediaFormat::kMimeType, |
| 546 media::mime_type::kApplicationOctetStream); | 546 media::mime_type::kApplicationOctetStream); |
| 547 media_format_.SetAsString(media::MediaFormat::kURL, url); | 547 media_format_.SetAsString(media::MediaFormat::kURL, url); |
| 548 | 548 |
| 549 // Setup the BufferedResourceLoader here. | 549 // Setup the BufferedResourceLoader here. |
| 550 scoped_refptr<BufferedResourceLoader> resource_loader = NULL; | 550 scoped_refptr<BufferedResourceLoader> resource_loader = NULL; |
| 551 { | 551 { |
| 552 AutoLock auto_lock(lock_); | 552 AutoLock auto_lock(lock_); |
| 553 if (!stopped_) { | 553 if (!stopped_) { |
| 554 buffered_resource_loader_ = new BufferedResourceLoader( | 554 buffered_resource_loader_ = new BufferedResourceLoader( |
| 555 render_loop_, | 555 render_loop_, |
| 556 bridge_factory_.get(), | 556 bridge_factory_.get(), |
| 557 url_, | 557 url_, |
| 558 kPositionNotSpecified, | 558 kPositionNotSpecified, |
| 559 kPositionNotSpecified); | 559 kPositionNotSpecified); |
| 560 resource_loader = buffered_resource_loader_; | 560 resource_loader = buffered_resource_loader_; |
| 561 } | 561 } |
| 562 } | 562 } |
| 563 | 563 |
| 564 // Use the local reference to start the request. | 564 // Use the local reference to start the request. |
| 565 if (resource_loader) { | 565 if (resource_loader) { |
| 566 if (net::ERR_IO_PENDING != resource_loader->Start( | 566 if (net::ERR_IO_PENDING != resource_loader->Start( |
| 567 NewCallback(this, &BufferedDataSource::InitialRequestStarted))) { | 567 NewCallback(this, &BufferedDataSource::InitialRequestStarted))) { |
| 568 host_->Error(media::PIPELINE_ERROR_NETWORK); | 568 host()->Error(media::PIPELINE_ERROR_NETWORK); |
| 569 return false; | 569 return false; |
| 570 } | 570 } |
| 571 return true; | 571 return true; |
| 572 } | 572 } |
| 573 host_->Error(media::PIPELINE_ERROR_NETWORK); | 573 host()->Error(media::PIPELINE_ERROR_NETWORK); |
| 574 return false; | 574 return false; |
| 575 } | 575 } |
| 576 | 576 |
| 577 size_t BufferedDataSource::Read(uint8* data, size_t size) { | 577 size_t BufferedDataSource::Read(uint8* data, size_t size) { |
| 578 // We try two times here: | 578 // We try two times here: |
| 579 // 1. Use the existing resource loader to seek and read from it. | 579 // 1. Use the existing resource loader to seek and read from it. |
| 580 // 2. If any of the above operations failed, we create a new resource loader | 580 // 2. If any of the above operations failed, we create a new resource loader |
| 581 // starting with a new range. Goto 1. | 581 // starting with a new range. Goto 1. |
| 582 for (size_t trials = kReadTrials; trials > 0; --trials) { | 582 for (size_t trials = kReadTrials; trials > 0; --trials) { |
| 583 scoped_refptr<BufferedResourceLoader> resource_loader = NULL; | 583 scoped_refptr<BufferedResourceLoader> resource_loader = NULL; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 return false; | 664 return false; |
| 665 } | 665 } |
| 666 | 666 |
| 667 bool BufferedDataSource::IsSeekable() { | 667 bool BufferedDataSource::IsSeekable() { |
| 668 return total_bytes_ != kPositionNotSpecified; | 668 return total_bytes_ != kPositionNotSpecified; |
| 669 } | 669 } |
| 670 | 670 |
| 671 void BufferedDataSource::HandleError(media::PipelineError error) { | 671 void BufferedDataSource::HandleError(media::PipelineError error) { |
| 672 AutoLock auto_lock(lock_); | 672 AutoLock auto_lock(lock_); |
| 673 if (!stopped_) { | 673 if (!stopped_) { |
| 674 host_->Error(error); | 674 host()->Error(error); |
| 675 } | 675 } |
| 676 } | 676 } |
| 677 | 677 |
| 678 void BufferedDataSource::InitialRequestStarted(int error) { | 678 void BufferedDataSource::InitialRequestStarted(int error) { |
| 679 // Don't take any lock and call to |host_| here, this method is called from | 679 // Don't take any lock and call to |host_| here, this method is called from |
| 680 // BufferedResourceLoader after the response has started or failed, it is | 680 // BufferedResourceLoader after the response has started or failed, it is |
| 681 // very likely we are called within a lock in BufferedResourceLoader. | 681 // very likely we are called within a lock in BufferedResourceLoader. |
| 682 // Acquiring an additional lock here we might have a deadlock situation, | 682 // Acquiring an additional lock here we might have a deadlock situation, |
| 683 // but one thing very sure is that pipeline thread is still alive, so we | 683 // but one thing very sure is that pipeline thread is still alive, so we |
| 684 // just need to post a task on that thread. | 684 // just need to post a task on that thread. |
| 685 pipeline_loop_->PostTask(FROM_HERE, | 685 pipeline_loop_->PostTask(FROM_HERE, |
| 686 NewRunnableMethod(this, | 686 NewRunnableMethod(this, |
| 687 &BufferedDataSource::OnInitialRequestStarted, error)); | 687 &BufferedDataSource::OnInitialRequestStarted, error)); |
| 688 } | 688 } |
| 689 | 689 |
| 690 void BufferedDataSource::OnInitialRequestStarted(int error) { | 690 void BufferedDataSource::OnInitialRequestStarted(int error) { |
| 691 // Acquiring a lock should not be needed because stopped_ is only written | 691 // Acquiring a lock should not be needed because stopped_ is only written |
| 692 // on pipeline thread and we are on pipeline thread but just to be safe. | 692 // on pipeline thread and we are on pipeline thread but just to be safe. |
| 693 AutoLock auto_lock(lock_); | 693 AutoLock auto_lock(lock_); |
| 694 if (!stopped_) { | 694 if (!stopped_) { |
| 695 if (error == net::OK) { | 695 if (error == net::OK) { |
| 696 total_bytes_ = buffered_resource_loader_->content_length(); | 696 total_bytes_ = buffered_resource_loader_->content_length(); |
| 697 if (IsSeekable()) { | 697 if (IsSeekable()) { |
| 698 host_->SetTotalBytes(total_bytes_); | 698 host()->SetTotalBytes(total_bytes_); |
| 699 // TODO(hclam): report the amount of bytes buffered accurately. | 699 // TODO(hclam): report the amount of bytes buffered accurately. |
| 700 host_->SetBufferedBytes(total_bytes_); | 700 host()->SetBufferedBytes(total_bytes_); |
| 701 } | 701 } |
| 702 host_->InitializationComplete(); | 702 host()->InitializationComplete(); |
| 703 } else { | 703 } else { |
| 704 host_->Error(media::PIPELINE_ERROR_NETWORK); | 704 host()->Error(media::PIPELINE_ERROR_NETWORK); |
| 705 } | 705 } |
| 706 } | 706 } |
| 707 } | 707 } |
| 708 | 708 |
| 709 const media::MediaFormat& BufferedDataSource::media_format() { | 709 const media::MediaFormat& BufferedDataSource::media_format() { |
| 710 return media_format_; | 710 return media_format_; |
| 711 } | 711 } |
| OLD | NEW |