| 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_reservation_manager.h" | 5 #include "webkit/browser/fileapi/quota/quota_reservation_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "webkit/browser/fileapi/quota/open_file_handle.h" | 15 #include "webkit/browser/fileapi/quota/open_file_handle.h" |
| 16 #include "webkit/browser/fileapi/quota/quota_reservation.h" | 16 #include "webkit/browser/fileapi/quota/quota_reservation.h" |
| 17 | 17 |
| 18 using fileapi::FileSystemType; | |
| 19 using fileapi::kFileSystemTypeTemporary; | 18 using fileapi::kFileSystemTypeTemporary; |
| 20 using fileapi::OpenFileHandle; | 19 using fileapi::OpenFileHandle; |
| 21 using fileapi::QuotaReservation; | 20 using fileapi::QuotaReservation; |
| 22 using fileapi::QuotaReservationManager; | 21 using fileapi::QuotaReservationManager; |
| 23 | 22 |
| 24 namespace content { | 23 namespace content { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 const char kOrigin[] = "http://example.com"; | 27 const char kOrigin[] = "http://example.com"; |
| 29 const FileSystemType kType = kFileSystemTypeTemporary; | 28 const fileapi::FileSystemType kType = kFileSystemTypeTemporary; |
| 30 const int64 kInitialFileSize = 1; | 29 const int64 kInitialFileSize = 1; |
| 31 | 30 |
| 32 typedef QuotaReservationManager::ReserveQuotaCallback ReserveQuotaCallback; | 31 typedef QuotaReservationManager::ReserveQuotaCallback ReserveQuotaCallback; |
| 33 | 32 |
| 34 int64 GetFileSize(const base::FilePath& path) { | 33 int64 GetFileSize(const base::FilePath& path) { |
| 35 int64 size = 0; | 34 int64 size = 0; |
| 36 base::GetFileSize(path, &size); | 35 base::GetFileSize(path, &size); |
| 37 return size; | 36 return size; |
| 38 } | 37 } |
| 39 | 38 |
| 40 void SetFileSize(const base::FilePath& path, int64 size) { | 39 void SetFileSize(const base::FilePath& path, int64 size) { |
| 41 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); | 40 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); |
| 42 ASSERT_TRUE(file.IsValid()); | 41 ASSERT_TRUE(file.IsValid()); |
| 43 ASSERT_TRUE(file.SetLength(size)); | 42 ASSERT_TRUE(file.SetLength(size)); |
| 44 } | 43 } |
| 45 | 44 |
| 46 class FakeBackend : public QuotaReservationManager::QuotaBackend { | 45 class FakeBackend : public QuotaReservationManager::QuotaBackend { |
| 47 public: | 46 public: |
| 48 FakeBackend() | 47 FakeBackend() |
| 49 : on_memory_usage_(kInitialFileSize), | 48 : on_memory_usage_(kInitialFileSize), |
| 50 on_disk_usage_(kInitialFileSize) {} | 49 on_disk_usage_(kInitialFileSize) {} |
| 51 virtual ~FakeBackend() {} | 50 virtual ~FakeBackend() {} |
| 52 | 51 |
| 53 virtual void ReserveQuota(const GURL& origin, | 52 virtual void ReserveQuota(const GURL& origin, |
| 54 FileSystemType type, | 53 fileapi::FileSystemType type, |
| 55 int64 delta, | 54 int64 delta, |
| 56 const ReserveQuotaCallback& callback) OVERRIDE { | 55 const ReserveQuotaCallback& callback) OVERRIDE { |
| 57 EXPECT_EQ(GURL(kOrigin), origin); | 56 EXPECT_EQ(GURL(kOrigin), origin); |
| 58 EXPECT_EQ(kType, type); | 57 EXPECT_EQ(kType, type); |
| 59 on_memory_usage_ += delta; | 58 on_memory_usage_ += delta; |
| 60 base::MessageLoopProxy::current()->PostTask( | 59 base::MessageLoopProxy::current()->PostTask( |
| 61 FROM_HERE, | 60 FROM_HERE, |
| 62 base::Bind(base::IgnoreResult(callback), base::File::FILE_OK, delta)); | 61 base::Bind(base::IgnoreResult(callback), base::File::FILE_OK, delta)); |
| 63 } | 62 } |
| 64 | 63 |
| 65 virtual void ReleaseReservedQuota(const GURL& origin, | 64 virtual void ReleaseReservedQuota(const GURL& origin, |
| 66 FileSystemType type, | 65 fileapi::FileSystemType type, |
| 67 int64 size) OVERRIDE { | 66 int64 size) OVERRIDE { |
| 68 EXPECT_LE(0, size); | 67 EXPECT_LE(0, size); |
| 69 EXPECT_EQ(GURL(kOrigin), origin); | 68 EXPECT_EQ(GURL(kOrigin), origin); |
| 70 EXPECT_EQ(kType, type); | 69 EXPECT_EQ(kType, type); |
| 71 on_memory_usage_ -= size; | 70 on_memory_usage_ -= size; |
| 72 } | 71 } |
| 73 | 72 |
| 74 virtual void CommitQuotaUsage(const GURL& origin, | 73 virtual void CommitQuotaUsage(const GURL& origin, |
| 75 FileSystemType type, | 74 fileapi::FileSystemType type, |
| 76 int64 delta) OVERRIDE { | 75 int64 delta) OVERRIDE { |
| 77 EXPECT_EQ(GURL(kOrigin), origin); | 76 EXPECT_EQ(GURL(kOrigin), origin); |
| 78 EXPECT_EQ(kType, type); | 77 EXPECT_EQ(kType, type); |
| 79 on_disk_usage_ += delta; | 78 on_disk_usage_ += delta; |
| 80 on_memory_usage_ += delta; | 79 on_memory_usage_ += delta; |
| 81 } | 80 } |
| 82 | 81 |
| 83 virtual void IncrementDirtyCount(const GURL& origin, | 82 virtual void IncrementDirtyCount(const GURL& origin, |
| 84 FileSystemType type) OVERRIDE {} | 83 fileapi::FileSystemType type) OVERRIDE {} |
| 85 virtual void DecrementDirtyCount(const GURL& origin, | 84 virtual void DecrementDirtyCount(const GURL& origin, |
| 86 FileSystemType type) OVERRIDE {} | 85 fileapi::FileSystemType type) OVERRIDE {} |
| 87 | 86 |
| 88 int64 on_memory_usage() { return on_memory_usage_; } | 87 int64 on_memory_usage() { return on_memory_usage_; } |
| 89 int64 on_disk_usage() { return on_disk_usage_; } | 88 int64 on_disk_usage() { return on_disk_usage_; } |
| 90 | 89 |
| 91 private: | 90 private: |
| 92 int64 on_memory_usage_; | 91 int64 on_memory_usage_; |
| 93 int64 on_disk_usage_; | 92 int64 on_disk_usage_; |
| 94 | 93 |
| 95 DISALLOW_COPY_AND_ASSIGN(FakeBackend); | 94 DISALLOW_COPY_AND_ASSIGN(FakeBackend); |
| 96 }; | 95 }; |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 | 358 |
| 360 EXPECT_EQ(kInitialFileSize + 10, GetFileSize(file_path())); | 359 EXPECT_EQ(kInitialFileSize + 10, GetFileSize(file_path())); |
| 361 EXPECT_EQ(kInitialFileSize + 15 + 20, fake_backend()->on_memory_usage()); | 360 EXPECT_EQ(kInitialFileSize + 15 + 20, fake_backend()->on_memory_usage()); |
| 362 EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_disk_usage()); | 361 EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_disk_usage()); |
| 363 | 362 |
| 364 reservation2 = NULL; | 363 reservation2 = NULL; |
| 365 EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_memory_usage()); | 364 EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_memory_usage()); |
| 366 } | 365 } |
| 367 | 366 |
| 368 } // namespace content | 367 } // namespace content |
| OLD | NEW |