| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/service_worker/service_worker_cache.h" | 5 #include "content/browser/service_worker/service_worker_cache.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 blob_storage_context, NULL, base::MessageLoopProxy::current().get()); | 40 blob_storage_context, NULL, base::MessageLoopProxy::current().get()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 // A disk_cache::Backend wrapper that can delay operations. | 43 // A disk_cache::Backend wrapper that can delay operations. |
| 44 class DelayableBackend : public disk_cache::Backend { | 44 class DelayableBackend : public disk_cache::Backend { |
| 45 public: | 45 public: |
| 46 DelayableBackend(scoped_ptr<disk_cache::Backend> backend) | 46 DelayableBackend(scoped_ptr<disk_cache::Backend> backend) |
| 47 : backend_(backend.Pass()), delay_open_(false) {} | 47 : backend_(backend.Pass()), delay_open_(false) {} |
| 48 | 48 |
| 49 // disk_cache::Backend overrides | 49 // disk_cache::Backend overrides |
| 50 virtual net::CacheType GetCacheType() const override { | 50 net::CacheType GetCacheType() const override { |
| 51 return backend_->GetCacheType(); | 51 return backend_->GetCacheType(); |
| 52 } | 52 } |
| 53 virtual int32 GetEntryCount() const override { | 53 int32 GetEntryCount() const override { return backend_->GetEntryCount(); } |
| 54 return backend_->GetEntryCount(); | 54 int OpenEntry(const std::string& key, |
| 55 } | 55 disk_cache::Entry** entry, |
| 56 virtual int OpenEntry(const std::string& key, | 56 const CompletionCallback& callback) override { |
| 57 disk_cache::Entry** entry, | |
| 58 const CompletionCallback& callback) override { | |
| 59 if (delay_open_) { | 57 if (delay_open_) { |
| 60 open_entry_callback_ = | 58 open_entry_callback_ = |
| 61 base::Bind(&DelayableBackend::OpenEntryDelayedImpl, | 59 base::Bind(&DelayableBackend::OpenEntryDelayedImpl, |
| 62 base::Unretained(this), key, entry, callback); | 60 base::Unretained(this), key, entry, callback); |
| 63 return net::ERR_IO_PENDING; | 61 return net::ERR_IO_PENDING; |
| 64 } | 62 } |
| 65 | 63 |
| 66 return backend_->OpenEntry(key, entry, callback); | 64 return backend_->OpenEntry(key, entry, callback); |
| 67 } | 65 } |
| 68 virtual int CreateEntry(const std::string& key, | 66 int CreateEntry(const std::string& key, |
| 69 disk_cache::Entry** entry, | 67 disk_cache::Entry** entry, |
| 70 const CompletionCallback& callback) override { | 68 const CompletionCallback& callback) override { |
| 71 return backend_->CreateEntry(key, entry, callback); | 69 return backend_->CreateEntry(key, entry, callback); |
| 72 } | 70 } |
| 73 virtual int DoomEntry(const std::string& key, | 71 int DoomEntry(const std::string& key, |
| 74 const CompletionCallback& callback) override { | 72 const CompletionCallback& callback) override { |
| 75 return backend_->DoomEntry(key, callback); | 73 return backend_->DoomEntry(key, callback); |
| 76 } | 74 } |
| 77 virtual int DoomAllEntries(const CompletionCallback& callback) override { | 75 int DoomAllEntries(const CompletionCallback& callback) override { |
| 78 return backend_->DoomAllEntries(callback); | 76 return backend_->DoomAllEntries(callback); |
| 79 } | 77 } |
| 80 virtual int DoomEntriesBetween(base::Time initial_time, | 78 int DoomEntriesBetween(base::Time initial_time, |
| 81 base::Time end_time, | 79 base::Time end_time, |
| 82 const CompletionCallback& callback) override { | 80 const CompletionCallback& callback) override { |
| 83 return backend_->DoomEntriesBetween(initial_time, end_time, callback); | 81 return backend_->DoomEntriesBetween(initial_time, end_time, callback); |
| 84 } | 82 } |
| 85 virtual int DoomEntriesSince(base::Time initial_time, | 83 int DoomEntriesSince(base::Time initial_time, |
| 86 const CompletionCallback& callback) override { | 84 const CompletionCallback& callback) override { |
| 87 return backend_->DoomEntriesSince(initial_time, callback); | 85 return backend_->DoomEntriesSince(initial_time, callback); |
| 88 } | 86 } |
| 89 virtual scoped_ptr<Iterator> CreateIterator() override { | 87 scoped_ptr<Iterator> CreateIterator() override { |
| 90 return backend_->CreateIterator(); | 88 return backend_->CreateIterator(); |
| 91 } | 89 } |
| 92 virtual void GetStats( | 90 void GetStats( |
| 93 std::vector<std::pair<std::string, std::string>>* stats) override { | 91 std::vector<std::pair<std::string, std::string>>* stats) override { |
| 94 return backend_->GetStats(stats); | 92 return backend_->GetStats(stats); |
| 95 } | 93 } |
| 96 virtual void OnExternalCacheHit(const std::string& key) override { | 94 void OnExternalCacheHit(const std::string& key) override { |
| 97 return backend_->OnExternalCacheHit(key); | 95 return backend_->OnExternalCacheHit(key); |
| 98 } | 96 } |
| 99 | 97 |
| 100 // Call to continue a delayed open. | 98 // Call to continue a delayed open. |
| 101 void OpenEntryContinue() { | 99 void OpenEntryContinue() { |
| 102 EXPECT_FALSE(open_entry_callback_.is_null()); | 100 EXPECT_FALSE(open_entry_callback_.is_null()); |
| 103 open_entry_callback_.Run(); | 101 open_entry_callback_.Run(); |
| 104 } | 102 } |
| 105 | 103 |
| 106 void set_delay_open(bool value) { delay_open_ = value; } | 104 void set_delay_open(bool value) { delay_open_ = value; } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 130 net::URLRequestContext* request_context, | 128 net::URLRequestContext* request_context, |
| 131 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 129 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 132 base::WeakPtr<storage::BlobStorageContext> blob_context) | 130 base::WeakPtr<storage::BlobStorageContext> blob_context) |
| 133 : ServiceWorkerCache(origin, | 131 : ServiceWorkerCache(origin, |
| 134 path, | 132 path, |
| 135 request_context, | 133 request_context, |
| 136 quota_manager_proxy, | 134 quota_manager_proxy, |
| 137 blob_context), | 135 blob_context), |
| 138 delay_backend_creation_(false) {} | 136 delay_backend_creation_(false) {} |
| 139 | 137 |
| 140 virtual void CreateBackend(const ErrorCallback& callback) override { | 138 void CreateBackend(const ErrorCallback& callback) override { |
| 141 backend_creation_callback_ = callback; | 139 backend_creation_callback_ = callback; |
| 142 if (delay_backend_creation_) | 140 if (delay_backend_creation_) |
| 143 return; | 141 return; |
| 144 ContinueCreateBackend(); | 142 ContinueCreateBackend(); |
| 145 } | 143 } |
| 146 | 144 |
| 147 void ContinueCreateBackend() { | 145 void ContinueCreateBackend() { |
| 148 ServiceWorkerCache::CreateBackend(backend_creation_callback_); | 146 ServiceWorkerCache::CreateBackend(backend_creation_callback_); |
| 149 } | 147 } |
| 150 | 148 |
| 151 void set_delay_backend_creation(bool delay) { | 149 void set_delay_backend_creation(bool delay) { |
| 152 delay_backend_creation_ = delay; | 150 delay_backend_creation_ = delay; |
| 153 } | 151 } |
| 154 | 152 |
| 155 // Swap the existing backend with a delayable one. The backend must have been | 153 // Swap the existing backend with a delayable one. The backend must have been |
| 156 // created before calling this. | 154 // created before calling this. |
| 157 DelayableBackend* UseDelayableBackend() { | 155 DelayableBackend* UseDelayableBackend() { |
| 158 EXPECT_TRUE(backend_); | 156 EXPECT_TRUE(backend_); |
| 159 DelayableBackend* delayable_backend = new DelayableBackend(backend_.Pass()); | 157 DelayableBackend* delayable_backend = new DelayableBackend(backend_.Pass()); |
| 160 backend_.reset(delayable_backend); | 158 backend_.reset(delayable_backend); |
| 161 return delayable_backend; | 159 return delayable_backend; |
| 162 } | 160 } |
| 163 | 161 |
| 164 private: | 162 private: |
| 165 virtual ~TestServiceWorkerCache() override {} | 163 ~TestServiceWorkerCache() override {} |
| 166 | 164 |
| 167 bool delay_backend_creation_; | 165 bool delay_backend_creation_; |
| 168 ErrorCallback backend_creation_callback_; | 166 ErrorCallback backend_creation_callback_; |
| 169 | 167 |
| 170 DISALLOW_COPY_AND_ASSIGN(TestServiceWorkerCache); | 168 DISALLOW_COPY_AND_ASSIGN(TestServiceWorkerCache); |
| 171 }; | 169 }; |
| 172 | 170 |
| 173 class ServiceWorkerCacheTest : public testing::Test { | 171 class ServiceWorkerCacheTest : public testing::Test { |
| 174 public: | 172 public: |
| 175 ServiceWorkerCacheTest() | 173 ServiceWorkerCacheTest() |
| (...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 EXPECT_TRUE(callback_closed_); | 906 EXPECT_TRUE(callback_closed_); |
| 909 | 907 |
| 910 VerifyAllOpsFail(); | 908 VerifyAllOpsFail(); |
| 911 } | 909 } |
| 912 | 910 |
| 913 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, | 911 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, |
| 914 ServiceWorkerCacheTestP, | 912 ServiceWorkerCacheTestP, |
| 915 ::testing::Values(false, true)); | 913 ::testing::Values(false, true)); |
| 916 | 914 |
| 917 } // namespace content | 915 } // namespace content |
| OLD | NEW |