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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 public: | 181 public: |
175 explicit ForwardingFilter(IPC::Sender* dest, | 182 explicit ForwardingFilter(IPC::Sender* dest, |
176 ResourceContext* resource_context) | 183 ResourceContext* resource_context) |
177 : ResourceMessageFilter( | 184 : ResourceMessageFilter( |
178 ChildProcessHostImpl::GenerateChildProcessUniqueId(), | 185 ChildProcessHostImpl::GenerateChildProcessUniqueId(), |
179 PROCESS_TYPE_RENDERER, NULL, NULL, NULL, | 186 PROCESS_TYPE_RENDERER, NULL, NULL, NULL, |
180 base::Bind(&ForwardingFilter::GetContexts, | 187 base::Bind(&ForwardingFilter::GetContexts, |
181 base::Unretained(this))), | 188 base::Unretained(this))), |
182 dest_(dest), | 189 dest_(dest), |
183 resource_context_(resource_context) { | 190 resource_context_(resource_context) { |
| 191 ChildProcessSecurityPolicyImpl::GetInstance()->Add(child_id()); |
184 set_peer_pid_for_testing(base::GetCurrentProcId()); | 192 set_peer_pid_for_testing(base::GetCurrentProcId()); |
185 } | 193 } |
186 | 194 |
187 // ResourceMessageFilter override | 195 // ResourceMessageFilter override |
188 virtual bool Send(IPC::Message* msg) OVERRIDE { | 196 virtual bool Send(IPC::Message* msg) OVERRIDE { |
189 if (!dest_) | 197 if (!dest_) |
190 return false; | 198 return false; |
191 return dest_->Send(msg); | 199 return dest_->Send(msg); |
192 } | 200 } |
193 | 201 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 } | 560 } |
553 } | 561 } |
554 | 562 |
555 private: | 563 private: |
556 bool create_two_throttles_; | 564 bool create_two_throttles_; |
557 int flags_; | 565 int flags_; |
558 int error_code_for_cancellation_; | 566 int error_code_for_cancellation_; |
559 scoped_ptr<base::SupportsUserData::Data> user_data_; | 567 scoped_ptr<base::SupportsUserData::Data> user_data_; |
560 }; | 568 }; |
561 | 569 |
| 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 |
562 class ResourceDispatcherHostTest : public testing::Test, | 595 class ResourceDispatcherHostTest : public testing::Test, |
563 public IPC::Sender { | 596 public IPC::Sender { |
564 public: | 597 public: |
565 ResourceDispatcherHostTest() | 598 ResourceDispatcherHostTest() |
566 : ui_thread_(BrowserThread::UI, &message_loop_), | 599 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
567 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), | |
568 cache_thread_(BrowserThread::CACHE, &message_loop_), | |
569 io_thread_(BrowserThread::IO, &message_loop_), | |
570 old_factory_(NULL), | 600 old_factory_(NULL), |
571 send_data_received_acks_(false) { | 601 send_data_received_acks_(false) { |
572 browser_context_.reset(new TestBrowserContext()); | 602 browser_context_.reset(new TestBrowserContext()); |
573 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); | 603 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); |
574 message_loop_.RunUntilIdle(); | 604 base::RunLoop().RunUntilIdle(); |
575 ResourceContext* resource_context = browser_context_->GetResourceContext(); | 605 ResourceContext* resource_context = browser_context_->GetResourceContext(); |
576 filter_ = new ForwardingFilter(this, resource_context); | 606 filter_ = new ForwardingFilter(this, resource_context); |
577 resource_context->GetRequestContext()->set_network_delegate( | 607 resource_context->GetRequestContext()->set_network_delegate( |
578 &network_delegate_); | 608 &network_delegate_); |
579 } | 609 } |
580 | 610 |
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 | |
588 // IPC::Sender implementation | 611 // IPC::Sender implementation |
589 virtual bool Send(IPC::Message* msg) OVERRIDE { | 612 virtual bool Send(IPC::Message* msg) OVERRIDE { |
590 accum_.AddMessage(*msg); | 613 accum_.AddMessage(*msg); |
591 | 614 |
592 if (send_data_received_acks_ && | 615 if (send_data_received_acks_ && |
593 msg->type() == ResourceMsg_DataReceived::ID) { | 616 msg->type() == ResourceMsg_DataReceived::ID) { |
594 GenerateDataReceivedACK(*msg); | 617 GenerateDataReceivedACK(*msg); |
595 } | 618 } |
596 | 619 |
| 620 if (wait_for_request_complete_loop_ && |
| 621 msg->type() == ResourceMsg_RequestComplete::ID) { |
| 622 wait_for_request_complete_loop_->Quit(); |
| 623 } |
| 624 |
597 delete msg; | 625 delete msg; |
598 return true; | 626 return true; |
599 } | 627 } |
600 | 628 |
601 protected: | 629 protected: |
602 // testing::Test | 630 // testing::Test |
603 virtual void SetUp() { | 631 virtual void SetUp() OVERRIDE { |
604 DCHECK(!test_fixture_); | 632 DCHECK(!test_fixture_); |
605 test_fixture_ = this; | 633 test_fixture_ = this; |
606 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); | 634 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); |
607 net::URLRequest::Deprecated::RegisterProtocolFactory( | 635 net::URLRequest::Deprecated::RegisterProtocolFactory( |
608 "test", | 636 "test", |
609 &ResourceDispatcherHostTest::Factory); | 637 &ResourceDispatcherHostTest::Factory); |
610 EnsureTestSchemeIsAllowed(); | 638 EnsureTestSchemeIsAllowed(); |
611 delay_start_ = false; | 639 delay_start_ = false; |
612 delay_complete_ = false; | 640 delay_complete_ = false; |
613 network_start_notification_ = false; | 641 network_start_notification_ = false; |
614 url_request_jobs_created_count_ = 0; | 642 url_request_jobs_created_count_ = 0; |
615 } | 643 } |
616 | 644 |
617 virtual void TearDown() { | 645 virtual void TearDown() { |
618 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL); | 646 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL); |
619 if (!scheme_.empty()) | 647 if (!scheme_.empty()) |
620 net::URLRequest::Deprecated::RegisterProtocolFactory( | 648 net::URLRequest::Deprecated::RegisterProtocolFactory( |
621 scheme_, old_factory_); | 649 scheme_, old_factory_); |
622 | 650 |
623 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); | 651 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); |
624 URLRequestTestDelayedStartJob::ClearQueue(); | 652 URLRequestTestDelayedStartJob::ClearQueue(); |
625 | 653 |
626 DCHECK(test_fixture_ == this); | 654 DCHECK(test_fixture_ == this); |
627 test_fixture_ = NULL; | 655 test_fixture_ = NULL; |
628 | 656 |
| 657 for (std::set<int>::iterator it = child_ids_.begin(); |
| 658 it != child_ids_.end(); ++it) { |
| 659 host_.CancelRequestsForProcess(*it); |
| 660 } |
| 661 |
629 host_.Shutdown(); | 662 host_.Shutdown(); |
630 | 663 |
631 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0); | 664 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0); |
632 | 665 |
633 // Flush the message loop to make application verifiers happy. | 666 // Flush the message loop to make application verifiers happy. |
634 if (ResourceDispatcherHostImpl::Get()) | 667 if (ResourceDispatcherHostImpl::Get()) |
635 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext( | 668 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext( |
636 browser_context_->GetResourceContext()); | 669 browser_context_->GetResourceContext()); |
637 | 670 |
638 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting(); | 671 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting(); |
639 | 672 |
640 browser_context_.reset(); | 673 browser_context_.reset(); |
641 message_loop_.RunUntilIdle(); | 674 base::RunLoop().RunUntilIdle(); |
642 } | 675 } |
643 | 676 |
644 // Creates a request using the current test object as the filter and | 677 // Creates a request using the current test object as the filter and |
645 // SubResource as the resource type. | 678 // SubResource as the resource type. |
646 void MakeTestRequest(int render_view_id, | 679 void MakeTestRequest(int render_view_id, |
647 int request_id, | 680 int request_id, |
648 const GURL& url); | 681 const GURL& url); |
649 | 682 |
650 // Generates a request using the given filter and resource type. | 683 // Generates a request using the given filter and resource type. |
651 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter, | 684 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter, |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 } | 800 } |
768 | 801 |
769 // Setting filters for testing renderer messages. | 802 // Setting filters for testing renderer messages. |
770 // Returns the previous filter. | 803 // Returns the previous filter. |
771 ResourceMessageFilter* SetFilter(ResourceMessageFilter* new_filter) { | 804 ResourceMessageFilter* SetFilter(ResourceMessageFilter* new_filter) { |
772 ResourceMessageFilter* old_filter = host_.filter_; | 805 ResourceMessageFilter* old_filter = host_.filter_; |
773 host_.filter_ = new_filter; | 806 host_.filter_ = new_filter; |
774 return old_filter; | 807 return old_filter; |
775 } | 808 } |
776 | 809 |
777 base::MessageLoopForIO message_loop_; | 810 void WaitForRequestComplete() { |
778 BrowserThreadImpl ui_thread_; | 811 DCHECK(!wait_for_request_complete_loop_); |
779 BrowserThreadImpl file_thread_; | 812 wait_for_request_complete_loop_.reset(new base::RunLoop); |
780 BrowserThreadImpl cache_thread_; | 813 wait_for_request_complete_loop_->Run(); |
781 BrowserThreadImpl io_thread_; | 814 wait_for_request_complete_loop_.reset(); |
| 815 } |
| 816 |
| 817 content::TestBrowserThreadBundle thread_bundle_; |
782 scoped_ptr<TestBrowserContext> browser_context_; | 818 scoped_ptr<TestBrowserContext> browser_context_; |
783 scoped_refptr<ForwardingFilter> filter_; | 819 scoped_refptr<ForwardingFilter> filter_; |
784 net::TestNetworkDelegate network_delegate_; | 820 net::TestNetworkDelegate network_delegate_; |
785 ResourceDispatcherHostImpl host_; | 821 ResourceDispatcherHostImpl host_; |
786 ResourceIPCAccumulator accum_; | 822 ResourceIPCAccumulator accum_; |
787 std::string response_headers_; | 823 std::string response_headers_; |
788 std::string response_data_; | 824 std::string response_data_; |
789 std::string scheme_; | 825 std::string scheme_; |
790 net::URLRequest::ProtocolFactory* old_factory_; | 826 net::URLRequest::ProtocolFactory* old_factory_; |
791 bool send_data_received_acks_; | 827 bool send_data_received_acks_; |
792 std::set<int> child_ids_; | 828 std::set<int> child_ids_; |
| 829 scoped_ptr<base::RunLoop> wait_for_request_complete_loop_; |
793 static ResourceDispatcherHostTest* test_fixture_; | 830 static ResourceDispatcherHostTest* test_fixture_; |
794 static bool delay_start_; | 831 static bool delay_start_; |
795 static bool delay_complete_; | 832 static bool delay_complete_; |
796 static bool network_start_notification_; | 833 static bool network_start_notification_; |
797 static int url_request_jobs_created_count_; | 834 static int url_request_jobs_created_count_; |
798 }; | 835 }; |
799 // Static. | 836 // Static. |
800 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; | 837 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; |
801 bool ResourceDispatcherHostTest::delay_start_ = false; | 838 bool ResourceDispatcherHostTest::delay_start_ = false; |
802 bool ResourceDispatcherHostTest::delay_complete_ = false; | 839 bool ResourceDispatcherHostTest::delay_complete_ = false; |
(...skipping 1902 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2705 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok); | 2742 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok); |
2706 } | 2743 } |
2707 | 2744 |
2708 base::MessageLoop::current()->RunUntilIdle(); | 2745 base::MessageLoop::current()->RunUntilIdle(); |
2709 | 2746 |
2710 msgs.clear(); | 2747 msgs.clear(); |
2711 accum_.GetClassifiedMessages(&msgs); | 2748 accum_.GetClassifiedMessages(&msgs); |
2712 } | 2749 } |
2713 } | 2750 } |
2714 | 2751 |
| 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 |
2715 } // namespace content | 2909 } // namespace content |
OLD | NEW |