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