| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "storage/browser/blob/blob_reader.h" | |
| 6 | |
| 7 #include <stddef.h> | 5 #include <stddef.h> |
| 8 #include <stdint.h> | 6 #include <stdint.h> |
| 9 #include <string.h> | 7 #include <string.h> |
| 8 #include <utility> |
| 10 | 9 |
| 11 #include "base/bind.h" | 10 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 13 #include "base/callback.h" | 12 #include "base/callback.h" |
| 14 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 15 #include "base/location.h" | 14 #include "base/location.h" |
| 16 #include "base/macros.h" | 15 #include "base/macros.h" |
| 17 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 19 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 20 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 21 #include "base/task_runner.h" | 20 #include "base/task_runner.h" |
| 22 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 23 #include "content/public/test/async_file_test_helper.h" | 22 #include "content/public/test/async_file_test_helper.h" |
| 24 #include "content/public/test/test_file_system_context.h" | 23 #include "content/public/test/test_file_system_context.h" |
| 25 #include "net/base/completion_callback.h" | 24 #include "net/base/completion_callback.h" |
| 26 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
| 27 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 28 #include "net/base/test_completion_callback.h" | 27 #include "net/base/test_completion_callback.h" |
| 29 #include "net/disk_cache/disk_cache.h" | 28 #include "net/disk_cache/disk_cache.h" |
| 30 #include "storage/browser/blob/blob_data_builder.h" | 29 #include "storage/browser/blob/blob_data_builder.h" |
| 31 #include "storage/browser/blob/blob_data_handle.h" | 30 #include "storage/browser/blob/blob_data_handle.h" |
| 31 #include "storage/browser/blob/blob_reader.h" |
| 32 #include "storage/browser/blob/blob_storage_context.h" | 32 #include "storage/browser/blob/blob_storage_context.h" |
| 33 #include "storage/browser/fileapi/file_stream_reader.h" | 33 #include "storage/browser/fileapi/file_stream_reader.h" |
| 34 #include "storage/browser/fileapi/file_system_context.h" | 34 #include "storage/browser/fileapi/file_system_context.h" |
| 35 #include "storage/browser/fileapi/file_system_file_util.h" | 35 #include "storage/browser/fileapi/file_system_file_util.h" |
| 36 #include "testing/gmock/include/gmock/gmock.h" | 36 #include "testing/gmock/include/gmock/gmock.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 38 #include "url/gurl.h" | 38 #include "url/gurl.h" |
| 39 | 39 |
| 40 using base::FilePath; | 40 using base::FilePath; |
| 41 using content::AsyncFileTestHelper; | 41 using content::AsyncFileTestHelper; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 53 private: | 53 private: |
| 54 ~EmptyDataHandle() override {} | 54 ~EmptyDataHandle() override {} |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 // A disk_cache::Entry that arbitrarily delays the completion of a read | 57 // A disk_cache::Entry that arbitrarily delays the completion of a read |
| 58 // operation to allow testing some races without flake. This is particularly | 58 // operation to allow testing some races without flake. This is particularly |
| 59 // relevant in this unit test, which uses the always-synchronous MEMORY_CACHE. | 59 // relevant in this unit test, which uses the always-synchronous MEMORY_CACHE. |
| 60 class DelayedReadEntry : public disk_cache::Entry { | 60 class DelayedReadEntry : public disk_cache::Entry { |
| 61 public: | 61 public: |
| 62 explicit DelayedReadEntry(disk_cache::ScopedEntryPtr entry) | 62 explicit DelayedReadEntry(disk_cache::ScopedEntryPtr entry) |
| 63 : entry_(entry.Pass()) {} | 63 : entry_(std::move(entry)) {} |
| 64 ~DelayedReadEntry() override { EXPECT_FALSE(HasPendingReadCallbacks()); } | 64 ~DelayedReadEntry() override { EXPECT_FALSE(HasPendingReadCallbacks()); } |
| 65 | 65 |
| 66 bool HasPendingReadCallbacks() { return !pending_read_callbacks_.empty(); } | 66 bool HasPendingReadCallbacks() { return !pending_read_callbacks_.empty(); } |
| 67 | 67 |
| 68 void RunPendingReadCallbacks() { | 68 void RunPendingReadCallbacks() { |
| 69 std::vector<base::Callback<void(void)>> callbacks; | 69 std::vector<base::Callback<void(void)>> callbacks; |
| 70 pending_read_callbacks_.swap(callbacks); | 70 pending_read_callbacks_.swap(callbacks); |
| 71 for (const auto& callback : callbacks) | 71 for (const auto& callback : callbacks) |
| 72 callback.Run(); | 72 callback.Run(); |
| 73 } | 73 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 147 |
| 148 scoped_ptr<disk_cache::Backend> CreateInMemoryDiskCache( | 148 scoped_ptr<disk_cache::Backend> CreateInMemoryDiskCache( |
| 149 const scoped_refptr<base::SingleThreadTaskRunner>& thread) { | 149 const scoped_refptr<base::SingleThreadTaskRunner>& thread) { |
| 150 scoped_ptr<disk_cache::Backend> cache; | 150 scoped_ptr<disk_cache::Backend> cache; |
| 151 net::TestCompletionCallback callback; | 151 net::TestCompletionCallback callback; |
| 152 int rv = disk_cache::CreateCacheBackend( | 152 int rv = disk_cache::CreateCacheBackend( |
| 153 net::MEMORY_CACHE, net::CACHE_BACKEND_DEFAULT, FilePath(), 0, false, | 153 net::MEMORY_CACHE, net::CACHE_BACKEND_DEFAULT, FilePath(), 0, false, |
| 154 thread, nullptr, &cache, callback.callback()); | 154 thread, nullptr, &cache, callback.callback()); |
| 155 EXPECT_EQ(net::OK, callback.GetResult(rv)); | 155 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 156 | 156 |
| 157 return cache.Pass(); | 157 return cache; |
| 158 } | 158 } |
| 159 | 159 |
| 160 disk_cache::ScopedEntryPtr CreateDiskCacheEntry(disk_cache::Backend* cache, | 160 disk_cache::ScopedEntryPtr CreateDiskCacheEntry(disk_cache::Backend* cache, |
| 161 const char* key, | 161 const char* key, |
| 162 const std::string& data) { | 162 const std::string& data) { |
| 163 disk_cache::Entry* temp_entry = nullptr; | 163 disk_cache::Entry* temp_entry = nullptr; |
| 164 net::TestCompletionCallback callback; | 164 net::TestCompletionCallback callback; |
| 165 int rv = cache->CreateEntry(key, &temp_entry, callback.callback()); | 165 int rv = cache->CreateEntry(key, &temp_entry, callback.callback()); |
| 166 if (callback.GetResult(rv) != net::OK) | 166 if (callback.GetResult(rv) != net::OK) |
| 167 return nullptr; | 167 return nullptr; |
| 168 disk_cache::ScopedEntryPtr entry(temp_entry); | 168 disk_cache::ScopedEntryPtr entry(temp_entry); |
| 169 | 169 |
| 170 scoped_refptr<net::StringIOBuffer> iobuffer = new net::StringIOBuffer(data); | 170 scoped_refptr<net::StringIOBuffer> iobuffer = new net::StringIOBuffer(data); |
| 171 rv = entry->WriteData(kTestDiskCacheStreamIndex, 0, iobuffer.get(), | 171 rv = entry->WriteData(kTestDiskCacheStreamIndex, 0, iobuffer.get(), |
| 172 iobuffer->size(), callback.callback(), false); | 172 iobuffer->size(), callback.callback(), false); |
| 173 EXPECT_EQ(static_cast<int>(data.size()), callback.GetResult(rv)); | 173 EXPECT_EQ(static_cast<int>(data.size()), callback.GetResult(rv)); |
| 174 return entry.Pass(); | 174 return entry; |
| 175 } | 175 } |
| 176 | 176 |
| 177 template <typename T> | 177 template <typename T> |
| 178 void SetValue(T* address, T value) { | 178 void SetValue(T* address, T value) { |
| 179 *address = value; | 179 *address = value; |
| 180 } | 180 } |
| 181 | 181 |
| 182 class FakeFileStreamReader : public FileStreamReader { | 182 class FakeFileStreamReader : public FileStreamReader { |
| 183 public: | 183 public: |
| 184 explicit FakeFileStreamReader(const std::string& contents) | 184 explicit FakeFileStreamReader(const std::string& contents) |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 | 318 |
| 319 void TearDown() override { | 319 void TearDown() override { |
| 320 reader_.reset(); | 320 reader_.reset(); |
| 321 blob_handle_.reset(); | 321 blob_handle_.reset(); |
| 322 message_loop_.RunUntilIdle(); | 322 message_loop_.RunUntilIdle(); |
| 323 base::RunLoop().RunUntilIdle(); | 323 base::RunLoop().RunUntilIdle(); |
| 324 } | 324 } |
| 325 | 325 |
| 326 protected: | 326 protected: |
| 327 void InitializeReader(BlobDataBuilder* builder) { | 327 void InitializeReader(BlobDataBuilder* builder) { |
| 328 blob_handle_ = builder ? context_.AddFinishedBlob(builder).Pass() : nullptr; | 328 blob_handle_ = builder ? context_.AddFinishedBlob(builder) : nullptr; |
| 329 provider_ = new MockFileStreamReaderProvider(); | 329 provider_ = new MockFileStreamReaderProvider(); |
| 330 scoped_ptr<BlobReader::FileStreamReaderProvider> temp_ptr(provider_); | 330 scoped_ptr<BlobReader::FileStreamReaderProvider> temp_ptr(provider_); |
| 331 reader_.reset(new BlobReader(blob_handle_.get(), temp_ptr.Pass(), | 331 reader_.reset(new BlobReader(blob_handle_.get(), std::move(temp_ptr), |
| 332 message_loop_.task_runner().get())); | 332 message_loop_.task_runner().get())); |
| 333 } | 333 } |
| 334 | 334 |
| 335 // Takes ownership of the file reader (the blob reader takes ownership). | 335 // Takes ownership of the file reader (the blob reader takes ownership). |
| 336 void ExpectLocalFileCall(const FilePath& file_path, | 336 void ExpectLocalFileCall(const FilePath& file_path, |
| 337 base::Time modification_time, | 337 base::Time modification_time, |
| 338 uint64_t initial_offset, | 338 uint64_t initial_offset, |
| 339 FakeFileStreamReader* reader) { | 339 FakeFileStreamReader* reader) { |
| 340 EXPECT_CALL(*provider_, CreateForLocalFileMock( | 340 EXPECT_CALL(*provider_, CreateForLocalFileMock( |
| 341 message_loop_.task_runner().get(), file_path, | 341 message_loop_.task_runner().get(), file_path, |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 TEST_F(BlobReaderTest, DiskCacheAsync) { | 722 TEST_F(BlobReaderTest, DiskCacheAsync) { |
| 723 scoped_ptr<disk_cache::Backend> cache = | 723 scoped_ptr<disk_cache::Backend> cache = |
| 724 CreateInMemoryDiskCache(message_loop_.task_runner()); | 724 CreateInMemoryDiskCache(message_loop_.task_runner()); |
| 725 ASSERT_TRUE(cache); | 725 ASSERT_TRUE(cache); |
| 726 | 726 |
| 727 BlobDataBuilder b("uuid"); | 727 BlobDataBuilder b("uuid"); |
| 728 const std::string kData = "Test Blob Data"; | 728 const std::string kData = "Test Blob Data"; |
| 729 scoped_refptr<BlobDataBuilder::DataHandle> data_handle = | 729 scoped_refptr<BlobDataBuilder::DataHandle> data_handle = |
| 730 new EmptyDataHandle(); | 730 new EmptyDataHandle(); |
| 731 scoped_ptr<DelayedReadEntry> delayed_read_entry(new DelayedReadEntry( | 731 scoped_ptr<DelayedReadEntry> delayed_read_entry(new DelayedReadEntry( |
| 732 CreateDiskCacheEntry(cache.get(), "test entry", kData).Pass())); | 732 CreateDiskCacheEntry(cache.get(), "test entry", kData))); |
| 733 b.AppendDiskCacheEntry(data_handle, delayed_read_entry.get(), | 733 b.AppendDiskCacheEntry(data_handle, delayed_read_entry.get(), |
| 734 kTestDiskCacheStreamIndex); | 734 kTestDiskCacheStreamIndex); |
| 735 this->InitializeReader(&b); | 735 this->InitializeReader(&b); |
| 736 | 736 |
| 737 int size_result = -1; | 737 int size_result = -1; |
| 738 EXPECT_FALSE(IsReaderTotalSizeCalculated()); | 738 EXPECT_FALSE(IsReaderTotalSizeCalculated()); |
| 739 EXPECT_EQ(BlobReader::Status::DONE, | 739 EXPECT_EQ(BlobReader::Status::DONE, |
| 740 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); | 740 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); |
| 741 CheckSizeCalculatedSynchronously(kData.size(), size_result); | 741 CheckSizeCalculatedSynchronously(kData.size(), size_result); |
| 742 | 742 |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1108 EXPECT_EQ(BlobReader::Status::DONE, | 1108 EXPECT_EQ(BlobReader::Status::DONE, |
| 1109 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); | 1109 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); |
| 1110 buffer = CreateBuffer(kDataSize + 1); | 1110 buffer = CreateBuffer(kDataSize + 1); |
| 1111 EXPECT_EQ(BlobReader::Status::NET_ERROR, | 1111 EXPECT_EQ(BlobReader::Status::NET_ERROR, |
| 1112 reader_->SetReadRange(0, kDataSize + 1)); | 1112 reader_->SetReadRange(0, kDataSize + 1)); |
| 1113 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, reader_->net_error()); | 1113 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, reader_->net_error()); |
| 1114 } | 1114 } |
| 1115 | 1115 |
| 1116 } // namespace | 1116 } // namespace |
| 1117 } // namespace storage | 1117 } // namespace storage |
| OLD | NEW |