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

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

Powered by Google App Engine
This is Rietveld 408576698