OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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_database.h" | 5 #include "content/browser/indexed_db/indexed_db_database.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include <set> | 9 #include <set> |
8 | 10 |
9 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" |
11 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
12 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
13 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
14 #include "content/browser/indexed_db/indexed_db.h" | 17 #include "content/browser/indexed_db/indexed_db.h" |
15 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 18 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
16 #include "content/browser/indexed_db/indexed_db_callbacks.h" | 19 #include "content/browser/indexed_db/indexed_db_callbacks.h" |
17 #include "content/browser/indexed_db/indexed_db_class_factory.h" | 20 #include "content/browser/indexed_db/indexed_db_class_factory.h" |
18 #include "content/browser/indexed_db/indexed_db_connection.h" | 21 #include "content/browser/indexed_db/indexed_db_connection.h" |
19 #include "content/browser/indexed_db/indexed_db_cursor.h" | 22 #include "content/browser/indexed_db/indexed_db_cursor.h" |
20 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" | 23 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 backing_store.get(), | 67 backing_store.get(), |
65 factory.get(), | 68 factory.get(), |
66 IndexedDBDatabase::Identifier(), | 69 IndexedDBDatabase::Identifier(), |
67 &s); | 70 &s); |
68 ASSERT_TRUE(s.ok()); | 71 ASSERT_TRUE(s.ok()); |
69 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 72 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
70 | 73 |
71 scoped_refptr<MockIndexedDBCallbacks> request1(new MockIndexedDBCallbacks()); | 74 scoped_refptr<MockIndexedDBCallbacks> request1(new MockIndexedDBCallbacks()); |
72 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks1( | 75 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks1( |
73 new MockIndexedDBDatabaseCallbacks()); | 76 new MockIndexedDBDatabaseCallbacks()); |
74 const int64 transaction_id1 = 1; | 77 const int64_t transaction_id1 = 1; |
75 IndexedDBPendingConnection connection1( | 78 IndexedDBPendingConnection connection1( |
76 request1, | 79 request1, |
77 callbacks1, | 80 callbacks1, |
78 kFakeChildProcessId, | 81 kFakeChildProcessId, |
79 transaction_id1, | 82 transaction_id1, |
80 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); | 83 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
81 db->OpenConnection(connection1); | 84 db->OpenConnection(connection1); |
82 | 85 |
83 EXPECT_FALSE(backing_store->HasOneRef()); // db, connection count > 0 | 86 EXPECT_FALSE(backing_store->HasOneRef()); // db, connection count > 0 |
84 | 87 |
85 scoped_refptr<MockIndexedDBCallbacks> request2(new MockIndexedDBCallbacks()); | 88 scoped_refptr<MockIndexedDBCallbacks> request2(new MockIndexedDBCallbacks()); |
86 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks2( | 89 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks2( |
87 new MockIndexedDBDatabaseCallbacks()); | 90 new MockIndexedDBDatabaseCallbacks()); |
88 const int64 transaction_id2 = 2; | 91 const int64_t transaction_id2 = 2; |
89 IndexedDBPendingConnection connection2( | 92 IndexedDBPendingConnection connection2( |
90 request2, | 93 request2, |
91 callbacks2, | 94 callbacks2, |
92 kFakeChildProcessId, | 95 kFakeChildProcessId, |
93 transaction_id2, | 96 transaction_id2, |
94 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); | 97 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
95 db->OpenConnection(connection2); | 98 db->OpenConnection(connection2); |
96 | 99 |
97 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection | 100 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection |
98 | 101 |
(...skipping 23 matching lines...) Expand all Loading... |
122 backing_store.get(), | 125 backing_store.get(), |
123 factory.get(), | 126 factory.get(), |
124 IndexedDBDatabase::Identifier(), | 127 IndexedDBDatabase::Identifier(), |
125 &s); | 128 &s); |
126 ASSERT_TRUE(s.ok()); | 129 ASSERT_TRUE(s.ok()); |
127 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 130 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
128 | 131 |
129 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks( | 132 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks( |
130 new MockIndexedDBDatabaseCallbacks()); | 133 new MockIndexedDBDatabaseCallbacks()); |
131 scoped_refptr<MockIndexedDBCallbacks> request(new MockIndexedDBCallbacks()); | 134 scoped_refptr<MockIndexedDBCallbacks> request(new MockIndexedDBCallbacks()); |
132 const int64 upgrade_transaction_id = 3; | 135 const int64_t upgrade_transaction_id = 3; |
133 IndexedDBPendingConnection connection( | 136 IndexedDBPendingConnection connection( |
134 request, | 137 request, |
135 callbacks, | 138 callbacks, |
136 kFakeChildProcessId, | 139 kFakeChildProcessId, |
137 upgrade_transaction_id, | 140 upgrade_transaction_id, |
138 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); | 141 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
139 database->OpenConnection(connection); | 142 database->OpenConnection(connection); |
140 EXPECT_EQ(database.get(), request->connection()->database()); | 143 EXPECT_EQ(database.get(), request->connection()->database()); |
141 | 144 |
142 const int64 transaction_id = 123; | 145 const int64_t transaction_id = 123; |
143 const std::vector<int64> scope; | 146 const std::vector<int64_t> scope; |
144 database->CreateTransaction(transaction_id, | 147 database->CreateTransaction(transaction_id, |
145 request->connection(), | 148 request->connection(), |
146 scope, | 149 scope, |
147 blink::WebIDBTransactionModeReadOnly); | 150 blink::WebIDBTransactionModeReadOnly); |
148 | 151 |
149 request->connection()->ForceClose(); | 152 request->connection()->ForceClose(); |
150 | 153 |
151 EXPECT_TRUE(backing_store->HasOneRef()); // local | 154 EXPECT_TRUE(backing_store->HasOneRef()); // local |
152 EXPECT_TRUE(callbacks->abort_called()); | 155 EXPECT_TRUE(callbacks->abort_called()); |
153 } | 156 } |
154 | 157 |
155 class MockDeleteCallbacks : public IndexedDBCallbacks { | 158 class MockDeleteCallbacks : public IndexedDBCallbacks { |
156 public: | 159 public: |
157 MockDeleteCallbacks() | 160 MockDeleteCallbacks() |
158 : IndexedDBCallbacks(NULL, 0, 0), | 161 : IndexedDBCallbacks(NULL, 0, 0), |
159 blocked_called_(false), | 162 blocked_called_(false), |
160 success_called_(false) {} | 163 success_called_(false) {} |
161 | 164 |
162 void OnBlocked(int64 existing_version) override { blocked_called_ = true; } | 165 void OnBlocked(int64_t existing_version) override { blocked_called_ = true; } |
163 void OnSuccess(int64 result) override { success_called_ = true; } | 166 void OnSuccess(int64_t result) override { success_called_ = true; } |
164 | 167 |
165 bool blocked_called() const { return blocked_called_; } | 168 bool blocked_called() const { return blocked_called_; } |
166 bool success_called() const { return success_called_; } | 169 bool success_called() const { return success_called_; } |
167 | 170 |
168 private: | 171 private: |
169 ~MockDeleteCallbacks() override {} | 172 ~MockDeleteCallbacks() override {} |
170 | 173 |
171 bool blocked_called_; | 174 bool blocked_called_; |
172 bool success_called_; | 175 bool success_called_; |
173 | 176 |
(...skipping 12 matching lines...) Expand all Loading... |
186 backing_store.get(), | 189 backing_store.get(), |
187 factory.get(), | 190 factory.get(), |
188 IndexedDBDatabase::Identifier(), | 191 IndexedDBDatabase::Identifier(), |
189 &s); | 192 &s); |
190 ASSERT_TRUE(s.ok()); | 193 ASSERT_TRUE(s.ok()); |
191 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 194 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
192 | 195 |
193 scoped_refptr<MockIndexedDBCallbacks> request1(new MockIndexedDBCallbacks()); | 196 scoped_refptr<MockIndexedDBCallbacks> request1(new MockIndexedDBCallbacks()); |
194 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks1( | 197 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks1( |
195 new MockIndexedDBDatabaseCallbacks()); | 198 new MockIndexedDBDatabaseCallbacks()); |
196 const int64 transaction_id1 = 1; | 199 const int64_t transaction_id1 = 1; |
197 IndexedDBPendingConnection connection( | 200 IndexedDBPendingConnection connection( |
198 request1, | 201 request1, |
199 callbacks1, | 202 callbacks1, |
200 kFakeChildProcessId, | 203 kFakeChildProcessId, |
201 transaction_id1, | 204 transaction_id1, |
202 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); | 205 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
203 db->OpenConnection(connection); | 206 db->OpenConnection(connection); |
204 | 207 |
205 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 208 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
206 | 209 |
(...skipping 27 matching lines...) Expand all Loading... |
234 leveldb::Status s; | 237 leveldb::Status s; |
235 db_ = IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 238 db_ = IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
236 backing_store_.get(), | 239 backing_store_.get(), |
237 factory_.get(), | 240 factory_.get(), |
238 IndexedDBDatabase::Identifier(), | 241 IndexedDBDatabase::Identifier(), |
239 &s); | 242 &s); |
240 ASSERT_TRUE(s.ok()); | 243 ASSERT_TRUE(s.ok()); |
241 | 244 |
242 request_ = new MockIndexedDBCallbacks(); | 245 request_ = new MockIndexedDBCallbacks(); |
243 callbacks_ = new MockIndexedDBDatabaseCallbacks(); | 246 callbacks_ = new MockIndexedDBDatabaseCallbacks(); |
244 const int64 transaction_id = 1; | 247 const int64_t transaction_id = 1; |
245 db_->OpenConnection(IndexedDBPendingConnection( | 248 db_->OpenConnection(IndexedDBPendingConnection( |
246 request_, | 249 request_, |
247 callbacks_, | 250 callbacks_, |
248 kFakeChildProcessId, | 251 kFakeChildProcessId, |
249 transaction_id, | 252 transaction_id, |
250 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION)); | 253 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION)); |
251 EXPECT_EQ(IndexedDBDatabaseMetadata::NO_INT_VERSION, | 254 EXPECT_EQ(IndexedDBDatabaseMetadata::NO_INT_VERSION, |
252 db_->metadata().int_version); | 255 db_->metadata().int_version); |
253 | 256 |
254 transaction_ = IndexedDBClassFactory::Get()->CreateIndexedDBTransaction( | 257 transaction_ = IndexedDBClassFactory::Get()->CreateIndexedDBTransaction( |
255 transaction_id, callbacks_, std::set<int64>() /*scope*/, | 258 transaction_id, callbacks_, std::set<int64_t>() /*scope*/, |
256 blink::WebIDBTransactionModeVersionChange, db_.get(), | 259 blink::WebIDBTransactionModeVersionChange, db_.get(), |
257 new IndexedDBFakeBackingStore::FakeTransaction(commit_success_)); | 260 new IndexedDBFakeBackingStore::FakeTransaction(commit_success_)); |
258 db_->TransactionCreated(transaction_.get()); | 261 db_->TransactionCreated(transaction_.get()); |
259 | 262 |
260 // Add a dummy task which takes the place of the VersionChangeOperation | 263 // Add a dummy task which takes the place of the VersionChangeOperation |
261 // which kicks off the upgrade. This ensures that the transaction has | 264 // which kicks off the upgrade. This ensures that the transaction has |
262 // processed at least one task before the CreateObjectStore call. | 265 // processed at least one task before the CreateObjectStore call. |
263 transaction_->ScheduleTask(base::Bind(&DummyOperation)); | 266 transaction_->ScheduleTask(base::Bind(&DummyOperation)); |
264 } | 267 } |
265 | 268 |
(...skipping 10 matching lines...) Expand all Loading... |
276 | 279 |
277 private: | 280 private: |
278 base::MessageLoop message_loop_; | 281 base::MessageLoop message_loop_; |
279 scoped_refptr<MockIndexedDBFactory> factory_; | 282 scoped_refptr<MockIndexedDBFactory> factory_; |
280 | 283 |
281 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest); | 284 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest); |
282 }; | 285 }; |
283 | 286 |
284 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) { | 287 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) { |
285 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 288 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
286 const int64 store_id = 1001; | 289 const int64_t store_id = 1001; |
287 db_->CreateObjectStore(transaction_->id(), | 290 db_->CreateObjectStore(transaction_->id(), |
288 store_id, | 291 store_id, |
289 ASCIIToUTF16("store"), | 292 ASCIIToUTF16("store"), |
290 IndexedDBKeyPath(), | 293 IndexedDBKeyPath(), |
291 false /*auto_increment*/); | 294 false /*auto_increment*/); |
292 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); | 295 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); |
293 RunPostedTasks(); | 296 RunPostedTasks(); |
294 transaction_->Commit(); | 297 transaction_->Commit(); |
295 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); | 298 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); |
296 } | 299 } |
297 | 300 |
298 TEST_F(IndexedDBDatabaseOperationTest, CreateIndex) { | 301 TEST_F(IndexedDBDatabaseOperationTest, CreateIndex) { |
299 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 302 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
300 const int64 store_id = 1001; | 303 const int64_t store_id = 1001; |
301 db_->CreateObjectStore(transaction_->id(), | 304 db_->CreateObjectStore(transaction_->id(), |
302 store_id, | 305 store_id, |
303 ASCIIToUTF16("store"), | 306 ASCIIToUTF16("store"), |
304 IndexedDBKeyPath(), | 307 IndexedDBKeyPath(), |
305 false /*auto_increment*/); | 308 false /*auto_increment*/); |
306 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); | 309 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); |
307 const int64 index_id = 2002; | 310 const int64_t index_id = 2002; |
308 db_->CreateIndex(transaction_->id(), | 311 db_->CreateIndex(transaction_->id(), |
309 store_id, | 312 store_id, |
310 index_id, | 313 index_id, |
311 ASCIIToUTF16("index"), | 314 ASCIIToUTF16("index"), |
312 IndexedDBKeyPath(), | 315 IndexedDBKeyPath(), |
313 false /*unique*/, | 316 false /*unique*/, |
314 false /*multi_entry*/); | 317 false /*multi_entry*/); |
315 EXPECT_EQ( | 318 EXPECT_EQ( |
316 1ULL, | 319 1ULL, |
317 db_->metadata().object_stores.find(store_id)->second.indexes.size()); | 320 db_->metadata().object_stores.find(store_id)->second.indexes.size()); |
(...skipping 11 matching lines...) Expand all Loading... |
329 IndexedDBDatabaseOperationAbortTest() { | 332 IndexedDBDatabaseOperationAbortTest() { |
330 commit_success_ = leveldb::Status::NotFound("Bummer."); | 333 commit_success_ = leveldb::Status::NotFound("Bummer."); |
331 } | 334 } |
332 | 335 |
333 private: | 336 private: |
334 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationAbortTest); | 337 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationAbortTest); |
335 }; | 338 }; |
336 | 339 |
337 TEST_F(IndexedDBDatabaseOperationAbortTest, CreateObjectStore) { | 340 TEST_F(IndexedDBDatabaseOperationAbortTest, CreateObjectStore) { |
338 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 341 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
339 const int64 store_id = 1001; | 342 const int64_t store_id = 1001; |
340 db_->CreateObjectStore(transaction_->id(), | 343 db_->CreateObjectStore(transaction_->id(), |
341 store_id, | 344 store_id, |
342 ASCIIToUTF16("store"), | 345 ASCIIToUTF16("store"), |
343 IndexedDBKeyPath(), | 346 IndexedDBKeyPath(), |
344 false /*auto_increment*/); | 347 false /*auto_increment*/); |
345 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); | 348 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); |
346 RunPostedTasks(); | 349 RunPostedTasks(); |
347 transaction_->Commit(); | 350 transaction_->Commit(); |
348 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 351 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
349 } | 352 } |
350 | 353 |
351 TEST_F(IndexedDBDatabaseOperationAbortTest, CreateIndex) { | 354 TEST_F(IndexedDBDatabaseOperationAbortTest, CreateIndex) { |
352 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 355 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
353 const int64 store_id = 1001; | 356 const int64_t store_id = 1001; |
354 db_->CreateObjectStore(transaction_->id(), | 357 db_->CreateObjectStore(transaction_->id(), |
355 store_id, | 358 store_id, |
356 ASCIIToUTF16("store"), | 359 ASCIIToUTF16("store"), |
357 IndexedDBKeyPath(), | 360 IndexedDBKeyPath(), |
358 false /*auto_increment*/); | 361 false /*auto_increment*/); |
359 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); | 362 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); |
360 const int64 index_id = 2002; | 363 const int64_t index_id = 2002; |
361 db_->CreateIndex(transaction_->id(), | 364 db_->CreateIndex(transaction_->id(), |
362 store_id, | 365 store_id, |
363 index_id, | 366 index_id, |
364 ASCIIToUTF16("index"), | 367 ASCIIToUTF16("index"), |
365 IndexedDBKeyPath(), | 368 IndexedDBKeyPath(), |
366 false /*unique*/, | 369 false /*unique*/, |
367 false /*multi_entry*/); | 370 false /*multi_entry*/); |
368 EXPECT_EQ( | 371 EXPECT_EQ( |
369 1ULL, | 372 1ULL, |
370 db_->metadata().object_stores.find(store_id)->second.indexes.size()); | 373 db_->metadata().object_stores.find(store_id)->second.indexes.size()); |
371 RunPostedTasks(); | 374 RunPostedTasks(); |
372 transaction_->Commit(); | 375 transaction_->Commit(); |
373 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 376 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
374 } | 377 } |
375 | 378 |
376 TEST_F(IndexedDBDatabaseOperationTest, CreatePutDelete) { | 379 TEST_F(IndexedDBDatabaseOperationTest, CreatePutDelete) { |
377 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 380 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
378 const int64 store_id = 1001; | 381 const int64_t store_id = 1001; |
379 | 382 |
380 // Creation is synchronous. | 383 // Creation is synchronous. |
381 db_->CreateObjectStore(transaction_->id(), | 384 db_->CreateObjectStore(transaction_->id(), |
382 store_id, | 385 store_id, |
383 ASCIIToUTF16("store"), | 386 ASCIIToUTF16("store"), |
384 IndexedDBKeyPath(), | 387 IndexedDBKeyPath(), |
385 false /*auto_increment*/); | 388 false /*auto_increment*/); |
386 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); | 389 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); |
387 | 390 |
388 | 391 |
(...skipping 19 matching lines...) Expand all Loading... |
408 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); | 411 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); |
409 | 412 |
410 // This will execute the Put then Delete. | 413 // This will execute the Put then Delete. |
411 RunPostedTasks(); | 414 RunPostedTasks(); |
412 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 415 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
413 | 416 |
414 transaction_->Commit(); // Cleans up the object hierarchy. | 417 transaction_->Commit(); // Cleans up the object hierarchy. |
415 } | 418 } |
416 | 419 |
417 } // namespace content | 420 } // namespace content |
OLD | NEW |