OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/browser/loader/resource_loader.h" | 5 #include "content/browser/loader/resource_loader.h" |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/location.h" | |
10 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/single_thread_task_runner.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "content/browser/browser_thread_impl.h" | 12 #include "content/browser/browser_thread_impl.h" |
15 #include "content/browser/loader/redirect_to_file_resource_handler.h" | 13 #include "content/browser/loader/redirect_to_file_resource_handler.h" |
16 #include "content/browser/loader/resource_loader_delegate.h" | 14 #include "content/browser/loader/resource_loader_delegate.h" |
17 #include "content/public/browser/client_certificate_delegate.h" | 15 #include "content/public/browser/client_certificate_delegate.h" |
18 #include "content/public/browser/resource_request_info.h" | 16 #include "content/public/browser/resource_request_info.h" |
19 #include "content/public/common/content_paths.h" | 17 #include "content/public/common/content_paths.h" |
20 #include "content/public/common/resource_response.h" | 18 #include "content/public/common/resource_response.h" |
21 #include "content/public/test/mock_resource_context.h" | 19 #include "content/public/test/mock_resource_context.h" |
22 #include "content/public/test/test_browser_context.h" | 20 #include "content/public/test/test_browser_context.h" |
23 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // reset |loader| and then call the callback. The caller is responsible for | 97 // reset |loader| and then call the callback. The caller is responsible for |
100 // ensuring the pointers remain valid until the process is complete. | 98 // ensuring the pointers remain valid until the process is complete. |
101 explicit LoaderDestroyingCertStore(scoped_ptr<ResourceLoader>* loader) | 99 explicit LoaderDestroyingCertStore(scoped_ptr<ResourceLoader>* loader) |
102 : loader_(loader) {} | 100 : loader_(loader) {} |
103 | 101 |
104 // net::ClientCertStore: | 102 // net::ClientCertStore: |
105 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, | 103 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, |
106 net::CertificateList* selected_certs, | 104 net::CertificateList* selected_certs, |
107 const base::Closure& callback) override { | 105 const base::Closure& callback) override { |
108 // Don't destroy |loader_| while it's on the stack. | 106 // Don't destroy |loader_| while it's on the stack. |
109 base::ThreadTaskRunnerHandle::Get()->PostTask( | 107 base::MessageLoop::current()->PostTask( |
110 FROM_HERE, base::Bind(&LoaderDestroyingCertStore::DoCallback, | 108 FROM_HERE, base::Bind(&LoaderDestroyingCertStore::DoCallback, |
111 base::Unretained(loader_), callback)); | 109 base::Unretained(loader_), callback)); |
112 } | 110 } |
113 | 111 |
114 private: | 112 private: |
115 static void DoCallback(scoped_ptr<ResourceLoader>* loader, | 113 static void DoCallback(scoped_ptr<ResourceLoader>* loader, |
116 const base::Closure& callback) { | 114 const base::Closure& callback) { |
117 loader->reset(); | 115 loader->reset(); |
118 callback.Run(); | 116 callback.Run(); |
119 } | 117 } |
(...skipping 10 matching lines...) Expand all Loading... |
130 | 128 |
131 static std::vector<std::string> test_authorities() { | 129 static std::vector<std::string> test_authorities() { |
132 return std::vector<std::string>(1, "dummy"); | 130 return std::vector<std::string>(1, "dummy"); |
133 } | 131 } |
134 | 132 |
135 // net::URLRequestTestJob: | 133 // net::URLRequestTestJob: |
136 void Start() override { | 134 void Start() override { |
137 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( | 135 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( |
138 new net::SSLCertRequestInfo); | 136 new net::SSLCertRequestInfo); |
139 cert_request_info->cert_authorities = test_authorities(); | 137 cert_request_info->cert_authorities = test_authorities(); |
140 base::ThreadTaskRunnerHandle::Get()->PostTask( | 138 base::MessageLoop::current()->PostTask( |
141 FROM_HERE, | 139 FROM_HERE, |
142 base::Bind(&MockClientCertURLRequestJob::NotifyCertificateRequested, | 140 base::Bind(&MockClientCertURLRequestJob::NotifyCertificateRequested, |
143 this, cert_request_info)); | 141 this, cert_request_info)); |
144 } | 142 } |
145 | 143 |
146 void ContinueWithCertificate(net::X509Certificate* cert) override { | 144 void ContinueWithCertificate(net::X509Certificate* cert) override { |
147 net::URLRequestTestJob::Start(); | 145 net::URLRequestTestJob::Start(); |
148 } | 146 } |
149 | 147 |
150 private: | 148 private: |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 net::ChunkedUploadDataStream stream_; | 400 net::ChunkedUploadDataStream stream_; |
403 uint64 size_; | 401 uint64 size_; |
404 | 402 |
405 DISALLOW_COPY_AND_ASSIGN(NonChunkedUploadDataStream); | 403 DISALLOW_COPY_AND_ASSIGN(NonChunkedUploadDataStream); |
406 }; | 404 }; |
407 | 405 |
408 // Fails to create a temporary file with the given error. | 406 // Fails to create a temporary file with the given error. |
409 void CreateTemporaryError( | 407 void CreateTemporaryError( |
410 base::File::Error error, | 408 base::File::Error error, |
411 const CreateTemporaryFileStreamCallback& callback) { | 409 const CreateTemporaryFileStreamCallback& callback) { |
412 base::ThreadTaskRunnerHandle::Get()->PostTask( | 410 base::MessageLoop::current()->PostTask( |
413 FROM_HERE, | 411 FROM_HERE, |
414 base::Bind(callback, error, base::Passed(scoped_ptr<net::FileStream>()), | 412 base::Bind(callback, error, base::Passed(scoped_ptr<net::FileStream>()), |
415 scoped_refptr<ShareableFileReference>())); | 413 scoped_refptr<ShareableFileReference>())); |
416 } | 414 } |
417 | 415 |
418 } // namespace | 416 } // namespace |
419 | 417 |
420 class ResourceLoaderTest : public testing::Test, | 418 class ResourceLoaderTest : public testing::Test, |
421 public ResourceLoaderDelegate { | 419 public ResourceLoaderDelegate { |
422 protected: | 420 protected: |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 // Make a temporary file. | 772 // Make a temporary file. |
775 CHECK(base::CreateTemporaryFile(&temp_path_)); | 773 CHECK(base::CreateTemporaryFile(&temp_path_)); |
776 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | | 774 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | |
777 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; | 775 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; |
778 base::File file(temp_path_, flags); | 776 base::File file(temp_path_, flags); |
779 CHECK(file.IsValid()); | 777 CHECK(file.IsValid()); |
780 | 778 |
781 // Create mock file streams and a ShareableFileReference. | 779 // Create mock file streams and a ShareableFileReference. |
782 scoped_ptr<net::testing::MockFileStream> file_stream( | 780 scoped_ptr<net::testing::MockFileStream> file_stream( |
783 new net::testing::MockFileStream(file.Pass(), | 781 new net::testing::MockFileStream(file.Pass(), |
784 base::ThreadTaskRunnerHandle::Get())); | 782 base::MessageLoopProxy::current())); |
785 file_stream_ = file_stream.get(); | 783 file_stream_ = file_stream.get(); |
786 deletable_file_ = ShareableFileReference::GetOrCreate( | 784 deletable_file_ = ShareableFileReference::GetOrCreate( |
787 temp_path_, | 785 temp_path_, |
788 ShareableFileReference::DELETE_ON_FINAL_RELEASE, | 786 ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
789 BrowserThread::GetMessageLoopProxyForThread( | 787 BrowserThread::GetMessageLoopProxyForThread( |
790 BrowserThread::FILE).get()); | 788 BrowserThread::FILE).get()); |
791 | 789 |
792 // Inject them into the handler. | 790 // Inject them into the handler. |
793 scoped_ptr<RedirectToFileResourceHandler> handler( | 791 scoped_ptr<RedirectToFileResourceHandler> handler( |
794 new RedirectToFileResourceHandler(leaf_handler.Pass(), request)); | 792 new RedirectToFileResourceHandler(leaf_handler.Pass(), request)); |
795 redirect_to_file_resource_handler_ = handler.get(); | 793 redirect_to_file_resource_handler_ = handler.get(); |
796 handler->SetCreateTemporaryFileStreamFunctionForTesting( | 794 handler->SetCreateTemporaryFileStreamFunctionForTesting( |
797 base::Bind(&ResourceLoaderRedirectToFileTest::PostCallback, | 795 base::Bind(&ResourceLoaderRedirectToFileTest::PostCallback, |
798 base::Unretained(this), | 796 base::Unretained(this), |
799 base::Passed(&file_stream))); | 797 base::Passed(&file_stream))); |
800 return handler.Pass(); | 798 return handler.Pass(); |
801 } | 799 } |
802 | 800 |
803 private: | 801 private: |
804 void PostCallback( | 802 void PostCallback( |
805 scoped_ptr<net::FileStream> file_stream, | 803 scoped_ptr<net::FileStream> file_stream, |
806 const CreateTemporaryFileStreamCallback& callback) { | 804 const CreateTemporaryFileStreamCallback& callback) { |
807 base::ThreadTaskRunnerHandle::Get()->PostTask( | 805 base::MessageLoop::current()->PostTask( |
808 FROM_HERE, base::Bind(callback, base::File::FILE_OK, | 806 FROM_HERE, |
809 base::Passed(&file_stream), deletable_file_)); | 807 base::Bind(callback, base::File::FILE_OK, |
| 808 base::Passed(&file_stream), deletable_file_)); |
810 } | 809 } |
811 | 810 |
812 base::FilePath temp_path_; | 811 base::FilePath temp_path_; |
813 scoped_refptr<ShareableFileReference> deletable_file_; | 812 scoped_refptr<ShareableFileReference> deletable_file_; |
814 // These are owned by the ResourceLoader. | 813 // These are owned by the ResourceLoader. |
815 net::testing::MockFileStream* file_stream_; | 814 net::testing::MockFileStream* file_stream_; |
816 RedirectToFileResourceHandler* redirect_to_file_resource_handler_; | 815 RedirectToFileResourceHandler* redirect_to_file_resource_handler_; |
817 }; | 816 }; |
818 | 817 |
819 // Tests that a RedirectToFileResourceHandler works and forwards everything | 818 // Tests that a RedirectToFileResourceHandler works and forwards everything |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); | 986 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); |
988 EXPECT_EQ(test_data(), contents); | 987 EXPECT_EQ(test_data(), contents); |
989 | 988 |
990 // Release the loader. The file should be gone now. | 989 // Release the loader. The file should be gone now. |
991 ReleaseLoader(); | 990 ReleaseLoader(); |
992 base::RunLoop().RunUntilIdle(); | 991 base::RunLoop().RunUntilIdle(); |
993 EXPECT_FALSE(base::PathExists(temp_path())); | 992 EXPECT_FALSE(base::PathExists(temp_path())); |
994 } | 993 } |
995 | 994 |
996 } // namespace content | 995 } // namespace content |
OLD | NEW |