Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: content/browser/indexed_db/indexed_db_factory_unittest.cc

Issue 1841553002: IndexedDB: Use url::Origin rather than GURL for representing origins (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@origin-idb
Patch Set: Rebased Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h> 5 #include <stdint.h>
6 #include <utility> 6 #include <utility>
7 7
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"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/test_simple_task_runner.h" 14 #include "base/test/test_simple_task_runner.h"
15 #include "content/browser/indexed_db/indexed_db_connection.h" 15 #include "content/browser/indexed_db/indexed_db_connection.h"
16 #include "content/browser/indexed_db/indexed_db_context_impl.h" 16 #include "content/browser/indexed_db/indexed_db_context_impl.h"
17 #include "content/browser/indexed_db/indexed_db_factory_impl.h" 17 #include "content/browser/indexed_db/indexed_db_factory_impl.h"
18 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" 18 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h"
19 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" 19 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h"
20 #include "content/browser/quota/mock_quota_manager_proxy.h" 20 #include "content/browser/quota/mock_quota_manager_proxy.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h" 22 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h"
23 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" 23 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h"
24 #include "url/gurl.h" 24 #include "url/gurl.h"
25 #include "url/origin.h"
25 26
26 using base::ASCIIToUTF16; 27 using base::ASCIIToUTF16;
28 using url::Origin;
27 29
28 namespace content { 30 namespace content {
29 31
30 namespace { 32 namespace {
31 33
32 class MockIDBFactory : public IndexedDBFactoryImpl { 34 class MockIDBFactory : public IndexedDBFactoryImpl {
33 public: 35 public:
34 explicit MockIDBFactory(IndexedDBContextImpl* context) 36 explicit MockIDBFactory(IndexedDBContextImpl* context)
35 : IndexedDBFactoryImpl(context) {} 37 : IndexedDBFactoryImpl(context) {}
36 scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore( 38 scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore(
37 const GURL& origin, 39 const Origin& origin,
38 const base::FilePath& data_directory) { 40 const base::FilePath& data_directory) {
39 blink::WebIDBDataLoss data_loss = 41 blink::WebIDBDataLoss data_loss =
40 blink::WebIDBDataLossNone; 42 blink::WebIDBDataLossNone;
41 std::string data_loss_message; 43 std::string data_loss_message;
42 bool disk_full; 44 bool disk_full;
43 leveldb::Status s; 45 leveldb::Status s;
44 scoped_refptr<IndexedDBBackingStore> backing_store = 46 scoped_refptr<IndexedDBBackingStore> backing_store =
45 OpenBackingStore(origin, 47 OpenBackingStore(origin,
46 data_directory, 48 data_directory,
47 NULL /* request_context */, 49 NULL /* request_context */,
48 &data_loss, 50 &data_loss,
49 &data_loss_message, 51 &data_loss_message,
50 &disk_full, 52 &disk_full,
51 &s); 53 &s);
52 EXPECT_EQ(blink::WebIDBDataLossNone, data_loss); 54 EXPECT_EQ(blink::WebIDBDataLossNone, data_loss);
53 return backing_store; 55 return backing_store;
54 } 56 }
55 57
56 void TestCloseBackingStore(IndexedDBBackingStore* backing_store) { 58 void TestCloseBackingStore(IndexedDBBackingStore* backing_store) {
57 CloseBackingStore(backing_store->origin_url()); 59 CloseBackingStore(backing_store->origin());
58 } 60 }
59 61
60 void TestReleaseBackingStore(IndexedDBBackingStore* backing_store, 62 void TestReleaseBackingStore(IndexedDBBackingStore* backing_store,
61 bool immediate) { 63 bool immediate) {
62 ReleaseBackingStore(backing_store->origin_url(), immediate); 64 ReleaseBackingStore(backing_store->origin(), immediate);
63 } 65 }
64 66
65 private: 67 private:
66 ~MockIDBFactory() override {} 68 ~MockIDBFactory() override {}
67 69
68 DISALLOW_COPY_AND_ASSIGN(MockIDBFactory); 70 DISALLOW_COPY_AND_ASSIGN(MockIDBFactory);
69 }; 71 };
70 72
71 } // namespace 73 } // namespace
72 74
(...skipping 21 matching lines...) Expand all
94 96
95 private: 97 private:
96 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 98 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
97 scoped_refptr<IndexedDBContextImpl> context_; 99 scoped_refptr<IndexedDBContextImpl> context_;
98 scoped_refptr<MockIDBFactory> idb_factory_; 100 scoped_refptr<MockIDBFactory> idb_factory_;
99 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; 101 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
100 DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryTest); 102 DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryTest);
101 }; 103 };
102 104
103 TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) { 105 TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) {
104 GURL origin1("http://localhost:81"); 106 const Origin origin1(GURL("http://localhost:81"));
105 GURL origin2("http://localhost:82"); 107 const Origin origin2(GURL("http://localhost:82"));
106 108
107 base::ScopedTempDir temp_directory; 109 base::ScopedTempDir temp_directory;
108 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 110 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
109 scoped_refptr<IndexedDBBackingStore> disk_store1 = 111 scoped_refptr<IndexedDBBackingStore> disk_store1 =
110 factory()->TestOpenBackingStore(origin1, temp_directory.path()); 112 factory()->TestOpenBackingStore(origin1, temp_directory.path());
111 113
112 scoped_refptr<IndexedDBBackingStore> disk_store2 = 114 scoped_refptr<IndexedDBBackingStore> disk_store2 =
113 factory()->TestOpenBackingStore(origin1, temp_directory.path()); 115 factory()->TestOpenBackingStore(origin1, temp_directory.path());
114 EXPECT_EQ(disk_store1.get(), disk_store2.get()); 116 EXPECT_EQ(disk_store1.get(), disk_store2.get());
115 117
116 scoped_refptr<IndexedDBBackingStore> disk_store3 = 118 scoped_refptr<IndexedDBBackingStore> disk_store3 =
117 factory()->TestOpenBackingStore(origin2, temp_directory.path()); 119 factory()->TestOpenBackingStore(origin2, temp_directory.path());
118 120
119 factory()->TestCloseBackingStore(disk_store1.get()); 121 factory()->TestCloseBackingStore(disk_store1.get());
120 factory()->TestCloseBackingStore(disk_store3.get()); 122 factory()->TestCloseBackingStore(disk_store3.get());
121 123
122 EXPECT_FALSE(disk_store1->HasOneRef()); 124 EXPECT_FALSE(disk_store1->HasOneRef());
123 EXPECT_FALSE(disk_store2->HasOneRef()); 125 EXPECT_FALSE(disk_store2->HasOneRef());
124 EXPECT_TRUE(disk_store3->HasOneRef()); 126 EXPECT_TRUE(disk_store3->HasOneRef());
125 127
126 disk_store2 = NULL; 128 disk_store2 = NULL;
127 EXPECT_TRUE(disk_store1->HasOneRef()); 129 EXPECT_TRUE(disk_store1->HasOneRef());
128 } 130 }
129 131
130 TEST_F(IndexedDBFactoryTest, BackingStoreLazyClose) { 132 TEST_F(IndexedDBFactoryTest, BackingStoreLazyClose) {
131 GURL origin("http://localhost:81"); 133 const Origin origin(GURL("http://localhost:81"));
132 134
133 base::ScopedTempDir temp_directory; 135 base::ScopedTempDir temp_directory;
134 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 136 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
135 scoped_refptr<IndexedDBBackingStore> store = 137 scoped_refptr<IndexedDBBackingStore> store =
136 factory()->TestOpenBackingStore(origin, temp_directory.path()); 138 factory()->TestOpenBackingStore(origin, temp_directory.path());
137 139
138 // Give up the local refptr so that the factory has the only 140 // Give up the local refptr so that the factory has the only
139 // outstanding reference. 141 // outstanding reference.
140 IndexedDBBackingStore* store_ptr = store.get(); 142 IndexedDBBackingStore* store_ptr = store.get();
141 store = NULL; 143 store = NULL;
142 EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); 144 EXPECT_FALSE(store_ptr->close_timer()->IsRunning());
143 factory()->TestReleaseBackingStore(store_ptr, false); 145 factory()->TestReleaseBackingStore(store_ptr, false);
144 EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); 146 EXPECT_TRUE(store_ptr->close_timer()->IsRunning());
145 147
146 factory()->TestOpenBackingStore(origin, temp_directory.path()); 148 factory()->TestOpenBackingStore(origin, temp_directory.path());
147 EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); 149 EXPECT_FALSE(store_ptr->close_timer()->IsRunning());
148 factory()->TestReleaseBackingStore(store_ptr, false); 150 factory()->TestReleaseBackingStore(store_ptr, false);
149 EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); 151 EXPECT_TRUE(store_ptr->close_timer()->IsRunning());
150 152
151 // Take back a ref ptr and ensure that the actual close 153 // Take back a ref ptr and ensure that the actual close
152 // stops a running timer. 154 // stops a running timer.
153 store = store_ptr; 155 store = store_ptr;
154 factory()->TestCloseBackingStore(store_ptr); 156 factory()->TestCloseBackingStore(store_ptr);
155 EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); 157 EXPECT_FALSE(store_ptr->close_timer()->IsRunning());
156 } 158 }
157 159
158 TEST_F(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { 160 TEST_F(IndexedDBFactoryTest, MemoryBackingStoreLifetime) {
159 GURL origin1("http://localhost:81"); 161 const Origin origin1(GURL("http://localhost:81"));
160 GURL origin2("http://localhost:82"); 162 const Origin origin2(GURL("http://localhost:82"));
161 163
162 scoped_refptr<IndexedDBBackingStore> mem_store1 = 164 scoped_refptr<IndexedDBBackingStore> mem_store1 =
163 factory()->TestOpenBackingStore(origin1, base::FilePath()); 165 factory()->TestOpenBackingStore(origin1, base::FilePath());
164 166
165 scoped_refptr<IndexedDBBackingStore> mem_store2 = 167 scoped_refptr<IndexedDBBackingStore> mem_store2 =
166 factory()->TestOpenBackingStore(origin1, base::FilePath()); 168 factory()->TestOpenBackingStore(origin1, base::FilePath());
167 EXPECT_EQ(mem_store1.get(), mem_store2.get()); 169 EXPECT_EQ(mem_store1.get(), mem_store2.get());
168 170
169 scoped_refptr<IndexedDBBackingStore> mem_store3 = 171 scoped_refptr<IndexedDBBackingStore> mem_store3 =
170 factory()->TestOpenBackingStore(origin2, base::FilePath()); 172 factory()->TestOpenBackingStore(origin2, base::FilePath());
(...skipping 16 matching lines...) Expand all
187 189
188 TEST_F(IndexedDBFactoryTest, RejectLongOrigins) { 190 TEST_F(IndexedDBFactoryTest, RejectLongOrigins) {
189 base::ScopedTempDir temp_directory; 191 base::ScopedTempDir temp_directory;
190 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 192 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
191 const base::FilePath base_path = temp_directory.path(); 193 const base::FilePath base_path = temp_directory.path();
192 194
193 int limit = base::GetMaximumPathComponentLength(base_path); 195 int limit = base::GetMaximumPathComponentLength(base_path);
194 EXPECT_GT(limit, 0); 196 EXPECT_GT(limit, 0);
195 197
196 std::string origin(limit + 1, 'x'); 198 std::string origin(limit + 1, 'x');
197 GURL too_long_origin("http://" + origin + ":81/"); 199 Origin too_long_origin(GURL("http://" + origin + ":81/"));
198 scoped_refptr<IndexedDBBackingStore> diskStore1 = 200 scoped_refptr<IndexedDBBackingStore> diskStore1 =
199 factory()->TestOpenBackingStore(too_long_origin, base_path); 201 factory()->TestOpenBackingStore(too_long_origin, base_path);
200 EXPECT_FALSE(diskStore1.get()); 202 EXPECT_FALSE(diskStore1.get());
201 203
202 GURL ok_origin("http://someorigin.com:82/"); 204 Origin ok_origin(GURL("http://someorigin.com:82/"));
203 scoped_refptr<IndexedDBBackingStore> diskStore2 = 205 scoped_refptr<IndexedDBBackingStore> diskStore2 =
204 factory()->TestOpenBackingStore(ok_origin, base_path); 206 factory()->TestOpenBackingStore(ok_origin, base_path);
205 EXPECT_TRUE(diskStore2.get()); 207 EXPECT_TRUE(diskStore2.get());
206 // We need a manual close or Windows can't delete the temp directory. 208 // We need a manual close or Windows can't delete the temp directory.
207 factory()->TestCloseBackingStore(diskStore2.get()); 209 factory()->TestCloseBackingStore(diskStore2.get());
208 } 210 }
209 211
210 class DiskFullFactory : public IndexedDBFactoryImpl { 212 class DiskFullFactory : public IndexedDBFactoryImpl {
211 public: 213 public:
212 explicit DiskFullFactory(IndexedDBContextImpl* context) 214 explicit DiskFullFactory(IndexedDBContextImpl* context)
213 : IndexedDBFactoryImpl(context) {} 215 : IndexedDBFactoryImpl(context) {}
214 216
215 private: 217 private:
216 ~DiskFullFactory() override {} 218 ~DiskFullFactory() override {}
217 scoped_refptr<IndexedDBBackingStore> OpenBackingStore( 219 scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
218 const GURL& origin_url, 220 const Origin& origin,
219 const base::FilePath& data_directory, 221 const base::FilePath& data_directory,
220 net::URLRequestContext* request_context, 222 net::URLRequestContext* request_context,
221 blink::WebIDBDataLoss* data_loss, 223 blink::WebIDBDataLoss* data_loss,
222 std::string* data_loss_message, 224 std::string* data_loss_message,
223 bool* disk_full, 225 bool* disk_full,
224 leveldb::Status* s) override { 226 leveldb::Status* s) override {
225 *disk_full = true; 227 *disk_full = true;
226 *s = leveldb::Status::IOError("Disk is full"); 228 *s = leveldb::Status::IOError("Disk is full");
227 return scoped_refptr<IndexedDBBackingStore>(); 229 return scoped_refptr<IndexedDBBackingStore>();
228 } 230 }
(...skipping 12 matching lines...) Expand all
241 bool error_called() const { return error_called_; } 243 bool error_called() const { return error_called_; }
242 244
243 private: 245 private:
244 ~LookingForQuotaErrorMockCallbacks() override {} 246 ~LookingForQuotaErrorMockCallbacks() override {}
245 bool error_called_; 247 bool error_called_;
246 248
247 DISALLOW_COPY_AND_ASSIGN(LookingForQuotaErrorMockCallbacks); 249 DISALLOW_COPY_AND_ASSIGN(LookingForQuotaErrorMockCallbacks);
248 }; 250 };
249 251
250 TEST_F(IndexedDBFactoryTest, QuotaErrorOnDiskFull) { 252 TEST_F(IndexedDBFactoryTest, QuotaErrorOnDiskFull) {
251 const GURL origin("http://localhost:81"); 253 const Origin origin(GURL("http://localhost:81"));
252 base::ScopedTempDir temp_directory; 254 base::ScopedTempDir temp_directory;
253 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 255 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
254 256
255 scoped_refptr<DiskFullFactory> factory = new DiskFullFactory(context()); 257 scoped_refptr<DiskFullFactory> factory = new DiskFullFactory(context());
256 scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks = 258 scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks =
257 new LookingForQuotaErrorMockCallbacks; 259 new LookingForQuotaErrorMockCallbacks;
258 scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks = 260 scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks =
259 new IndexedDBDatabaseCallbacks(NULL, 0, 0); 261 new IndexedDBDatabaseCallbacks(NULL, 0, 0);
260 const base::string16 name(ASCIIToUTF16("name")); 262 const base::string16 name(ASCIIToUTF16("name"));
261 IndexedDBPendingConnection connection(callbacks, 263 IndexedDBPendingConnection connection(callbacks,
262 dummy_database_callbacks, 264 dummy_database_callbacks,
263 0, /* child_process_id */ 265 0, /* child_process_id */
264 2, /* transaction_id */ 266 2, /* transaction_id */
265 1 /* version */); 267 1 /* version */);
266 factory->Open(name, connection, NULL /* request_context */, 268 factory->Open(name, connection, NULL /* request_context */, origin,
267 url::Origin(origin), temp_directory.path()); 269 temp_directory.path());
268 EXPECT_TRUE(callbacks->error_called()); 270 EXPECT_TRUE(callbacks->error_called());
269 } 271 }
270 272
271 TEST_F(IndexedDBFactoryTest, BackingStoreReleasedOnForcedClose) { 273 TEST_F(IndexedDBFactoryTest, BackingStoreReleasedOnForcedClose) {
272 GURL origin("http://localhost:81"); 274 const Origin origin(GURL("http://localhost:81"));
273 275
274 base::ScopedTempDir temp_directory; 276 base::ScopedTempDir temp_directory;
275 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 277 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
276 278
277 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); 279 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks());
278 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( 280 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks(
279 new MockIndexedDBDatabaseCallbacks()); 281 new MockIndexedDBDatabaseCallbacks());
280 const int64_t transaction_id = 1; 282 const int64_t transaction_id = 1;
281 IndexedDBPendingConnection connection( 283 IndexedDBPendingConnection connection(
282 callbacks, db_callbacks, 0, /* child_process_id */ 284 callbacks, db_callbacks, 0, /* child_process_id */
283 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION); 285 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION);
284 factory()->Open(ASCIIToUTF16("db"), connection, NULL /* request_context */, 286 factory()->Open(ASCIIToUTF16("db"), connection, NULL /* request_context */,
285 url::Origin(origin), temp_directory.path()); 287 origin, temp_directory.path());
286 288
287 EXPECT_TRUE(callbacks->connection()); 289 EXPECT_TRUE(callbacks->connection());
288 290
289 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 291 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
290 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); 292 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin));
291 293
292 callbacks->connection()->ForceClose(); 294 callbacks->connection()->ForceClose();
293 295
294 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); 296 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin));
295 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); 297 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin));
296 } 298 }
297 299
298 TEST_F(IndexedDBFactoryTest, BackingStoreReleaseDelayedOnClose) { 300 TEST_F(IndexedDBFactoryTest, BackingStoreReleaseDelayedOnClose) {
299 GURL origin("http://localhost:81"); 301 const Origin origin(GURL("http://localhost:81"));
300 302
301 base::ScopedTempDir temp_directory; 303 base::ScopedTempDir temp_directory;
302 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 304 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
303 305
304 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); 306 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks());
305 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( 307 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks(
306 new MockIndexedDBDatabaseCallbacks()); 308 new MockIndexedDBDatabaseCallbacks());
307 const int64_t transaction_id = 1; 309 const int64_t transaction_id = 1;
308 IndexedDBPendingConnection connection( 310 IndexedDBPendingConnection connection(
309 callbacks, db_callbacks, 0, /* child_process_id */ 311 callbacks, db_callbacks, 0, /* child_process_id */
310 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION); 312 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION);
311 factory()->Open(ASCIIToUTF16("db"), connection, NULL /* request_context */, 313 factory()->Open(ASCIIToUTF16("db"), connection, NULL /* request_context */,
312 url::Origin(origin), temp_directory.path()); 314 origin, temp_directory.path());
313 315
314 EXPECT_TRUE(callbacks->connection()); 316 EXPECT_TRUE(callbacks->connection());
315 IndexedDBBackingStore* store = 317 IndexedDBBackingStore* store =
316 callbacks->connection()->database()->backing_store(); 318 callbacks->connection()->database()->backing_store();
317 EXPECT_FALSE(store->HasOneRef()); // Factory and database. 319 EXPECT_FALSE(store->HasOneRef()); // Factory and database.
318 320
319 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 321 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
320 callbacks->connection()->Close(); 322 callbacks->connection()->Close();
321 EXPECT_TRUE(store->HasOneRef()); // Factory. 323 EXPECT_TRUE(store->HasOneRef()); // Factory.
322 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 324 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
323 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); 325 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin));
324 EXPECT_TRUE(store->close_timer()->IsRunning()); 326 EXPECT_TRUE(store->close_timer()->IsRunning());
325 327
326 // Take a ref so it won't be destroyed out from under the test. 328 // Take a ref so it won't be destroyed out from under the test.
327 scoped_refptr<IndexedDBBackingStore> store_ref = store; 329 scoped_refptr<IndexedDBBackingStore> store_ref = store;
328 // Now simulate shutdown, which should stop the timer. 330 // Now simulate shutdown, which should stop the timer.
329 factory()->ContextDestroyed(); 331 factory()->ContextDestroyed();
330 EXPECT_TRUE(store->HasOneRef()); // Local. 332 EXPECT_TRUE(store->HasOneRef()); // Local.
331 EXPECT_FALSE(store->close_timer()->IsRunning()); 333 EXPECT_FALSE(store->close_timer()->IsRunning());
332 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); 334 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin));
333 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); 335 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin));
334 } 336 }
335 337
336 TEST_F(IndexedDBFactoryTest, DeleteDatabaseClosesBackingStore) { 338 TEST_F(IndexedDBFactoryTest, DeleteDatabaseClosesBackingStore) {
337 GURL origin("http://localhost:81"); 339 const Origin origin(GURL("http://localhost:81"));
338 340
339 base::ScopedTempDir temp_directory; 341 base::ScopedTempDir temp_directory;
340 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 342 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
341 343
342 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); 344 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin));
343 345
344 const bool expect_connection = false; 346 const bool expect_connection = false;
345 scoped_refptr<MockIndexedDBCallbacks> callbacks( 347 scoped_refptr<MockIndexedDBCallbacks> callbacks(
346 new MockIndexedDBCallbacks(expect_connection)); 348 new MockIndexedDBCallbacks(expect_connection));
347 factory()->DeleteDatabase(ASCIIToUTF16("db"), NULL /* request_context */, 349 factory()->DeleteDatabase(ASCIIToUTF16("db"), NULL /* request_context */,
348 callbacks, url::Origin(origin), 350 callbacks, origin, temp_directory.path());
349 temp_directory.path());
350 351
351 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 352 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
352 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); 353 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin));
353 354
354 // Now simulate shutdown, which should stop the timer. 355 // Now simulate shutdown, which should stop the timer.
355 factory()->ContextDestroyed(); 356 factory()->ContextDestroyed();
356 357
357 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); 358 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin));
358 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); 359 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin));
359 } 360 }
360 361
361 TEST_F(IndexedDBFactoryTest, GetDatabaseNamesClosesBackingStore) { 362 TEST_F(IndexedDBFactoryTest, GetDatabaseNamesClosesBackingStore) {
362 GURL origin("http://localhost:81"); 363 const Origin origin(GURL("http://localhost:81"));
363 364
364 base::ScopedTempDir temp_directory; 365 base::ScopedTempDir temp_directory;
365 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 366 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
366 367
367 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); 368 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin));
368 369
369 const bool expect_connection = false; 370 const bool expect_connection = false;
370 scoped_refptr<MockIndexedDBCallbacks> callbacks( 371 scoped_refptr<MockIndexedDBCallbacks> callbacks(
371 new MockIndexedDBCallbacks(expect_connection)); 372 new MockIndexedDBCallbacks(expect_connection));
372 factory()->GetDatabaseNames(callbacks, url::Origin(origin), 373 factory()->GetDatabaseNames(callbacks, origin, temp_directory.path(),
373 temp_directory.path(),
374 NULL /* request_context */); 374 NULL /* request_context */);
375 375
376 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 376 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
377 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); 377 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin));
378 378
379 // Now simulate shutdown, which should stop the timer. 379 // Now simulate shutdown, which should stop the timer.
380 factory()->ContextDestroyed(); 380 factory()->ContextDestroyed();
381 381
382 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); 382 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin));
383 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); 383 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin));
384 } 384 }
385 385
386 TEST_F(IndexedDBFactoryTest, ForceCloseReleasesBackingStore) { 386 TEST_F(IndexedDBFactoryTest, ForceCloseReleasesBackingStore) {
387 GURL origin("http://localhost:81"); 387 const Origin origin(GURL("http://localhost:81"));
388 388
389 base::ScopedTempDir temp_directory; 389 base::ScopedTempDir temp_directory;
390 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 390 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
391 391
392 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); 392 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks());
393 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( 393 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks(
394 new MockIndexedDBDatabaseCallbacks()); 394 new MockIndexedDBDatabaseCallbacks());
395 const int64_t transaction_id = 1; 395 const int64_t transaction_id = 1;
396 IndexedDBPendingConnection connection( 396 IndexedDBPendingConnection connection(
397 callbacks, db_callbacks, 0, /* child_process_id */ 397 callbacks, db_callbacks, 0, /* child_process_id */
398 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION); 398 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION);
399 factory()->Open(ASCIIToUTF16("db"), connection, NULL /* request_context */, 399 factory()->Open(ASCIIToUTF16("db"), connection, NULL /* request_context */,
400 url::Origin(origin), temp_directory.path()); 400 origin, temp_directory.path());
401 401
402 EXPECT_TRUE(callbacks->connection()); 402 EXPECT_TRUE(callbacks->connection());
403 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 403 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
404 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); 404 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin));
405 405
406 callbacks->connection()->Close(); 406 callbacks->connection()->Close();
407 407
408 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); 408 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin));
409 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); 409 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin));
410 410
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 bool saw_error() const { return saw_error_; } 451 bool saw_error() const { return saw_error_; }
452 452
453 private: 453 private:
454 ~ErrorCallbacks() override {} 454 ~ErrorCallbacks() override {}
455 bool saw_error_; 455 bool saw_error_;
456 456
457 DISALLOW_COPY_AND_ASSIGN(ErrorCallbacks); 457 DISALLOW_COPY_AND_ASSIGN(ErrorCallbacks);
458 }; 458 };
459 459
460 TEST_F(IndexedDBFactoryTest, DatabaseFailedOpen) { 460 TEST_F(IndexedDBFactoryTest, DatabaseFailedOpen) {
461 GURL origin("http://localhost:81"); 461 const Origin origin(GURL("http://localhost:81"));
462 462
463 base::ScopedTempDir temp_directory; 463 base::ScopedTempDir temp_directory;
464 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 464 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
465 465
466 const base::string16 db_name(ASCIIToUTF16("db")); 466 const base::string16 db_name(ASCIIToUTF16("db"));
467 const int64_t db_version = 2; 467 const int64_t db_version = 2;
468 const int64_t transaction_id = 1; 468 const int64_t transaction_id = 1;
469 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks( 469 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks(
470 new MockIndexedDBDatabaseCallbacks()); 470 new MockIndexedDBDatabaseCallbacks());
471 471
472 // Open at version 2, then close. 472 // Open at version 2, then close.
473 { 473 {
474 scoped_refptr<MockIndexedDBCallbacks> callbacks( 474 scoped_refptr<MockIndexedDBCallbacks> callbacks(
475 new UpgradeNeededCallbacks()); 475 new UpgradeNeededCallbacks());
476 IndexedDBPendingConnection connection(callbacks, 476 IndexedDBPendingConnection connection(callbacks,
477 db_callbacks, 477 db_callbacks,
478 0, /* child_process_id */ 478 0, /* child_process_id */
479 transaction_id, 479 transaction_id,
480 db_version); 480 db_version);
481 factory()->Open(db_name, connection, NULL /* request_context */, 481 factory()->Open(db_name, connection, NULL /* request_context */, origin,
482 url::Origin(origin), temp_directory.path()); 482 temp_directory.path());
483 EXPECT_TRUE(factory()->IsDatabaseOpen(origin, db_name)); 483 EXPECT_TRUE(factory()->IsDatabaseOpen(origin, db_name));
484 484
485 // Pump the message loop so the upgrade transaction can run. 485 // Pump the message loop so the upgrade transaction can run.
486 base::MessageLoop::current()->RunUntilIdle(); 486 base::MessageLoop::current()->RunUntilIdle();
487 EXPECT_TRUE(callbacks->connection()); 487 EXPECT_TRUE(callbacks->connection());
488 callbacks->connection()->database()->Commit(transaction_id); 488 callbacks->connection()->database()->Commit(transaction_id);
489 489
490 callbacks->connection()->Close(); 490 callbacks->connection()->Close();
491 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); 491 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name));
492 } 492 }
493 493
494 // Open at version < 2, which will fail; ensure factory doesn't retain 494 // Open at version < 2, which will fail; ensure factory doesn't retain
495 // the database object. 495 // the database object.
496 { 496 {
497 scoped_refptr<ErrorCallbacks> callbacks(new ErrorCallbacks()); 497 scoped_refptr<ErrorCallbacks> callbacks(new ErrorCallbacks());
498 IndexedDBPendingConnection connection(callbacks, 498 IndexedDBPendingConnection connection(callbacks,
499 db_callbacks, 499 db_callbacks,
500 0, /* child_process_id */ 500 0, /* child_process_id */
501 transaction_id, 501 transaction_id,
502 db_version - 1); 502 db_version - 1);
503 factory()->Open(db_name, connection, NULL /* request_context */, 503 factory()->Open(db_name, connection, NULL /* request_context */, origin,
504 url::Origin(origin), temp_directory.path()); 504 temp_directory.path());
505 EXPECT_TRUE(callbacks->saw_error()); 505 EXPECT_TRUE(callbacks->saw_error());
506 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); 506 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name));
507 } 507 }
508 508
509 // Terminate all pending-close timers. 509 // Terminate all pending-close timers.
510 factory()->ForceClose(origin); 510 factory()->ForceClose(origin);
511 } 511 }
512 512
513 } // namespace content 513 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_factory_impl.cc ('k') | content/browser/indexed_db/indexed_db_fake_backing_store.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698