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