OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 <map> | 5 #include <map> |
6 #include <queue> | 6 #include <queue> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/location.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/single_thread_task_runner.h" | |
15 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
16 #include "base/thread_task_runner_handle.h" | |
17 #include "content/browser/quota/mock_quota_manager.h" | 15 #include "content/browser/quota/mock_quota_manager.h" |
18 #include "content/browser/quota/mock_quota_manager_proxy.h" | 16 #include "content/browser/quota/mock_quota_manager_proxy.h" |
19 #include "content/public/test/async_file_test_helper.h" | 17 #include "content/public/test/async_file_test_helper.h" |
20 #include "content/public/test/test_file_system_backend.h" | 18 #include "content/public/test/test_file_system_backend.h" |
21 #include "content/public/test/test_file_system_context.h" | 19 #include "content/public/test/test_file_system_context.h" |
22 #include "content/test/fileapi_test_file_set.h" | 20 #include "content/test/fileapi_test_file_set.h" |
23 #include "storage/browser/fileapi/copy_or_move_file_validator.h" | 21 #include "storage/browser/fileapi/copy_or_move_file_validator.h" |
24 #include "storage/browser/fileapi/copy_or_move_operation_delegate.h" | 22 #include "storage/browser/fileapi/copy_or_move_operation_delegate.h" |
25 #include "storage/browser/fileapi/file_stream_reader.h" | 23 #include "storage/browser/fileapi/file_stream_reader.h" |
26 #include "storage/browser/fileapi/file_stream_writer.h" | 24 #include "storage/browser/fileapi/file_stream_writer.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 base::File::FILE_ERROR_SECURITY), | 72 base::File::FILE_ERROR_SECURITY), |
75 write_result_(post_copy_valid ? base::File::FILE_OK : | 73 write_result_(post_copy_valid ? base::File::FILE_OK : |
76 base::File::FILE_ERROR_SECURITY), | 74 base::File::FILE_ERROR_SECURITY), |
77 reject_string_(reject_string) { | 75 reject_string_(reject_string) { |
78 } | 76 } |
79 ~TestValidator() override {} | 77 ~TestValidator() override {} |
80 | 78 |
81 void StartPreWriteValidation( | 79 void StartPreWriteValidation( |
82 const ResultCallback& result_callback) override { | 80 const ResultCallback& result_callback) override { |
83 // Post the result since a real validator must do work asynchronously. | 81 // Post the result since a real validator must do work asynchronously. |
84 base::ThreadTaskRunnerHandle::Get()->PostTask( | 82 base::MessageLoop::current()->PostTask( |
85 FROM_HERE, base::Bind(result_callback, result_)); | 83 FROM_HERE, base::Bind(result_callback, result_)); |
86 } | 84 } |
87 | 85 |
88 void StartPostWriteValidation( | 86 void StartPostWriteValidation( |
89 const base::FilePath& dest_platform_path, | 87 const base::FilePath& dest_platform_path, |
90 const ResultCallback& result_callback) override { | 88 const ResultCallback& result_callback) override { |
91 base::File::Error result = write_result_; | 89 base::File::Error result = write_result_; |
92 std::string unsafe = dest_platform_path.BaseName().AsUTF8Unsafe(); | 90 std::string unsafe = dest_platform_path.BaseName().AsUTF8Unsafe(); |
93 if (unsafe.find(reject_string_) != std::string::npos) { | 91 if (unsafe.find(reject_string_) != std::string::npos) { |
94 result = base::File::FILE_ERROR_SECURITY; | 92 result = base::File::FILE_ERROR_SECURITY; |
95 } | 93 } |
96 // Post the result since a real validator must do work asynchronously. | 94 // Post the result since a real validator must do work asynchronously. |
97 base::ThreadTaskRunnerHandle::Get()->PostTask( | 95 base::MessageLoop::current()->PostTask( |
98 FROM_HERE, base::Bind(result_callback, result)); | 96 FROM_HERE, base::Bind(result_callback, result)); |
99 } | 97 } |
100 | 98 |
101 private: | 99 private: |
102 base::File::Error result_; | 100 base::File::Error result_; |
103 base::File::Error write_result_; | 101 base::File::Error write_result_; |
104 std::string reject_string_; | 102 std::string reject_string_; |
105 | 103 |
106 DISALLOW_COPY_AND_ASSIGN(TestValidator); | 104 DISALLOW_COPY_AND_ASSIGN(TestValidator); |
107 }; | 105 }; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 140 |
143 class ScopedThreadStopper { | 141 class ScopedThreadStopper { |
144 public: | 142 public: |
145 ScopedThreadStopper(base::Thread* thread) : thread_(thread) { | 143 ScopedThreadStopper(base::Thread* thread) : thread_(thread) { |
146 } | 144 } |
147 | 145 |
148 ~ScopedThreadStopper() { | 146 ~ScopedThreadStopper() { |
149 if (thread_) { | 147 if (thread_) { |
150 // Give another chance for deleted streams to perform Close. | 148 // Give another chance for deleted streams to perform Close. |
151 base::RunLoop run_loop; | 149 base::RunLoop run_loop; |
152 thread_->task_runner()->PostTaskAndReply( | 150 thread_->message_loop_proxy()->PostTaskAndReply( |
153 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure()); | 151 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure()); |
154 run_loop.Run(); | 152 run_loop.Run(); |
155 thread_->Stop(); | 153 thread_->Stop(); |
156 } | 154 } |
157 } | 155 } |
158 | 156 |
159 bool is_valid() const { return thread_; } | 157 bool is_valid() const { return thread_; } |
160 | 158 |
161 private: | 159 private: |
162 base::Thread* thread_; | 160 base::Thread* thread_; |
(...skipping 23 matching lines...) Expand all Loading... |
186 | 184 |
187 void SetUpNoValidator() { | 185 void SetUpNoValidator() { |
188 SetUp(true, false); | 186 SetUp(true, false); |
189 } | 187 } |
190 | 188 |
191 void SetUp(bool require_copy_or_move_validator, | 189 void SetUp(bool require_copy_or_move_validator, |
192 bool init_copy_or_move_validator) { | 190 bool init_copy_or_move_validator) { |
193 ASSERT_TRUE(base_.CreateUniqueTempDir()); | 191 ASSERT_TRUE(base_.CreateUniqueTempDir()); |
194 base::FilePath base_dir = base_.path(); | 192 base::FilePath base_dir = base_.path(); |
195 quota_manager_ = | 193 quota_manager_ = |
196 new MockQuotaManager(false /* is_incognito */, base_dir, | 194 new MockQuotaManager(false /* is_incognito */, |
197 base::ThreadTaskRunnerHandle::Get().get(), | 195 base_dir, |
198 base::ThreadTaskRunnerHandle::Get().get(), | 196 base::MessageLoopProxy::current().get(), |
199 NULL /* special storage policy */); | 197 base::MessageLoopProxy::current().get(), |
| 198 NULL /* special storage policy */); |
200 quota_manager_proxy_ = new MockQuotaManagerProxy( | 199 quota_manager_proxy_ = new MockQuotaManagerProxy( |
201 quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); | 200 quota_manager_.get(), base::MessageLoopProxy::current().get()); |
202 file_system_context_ = | 201 file_system_context_ = |
203 CreateFileSystemContextForTesting(quota_manager_proxy_.get(), base_dir); | 202 CreateFileSystemContextForTesting(quota_manager_proxy_.get(), base_dir); |
204 | 203 |
205 // Prepare the origin's root directory. | 204 // Prepare the origin's root directory. |
206 storage::FileSystemBackend* backend = | 205 storage::FileSystemBackend* backend = |
207 file_system_context_->GetFileSystemBackend(src_type_); | 206 file_system_context_->GetFileSystemBackend(src_type_); |
208 backend->ResolveURL( | 207 backend->ResolveURL( |
209 FileSystemURL::CreateForTest(origin_, src_type_, base::FilePath()), | 208 FileSystemURL::CreateForTest(origin_, src_type_, base::FilePath()), |
210 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 209 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
211 base::Bind(&ExpectOk)); | 210 base::Bind(&ExpectOk)); |
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; | 721 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; |
723 base::WriteFile(source_path, kTestData, | 722 base::WriteFile(source_path, kTestData, |
724 arraysize(kTestData) - 1); // Exclude trailing '\0'. | 723 arraysize(kTestData) - 1); // Exclude trailing '\0'. |
725 | 724 |
726 base::MessageLoopForIO message_loop; | 725 base::MessageLoopForIO message_loop; |
727 base::Thread file_thread("file_thread"); | 726 base::Thread file_thread("file_thread"); |
728 ASSERT_TRUE(file_thread.Start()); | 727 ASSERT_TRUE(file_thread.Start()); |
729 ScopedThreadStopper thread_stopper(&file_thread); | 728 ScopedThreadStopper thread_stopper(&file_thread); |
730 ASSERT_TRUE(thread_stopper.is_valid()); | 729 ASSERT_TRUE(thread_stopper.is_valid()); |
731 | 730 |
732 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | 731 scoped_refptr<base::MessageLoopProxy> task_runner = |
733 file_thread.task_runner(); | 732 file_thread.message_loop_proxy(); |
734 | 733 |
735 scoped_ptr<storage::FileStreamReader> reader( | 734 scoped_ptr<storage::FileStreamReader> reader( |
736 storage::FileStreamReader::CreateForLocalFile( | 735 storage::FileStreamReader::CreateForLocalFile( |
737 task_runner.get(), source_path, 0, base::Time())); | 736 task_runner.get(), source_path, 0, base::Time())); |
738 | 737 |
739 scoped_ptr<FileStreamWriter> writer(FileStreamWriter::CreateForLocalFile( | 738 scoped_ptr<FileStreamWriter> writer(FileStreamWriter::CreateForLocalFile( |
740 task_runner.get(), dest_path, 0, FileStreamWriter::CREATE_NEW_FILE)); | 739 task_runner.get(), dest_path, 0, FileStreamWriter::CREATE_NEW_FILE)); |
741 | 740 |
742 std::vector<int64> progress; | 741 std::vector<int64> progress; |
743 CopyOrMoveOperationDelegate::StreamCopyHelper helper( | 742 CopyOrMoveOperationDelegate::StreamCopyHelper helper( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
778 base::WriteFile(source_path, kTestData, | 777 base::WriteFile(source_path, kTestData, |
779 arraysize(kTestData) - 1); // Exclude trailing '\0'. | 778 arraysize(kTestData) - 1); // Exclude trailing '\0'. |
780 | 779 |
781 | 780 |
782 base::MessageLoopForIO message_loop; | 781 base::MessageLoopForIO message_loop; |
783 base::Thread file_thread("file_thread"); | 782 base::Thread file_thread("file_thread"); |
784 ASSERT_TRUE(file_thread.Start()); | 783 ASSERT_TRUE(file_thread.Start()); |
785 ScopedThreadStopper thread_stopper(&file_thread); | 784 ScopedThreadStopper thread_stopper(&file_thread); |
786 ASSERT_TRUE(thread_stopper.is_valid()); | 785 ASSERT_TRUE(thread_stopper.is_valid()); |
787 | 786 |
788 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | 787 scoped_refptr<base::MessageLoopProxy> task_runner = |
789 file_thread.task_runner(); | 788 file_thread.message_loop_proxy(); |
790 | 789 |
791 scoped_ptr<storage::FileStreamReader> reader( | 790 scoped_ptr<storage::FileStreamReader> reader( |
792 storage::FileStreamReader::CreateForLocalFile( | 791 storage::FileStreamReader::CreateForLocalFile( |
793 task_runner.get(), source_path, 0, base::Time())); | 792 task_runner.get(), source_path, 0, base::Time())); |
794 | 793 |
795 scoped_ptr<FileStreamWriter> writer(FileStreamWriter::CreateForLocalFile( | 794 scoped_ptr<FileStreamWriter> writer(FileStreamWriter::CreateForLocalFile( |
796 task_runner.get(), dest_path, 0, FileStreamWriter::CREATE_NEW_FILE)); | 795 task_runner.get(), dest_path, 0, FileStreamWriter::CREATE_NEW_FILE)); |
797 | 796 |
798 std::vector<int64> progress; | 797 std::vector<int64> progress; |
799 CopyOrMoveOperationDelegate::StreamCopyHelper helper( | 798 CopyOrMoveOperationDelegate::StreamCopyHelper helper( |
(...skipping 29 matching lines...) Expand all Loading... |
829 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; | 828 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; |
830 base::WriteFile(source_path, kTestData, | 829 base::WriteFile(source_path, kTestData, |
831 arraysize(kTestData) - 1); // Exclude trailing '\0'. | 830 arraysize(kTestData) - 1); // Exclude trailing '\0'. |
832 | 831 |
833 base::MessageLoopForIO message_loop; | 832 base::MessageLoopForIO message_loop; |
834 base::Thread file_thread("file_thread"); | 833 base::Thread file_thread("file_thread"); |
835 ASSERT_TRUE(file_thread.Start()); | 834 ASSERT_TRUE(file_thread.Start()); |
836 ScopedThreadStopper thread_stopper(&file_thread); | 835 ScopedThreadStopper thread_stopper(&file_thread); |
837 ASSERT_TRUE(thread_stopper.is_valid()); | 836 ASSERT_TRUE(thread_stopper.is_valid()); |
838 | 837 |
839 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | 838 scoped_refptr<base::MessageLoopProxy> task_runner = |
840 file_thread.task_runner(); | 839 file_thread.message_loop_proxy(); |
841 | 840 |
842 scoped_ptr<storage::FileStreamReader> reader( | 841 scoped_ptr<storage::FileStreamReader> reader( |
843 storage::FileStreamReader::CreateForLocalFile( | 842 storage::FileStreamReader::CreateForLocalFile( |
844 task_runner.get(), source_path, 0, base::Time())); | 843 task_runner.get(), source_path, 0, base::Time())); |
845 | 844 |
846 scoped_ptr<FileStreamWriter> writer(FileStreamWriter::CreateForLocalFile( | 845 scoped_ptr<FileStreamWriter> writer(FileStreamWriter::CreateForLocalFile( |
847 task_runner.get(), dest_path, 0, FileStreamWriter::CREATE_NEW_FILE)); | 846 task_runner.get(), dest_path, 0, FileStreamWriter::CREATE_NEW_FILE)); |
848 | 847 |
849 std::vector<int64> progress; | 848 std::vector<int64> progress; |
850 CopyOrMoveOperationDelegate::StreamCopyHelper helper( | 849 CopyOrMoveOperationDelegate::StreamCopyHelper helper( |
851 reader.Pass(), writer.Pass(), | 850 reader.Pass(), writer.Pass(), |
852 storage::FlushPolicy::NO_FLUSH_ON_COMPLETION, | 851 storage::FlushPolicy::NO_FLUSH_ON_COMPLETION, |
853 10, // buffer size | 852 10, // buffer size |
854 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), | 853 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), |
855 base::TimeDelta()); // For testing, we need all the progress. | 854 base::TimeDelta()); // For testing, we need all the progress. |
856 | 855 |
857 // Call Cancel() later. | 856 // Call Cancel() later. |
858 base::ThreadTaskRunnerHandle::Get()->PostTask( | 857 base::MessageLoopProxy::current()->PostTask( |
859 FROM_HERE, | 858 FROM_HERE, |
860 base::Bind(&CopyOrMoveOperationDelegate::StreamCopyHelper::Cancel, | 859 base::Bind(&CopyOrMoveOperationDelegate::StreamCopyHelper::Cancel, |
861 base::Unretained(&helper))); | 860 base::Unretained(&helper))); |
862 | 861 |
863 base::File::Error error = base::File::FILE_ERROR_FAILED; | 862 base::File::Error error = base::File::FILE_ERROR_FAILED; |
864 base::RunLoop run_loop; | 863 base::RunLoop run_loop; |
865 helper.Run(base::Bind(&AssignAndQuit, &run_loop, &error)); | 864 helper.Run(base::Bind(&AssignAndQuit, &run_loop, &error)); |
866 run_loop.Run(); | 865 run_loop.Run(); |
867 | 866 |
868 EXPECT_EQ(base::File::FILE_ERROR_ABORT, error); | 867 EXPECT_EQ(base::File::FILE_ERROR_ABORT, error); |
869 } | 868 } |
870 | 869 |
871 } // namespace content | 870 } // namespace content |
OLD | NEW |