| 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 "webkit/browser/fileapi/quota/quota_backend_impl.h" | 5 #include "webkit/browser/fileapi/quota/quota_backend_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 12 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
| 13 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 13 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
| 14 #include "webkit/browser/fileapi/file_system_usage_cache.h" | 14 #include "webkit/browser/fileapi/file_system_usage_cache.h" |
| 15 #include "webkit/browser/fileapi/obfuscated_file_util.h" | 15 #include "webkit/browser/fileapi/obfuscated_file_util.h" |
| 16 #include "webkit/browser/quota/quota_manager_proxy.h" | 16 #include "webkit/browser/quota/quota_manager_proxy.h" |
| 17 | 17 |
| 18 using fileapi::FileSystemUsageCache; | 18 using storage::FileSystemUsageCache; |
| 19 using fileapi::ObfuscatedFileUtil; | 19 using storage::ObfuscatedFileUtil; |
| 20 using fileapi::QuotaBackendImpl; | 20 using storage::QuotaBackendImpl; |
| 21 using fileapi::SandboxFileSystemBackendDelegate; | 21 using storage::SandboxFileSystemBackendDelegate; |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char kOrigin[] = "http://example.com"; | 27 const char kOrigin[] = "http://example.com"; |
| 28 | 28 |
| 29 bool DidReserveQuota(bool accepted, | 29 bool DidReserveQuota(bool accepted, |
| 30 base::File::Error* error_out, | 30 base::File::Error* error_out, |
| 31 int64* delta_out, | 31 int64* delta_out, |
| 32 base::File::Error error, | 32 base::File::Error error, |
| 33 int64 delta) { | 33 int64 delta) { |
| 34 DCHECK(error_out); | 34 DCHECK(error_out); |
| 35 DCHECK(delta_out); | 35 DCHECK(delta_out); |
| 36 *error_out = error; | 36 *error_out = error; |
| 37 *delta_out = delta; | 37 *delta_out = delta; |
| 38 return accepted; | 38 return accepted; |
| 39 } | 39 } |
| 40 | 40 |
| 41 class MockQuotaManagerProxy : public quota::QuotaManagerProxy { | 41 class MockQuotaManagerProxy : public storage::QuotaManagerProxy { |
| 42 public: | 42 public: |
| 43 MockQuotaManagerProxy() | 43 MockQuotaManagerProxy() |
| 44 : QuotaManagerProxy(NULL, NULL), | 44 : QuotaManagerProxy(NULL, NULL), |
| 45 storage_modified_count_(0), | 45 storage_modified_count_(0), |
| 46 usage_(0), quota_(0) {} | 46 usage_(0), quota_(0) {} |
| 47 | 47 |
| 48 // We don't mock them. | 48 // We don't mock them. |
| 49 virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} | 49 virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} |
| 50 virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} | 50 virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} |
| 51 virtual void SetUsageCacheEnabled(quota::QuotaClient::ID client_id, | 51 virtual void SetUsageCacheEnabled(storage::QuotaClient::ID client_id, |
| 52 const GURL& origin, | 52 const GURL& origin, |
| 53 quota::StorageType type, | 53 storage::StorageType type, |
| 54 bool enabled) OVERRIDE {} | 54 bool enabled) OVERRIDE {} |
| 55 | 55 |
| 56 virtual void NotifyStorageModified( | 56 virtual void NotifyStorageModified(storage::QuotaClient::ID client_id, |
| 57 quota::QuotaClient::ID client_id, | 57 const GURL& origin, |
| 58 const GURL& origin, | 58 storage::StorageType type, |
| 59 quota::StorageType type, | 59 int64 delta) OVERRIDE { |
| 60 int64 delta) OVERRIDE { | |
| 61 ++storage_modified_count_; | 60 ++storage_modified_count_; |
| 62 usage_ += delta; | 61 usage_ += delta; |
| 63 ASSERT_LE(usage_, quota_); | 62 ASSERT_LE(usage_, quota_); |
| 64 } | 63 } |
| 65 | 64 |
| 66 virtual void GetUsageAndQuota( | 65 virtual void GetUsageAndQuota( |
| 67 base::SequencedTaskRunner* original_task_runner, | 66 base::SequencedTaskRunner* original_task_runner, |
| 68 const GURL& origin, | 67 const GURL& origin, |
| 69 quota::StorageType type, | 68 storage::StorageType type, |
| 70 const GetUsageAndQuotaCallback& callback) OVERRIDE { | 69 const GetUsageAndQuotaCallback& callback) OVERRIDE { |
| 71 callback.Run(quota::kQuotaStatusOk, usage_, quota_); | 70 callback.Run(storage::kQuotaStatusOk, usage_, quota_); |
| 72 } | 71 } |
| 73 | 72 |
| 74 int storage_modified_count() { return storage_modified_count_; } | 73 int storage_modified_count() { return storage_modified_count_; } |
| 75 int64 usage() { return usage_; } | 74 int64 usage() { return usage_; } |
| 76 void set_usage(int64 usage) { usage_ = usage; } | 75 void set_usage(int64 usage) { usage_ = usage; } |
| 77 void set_quota(int64 quota) { quota_ = quota; } | 76 void set_quota(int64 quota) { quota_ = quota; } |
| 78 | 77 |
| 79 protected: | 78 protected: |
| 80 virtual ~MockQuotaManagerProxy() {} | 79 virtual ~MockQuotaManagerProxy() {} |
| 81 | 80 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 108 | 107 |
| 109 virtual void TearDown() OVERRIDE { | 108 virtual void TearDown() OVERRIDE { |
| 110 backend_.reset(); | 109 backend_.reset(); |
| 111 quota_manager_proxy_ = NULL; | 110 quota_manager_proxy_ = NULL; |
| 112 file_util_.reset(); | 111 file_util_.reset(); |
| 113 message_loop_.RunUntilIdle(); | 112 message_loop_.RunUntilIdle(); |
| 114 } | 113 } |
| 115 | 114 |
| 116 protected: | 115 protected: |
| 117 void InitializeForOriginAndType(const GURL& origin, | 116 void InitializeForOriginAndType(const GURL& origin, |
| 118 fileapi::FileSystemType type) { | 117 storage::FileSystemType type) { |
| 119 ASSERT_TRUE(file_util_->InitOriginDatabase(origin, true /* create */)); | 118 ASSERT_TRUE(file_util_->InitOriginDatabase(origin, true /* create */)); |
| 120 ASSERT_TRUE(file_util_->origin_database_ != NULL); | 119 ASSERT_TRUE(file_util_->origin_database_ != NULL); |
| 121 | 120 |
| 122 std::string type_string = | 121 std::string type_string = |
| 123 SandboxFileSystemBackendDelegate::GetTypeString(type); | 122 SandboxFileSystemBackendDelegate::GetTypeString(type); |
| 124 base::File::Error error = base::File::FILE_ERROR_FAILED; | 123 base::File::Error error = base::File::FILE_ERROR_FAILED; |
| 125 base::FilePath path = file_util_->GetDirectoryForOriginAndType( | 124 base::FilePath path = file_util_->GetDirectoryForOriginAndType( |
| 126 origin, type_string, true /* create */, &error); | 125 origin, type_string, true /* create */, &error); |
| 127 ASSERT_EQ(base::File::FILE_OK, error); | 126 ASSERT_EQ(base::File::FILE_OK, error); |
| 128 | 127 |
| 129 ASSERT_TRUE(file_system_usage_cache_.UpdateUsage( | 128 ASSERT_TRUE(file_system_usage_cache_.UpdateUsage( |
| 130 GetUsageCachePath(origin, type), 0)); | 129 GetUsageCachePath(origin, type), 0)); |
| 131 } | 130 } |
| 132 | 131 |
| 133 base::SequencedTaskRunner* file_task_runner() { | 132 base::SequencedTaskRunner* file_task_runner() { |
| 134 return base::MessageLoopProxy::current().get(); | 133 return base::MessageLoopProxy::current().get(); |
| 135 } | 134 } |
| 136 | 135 |
| 137 base::FilePath GetUsageCachePath(const GURL& origin, | 136 base::FilePath GetUsageCachePath(const GURL& origin, |
| 138 fileapi::FileSystemType type) { | 137 storage::FileSystemType type) { |
| 139 base::FilePath path; | 138 base::FilePath path; |
| 140 base::File::Error error = | 139 base::File::Error error = |
| 141 backend_->GetUsageCachePath(origin, type, &path); | 140 backend_->GetUsageCachePath(origin, type, &path); |
| 142 EXPECT_EQ(base::File::FILE_OK, error); | 141 EXPECT_EQ(base::File::FILE_OK, error); |
| 143 EXPECT_FALSE(path.empty()); | 142 EXPECT_FALSE(path.empty()); |
| 144 return path; | 143 return path; |
| 145 } | 144 } |
| 146 | 145 |
| 147 base::MessageLoop message_loop_; | 146 base::MessageLoop message_loop_; |
| 148 base::ScopedTempDir data_dir_; | 147 base::ScopedTempDir data_dir_; |
| 149 scoped_ptr<leveldb::Env> in_memory_env_; | 148 scoped_ptr<leveldb::Env> in_memory_env_; |
| 150 scoped_ptr<ObfuscatedFileUtil> file_util_; | 149 scoped_ptr<ObfuscatedFileUtil> file_util_; |
| 151 FileSystemUsageCache file_system_usage_cache_; | 150 FileSystemUsageCache file_system_usage_cache_; |
| 152 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 151 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
| 153 scoped_ptr<QuotaBackendImpl> backend_; | 152 scoped_ptr<QuotaBackendImpl> backend_; |
| 154 | 153 |
| 155 private: | 154 private: |
| 156 DISALLOW_COPY_AND_ASSIGN(QuotaBackendImplTest); | 155 DISALLOW_COPY_AND_ASSIGN(QuotaBackendImplTest); |
| 157 }; | 156 }; |
| 158 | 157 |
| 159 TEST_F(QuotaBackendImplTest, ReserveQuota_Basic) { | 158 TEST_F(QuotaBackendImplTest, ReserveQuota_Basic) { |
| 160 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary; | 159 storage::FileSystemType type = storage::kFileSystemTypeTemporary; |
| 161 InitializeForOriginAndType(GURL(kOrigin), type); | 160 InitializeForOriginAndType(GURL(kOrigin), type); |
| 162 quota_manager_proxy_->set_quota(10000); | 161 quota_manager_proxy_->set_quota(10000); |
| 163 | 162 |
| 164 int64 delta = 0; | 163 int64 delta = 0; |
| 165 | 164 |
| 166 const int64 kDelta1 = 1000; | 165 const int64 kDelta1 = 1000; |
| 167 base::File::Error error = base::File::FILE_ERROR_FAILED; | 166 base::File::Error error = base::File::FILE_ERROR_FAILED; |
| 168 backend_->ReserveQuota(GURL(kOrigin), type, kDelta1, | 167 backend_->ReserveQuota(GURL(kOrigin), type, kDelta1, |
| 169 base::Bind(&DidReserveQuota, true, &error, &delta)); | 168 base::Bind(&DidReserveQuota, true, &error, &delta)); |
| 170 EXPECT_EQ(base::File::FILE_OK, error); | 169 EXPECT_EQ(base::File::FILE_OK, error); |
| 171 EXPECT_EQ(kDelta1, delta); | 170 EXPECT_EQ(kDelta1, delta); |
| 172 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); | 171 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); |
| 173 | 172 |
| 174 const int64 kDelta2 = -300; | 173 const int64 kDelta2 = -300; |
| 175 error = base::File::FILE_ERROR_FAILED; | 174 error = base::File::FILE_ERROR_FAILED; |
| 176 backend_->ReserveQuota(GURL(kOrigin), type, kDelta2, | 175 backend_->ReserveQuota(GURL(kOrigin), type, kDelta2, |
| 177 base::Bind(&DidReserveQuota, true, &error, &delta)); | 176 base::Bind(&DidReserveQuota, true, &error, &delta)); |
| 178 EXPECT_EQ(base::File::FILE_OK, error); | 177 EXPECT_EQ(base::File::FILE_OK, error); |
| 179 EXPECT_EQ(kDelta2, delta); | 178 EXPECT_EQ(kDelta2, delta); |
| 180 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); | 179 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); |
| 181 | 180 |
| 182 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | 181 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); |
| 183 } | 182 } |
| 184 | 183 |
| 185 TEST_F(QuotaBackendImplTest, ReserveQuota_NoSpace) { | 184 TEST_F(QuotaBackendImplTest, ReserveQuota_NoSpace) { |
| 186 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary; | 185 storage::FileSystemType type = storage::kFileSystemTypeTemporary; |
| 187 InitializeForOriginAndType(GURL(kOrigin), type); | 186 InitializeForOriginAndType(GURL(kOrigin), type); |
| 188 quota_manager_proxy_->set_quota(100); | 187 quota_manager_proxy_->set_quota(100); |
| 189 | 188 |
| 190 int64 delta = 0; | 189 int64 delta = 0; |
| 191 | 190 |
| 192 const int64 kDelta = 1000; | 191 const int64 kDelta = 1000; |
| 193 base::File::Error error = base::File::FILE_ERROR_FAILED; | 192 base::File::Error error = base::File::FILE_ERROR_FAILED; |
| 194 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, | 193 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, |
| 195 base::Bind(&DidReserveQuota, true, &error, &delta)); | 194 base::Bind(&DidReserveQuota, true, &error, &delta)); |
| 196 EXPECT_EQ(base::File::FILE_OK, error); | 195 EXPECT_EQ(base::File::FILE_OK, error); |
| 197 EXPECT_EQ(100, delta); | 196 EXPECT_EQ(100, delta); |
| 198 EXPECT_EQ(100, quota_manager_proxy_->usage()); | 197 EXPECT_EQ(100, quota_manager_proxy_->usage()); |
| 199 | 198 |
| 200 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); | 199 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); |
| 201 } | 200 } |
| 202 | 201 |
| 203 TEST_F(QuotaBackendImplTest, ReserveQuota_Revert) { | 202 TEST_F(QuotaBackendImplTest, ReserveQuota_Revert) { |
| 204 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary; | 203 storage::FileSystemType type = storage::kFileSystemTypeTemporary; |
| 205 InitializeForOriginAndType(GURL(kOrigin), type); | 204 InitializeForOriginAndType(GURL(kOrigin), type); |
| 206 quota_manager_proxy_->set_quota(10000); | 205 quota_manager_proxy_->set_quota(10000); |
| 207 | 206 |
| 208 int64 delta = 0; | 207 int64 delta = 0; |
| 209 | 208 |
| 210 const int64 kDelta = 1000; | 209 const int64 kDelta = 1000; |
| 211 base::File::Error error = base::File::FILE_ERROR_FAILED; | 210 base::File::Error error = base::File::FILE_ERROR_FAILED; |
| 212 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, | 211 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, |
| 213 base::Bind(&DidReserveQuota, false, &error, &delta)); | 212 base::Bind(&DidReserveQuota, false, &error, &delta)); |
| 214 EXPECT_EQ(base::File::FILE_OK, error); | 213 EXPECT_EQ(base::File::FILE_OK, error); |
| 215 EXPECT_EQ(kDelta, delta); | 214 EXPECT_EQ(kDelta, delta); |
| 216 EXPECT_EQ(0, quota_manager_proxy_->usage()); | 215 EXPECT_EQ(0, quota_manager_proxy_->usage()); |
| 217 | 216 |
| 218 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | 217 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); |
| 219 } | 218 } |
| 220 | 219 |
| 221 TEST_F(QuotaBackendImplTest, ReleaseReservedQuota) { | 220 TEST_F(QuotaBackendImplTest, ReleaseReservedQuota) { |
| 222 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary; | 221 storage::FileSystemType type = storage::kFileSystemTypeTemporary; |
| 223 InitializeForOriginAndType(GURL(kOrigin), type); | 222 InitializeForOriginAndType(GURL(kOrigin), type); |
| 224 const int64 kInitialUsage = 2000; | 223 const int64 kInitialUsage = 2000; |
| 225 quota_manager_proxy_->set_usage(kInitialUsage); | 224 quota_manager_proxy_->set_usage(kInitialUsage); |
| 226 quota_manager_proxy_->set_quota(10000); | 225 quota_manager_proxy_->set_quota(10000); |
| 227 | 226 |
| 228 const int64 kSize = 1000; | 227 const int64 kSize = 1000; |
| 229 backend_->ReleaseReservedQuota(GURL(kOrigin), type, kSize); | 228 backend_->ReleaseReservedQuota(GURL(kOrigin), type, kSize); |
| 230 EXPECT_EQ(kInitialUsage - kSize, quota_manager_proxy_->usage()); | 229 EXPECT_EQ(kInitialUsage - kSize, quota_manager_proxy_->usage()); |
| 231 | 230 |
| 232 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); | 231 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); |
| 233 } | 232 } |
| 234 | 233 |
| 235 TEST_F(QuotaBackendImplTest, CommitQuotaUsage) { | 234 TEST_F(QuotaBackendImplTest, CommitQuotaUsage) { |
| 236 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary; | 235 storage::FileSystemType type = storage::kFileSystemTypeTemporary; |
| 237 InitializeForOriginAndType(GURL(kOrigin), type); | 236 InitializeForOriginAndType(GURL(kOrigin), type); |
| 238 quota_manager_proxy_->set_quota(10000); | 237 quota_manager_proxy_->set_quota(10000); |
| 239 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); | 238 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); |
| 240 | 239 |
| 241 const int64 kDelta1 = 1000; | 240 const int64 kDelta1 = 1000; |
| 242 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta1); | 241 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta1); |
| 243 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); | 242 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); |
| 244 int64 usage = 0; | 243 int64 usage = 0; |
| 245 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); | 244 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); |
| 246 EXPECT_EQ(kDelta1, usage); | 245 EXPECT_EQ(kDelta1, usage); |
| 247 | 246 |
| 248 const int64 kDelta2 = -300; | 247 const int64 kDelta2 = -300; |
| 249 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta2); | 248 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta2); |
| 250 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); | 249 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); |
| 251 usage = 0; | 250 usage = 0; |
| 252 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); | 251 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); |
| 253 EXPECT_EQ(kDelta1 + kDelta2, usage); | 252 EXPECT_EQ(kDelta1 + kDelta2, usage); |
| 254 | 253 |
| 255 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | 254 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); |
| 256 } | 255 } |
| 257 | 256 |
| 258 TEST_F(QuotaBackendImplTest, DirtyCount) { | 257 TEST_F(QuotaBackendImplTest, DirtyCount) { |
| 259 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary; | 258 storage::FileSystemType type = storage::kFileSystemTypeTemporary; |
| 260 InitializeForOriginAndType(GURL(kOrigin), type); | 259 InitializeForOriginAndType(GURL(kOrigin), type); |
| 261 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); | 260 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); |
| 262 | 261 |
| 263 backend_->IncrementDirtyCount(GURL(kOrigin), type); | 262 backend_->IncrementDirtyCount(GURL(kOrigin), type); |
| 264 uint32 dirty = 0; | 263 uint32 dirty = 0; |
| 265 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); | 264 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); |
| 266 EXPECT_EQ(1u, dirty); | 265 EXPECT_EQ(1u, dirty); |
| 267 | 266 |
| 268 backend_->DecrementDirtyCount(GURL(kOrigin), type); | 267 backend_->DecrementDirtyCount(GURL(kOrigin), type); |
| 269 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); | 268 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); |
| 270 EXPECT_EQ(0u, dirty); | 269 EXPECT_EQ(0u, dirty); |
| 271 } | 270 } |
| 272 | 271 |
| 273 } // namespace content | 272 } // namespace content |
| OLD | NEW |