| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/basictypes.h" |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 9 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 10 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 11 #include "base/pickle.h" | 13 #include "base/pickle.h" |
| 12 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 13 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 15 #include "content/browser/browser_thread_impl.h" | 17 #include "content/browser/browser_thread_impl.h" |
| 16 #include "content/browser/child_process_security_policy_impl.h" | 18 #include "content/browser/child_process_security_policy_impl.h" |
| 17 #include "content/browser/loader/cross_site_resource_handler.h" | 19 #include "content/browser/loader/cross_site_resource_handler.h" |
| 18 #include "content/browser/loader/detachable_resource_handler.h" | 20 #include "content/browser/loader/detachable_resource_handler.h" |
| 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 21 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 20 #include "content/browser/loader/resource_loader.h" | 22 #include "content/browser/loader/resource_loader.h" |
| 21 #include "content/browser/loader/resource_message_filter.h" | 23 #include "content/browser/loader/resource_message_filter.h" |
| 22 #include "content/browser/loader/resource_request_info_impl.h" | 24 #include "content/browser/loader/resource_request_info_impl.h" |
| 23 #include "content/browser/worker_host/worker_service_impl.h" | 25 #include "content/browser/worker_host/worker_service_impl.h" |
| 24 #include "content/common/child_process_host_impl.h" | 26 #include "content/common/child_process_host_impl.h" |
| 25 #include "content/common/resource_messages.h" | 27 #include "content/common/resource_messages.h" |
| 26 #include "content/common/view_messages.h" | 28 #include "content/common/view_messages.h" |
| 27 #include "content/public/browser/global_request_id.h" | 29 #include "content/public/browser/global_request_id.h" |
| 28 #include "content/public/browser/resource_context.h" | 30 #include "content/public/browser/resource_context.h" |
| 29 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 31 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 30 #include "content/public/browser/resource_request_info.h" | 32 #include "content/public/browser/resource_request_info.h" |
| 31 #include "content/public/browser/resource_throttle.h" | 33 #include "content/public/browser/resource_throttle.h" |
| 32 #include "content/public/common/process_type.h" | 34 #include "content/public/common/process_type.h" |
| 33 #include "content/public/common/resource_response.h" | 35 #include "content/public/common/resource_response.h" |
| 34 #include "content/public/test/test_browser_context.h" | 36 #include "content/public/test/test_browser_context.h" |
| 37 #include "content/public/test/test_browser_thread_bundle.h" |
| 35 #include "content/test/test_content_browser_client.h" | 38 #include "content/test/test_content_browser_client.h" |
| 36 #include "net/base/net_errors.h" | 39 #include "net/base/net_errors.h" |
| 37 #include "net/base/request_priority.h" | 40 #include "net/base/request_priority.h" |
| 38 #include "net/base/upload_bytes_element_reader.h" | 41 #include "net/base/upload_bytes_element_reader.h" |
| 39 #include "net/base/upload_data_stream.h" | 42 #include "net/base/upload_data_stream.h" |
| 40 #include "net/http/http_util.h" | 43 #include "net/http/http_util.h" |
| 41 #include "net/url_request/url_request.h" | 44 #include "net/url_request/url_request.h" |
| 42 #include "net/url_request/url_request_context.h" | 45 #include "net/url_request/url_request_context.h" |
| 43 #include "net/url_request/url_request_job.h" | 46 #include "net/url_request/url_request_job.h" |
| 44 #include "net/url_request/url_request_simple_job.h" | 47 #include "net/url_request/url_request_simple_job.h" |
| 45 #include "net/url_request/url_request_test_job.h" | 48 #include "net/url_request/url_request_test_job.h" |
| 46 #include "net/url_request/url_request_test_util.h" | 49 #include "net/url_request/url_request_test_util.h" |
| 47 #include "testing/gtest/include/gtest/gtest.h" | 50 #include "testing/gtest/include/gtest/gtest.h" |
| 48 #include "webkit/common/appcache/appcache_interfaces.h" | 51 #include "webkit/common/appcache/appcache_interfaces.h" |
| 52 #include "webkit/common/blob/shareable_file_reference.h" |
| 49 | 53 |
| 50 // TODO(eroman): Write unit tests for SafeBrowsing that exercise | 54 // TODO(eroman): Write unit tests for SafeBrowsing that exercise |
| 51 // SafeBrowsingResourceHandler. | 55 // SafeBrowsingResourceHandler. |
| 52 | 56 |
| 57 using webkit_blob::ShareableFileReference; |
| 58 |
| 53 namespace content { | 59 namespace content { |
| 54 | 60 |
| 55 namespace { | 61 namespace { |
| 56 | 62 |
| 57 // Returns the resource response header structure for this request. | 63 // Returns the resource response header structure for this request. |
| 58 void GetResponseHead(const std::vector<IPC::Message>& messages, | 64 void GetResponseHead(const std::vector<IPC::Message>& messages, |
| 59 ResourceResponseHead* response_head) { | 65 ResourceResponseHead* response_head) { |
| 60 ASSERT_GE(messages.size(), 2U); | 66 ASSERT_GE(messages.size(), 2U); |
| 61 | 67 |
| 62 // The first messages should be received response. | 68 // The first messages should be received response. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 79 } // namespace | 85 } // namespace |
| 80 | 86 |
| 81 static int RequestIDForMessage(const IPC::Message& msg) { | 87 static int RequestIDForMessage(const IPC::Message& msg) { |
| 82 int request_id = -1; | 88 int request_id = -1; |
| 83 switch (msg.type()) { | 89 switch (msg.type()) { |
| 84 case ResourceMsg_UploadProgress::ID: | 90 case ResourceMsg_UploadProgress::ID: |
| 85 case ResourceMsg_ReceivedResponse::ID: | 91 case ResourceMsg_ReceivedResponse::ID: |
| 86 case ResourceMsg_ReceivedRedirect::ID: | 92 case ResourceMsg_ReceivedRedirect::ID: |
| 87 case ResourceMsg_SetDataBuffer::ID: | 93 case ResourceMsg_SetDataBuffer::ID: |
| 88 case ResourceMsg_DataReceived::ID: | 94 case ResourceMsg_DataReceived::ID: |
| 95 case ResourceMsg_DataDownloaded::ID: |
| 89 case ResourceMsg_RequestComplete::ID: { | 96 case ResourceMsg_RequestComplete::ID: { |
| 90 bool result = PickleIterator(msg).ReadInt(&request_id); | 97 bool result = PickleIterator(msg).ReadInt(&request_id); |
| 91 DCHECK(result); | 98 DCHECK(result); |
| 92 break; | 99 break; |
| 93 } | 100 } |
| 94 } | 101 } |
| 95 return request_id; | 102 return request_id; |
| 96 } | 103 } |
| 97 | 104 |
| 98 static ResourceHostMsg_Request CreateResourceRequest( | 105 static ResourceHostMsg_Request CreateResourceRequest( |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 public: | 182 public: |
| 176 explicit ForwardingFilter(IPC::Sender* dest, | 183 explicit ForwardingFilter(IPC::Sender* dest, |
| 177 ResourceContext* resource_context) | 184 ResourceContext* resource_context) |
| 178 : ResourceMessageFilter( | 185 : ResourceMessageFilter( |
| 179 ChildProcessHostImpl::GenerateChildProcessUniqueId(), | 186 ChildProcessHostImpl::GenerateChildProcessUniqueId(), |
| 180 PROCESS_TYPE_RENDERER, NULL, NULL, NULL, | 187 PROCESS_TYPE_RENDERER, NULL, NULL, NULL, |
| 181 base::Bind(&ForwardingFilter::GetContexts, | 188 base::Bind(&ForwardingFilter::GetContexts, |
| 182 base::Unretained(this))), | 189 base::Unretained(this))), |
| 183 dest_(dest), | 190 dest_(dest), |
| 184 resource_context_(resource_context) { | 191 resource_context_(resource_context) { |
| 192 ChildProcessSecurityPolicyImpl::GetInstance()->Add(child_id()); |
| 185 set_peer_pid_for_testing(base::GetCurrentProcId()); | 193 set_peer_pid_for_testing(base::GetCurrentProcId()); |
| 186 } | 194 } |
| 187 | 195 |
| 188 // ResourceMessageFilter override | 196 // ResourceMessageFilter override |
| 189 virtual bool Send(IPC::Message* msg) OVERRIDE { | 197 virtual bool Send(IPC::Message* msg) OVERRIDE { |
| 190 if (!dest_) | 198 if (!dest_) |
| 191 return false; | 199 return false; |
| 192 return dest_->Send(msg); | 200 return dest_->Send(msg); |
| 193 } | 201 } |
| 194 | 202 |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 } | 561 } |
| 554 } | 562 } |
| 555 | 563 |
| 556 private: | 564 private: |
| 557 bool create_two_throttles_; | 565 bool create_two_throttles_; |
| 558 int flags_; | 566 int flags_; |
| 559 int error_code_for_cancellation_; | 567 int error_code_for_cancellation_; |
| 560 scoped_ptr<base::SupportsUserData::Data> user_data_; | 568 scoped_ptr<base::SupportsUserData::Data> user_data_; |
| 561 }; | 569 }; |
| 562 | 570 |
| 571 // Waits for a ShareableFileReference to be released. |
| 572 class ShareableFileReleaseWaiter { |
| 573 public: |
| 574 ShareableFileReleaseWaiter(const base::FilePath& path) { |
| 575 scoped_refptr<ShareableFileReference> file = |
| 576 ShareableFileReference::Get(path); |
| 577 file->AddFinalReleaseCallback( |
| 578 base::Bind(&ShareableFileReleaseWaiter::Released, |
| 579 base::Unretained(this))); |
| 580 } |
| 581 |
| 582 void Wait() { |
| 583 loop_.Run(); |
| 584 } |
| 585 |
| 586 private: |
| 587 void Released(const base::FilePath& path) { |
| 588 loop_.Quit(); |
| 589 } |
| 590 |
| 591 base::RunLoop loop_; |
| 592 |
| 593 DISALLOW_COPY_AND_ASSIGN(ShareableFileReleaseWaiter); |
| 594 }; |
| 595 |
| 563 class ResourceDispatcherHostTest : public testing::Test, | 596 class ResourceDispatcherHostTest : public testing::Test, |
| 564 public IPC::Sender { | 597 public IPC::Sender { |
| 565 public: | 598 public: |
| 566 ResourceDispatcherHostTest() | 599 ResourceDispatcherHostTest() |
| 567 : ui_thread_(BrowserThread::UI, &message_loop_), | 600 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 568 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), | |
| 569 cache_thread_(BrowserThread::CACHE, &message_loop_), | |
| 570 io_thread_(BrowserThread::IO, &message_loop_), | |
| 571 old_factory_(NULL), | 601 old_factory_(NULL), |
| 572 send_data_received_acks_(false) { | 602 send_data_received_acks_(false) { |
| 573 browser_context_.reset(new TestBrowserContext()); | 603 browser_context_.reset(new TestBrowserContext()); |
| 574 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); | 604 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); |
| 575 message_loop_.RunUntilIdle(); | 605 base::RunLoop().RunUntilIdle(); |
| 576 ResourceContext* resource_context = browser_context_->GetResourceContext(); | 606 ResourceContext* resource_context = browser_context_->GetResourceContext(); |
| 577 filter_ = new ForwardingFilter(this, resource_context); | 607 filter_ = new ForwardingFilter(this, resource_context); |
| 578 resource_context->GetRequestContext()->set_network_delegate( | 608 resource_context->GetRequestContext()->set_network_delegate( |
| 579 &network_delegate_); | 609 &network_delegate_); |
| 580 } | 610 } |
| 581 | 611 |
| 582 virtual ~ResourceDispatcherHostTest() { | |
| 583 for (std::set<int>::iterator it = child_ids_.begin(); | |
| 584 it != child_ids_.end(); ++it) { | |
| 585 host_.CancelRequestsForProcess(*it); | |
| 586 } | |
| 587 } | |
| 588 | |
| 589 // IPC::Sender implementation | 612 // IPC::Sender implementation |
| 590 virtual bool Send(IPC::Message* msg) OVERRIDE { | 613 virtual bool Send(IPC::Message* msg) OVERRIDE { |
| 591 accum_.AddMessage(*msg); | 614 accum_.AddMessage(*msg); |
| 592 | 615 |
| 593 if (send_data_received_acks_ && | 616 if (send_data_received_acks_ && |
| 594 msg->type() == ResourceMsg_DataReceived::ID) { | 617 msg->type() == ResourceMsg_DataReceived::ID) { |
| 595 GenerateDataReceivedACK(*msg); | 618 GenerateDataReceivedACK(*msg); |
| 596 } | 619 } |
| 597 | 620 |
| 621 if (wait_for_request_complete_loop_ && |
| 622 msg->type() == ResourceMsg_RequestComplete::ID) { |
| 623 wait_for_request_complete_loop_->Quit(); |
| 624 } |
| 625 |
| 598 delete msg; | 626 delete msg; |
| 599 return true; | 627 return true; |
| 600 } | 628 } |
| 601 | 629 |
| 602 protected: | 630 protected: |
| 603 // testing::Test | 631 // testing::Test |
| 604 virtual void SetUp() { | 632 virtual void SetUp() OVERRIDE { |
| 605 DCHECK(!test_fixture_); | 633 DCHECK(!test_fixture_); |
| 606 test_fixture_ = this; | 634 test_fixture_ = this; |
| 607 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); | 635 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); |
| 608 net::URLRequest::Deprecated::RegisterProtocolFactory( | 636 net::URLRequest::Deprecated::RegisterProtocolFactory( |
| 609 "test", | 637 "test", |
| 610 &ResourceDispatcherHostTest::Factory); | 638 &ResourceDispatcherHostTest::Factory); |
| 611 EnsureTestSchemeIsAllowed(); | 639 EnsureTestSchemeIsAllowed(); |
| 612 delay_start_ = false; | 640 delay_start_ = false; |
| 613 delay_complete_ = false; | 641 delay_complete_ = false; |
| 614 network_start_notification_ = false; | 642 network_start_notification_ = false; |
| 615 url_request_jobs_created_count_ = 0; | 643 url_request_jobs_created_count_ = 0; |
| 616 } | 644 } |
| 617 | 645 |
| 618 virtual void TearDown() { | 646 virtual void TearDown() { |
| 619 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL); | 647 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL); |
| 620 if (!scheme_.empty()) | 648 if (!scheme_.empty()) |
| 621 net::URLRequest::Deprecated::RegisterProtocolFactory( | 649 net::URLRequest::Deprecated::RegisterProtocolFactory( |
| 622 scheme_, old_factory_); | 650 scheme_, old_factory_); |
| 623 | 651 |
| 624 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); | 652 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); |
| 625 URLRequestTestDelayedStartJob::ClearQueue(); | 653 URLRequestTestDelayedStartJob::ClearQueue(); |
| 626 | 654 |
| 627 DCHECK(test_fixture_ == this); | 655 DCHECK(test_fixture_ == this); |
| 628 test_fixture_ = NULL; | 656 test_fixture_ = NULL; |
| 629 | 657 |
| 658 for (std::set<int>::iterator it = child_ids_.begin(); |
| 659 it != child_ids_.end(); ++it) { |
| 660 host_.CancelRequestsForProcess(*it); |
| 661 } |
| 662 |
| 630 host_.Shutdown(); | 663 host_.Shutdown(); |
| 631 | 664 |
| 632 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0); | 665 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0); |
| 633 | 666 |
| 634 // Flush the message loop to make application verifiers happy. | 667 // Flush the message loop to make application verifiers happy. |
| 635 if (ResourceDispatcherHostImpl::Get()) | 668 if (ResourceDispatcherHostImpl::Get()) |
| 636 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext( | 669 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext( |
| 637 browser_context_->GetResourceContext()); | 670 browser_context_->GetResourceContext()); |
| 638 | 671 |
| 639 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting(); | 672 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting(); |
| 640 | 673 |
| 641 browser_context_.reset(); | 674 browser_context_.reset(); |
| 642 message_loop_.RunUntilIdle(); | 675 base::RunLoop().RunUntilIdle(); |
| 643 } | 676 } |
| 644 | 677 |
| 645 // Creates a request using the current test object as the filter and | 678 // Creates a request using the current test object as the filter and |
| 646 // SubResource as the resource type. | 679 // SubResource as the resource type. |
| 647 void MakeTestRequest(int render_view_id, | 680 void MakeTestRequest(int render_view_id, |
| 648 int request_id, | 681 int request_id, |
| 649 const GURL& url); | 682 const GURL& url); |
| 650 | 683 |
| 651 // Generates a request using the given filter and resource type. | 684 // Generates a request using the given filter and resource type. |
| 652 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter, | 685 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter, |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 } | 801 } |
| 769 | 802 |
| 770 // Setting filters for testing renderer messages. | 803 // Setting filters for testing renderer messages. |
| 771 // Returns the previous filter. | 804 // Returns the previous filter. |
| 772 ResourceMessageFilter* SetFilter(ResourceMessageFilter* new_filter) { | 805 ResourceMessageFilter* SetFilter(ResourceMessageFilter* new_filter) { |
| 773 ResourceMessageFilter* old_filter = host_.filter_; | 806 ResourceMessageFilter* old_filter = host_.filter_; |
| 774 host_.filter_ = new_filter; | 807 host_.filter_ = new_filter; |
| 775 return old_filter; | 808 return old_filter; |
| 776 } | 809 } |
| 777 | 810 |
| 778 base::MessageLoopForIO message_loop_; | 811 void WaitForRequestComplete() { |
| 779 BrowserThreadImpl ui_thread_; | 812 DCHECK(!wait_for_request_complete_loop_); |
| 780 BrowserThreadImpl file_thread_; | 813 wait_for_request_complete_loop_.reset(new base::RunLoop); |
| 781 BrowserThreadImpl cache_thread_; | 814 wait_for_request_complete_loop_->Run(); |
| 782 BrowserThreadImpl io_thread_; | 815 wait_for_request_complete_loop_.reset(); |
| 816 } |
| 817 |
| 818 content::TestBrowserThreadBundle thread_bundle_; |
| 783 scoped_ptr<TestBrowserContext> browser_context_; | 819 scoped_ptr<TestBrowserContext> browser_context_; |
| 784 scoped_refptr<ForwardingFilter> filter_; | 820 scoped_refptr<ForwardingFilter> filter_; |
| 785 net::TestNetworkDelegate network_delegate_; | 821 net::TestNetworkDelegate network_delegate_; |
| 786 ResourceDispatcherHostImpl host_; | 822 ResourceDispatcherHostImpl host_; |
| 787 ResourceIPCAccumulator accum_; | 823 ResourceIPCAccumulator accum_; |
| 788 std::string response_headers_; | 824 std::string response_headers_; |
| 789 std::string response_data_; | 825 std::string response_data_; |
| 790 std::string scheme_; | 826 std::string scheme_; |
| 791 net::URLRequest::ProtocolFactory* old_factory_; | 827 net::URLRequest::ProtocolFactory* old_factory_; |
| 792 bool send_data_received_acks_; | 828 bool send_data_received_acks_; |
| 793 std::set<int> child_ids_; | 829 std::set<int> child_ids_; |
| 830 scoped_ptr<base::RunLoop> wait_for_request_complete_loop_; |
| 794 static ResourceDispatcherHostTest* test_fixture_; | 831 static ResourceDispatcherHostTest* test_fixture_; |
| 795 static bool delay_start_; | 832 static bool delay_start_; |
| 796 static bool delay_complete_; | 833 static bool delay_complete_; |
| 797 static bool network_start_notification_; | 834 static bool network_start_notification_; |
| 798 static int url_request_jobs_created_count_; | 835 static int url_request_jobs_created_count_; |
| 799 }; | 836 }; |
| 800 // Static. | 837 // Static. |
| 801 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; | 838 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; |
| 802 bool ResourceDispatcherHostTest::delay_start_ = false; | 839 bool ResourceDispatcherHostTest::delay_start_ = false; |
| 803 bool ResourceDispatcherHostTest::delay_complete_ = false; | 840 bool ResourceDispatcherHostTest::delay_complete_ = false; |
| (...skipping 1902 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2706 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok); | 2743 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok); |
| 2707 } | 2744 } |
| 2708 | 2745 |
| 2709 base::MessageLoop::current()->RunUntilIdle(); | 2746 base::MessageLoop::current()->RunUntilIdle(); |
| 2710 | 2747 |
| 2711 msgs.clear(); | 2748 msgs.clear(); |
| 2712 accum_.GetClassifiedMessages(&msgs); | 2749 accum_.GetClassifiedMessages(&msgs); |
| 2713 } | 2750 } |
| 2714 } | 2751 } |
| 2715 | 2752 |
| 2753 // Tests the dispatcher host's temporary file management. |
| 2754 TEST_F(ResourceDispatcherHostTest, RegisterDownloadedTempFile) { |
| 2755 const int kRequestID = 1; |
| 2756 |
| 2757 // Create a temporary file. |
| 2758 base::FilePath file_path; |
| 2759 ASSERT_TRUE(base::CreateTemporaryFile(&file_path)); |
| 2760 scoped_refptr<ShareableFileReference> deletable_file = |
| 2761 ShareableFileReference::GetOrCreate( |
| 2762 file_path, |
| 2763 ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
| 2764 BrowserThread::GetMessageLoopProxyForThread( |
| 2765 BrowserThread::FILE).get()); |
| 2766 |
| 2767 // Not readable. |
| 2768 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2769 filter_->child_id(), file_path)); |
| 2770 |
| 2771 // Register it for a resource request. |
| 2772 host_.RegisterDownloadedTempFile(filter_->child_id(), kRequestID, file_path); |
| 2773 |
| 2774 // Should be readable now. |
| 2775 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2776 filter_->child_id(), file_path)); |
| 2777 |
| 2778 // The child releases from the request. |
| 2779 bool msg_was_ok = true; |
| 2780 ResourceHostMsg_ReleaseDownloadedFile release_msg(kRequestID); |
| 2781 host_.OnMessageReceived(release_msg, filter_, &msg_was_ok); |
| 2782 ASSERT_TRUE(msg_was_ok); |
| 2783 |
| 2784 // Still readable because there is another reference to the file. (The child |
| 2785 // may take additional blob references.) |
| 2786 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2787 filter_->child_id(), file_path)); |
| 2788 |
| 2789 // Release extra references and wait for the file to be deleted. (This relies |
| 2790 // on the delete happening on the FILE thread which is mapped to main thread |
| 2791 // in this test.) |
| 2792 deletable_file = NULL; |
| 2793 base::RunLoop().RunUntilIdle(); |
| 2794 |
| 2795 // The file is no longer readable to the child and has been deleted. |
| 2796 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2797 filter_->child_id(), file_path)); |
| 2798 EXPECT_FALSE(base::PathExists(file_path)); |
| 2799 } |
| 2800 |
| 2801 // Tests that temporary files held on behalf of child processes are released |
| 2802 // when the child process dies. |
| 2803 TEST_F(ResourceDispatcherHostTest, ReleaseTemporiesOnProcessExit) { |
| 2804 const int kRequestID = 1; |
| 2805 |
| 2806 // Create a temporary file. |
| 2807 base::FilePath file_path; |
| 2808 ASSERT_TRUE(base::CreateTemporaryFile(&file_path)); |
| 2809 scoped_refptr<ShareableFileReference> deletable_file = |
| 2810 ShareableFileReference::GetOrCreate( |
| 2811 file_path, |
| 2812 ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
| 2813 BrowserThread::GetMessageLoopProxyForThread( |
| 2814 BrowserThread::FILE).get()); |
| 2815 |
| 2816 // Register it for a resource request. |
| 2817 host_.RegisterDownloadedTempFile(filter_->child_id(), kRequestID, file_path); |
| 2818 deletable_file = NULL; |
| 2819 |
| 2820 // Should be readable now. |
| 2821 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2822 filter_->child_id(), file_path)); |
| 2823 |
| 2824 // Let the process die. |
| 2825 filter_->OnChannelClosing(); |
| 2826 base::RunLoop().RunUntilIdle(); |
| 2827 |
| 2828 // The file is no longer readable to the child and has been deleted. |
| 2829 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2830 filter_->child_id(), file_path)); |
| 2831 EXPECT_FALSE(base::PathExists(file_path)); |
| 2832 } |
| 2833 |
| 2834 TEST_F(ResourceDispatcherHostTest, DownloadToFile) { |
| 2835 // Make a request which downloads to file. |
| 2836 ResourceHostMsg_Request request = CreateResourceRequest( |
| 2837 "GET", ResourceType::SUB_RESOURCE, net::URLRequestTestJob::test_url_1()); |
| 2838 request.download_to_file = true; |
| 2839 ResourceHostMsg_RequestResource request_msg(0, 1, request); |
| 2840 bool msg_was_ok; |
| 2841 host_.OnMessageReceived(request_msg, filter_, &msg_was_ok); |
| 2842 ASSERT_TRUE(msg_was_ok); |
| 2843 |
| 2844 // Running the message loop until idle does not work because |
| 2845 // RedirectToFileResourceHandler posts things to base::WorkerPool. Instead, |
| 2846 // wait for the ResourceMsg_RequestComplete to go out. Then run the event loop |
| 2847 // until idle so the loader is gone. |
| 2848 WaitForRequestComplete(); |
| 2849 base::RunLoop().RunUntilIdle(); |
| 2850 EXPECT_EQ(0, host_.pending_requests()); |
| 2851 |
| 2852 ResourceIPCAccumulator::ClassifiedMessages msgs; |
| 2853 accum_.GetClassifiedMessages(&msgs); |
| 2854 |
| 2855 ASSERT_EQ(1U, msgs.size()); |
| 2856 const std::vector<IPC::Message>& messages = msgs[0]; |
| 2857 |
| 2858 // The request should contain the following messages: |
| 2859 // ReceivedResponse (indicates headers received and filename) |
| 2860 // DataDownloaded* (bytes downloaded and total length) |
| 2861 // RequestComplete (request is done) |
| 2862 |
| 2863 // ReceivedResponse |
| 2864 ResourceResponseHead response_head; |
| 2865 GetResponseHead(messages, &response_head); |
| 2866 ASSERT_FALSE(response_head.download_file_path.empty()); |
| 2867 |
| 2868 // DataDownloaded |
| 2869 size_t total_len = 0; |
| 2870 for (size_t i = 1; i < messages.size() - 1; i++) { |
| 2871 ASSERT_EQ(ResourceMsg_DataDownloaded::ID, messages[i].type()); |
| 2872 PickleIterator iter(messages[i]); |
| 2873 int request_id, data_len; |
| 2874 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &request_id)); |
| 2875 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &data_len)); |
| 2876 total_len += data_len; |
| 2877 } |
| 2878 EXPECT_EQ(net::URLRequestTestJob::test_data_1().size(), total_len); |
| 2879 |
| 2880 // RequestComplete |
| 2881 CheckRequestCompleteErrorCode(messages.back(), net::OK); |
| 2882 |
| 2883 // Verify that the data ended up in the temporary file. |
| 2884 std::string contents; |
| 2885 ASSERT_TRUE(base::ReadFileToString(response_head.download_file_path, |
| 2886 &contents)); |
| 2887 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), contents); |
| 2888 |
| 2889 // The file should be readable by the child. |
| 2890 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2891 filter_->child_id(), response_head.download_file_path)); |
| 2892 |
| 2893 // When the renderer releases the file, it should be deleted. Again, |
| 2894 // RunUntilIdle doesn't work because base::WorkerPool is involved. |
| 2895 ShareableFileReleaseWaiter waiter(response_head.download_file_path); |
| 2896 ResourceHostMsg_ReleaseDownloadedFile release_msg(1); |
| 2897 host_.OnMessageReceived(release_msg, filter_, &msg_was_ok); |
| 2898 ASSERT_TRUE(msg_was_ok); |
| 2899 waiter.Wait(); |
| 2900 // The release callback runs before the delete is scheduled, so pump the |
| 2901 // message loop for the delete itself. (This relies on the delete happening on |
| 2902 // the FILE thread which is mapped to main thread in this test.) |
| 2903 base::RunLoop().RunUntilIdle(); |
| 2904 |
| 2905 EXPECT_FALSE(base::PathExists(response_head.download_file_path)); |
| 2906 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2907 filter_->child_id(), response_head.download_file_path)); |
| 2908 } |
| 2909 |
| 2716 } // namespace content | 2910 } // namespace content |
| OLD | NEW |