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/detachable_resource_handler.h" | 19 #include "content/browser/loader/detachable_resource_handler.h" |
18 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
19 #include "content/browser/loader/resource_loader.h" | 21 #include "content/browser/loader/resource_loader.h" |
20 #include "content/browser/loader/resource_message_filter.h" | 22 #include "content/browser/loader/resource_message_filter.h" |
21 #include "content/browser/loader/resource_request_info_impl.h" | 23 #include "content/browser/loader/resource_request_info_impl.h" |
22 #include "content/browser/worker_host/worker_service_impl.h" | 24 #include "content/browser/worker_host/worker_service_impl.h" |
23 #include "content/common/child_process_host_impl.h" | 25 #include "content/common/child_process_host_impl.h" |
24 #include "content/common/resource_messages.h" | 26 #include "content/common/resource_messages.h" |
25 #include "content/common/view_messages.h" | 27 #include "content/common/view_messages.h" |
26 #include "content/public/browser/global_request_id.h" | 28 #include "content/public/browser/global_request_id.h" |
27 #include "content/public/browser/resource_context.h" | 29 #include "content/public/browser/resource_context.h" |
28 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 30 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
29 #include "content/public/browser/resource_request_info.h" | 31 #include "content/public/browser/resource_request_info.h" |
30 #include "content/public/browser/resource_throttle.h" | 32 #include "content/public/browser/resource_throttle.h" |
31 #include "content/public/common/process_type.h" | 33 #include "content/public/common/process_type.h" |
32 #include "content/public/common/resource_response.h" | 34 #include "content/public/common/resource_response.h" |
33 #include "content/public/test/test_browser_context.h" | 35 #include "content/public/test/test_browser_context.h" |
| 36 #include "content/public/test/test_browser_thread_bundle.h" |
34 #include "content/test/test_content_browser_client.h" | 37 #include "content/test/test_content_browser_client.h" |
35 #include "net/base/net_errors.h" | 38 #include "net/base/net_errors.h" |
36 #include "net/base/request_priority.h" | 39 #include "net/base/request_priority.h" |
37 #include "net/base/upload_bytes_element_reader.h" | 40 #include "net/base/upload_bytes_element_reader.h" |
38 #include "net/base/upload_data_stream.h" | 41 #include "net/base/upload_data_stream.h" |
39 #include "net/http/http_util.h" | 42 #include "net/http/http_util.h" |
40 #include "net/url_request/url_request.h" | 43 #include "net/url_request/url_request.h" |
41 #include "net/url_request/url_request_context.h" | 44 #include "net/url_request/url_request_context.h" |
42 #include "net/url_request/url_request_job.h" | 45 #include "net/url_request/url_request_job.h" |
43 #include "net/url_request/url_request_simple_job.h" | 46 #include "net/url_request/url_request_simple_job.h" |
44 #include "net/url_request/url_request_test_job.h" | 47 #include "net/url_request/url_request_test_job.h" |
45 #include "net/url_request/url_request_test_util.h" | 48 #include "net/url_request/url_request_test_util.h" |
46 #include "testing/gtest/include/gtest/gtest.h" | 49 #include "testing/gtest/include/gtest/gtest.h" |
47 #include "webkit/common/appcache/appcache_interfaces.h" | 50 #include "webkit/common/appcache/appcache_interfaces.h" |
| 51 #include "webkit/common/blob/shareable_file_reference.h" |
48 | 52 |
49 // TODO(eroman): Write unit tests for SafeBrowsing that exercise | 53 // TODO(eroman): Write unit tests for SafeBrowsing that exercise |
50 // SafeBrowsingResourceHandler. | 54 // SafeBrowsingResourceHandler. |
51 | 55 |
| 56 using webkit_blob::ShareableFileReference; |
| 57 |
52 namespace content { | 58 namespace content { |
53 | 59 |
54 namespace { | 60 namespace { |
55 | 61 |
56 // Returns the resource response header structure for this request. | 62 // Returns the resource response header structure for this request. |
57 void GetResponseHead(const std::vector<IPC::Message>& messages, | 63 void GetResponseHead(const std::vector<IPC::Message>& messages, |
58 ResourceResponseHead* response_head) { | 64 ResourceResponseHead* response_head) { |
59 ASSERT_GE(messages.size(), 2U); | 65 ASSERT_GE(messages.size(), 2U); |
60 | 66 |
61 // The first messages should be received response. | 67 // The first messages should be received response. |
(...skipping 16 matching lines...) Expand all Loading... |
78 } // namespace | 84 } // namespace |
79 | 85 |
80 static int RequestIDForMessage(const IPC::Message& msg) { | 86 static int RequestIDForMessage(const IPC::Message& msg) { |
81 int request_id = -1; | 87 int request_id = -1; |
82 switch (msg.type()) { | 88 switch (msg.type()) { |
83 case ResourceMsg_UploadProgress::ID: | 89 case ResourceMsg_UploadProgress::ID: |
84 case ResourceMsg_ReceivedResponse::ID: | 90 case ResourceMsg_ReceivedResponse::ID: |
85 case ResourceMsg_ReceivedRedirect::ID: | 91 case ResourceMsg_ReceivedRedirect::ID: |
86 case ResourceMsg_SetDataBuffer::ID: | 92 case ResourceMsg_SetDataBuffer::ID: |
87 case ResourceMsg_DataReceived::ID: | 93 case ResourceMsg_DataReceived::ID: |
| 94 case ResourceMsg_DataDownloaded::ID: |
88 case ResourceMsg_RequestComplete::ID: { | 95 case ResourceMsg_RequestComplete::ID: { |
89 bool result = PickleIterator(msg).ReadInt(&request_id); | 96 bool result = PickleIterator(msg).ReadInt(&request_id); |
90 DCHECK(result); | 97 DCHECK(result); |
91 break; | 98 break; |
92 } | 99 } |
93 } | 100 } |
94 return request_id; | 101 return request_id; |
95 } | 102 } |
96 | 103 |
97 static ResourceHostMsg_Request CreateResourceRequest( | 104 static ResourceHostMsg_Request CreateResourceRequest( |
(...skipping 76 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 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 } | 523 } |
516 } | 524 } |
517 | 525 |
518 private: | 526 private: |
519 bool create_two_throttles_; | 527 bool create_two_throttles_; |
520 int flags_; | 528 int flags_; |
521 int error_code_for_cancellation_; | 529 int error_code_for_cancellation_; |
522 scoped_ptr<base::SupportsUserData::Data> user_data_; | 530 scoped_ptr<base::SupportsUserData::Data> user_data_; |
523 }; | 531 }; |
524 | 532 |
| 533 class ShareableFileReferenceWaiter { |
| 534 public: |
| 535 ShareableFileReferenceWaiter(const base::FilePath& path) { |
| 536 scoped_refptr<ShareableFileReference> file = |
| 537 ShareableFileReference::Get(path); |
| 538 file->AddFinalReleaseCallback( |
| 539 base::Bind(&ShareableFileReferenceWaiter::Released, |
| 540 base::Unretained(this))); |
| 541 } |
| 542 |
| 543 void Wait() { |
| 544 loop_.Run(); |
| 545 } |
| 546 |
| 547 private: |
| 548 void Released(const base::FilePath& path) { |
| 549 loop_.Quit(); |
| 550 } |
| 551 |
| 552 base::RunLoop loop_; |
| 553 DISALLOW_COPY_AND_ASSIGN(ShareableFileReferenceWaiter); |
| 554 }; |
| 555 |
525 class ResourceDispatcherHostTest : public testing::Test, | 556 class ResourceDispatcherHostTest : public testing::Test, |
526 public IPC::Sender { | 557 public IPC::Sender { |
527 public: | 558 public: |
528 ResourceDispatcherHostTest() | 559 ResourceDispatcherHostTest() |
529 : ui_thread_(BrowserThread::UI, &message_loop_), | 560 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
530 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), | |
531 cache_thread_(BrowserThread::CACHE, &message_loop_), | |
532 io_thread_(BrowserThread::IO, &message_loop_), | |
533 old_factory_(NULL), | 561 old_factory_(NULL), |
534 send_data_received_acks_(false) { | 562 send_data_received_acks_(false) { |
535 browser_context_.reset(new TestBrowserContext()); | 563 browser_context_.reset(new TestBrowserContext()); |
536 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); | 564 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); |
537 message_loop_.RunUntilIdle(); | 565 base::RunLoop().RunUntilIdle(); |
538 ResourceContext* resource_context = browser_context_->GetResourceContext(); | 566 ResourceContext* resource_context = browser_context_->GetResourceContext(); |
539 filter_ = new ForwardingFilter(this, resource_context); | 567 filter_ = new ForwardingFilter(this, resource_context); |
540 resource_context->GetRequestContext()->set_network_delegate( | 568 resource_context->GetRequestContext()->set_network_delegate( |
541 &network_delegate_); | 569 &network_delegate_); |
542 } | 570 } |
543 | 571 |
544 virtual ~ResourceDispatcherHostTest() { | |
545 for (std::set<int>::iterator it = child_ids_.begin(); | |
546 it != child_ids_.end(); ++it) { | |
547 host_.CancelRequestsForProcess(*it); | |
548 } | |
549 } | |
550 | |
551 // IPC::Sender implementation | 572 // IPC::Sender implementation |
552 virtual bool Send(IPC::Message* msg) OVERRIDE { | 573 virtual bool Send(IPC::Message* msg) OVERRIDE { |
553 accum_.AddMessage(*msg); | 574 accum_.AddMessage(*msg); |
554 | 575 |
555 if (send_data_received_acks_ && | 576 if (send_data_received_acks_ && |
556 msg->type() == ResourceMsg_DataReceived::ID) { | 577 msg->type() == ResourceMsg_DataReceived::ID) { |
557 GenerateDataReceivedACK(*msg); | 578 GenerateDataReceivedACK(*msg); |
558 } | 579 } |
559 | 580 |
| 581 if (wait_for_request_complete_loop_ && |
| 582 msg->type() == ResourceMsg_RequestComplete::ID) { |
| 583 wait_for_request_complete_loop_->Quit(); |
| 584 } |
| 585 |
560 delete msg; | 586 delete msg; |
561 return true; | 587 return true; |
562 } | 588 } |
563 | 589 |
564 protected: | 590 protected: |
565 // testing::Test | 591 // testing::Test |
566 virtual void SetUp() { | 592 virtual void SetUp() { |
567 DCHECK(!test_fixture_); | 593 DCHECK(!test_fixture_); |
568 test_fixture_ = this; | 594 test_fixture_ = this; |
569 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); | 595 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); |
(...skipping 11 matching lines...) Expand all Loading... |
581 if (!scheme_.empty()) | 607 if (!scheme_.empty()) |
582 net::URLRequest::Deprecated::RegisterProtocolFactory( | 608 net::URLRequest::Deprecated::RegisterProtocolFactory( |
583 scheme_, old_factory_); | 609 scheme_, old_factory_); |
584 | 610 |
585 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); | 611 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); |
586 URLRequestTestDelayedStartJob::ClearQueue(); | 612 URLRequestTestDelayedStartJob::ClearQueue(); |
587 | 613 |
588 DCHECK(test_fixture_ == this); | 614 DCHECK(test_fixture_ == this); |
589 test_fixture_ = NULL; | 615 test_fixture_ = NULL; |
590 | 616 |
| 617 for (std::set<int>::iterator it = child_ids_.begin(); |
| 618 it != child_ids_.end(); ++it) { |
| 619 host_.CancelRequestsForProcess(*it); |
| 620 } |
| 621 |
591 host_.Shutdown(); | 622 host_.Shutdown(); |
592 | 623 |
593 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0); | 624 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0); |
594 | 625 |
595 // Flush the message loop to make application verifiers happy. | 626 // Flush the message loop to make application verifiers happy. |
596 if (ResourceDispatcherHostImpl::Get()) | 627 if (ResourceDispatcherHostImpl::Get()) |
597 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext( | 628 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext( |
598 browser_context_->GetResourceContext()); | 629 browser_context_->GetResourceContext()); |
599 | 630 |
600 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting(); | 631 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting(); |
601 | 632 |
602 browser_context_.reset(); | 633 browser_context_.reset(); |
603 message_loop_.RunUntilIdle(); | 634 base::RunLoop().RunUntilIdle(); |
604 } | 635 } |
605 | 636 |
606 // Creates a request using the current test object as the filter and | 637 // Creates a request using the current test object as the filter and |
607 // SubResource as the resource type. | 638 // SubResource as the resource type. |
608 void MakeTestRequest(int render_view_id, | 639 void MakeTestRequest(int render_view_id, |
609 int request_id, | 640 int request_id, |
610 const GURL& url); | 641 const GURL& url); |
611 | 642 |
612 // Generates a request using the given filter and resource type. | 643 // Generates a request using the given filter and resource type. |
613 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter, | 644 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter, |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 bool result = PickleIterator(msg).ReadInt(&request_id); | 741 bool result = PickleIterator(msg).ReadInt(&request_id); |
711 DCHECK(result); | 742 DCHECK(result); |
712 scoped_ptr<IPC::Message> ack( | 743 scoped_ptr<IPC::Message> ack( |
713 new ResourceHostMsg_DataReceived_ACK(request_id)); | 744 new ResourceHostMsg_DataReceived_ACK(request_id)); |
714 | 745 |
715 base::MessageLoop::current()->PostTask( | 746 base::MessageLoop::current()->PostTask( |
716 FROM_HERE, | 747 FROM_HERE, |
717 base::Bind(&GenerateIPCMessage, filter_, base::Passed(&ack))); | 748 base::Bind(&GenerateIPCMessage, filter_, base::Passed(&ack))); |
718 } | 749 } |
719 | 750 |
720 base::MessageLoopForIO message_loop_; | 751 void WaitForRequestComplete() { |
721 BrowserThreadImpl ui_thread_; | 752 DCHECK(!wait_for_request_complete_loop_); |
722 BrowserThreadImpl file_thread_; | 753 wait_for_request_complete_loop_.reset(new base::RunLoop); |
723 BrowserThreadImpl cache_thread_; | 754 wait_for_request_complete_loop_->Run(); |
724 BrowserThreadImpl io_thread_; | 755 wait_for_request_complete_loop_.reset(); |
| 756 } |
| 757 |
| 758 content::TestBrowserThreadBundle thread_bundle_; |
725 scoped_ptr<TestBrowserContext> browser_context_; | 759 scoped_ptr<TestBrowserContext> browser_context_; |
726 scoped_refptr<ForwardingFilter> filter_; | 760 scoped_refptr<ForwardingFilter> filter_; |
727 net::TestNetworkDelegate network_delegate_; | 761 net::TestNetworkDelegate network_delegate_; |
728 ResourceDispatcherHostImpl host_; | 762 ResourceDispatcherHostImpl host_; |
729 ResourceIPCAccumulator accum_; | 763 ResourceIPCAccumulator accum_; |
730 std::string response_headers_; | 764 std::string response_headers_; |
731 std::string response_data_; | 765 std::string response_data_; |
732 std::string scheme_; | 766 std::string scheme_; |
733 net::URLRequest::ProtocolFactory* old_factory_; | 767 net::URLRequest::ProtocolFactory* old_factory_; |
734 bool send_data_received_acks_; | 768 bool send_data_received_acks_; |
735 std::set<int> child_ids_; | 769 std::set<int> child_ids_; |
| 770 scoped_ptr<base::RunLoop> wait_for_request_complete_loop_; |
736 static ResourceDispatcherHostTest* test_fixture_; | 771 static ResourceDispatcherHostTest* test_fixture_; |
737 static bool delay_start_; | 772 static bool delay_start_; |
738 static bool delay_complete_; | 773 static bool delay_complete_; |
739 static int url_request_jobs_created_count_; | 774 static int url_request_jobs_created_count_; |
740 }; | 775 }; |
741 // Static. | 776 // Static. |
742 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; | 777 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; |
743 bool ResourceDispatcherHostTest::delay_start_ = false; | 778 bool ResourceDispatcherHostTest::delay_start_ = false; |
744 bool ResourceDispatcherHostTest::delay_complete_ = false; | 779 bool ResourceDispatcherHostTest::delay_complete_ = false; |
745 int ResourceDispatcherHostTest::url_request_jobs_created_count_ = 0; | 780 int ResourceDispatcherHostTest::url_request_jobs_created_count_ = 0; |
(...skipping 1855 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2601 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok); | 2636 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok); |
2602 } | 2637 } |
2603 | 2638 |
2604 base::MessageLoop::current()->RunUntilIdle(); | 2639 base::MessageLoop::current()->RunUntilIdle(); |
2605 | 2640 |
2606 msgs.clear(); | 2641 msgs.clear(); |
2607 accum_.GetClassifiedMessages(&msgs); | 2642 accum_.GetClassifiedMessages(&msgs); |
2608 } | 2643 } |
2609 } | 2644 } |
2610 | 2645 |
| 2646 // Tests the dispatcher host's temporary file management. |
| 2647 TEST_F(ResourceDispatcherHostTest, RegisterDownloadedTempFile) { |
| 2648 const int kRequestID = 1; |
| 2649 |
| 2650 // Create a temporary file. |
| 2651 base::FilePath file_path; |
| 2652 ASSERT_TRUE(base::CreateTemporaryFile(&file_path)); |
| 2653 scoped_refptr<ShareableFileReference> deletable_file = |
| 2654 ShareableFileReference::GetOrCreate( |
| 2655 file_path, |
| 2656 ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
| 2657 BrowserThread::GetMessageLoopProxyForThread( |
| 2658 BrowserThread::FILE).get()); |
| 2659 |
| 2660 // Not readable. |
| 2661 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2662 filter_->child_id(), file_path)); |
| 2663 |
| 2664 // Register it for a resource request. |
| 2665 host_.RegisterDownloadedTempFile(filter_->child_id(), kRequestID, file_path); |
| 2666 |
| 2667 // Should be readable now. |
| 2668 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2669 filter_->child_id(), file_path)); |
| 2670 |
| 2671 // The child releases from the request. |
| 2672 bool msg_was_ok = true; |
| 2673 ResourceHostMsg_ReleaseDownloadedFile release_msg(kRequestID); |
| 2674 host_.OnMessageReceived(release_msg, filter_, &msg_was_ok); |
| 2675 ASSERT_TRUE(msg_was_ok); |
| 2676 |
| 2677 // Still readable because there is another reference to the file. (The child |
| 2678 // may take additional blob references.) |
| 2679 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2680 filter_->child_id(), file_path)); |
| 2681 |
| 2682 // Release extra references. |
| 2683 deletable_file = NULL; |
| 2684 base::MessageLoop::current()->RunUntilIdle(); |
| 2685 |
| 2686 // The file is no longer readable to the child and has been deleted. |
| 2687 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2688 filter_->child_id(), file_path)); |
| 2689 EXPECT_FALSE(base::PathExists(file_path)); |
| 2690 } |
| 2691 |
| 2692 // Tests that temporary files held on behalf of child processes are released |
| 2693 // when the child process dies. |
| 2694 TEST_F(ResourceDispatcherHostTest, ReleaseTemporiesOnProcessExit) { |
| 2695 const int kRequestID = 1; |
| 2696 |
| 2697 // Create a temporary file. |
| 2698 base::FilePath file_path; |
| 2699 ASSERT_TRUE(base::CreateTemporaryFile(&file_path)); |
| 2700 scoped_refptr<ShareableFileReference> deletable_file = |
| 2701 ShareableFileReference::GetOrCreate( |
| 2702 file_path, |
| 2703 ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
| 2704 BrowserThread::GetMessageLoopProxyForThread( |
| 2705 BrowserThread::FILE).get()); |
| 2706 |
| 2707 // Register it for a resource request. |
| 2708 host_.RegisterDownloadedTempFile(filter_->child_id(), kRequestID, file_path); |
| 2709 deletable_file = NULL; |
| 2710 |
| 2711 // Should be readable now. |
| 2712 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2713 filter_->child_id(), file_path)); |
| 2714 |
| 2715 // Let the process die. |
| 2716 filter_->OnChannelClosing(); |
| 2717 base::MessageLoop::current()->RunUntilIdle(); |
| 2718 |
| 2719 // The file is no longer readable to the child and has been deleted. |
| 2720 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2721 filter_->child_id(), file_path)); |
| 2722 EXPECT_FALSE(base::PathExists(file_path)); |
| 2723 } |
| 2724 |
| 2725 TEST_F(ResourceDispatcherHostTest, DownloadToFile) { |
| 2726 // Make a request which downloads to file. |
| 2727 ResourceHostMsg_Request request = CreateResourceRequest( |
| 2728 "GET", ResourceType::SUB_RESOURCE, net::URLRequestTestJob::test_url_1()); |
| 2729 request.download_to_file = true; |
| 2730 ResourceHostMsg_RequestResource request_msg(0, 1, request); |
| 2731 bool msg_was_ok; |
| 2732 host_.OnMessageReceived(request_msg, filter_, &msg_was_ok); |
| 2733 ASSERT_TRUE(msg_was_ok); |
| 2734 |
| 2735 // Running the message loop until idle does not work because |
| 2736 // RedirectToFileResourceHandler posts things to base::WorkerPool. Instead, |
| 2737 // wait for the ResourceMsg_RequestComplete to go out. Then run the event loop |
| 2738 // until idle so the loader is gone. |
| 2739 WaitForRequestComplete(); |
| 2740 base::MessageLoop::current()->RunUntilIdle(); |
| 2741 EXPECT_EQ(0, host_.pending_requests()); |
| 2742 |
| 2743 ResourceIPCAccumulator::ClassifiedMessages msgs; |
| 2744 accum_.GetClassifiedMessages(&msgs); |
| 2745 |
| 2746 ASSERT_EQ(1U, msgs.size()); |
| 2747 const std::vector<IPC::Message>& messages = msgs[0]; |
| 2748 |
| 2749 // The request should contain the following messages: |
| 2750 // ReceivedResponse (indicates headers received and filename) |
| 2751 // DataDownloaded* (bytes downloaded and total length) |
| 2752 // RequestComplete (request is done) |
| 2753 |
| 2754 // ReceivedResponse |
| 2755 ResourceResponseHead response_head; |
| 2756 GetResponseHead(messages, &response_head); |
| 2757 ASSERT_FALSE(response_head.download_file_path.empty()); |
| 2758 |
| 2759 // DataDownloaded |
| 2760 size_t total_len = 0; |
| 2761 for (size_t i = 1; i < messages.size() - 1; i++) { |
| 2762 ASSERT_EQ(ResourceMsg_DataDownloaded::ID, messages[i].type()); |
| 2763 PickleIterator iter(messages[i]); |
| 2764 int request_id, data_len; |
| 2765 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &request_id)); |
| 2766 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &data_len)); |
| 2767 total_len += data_len; |
| 2768 } |
| 2769 EXPECT_EQ(net::URLRequestTestJob::test_data_1().size(), total_len); |
| 2770 |
| 2771 // RequestComplete |
| 2772 CheckRequestCompleteErrorCode(messages.back(), net::OK); |
| 2773 |
| 2774 // Verify that the data ended up in the temporary file. |
| 2775 std::string contents; |
| 2776 ASSERT_TRUE(base::ReadFileToString(response_head.download_file_path, |
| 2777 &contents)); |
| 2778 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), contents); |
| 2779 |
| 2780 // The file should be readable by the child. |
| 2781 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2782 filter_->child_id(), response_head.download_file_path)); |
| 2783 |
| 2784 // When the renderer releases the file, it should be deleted. Again, |
| 2785 // RunUntilIdle doesn't work because base::WorkerPool is involved. |
| 2786 ShareableFileReferenceWaiter waiter(response_head.download_file_path); |
| 2787 ResourceHostMsg_ReleaseDownloadedFile release_msg(1); |
| 2788 host_.OnMessageReceived(release_msg, filter_, &msg_was_ok); |
| 2789 ASSERT_TRUE(msg_was_ok); |
| 2790 waiter.Wait(); |
| 2791 // The release callback runs before the delete is scheduled, so pump the |
| 2792 // message loop for the delete itself. |
| 2793 base::MessageLoop::current()->RunUntilIdle(); |
| 2794 |
| 2795 EXPECT_FALSE(base::PathExists(response_head.download_file_path)); |
| 2796 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 2797 filter_->child_id(), response_head.download_file_path)); |
| 2798 } |
| 2799 |
2611 } // namespace content | 2800 } // namespace content |
OLD | NEW |