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

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

Issue 2386683003: IndexedDB: Ignore open/delete requests from terminated renderers. (Closed)
Patch Set: Created 4 years, 2 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 "content/browser/indexed_db/indexed_db_database.h" 5 #include "content/browser/indexed_db/indexed_db_database.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 request->connection(), 143 request->connection(),
144 scope, 144 scope,
145 blink::WebIDBTransactionModeReadOnly); 145 blink::WebIDBTransactionModeReadOnly);
146 146
147 request->connection()->ForceClose(); 147 request->connection()->ForceClose();
148 148
149 EXPECT_TRUE(backing_store->HasOneRef()); // local 149 EXPECT_TRUE(backing_store->HasOneRef()); // local
150 EXPECT_TRUE(callbacks->abort_called()); 150 EXPECT_TRUE(callbacks->abort_called());
151 } 151 }
152 152
153 class MockDeleteCallbacks : public IndexedDBCallbacks { 153 class MockCallbacks : public IndexedDBCallbacks {
154 public: 154 public:
155 MockDeleteCallbacks() 155 MockCallbacks()
156 : IndexedDBCallbacks(NULL, 0, 0), 156 : IndexedDBCallbacks(NULL, 0, 0) {}
157 blocked_called_(false),
158 success_called_(false) {}
159 157
160 void OnBlocked(int64_t existing_version) override { blocked_called_ = true; } 158 void OnBlocked(int64_t existing_version) override { blocked_called_ = true; }
161 void OnSuccess(int64_t result) override { success_called_ = true; } 159 void OnSuccess(int64_t result) override { success_called_ = true; }
160 void OnError(const IndexedDBDatabaseError& error) override {
161 error_called_ = true;
162 }
163 bool IsValid() const override { return valid_; }
162 164
163 bool blocked_called() const { return blocked_called_; } 165 bool blocked_called() const { return blocked_called_; }
164 bool success_called() const { return success_called_; } 166 bool success_called() const { return success_called_; }
167 bool error_called() const { return error_called_; }
168 void set_valid(bool valid) { valid_ = valid; }
165 169
166 private: 170 private:
167 ~MockDeleteCallbacks() override {} 171 ~MockCallbacks() override {}
168 172
169 bool blocked_called_; 173 bool blocked_called_ = false;
170 bool success_called_; 174 bool success_called_ = false;
175 bool error_called_ = false;
176 bool valid_ = true;
171 177
172 DISALLOW_COPY_AND_ASSIGN(MockDeleteCallbacks); 178 DISALLOW_COPY_AND_ASSIGN(MockCallbacks);
173 }; 179 };
174 180
175 TEST(IndexedDBDatabaseTest, PendingDelete) { 181 TEST(IndexedDBDatabaseTest, PendingDelete) {
176 scoped_refptr<IndexedDBFakeBackingStore> backing_store = 182 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
177 new IndexedDBFakeBackingStore(); 183 new IndexedDBFakeBackingStore();
178 EXPECT_TRUE(backing_store->HasOneRef()); // local 184 EXPECT_TRUE(backing_store->HasOneRef()); // local
179 185
180 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); 186 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory();
181 leveldb::Status s; 187 leveldb::Status s;
182 scoped_refptr<IndexedDBDatabase> db = 188 scoped_refptr<IndexedDBDatabase> db =
(...skipping 13 matching lines...) Expand all
196 base::MakeUnique<IndexedDBPendingConnection>( 202 base::MakeUnique<IndexedDBPendingConnection>(
197 request1, callbacks1, kFakeChildProcessId, transaction_id1, 203 request1, callbacks1, kFakeChildProcessId, transaction_id1,
198 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); 204 IndexedDBDatabaseMetadata::DEFAULT_VERSION));
199 db->OpenConnection(std::move(connection)); 205 db->OpenConnection(std::move(connection));
200 206
201 EXPECT_EQ(db->ConnectionCount(), 1UL); 207 EXPECT_EQ(db->ConnectionCount(), 1UL);
202 EXPECT_EQ(db->ActiveOpenDeleteCount(), 0UL); 208 EXPECT_EQ(db->ActiveOpenDeleteCount(), 0UL);
203 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL); 209 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL);
204 EXPECT_FALSE(backing_store->HasOneRef()); // local and db 210 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
205 211
206 scoped_refptr<MockDeleteCallbacks> request2(new MockDeleteCallbacks()); 212 scoped_refptr<MockCallbacks> request2(new MockCallbacks());
207 db->DeleteDatabase(request2); 213 db->DeleteDatabase(request2);
208 EXPECT_EQ(db->ConnectionCount(), 1UL); 214 EXPECT_EQ(db->ConnectionCount(), 1UL);
209 EXPECT_EQ(db->ActiveOpenDeleteCount(), 1UL); 215 EXPECT_EQ(db->ActiveOpenDeleteCount(), 1UL);
210 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL); 216 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL);
211 217
212 EXPECT_FALSE(request2->blocked_called()); 218 EXPECT_FALSE(request2->blocked_called());
213 db->VersionChangeIgnored(); 219 db->VersionChangeIgnored();
214 EXPECT_TRUE(request2->blocked_called()); 220 EXPECT_TRUE(request2->blocked_called());
215 EXPECT_EQ(db->ConnectionCount(), 1UL); 221 EXPECT_EQ(db->ConnectionCount(), 1UL);
216 EXPECT_EQ(db->ActiveOpenDeleteCount(), 1UL); 222 EXPECT_EQ(db->ActiveOpenDeleteCount(), 1UL);
217 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL); 223 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL);
218 224
219 EXPECT_FALSE(backing_store->HasOneRef()); // local and db 225 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
220 226
221 db->Close(request1->connection(), true /* forced */); 227 db->Close(request1->connection(), true /* forced */);
222 EXPECT_EQ(db->ConnectionCount(), 0UL); 228 EXPECT_EQ(db->ConnectionCount(), 0UL);
223 EXPECT_EQ(db->ActiveOpenDeleteCount(), 0UL); 229 EXPECT_EQ(db->ActiveOpenDeleteCount(), 0UL);
224 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL); 230 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL);
225 231
226 EXPECT_FALSE(db->backing_store()); 232 EXPECT_FALSE(db->backing_store());
227 EXPECT_TRUE(backing_store->HasOneRef()); // local 233 EXPECT_TRUE(backing_store->HasOneRef()); // local
228 EXPECT_TRUE(request2->success_called()); 234 EXPECT_TRUE(request2->success_called());
229 } 235 }
230 236
237 TEST(IndexedDBDatabaseTest, ConnectionRequestsNoLongerValid) {
238 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
239 new IndexedDBFakeBackingStore();
240
241 const int64_t transaction_id1 = 1;
242 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory();
243 leveldb::Status s;
244 scoped_refptr<IndexedDBDatabase> db =
245 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
246 backing_store.get(),
247 factory.get(),
248 IndexedDBDatabase::Identifier(),
249 &s);
250
251 // Make a connection request. This will be processed immediately.
252 scoped_refptr<MockIndexedDBCallbacks> request1(new MockIndexedDBCallbacks());
253 {
254 std::unique_ptr<IndexedDBPendingConnection> connection(
255 base::MakeUnique<IndexedDBPendingConnection>(
256 request1, make_scoped_refptr(new MockIndexedDBDatabaseCallbacks()),
257 kFakeChildProcessId, transaction_id1,
258 IndexedDBDatabaseMetadata::DEFAULT_VERSION));
259 db->OpenConnection(std::move(connection));
260 }
261
262 EXPECT_EQ(db->ConnectionCount(), 1UL);
263 EXPECT_EQ(db->ActiveOpenDeleteCount(), 0UL);
264 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL);
265
266 // Make a delete request. This will be blocked by the open.
267 scoped_refptr<MockCallbacks> request2(new MockCallbacks());
268 db->DeleteDatabase(request2);
269
270 EXPECT_EQ(db->ConnectionCount(), 1UL);
271 EXPECT_EQ(db->ActiveOpenDeleteCount(), 1UL);
272 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL);
273
274 db->VersionChangeIgnored();
275
276 EXPECT_TRUE(request2->blocked_called());
277
278 // Make another delete request. This will be waiting in the queue.
279 scoped_refptr<MockCallbacks> request3(new MockCallbacks());
280 db->DeleteDatabase(request3);
281 EXPECT_FALSE(request3->HasOneRef()); // local, db
282
283 EXPECT_EQ(db->ConnectionCount(), 1UL);
284 EXPECT_EQ(db->ActiveOpenDeleteCount(), 1UL);
285 EXPECT_EQ(db->PendingOpenDeleteCount(), 1UL);
286
287 // Make another connection request. This will also be waiting in the queue.
288 scoped_refptr<MockCallbacks> request4(new MockCallbacks());
289 {
290 std::unique_ptr<IndexedDBPendingConnection> connection(
291 base::MakeUnique<IndexedDBPendingConnection>(
292 request4, make_scoped_refptr(new MockIndexedDBDatabaseCallbacks()),
293 kFakeChildProcessId, transaction_id1,
294 IndexedDBDatabaseMetadata::DEFAULT_VERSION));
295 db->OpenConnection(std::move(connection));
296 }
297
298 EXPECT_EQ(db->ConnectionCount(), 1UL);
299 EXPECT_EQ(db->ActiveOpenDeleteCount(), 1UL);
300 EXPECT_EQ(db->PendingOpenDeleteCount(), 2UL);
301
302 // Finally yet another delete request, also waiting in the queue.
303 scoped_refptr<MockCallbacks> request5(new MockCallbacks());
304 db->DeleteDatabase(request2);
305
306 EXPECT_EQ(db->ConnectionCount(), 1UL);
307 EXPECT_EQ(db->ActiveOpenDeleteCount(), 1UL);
308 EXPECT_EQ(db->PendingOpenDeleteCount(), 3UL);
309
310 // Simulate renderer going away.
311 request3->set_valid(false);
312 request4->set_valid(false);
313
314 // Close the blocking connection. First delete request should succeed.
315 EXPECT_FALSE(request2->success_called());
316 db->Close(request1->connection(), true /* forced */);
317 EXPECT_TRUE(request2->success_called());
318
319 // Invalid requests should be abandoned.
320 EXPECT_FALSE(request3->blocked_called());
321 EXPECT_FALSE(request3->success_called());
322 EXPECT_FALSE(request3->error_called());
323 EXPECT_TRUE(request3->HasOneRef()); // local
324
325 EXPECT_FALSE(request4->blocked_called());
326 EXPECT_FALSE(request4->success_called());
327 EXPECT_FALSE(request4->error_called());
328 EXPECT_TRUE(request4->HasOneRef()); // local
329
330 // Final delete request should also run.
331 EXPECT_TRUE(request2->success_called());
332
333 // And everything else should be complete.
334 EXPECT_EQ(db->ConnectionCount(), 0UL);
335 EXPECT_EQ(db->ActiveOpenDeleteCount(), 0UL);
336 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL);
337 }
338
231 void DummyOperation(IndexedDBTransaction* transaction) { 339 void DummyOperation(IndexedDBTransaction* transaction) {
232 } 340 }
233 341
234 class IndexedDBDatabaseOperationTest : public testing::Test { 342 class IndexedDBDatabaseOperationTest : public testing::Test {
235 public: 343 public:
236 IndexedDBDatabaseOperationTest() 344 IndexedDBDatabaseOperationTest()
237 : commit_success_(leveldb::Status::OK()), 345 : commit_success_(leveldb::Status::OK()),
238 factory_(new MockIndexedDBFactory()) {} 346 factory_(new MockIndexedDBFactory()) {}
239 347
240 void SetUp() override { 348 void SetUp() override {
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 519 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
412 520
413 // This will execute the Put then Delete. 521 // This will execute the Put then Delete.
414 RunPostedTasks(); 522 RunPostedTasks();
415 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 523 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
416 524
417 transaction_->Commit(); // Cleans up the object hierarchy. 525 transaction_->Commit(); // Cleans up the object hierarchy.
418 } 526 }
419 527
420 } // namespace content 528 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698