OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "content/browser/indexed_db/indexed_db_backing_store.h" | 5 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "third_party/WebKit/public/platform/WebIDBTypes.h" | 27 #include "third_party/WebKit/public/platform/WebIDBTypes.h" |
28 | 28 |
29 using base::ASCIIToUTF16; | 29 using base::ASCIIToUTF16; |
30 | 30 |
31 namespace content { | 31 namespace content { |
32 | 32 |
33 namespace { | 33 namespace { |
34 | 34 |
35 class Comparator : public LevelDBComparator { | 35 class Comparator : public LevelDBComparator { |
36 public: | 36 public: |
37 virtual int Compare(const base::StringPiece& a, | 37 int Compare(const base::StringPiece& a, |
38 const base::StringPiece& b) const override { | 38 const base::StringPiece& b) const override { |
39 return content::Compare(a, b, false /*index_keys*/); | 39 return content::Compare(a, b, false /*index_keys*/); |
40 } | 40 } |
41 virtual const char* Name() const override { return "idb_cmp1"; } | 41 const char* Name() const override { return "idb_cmp1"; } |
42 }; | 42 }; |
43 | 43 |
44 class DefaultLevelDBFactory : public LevelDBFactory { | 44 class DefaultLevelDBFactory : public LevelDBFactory { |
45 public: | 45 public: |
46 DefaultLevelDBFactory() {} | 46 DefaultLevelDBFactory() {} |
47 virtual leveldb::Status OpenLevelDB(const base::FilePath& file_name, | 47 leveldb::Status OpenLevelDB(const base::FilePath& file_name, |
48 const LevelDBComparator* comparator, | 48 const LevelDBComparator* comparator, |
49 scoped_ptr<LevelDBDatabase>* db, | 49 scoped_ptr<LevelDBDatabase>* db, |
50 bool* is_disk_full) override { | 50 bool* is_disk_full) override { |
51 return LevelDBDatabase::Open(file_name, comparator, db, is_disk_full); | 51 return LevelDBDatabase::Open(file_name, comparator, db, is_disk_full); |
52 } | 52 } |
53 virtual leveldb::Status DestroyLevelDB( | 53 leveldb::Status DestroyLevelDB(const base::FilePath& file_name) override { |
54 const base::FilePath& file_name) override { | |
55 return LevelDBDatabase::Destroy(file_name); | 54 return LevelDBDatabase::Destroy(file_name); |
56 } | 55 } |
57 | 56 |
58 private: | 57 private: |
59 DISALLOW_COPY_AND_ASSIGN(DefaultLevelDBFactory); | 58 DISALLOW_COPY_AND_ASSIGN(DefaultLevelDBFactory); |
60 }; | 59 }; |
61 | 60 |
62 class TestableIndexedDBBackingStore : public IndexedDBBackingStore { | 61 class TestableIndexedDBBackingStore : public IndexedDBBackingStore { |
63 public: | 62 public: |
64 static scoped_refptr<TestableIndexedDBBackingStore> Open( | 63 static scoped_refptr<TestableIndexedDBBackingStore> Open( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 106 |
108 const std::vector<IndexedDBBackingStore::Transaction::WriteDescriptor>& | 107 const std::vector<IndexedDBBackingStore::Transaction::WriteDescriptor>& |
109 writes() const { | 108 writes() const { |
110 return writes_; | 109 return writes_; |
111 } | 110 } |
112 void ClearWrites() { writes_.clear(); } | 111 void ClearWrites() { writes_.clear(); } |
113 const std::vector<int64>& removals() const { return removals_; } | 112 const std::vector<int64>& removals() const { return removals_; } |
114 void ClearRemovals() { removals_.clear(); } | 113 void ClearRemovals() { removals_.clear(); } |
115 | 114 |
116 protected: | 115 protected: |
117 virtual ~TestableIndexedDBBackingStore() {} | 116 ~TestableIndexedDBBackingStore() override {} |
118 | 117 |
119 virtual bool WriteBlobFile( | 118 bool WriteBlobFile( |
120 int64 database_id, | 119 int64 database_id, |
121 const Transaction::WriteDescriptor& descriptor, | 120 const Transaction::WriteDescriptor& descriptor, |
122 Transaction::ChainedBlobWriter* chained_blob_writer) override { | 121 Transaction::ChainedBlobWriter* chained_blob_writer) override { |
123 if (KeyPrefix::IsValidDatabaseId(database_id_)) { | 122 if (KeyPrefix::IsValidDatabaseId(database_id_)) { |
124 if (database_id_ != database_id) { | 123 if (database_id_ != database_id) { |
125 return false; | 124 return false; |
126 } | 125 } |
127 } else { | 126 } else { |
128 database_id_ = database_id; | 127 database_id_ = database_id; |
129 } | 128 } |
130 writes_.push_back(descriptor); | 129 writes_.push_back(descriptor); |
131 task_runner()->PostTask( | 130 task_runner()->PostTask( |
132 FROM_HERE, | 131 FROM_HERE, |
133 base::Bind(&Transaction::ChainedBlobWriter::ReportWriteCompletion, | 132 base::Bind(&Transaction::ChainedBlobWriter::ReportWriteCompletion, |
134 chained_blob_writer, | 133 chained_blob_writer, |
135 true, | 134 true, |
136 1)); | 135 1)); |
137 return true; | 136 return true; |
138 } | 137 } |
139 | 138 |
140 virtual bool RemoveBlobFile(int64 database_id, int64 key) override { | 139 bool RemoveBlobFile(int64 database_id, int64 key) override { |
141 if (database_id_ != database_id || | 140 if (database_id_ != database_id || |
142 !KeyPrefix::IsValidDatabaseId(database_id)) { | 141 !KeyPrefix::IsValidDatabaseId(database_id)) { |
143 return false; | 142 return false; |
144 } | 143 } |
145 removals_.push_back(key); | 144 removals_.push_back(key); |
146 return true; | 145 return true; |
147 } | 146 } |
148 | 147 |
149 // Timers don't play nicely with unit tests. | 148 // Timers don't play nicely with unit tests. |
150 virtual void StartJournalCleaningTimer() override { | 149 void StartJournalCleaningTimer() override { |
151 CleanPrimaryJournalIgnoreReturn(); | 150 CleanPrimaryJournalIgnoreReturn(); |
152 } | 151 } |
153 | 152 |
154 private: | 153 private: |
155 TestableIndexedDBBackingStore(IndexedDBFactory* indexed_db_factory, | 154 TestableIndexedDBBackingStore(IndexedDBFactory* indexed_db_factory, |
156 const GURL& origin_url, | 155 const GURL& origin_url, |
157 const base::FilePath& blob_path, | 156 const base::FilePath& blob_path, |
158 net::URLRequestContext* request_context, | 157 net::URLRequestContext* request_context, |
159 scoped_ptr<LevelDBDatabase> db, | 158 scoped_ptr<LevelDBDatabase> db, |
160 scoped_ptr<LevelDBComparator> comparator, | 159 scoped_ptr<LevelDBComparator> comparator, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 &data_loss, | 193 &data_loss, |
195 &data_loss_reason, | 194 &data_loss_reason, |
196 &disk_full, | 195 &disk_full, |
197 &status); | 196 &status); |
198 scoped_refptr<TestableIndexedDBBackingStore> testable_store = | 197 scoped_refptr<TestableIndexedDBBackingStore> testable_store = |
199 static_cast<TestableIndexedDBBackingStore*>(backing_store.get()); | 198 static_cast<TestableIndexedDBBackingStore*>(backing_store.get()); |
200 return testable_store; | 199 return testable_store; |
201 } | 200 } |
202 | 201 |
203 protected: | 202 protected: |
204 virtual ~TestIDBFactory() {} | 203 ~TestIDBFactory() override {} |
205 | 204 |
206 virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStoreHelper( | 205 scoped_refptr<IndexedDBBackingStore> OpenBackingStoreHelper( |
207 const GURL& origin_url, | 206 const GURL& origin_url, |
208 const base::FilePath& data_directory, | 207 const base::FilePath& data_directory, |
209 net::URLRequestContext* request_context, | 208 net::URLRequestContext* request_context, |
210 blink::WebIDBDataLoss* data_loss, | 209 blink::WebIDBDataLoss* data_loss, |
211 std::string* data_loss_message, | 210 std::string* data_loss_message, |
212 bool* disk_full, | 211 bool* disk_full, |
213 bool first_time, | 212 bool first_time, |
214 leveldb::Status* status) override { | 213 leveldb::Status* status) override { |
215 DefaultLevelDBFactory leveldb_factory; | 214 DefaultLevelDBFactory leveldb_factory; |
216 return TestableIndexedDBBackingStore::Open(this, | 215 return TestableIndexedDBBackingStore::Open(this, |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 IndexedDBValue m_value3; | 351 IndexedDBValue m_value3; |
353 std::vector<IndexedDBBlobInfo> m_blob_info; | 352 std::vector<IndexedDBBlobInfo> m_blob_info; |
354 | 353 |
355 private: | 354 private: |
356 DISALLOW_COPY_AND_ASSIGN(IndexedDBBackingStoreTest); | 355 DISALLOW_COPY_AND_ASSIGN(IndexedDBBackingStoreTest); |
357 }; | 356 }; |
358 | 357 |
359 class TestCallback : public IndexedDBBackingStore::BlobWriteCallback { | 358 class TestCallback : public IndexedDBBackingStore::BlobWriteCallback { |
360 public: | 359 public: |
361 TestCallback() : called(false), succeeded(false) {} | 360 TestCallback() : called(false), succeeded(false) {} |
362 virtual void Run(bool succeeded_in) override { | 361 void Run(bool succeeded_in) override { |
363 called = true; | 362 called = true; |
364 succeeded = succeeded_in; | 363 succeeded = succeeded_in; |
365 } | 364 } |
366 bool called; | 365 bool called; |
367 bool succeeded; | 366 bool succeeded; |
368 | 367 |
369 protected: | 368 protected: |
370 virtual ~TestCallback() {} | 369 ~TestCallback() override {} |
371 | 370 |
372 private: | 371 private: |
373 DISALLOW_COPY_AND_ASSIGN(TestCallback); | 372 DISALLOW_COPY_AND_ASSIGN(TestCallback); |
374 }; | 373 }; |
375 | 374 |
376 TEST_F(IndexedDBBackingStoreTest, PutGetConsistency) { | 375 TEST_F(IndexedDBBackingStoreTest, PutGetConsistency) { |
377 { | 376 { |
378 IndexedDBBackingStore::Transaction transaction1(backing_store_.get()); | 377 IndexedDBBackingStore::Transaction transaction1(backing_store_.get()); |
379 transaction1.Begin(); | 378 transaction1.Begin(); |
380 ScopedVector<storage::BlobDataHandle> handles; | 379 ScopedVector<storage::BlobDataHandle> handles; |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
998 | 997 |
999 std::vector<base::string16> names = backing_store_->GetDatabaseNames(&s); | 998 std::vector<base::string16> names = backing_store_->GetDatabaseNames(&s); |
1000 EXPECT_TRUE(s.ok()); | 999 EXPECT_TRUE(s.ok()); |
1001 EXPECT_EQ(names.size(), 1ULL); | 1000 EXPECT_EQ(names.size(), 1ULL); |
1002 EXPECT_EQ(names[0], db1_name); | 1001 EXPECT_EQ(names[0], db1_name); |
1003 } | 1002 } |
1004 | 1003 |
1005 } // namespace | 1004 } // namespace |
1006 | 1005 |
1007 } // namespace content | 1006 } // namespace content |
OLD | NEW |