OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/indexed_db/indexed_db_factory.h" | 5 #include "content/browser/indexed_db/indexed_db_factory.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 10 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
11 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 11 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
12 #include "content/browser/indexed_db/indexed_db_tracing.h" | 12 #include "content/browser/indexed_db/indexed_db_tracing.h" |
13 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h" | 13 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h" |
14 #include "third_party/WebKit/public/platform/WebIDBDatabaseException.h" | 14 #include "third_party/WebKit/public/platform/WebIDBDatabaseException.h" |
15 #include "webkit/common/database/database_identifier.h" | 15 #include "webkit/common/database/database_identifier.h" |
16 | 16 |
17 using base::ASCIIToUTF16; | 17 using base::ASCIIToUTF16; |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 const int64 kBackingStoreGracePeriodMs = 2000; | 21 const int64 kBackingStoreGracePeriodMs = 2000; |
22 | 22 |
23 IndexedDBFactory::IndexedDBFactory(IndexedDBContextImpl* context) | 23 IndexedDBFactory::IndexedDBFactory(IndexedDBContextImpl* context) |
24 : context_(context) {} | 24 : context_(context) {} |
25 | 25 |
26 IndexedDBFactory::~IndexedDBFactory() {} | 26 IndexedDBFactory::~IndexedDBFactory() {} |
27 | 27 |
28 void IndexedDBFactory::RemoveDatabaseFromMaps( | |
29 const IndexedDBDatabase::Identifier& identifier) { | |
30 IndexedDBDatabaseMap::iterator it = database_map_.find(identifier); | |
31 DCHECK(it != database_map_.end()); | |
32 IndexedDBDatabase* database = it->second; | |
33 database_map_.erase(it); | |
34 | |
35 std::pair<OriginDbMapIterator, OriginDbMapIterator> range = | |
36 GetOpenDatabasesForOrigin(database->identifier().first); | |
37 DCHECK(range.first != range.second); | |
38 for (OriginDbMapIterator it2 = range.first; it2 != range.second; ++it2) { | |
39 if (it2->second == database) { | |
40 origin_dbs_.erase(it2); | |
41 break; | |
42 } | |
43 } | |
44 } | |
45 | |
28 void IndexedDBFactory::ReleaseDatabase( | 46 void IndexedDBFactory::ReleaseDatabase( |
29 const IndexedDBDatabase::Identifier& identifier, | 47 const IndexedDBDatabase::Identifier& identifier, |
30 bool forcedClose) { | 48 bool forcedClose) { |
31 IndexedDBDatabaseMap::iterator it = database_map_.find(identifier); | 49 |
32 DCHECK(it != database_map_.end()); | 50 DCHECK(!database_map_.find(identifier)->second->backing_store()); |
33 DCHECK(!it->second->backing_store()); | 51 |
34 database_map_.erase(it); | 52 RemoveDatabaseFromMaps(identifier); |
35 | 53 |
36 // No grace period on a forced-close, as the initiator is | 54 // No grace period on a forced-close, as the initiator is |
37 // assuming the backing store will be released once all | 55 // assuming the backing store will be released once all |
38 // connections are closed. | 56 // connections are closed. |
39 ReleaseBackingStore(identifier.first, forcedClose); | 57 ReleaseBackingStore(identifier.first, forcedClose); |
40 } | 58 } |
41 | 59 |
42 void IndexedDBFactory::ReleaseBackingStore(const GURL& origin_url, | 60 void IndexedDBFactory::ReleaseBackingStore(const GURL& origin_url, |
43 bool immediate) { | 61 bool immediate) { |
44 // Only close if this is the last reference. | 62 // Only close if this is the last reference. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 // Scope so that the implicit scoped_refptr<> is freed. | 103 // Scope so that the implicit scoped_refptr<> is freed. |
86 IndexedDBBackingStoreMap::const_iterator it = | 104 IndexedDBBackingStoreMap::const_iterator it = |
87 backing_store_map_.find(origin_url); | 105 backing_store_map_.find(origin_url); |
88 DCHECK(it != backing_store_map_.end()); | 106 DCHECK(it != backing_store_map_.end()); |
89 ptr = it->second.get(); | 107 ptr = it->second.get(); |
90 } | 108 } |
91 return ptr->HasOneRef(); | 109 return ptr->HasOneRef(); |
92 } | 110 } |
93 | 111 |
94 void IndexedDBFactory::ForceClose(const GURL& origin_url) { | 112 void IndexedDBFactory::ForceClose(const GURL& origin_url) { |
113 std::pair<OriginDbMapIterator, OriginDbMapIterator> range = | |
114 GetOpenDatabasesForOrigin(origin_url); | |
115 | |
116 // Iterate this way because ForceClose will call back into this instance | |
jsbell
2014/01/03 00:34:25
Would the standard "advance iterator before erasin
cmumford
2014/01/03 21:34:01
I wasn't confident enough with the multimap iterat
| |
117 // to remove the database from this map - thus invaliding an iterator. | |
118 while (range.first != range.second) { | |
119 range.first->second->ForceClose(); // Force close the database | |
120 range = GetOpenDatabasesForOrigin(origin_url); | |
121 } | |
122 | |
95 if (backing_store_map_.find(origin_url) != backing_store_map_.end()) | 123 if (backing_store_map_.find(origin_url) != backing_store_map_.end()) |
96 ReleaseBackingStore(origin_url, true /* immediate */); | 124 ReleaseBackingStore(origin_url, true /* immediate */); |
97 } | 125 } |
98 | 126 |
99 void IndexedDBFactory::ContextDestroyed() { | 127 void IndexedDBFactory::ContextDestroyed() { |
100 // Timers on backing stores hold a reference to this factory. When the | 128 // Timers on backing stores hold a reference to this factory. When the |
101 // context (which nominally owns this factory) is destroyed during thread | 129 // context (which nominally owns this factory) is destroyed during thread |
102 // termination the timers must be stopped so that this factory and the | 130 // termination the timers must be stopped so that this factory and the |
103 // stores can be disposed of. | 131 // stores can be disposed of. |
104 for (IndexedDBBackingStoreMap::iterator it = backing_store_map_.begin(); | 132 for (IndexedDBBackingStoreMap::iterator it = backing_store_map_.begin(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 if (!database) { | 204 if (!database) { |
177 callbacks->OnError(IndexedDBDatabaseError( | 205 callbacks->OnError(IndexedDBDatabaseError( |
178 blink::WebIDBDatabaseExceptionUnknownError, | 206 blink::WebIDBDatabaseExceptionUnknownError, |
179 ASCIIToUTF16( | 207 ASCIIToUTF16( |
180 "Internal error creating database backend for " | 208 "Internal error creating database backend for " |
181 "indexedDB.deleteDatabase."))); | 209 "indexedDB.deleteDatabase."))); |
182 return; | 210 return; |
183 } | 211 } |
184 | 212 |
185 database_map_[unique_identifier] = database; | 213 database_map_[unique_identifier] = database; |
214 origin_dbs_.insert(std::make_pair(origin_url, database)); | |
186 database->DeleteDatabase(callbacks); | 215 database->DeleteDatabase(callbacks); |
187 database_map_.erase(unique_identifier); | 216 RemoveDatabaseFromMaps(unique_identifier); |
188 database = NULL; | 217 database = NULL; |
189 backing_store = NULL; | 218 backing_store = NULL; |
190 ReleaseBackingStore(origin_url, false /* immediate */); | 219 ReleaseBackingStore(origin_url, false /* immediate */); |
191 } | 220 } |
192 | 221 |
193 void IndexedDBFactory::DatabaseDeleted( | 222 void IndexedDBFactory::DatabaseDeleted( |
194 const IndexedDBDatabase::Identifier& identifier) { | 223 const IndexedDBDatabase::Identifier& identifier) { |
195 // NULL after ContextDestroyed() called, and in some unit tests. | 224 // NULL after ContextDestroyed() called, and in some unit tests. |
196 if (!context_) | 225 if (!context_) |
197 return; | 226 return; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 } else { | 346 } else { |
318 database = it->second; | 347 database = it->second; |
319 } | 348 } |
320 | 349 |
321 if (data_loss != blink::WebIDBDataLossNone) | 350 if (data_loss != blink::WebIDBDataLossNone) |
322 callbacks->OnDataLoss(data_loss, data_loss_message); | 351 callbacks->OnDataLoss(data_loss, data_loss_message); |
323 | 352 |
324 database->OpenConnection( | 353 database->OpenConnection( |
325 callbacks, database_callbacks, transaction_id, version); | 354 callbacks, database_callbacks, transaction_id, version); |
326 | 355 |
327 if (!was_open && database->ConnectionCount() > 0) | 356 if (!was_open && database->ConnectionCount() > 0) { |
328 database_map_[unique_identifier] = database; | 357 database_map_[unique_identifier] = database; |
358 origin_dbs_.insert(std::make_pair(origin_url, database)); | |
359 } | |
329 } | 360 } |
330 | 361 |
331 std::vector<IndexedDBDatabase*> IndexedDBFactory::GetOpenDatabasesForOrigin( | 362 std::pair<IndexedDBFactory::OriginDbMapIterator, |
332 const GURL& origin_url) const { | 363 IndexedDBFactory::OriginDbMapIterator> |
333 std::vector<IndexedDBDatabase*> result; | 364 IndexedDBFactory::GetOpenDatabasesForOrigin(const GURL& origin_url) const { |
334 for (IndexedDBDatabaseMap::const_iterator it = database_map_.begin(); | 365 return origin_dbs_.equal_range(origin_url); |
335 it != database_map_.end(); | 366 } |
336 ++it) { | 367 |
337 if (it->first.first == origin_url) | 368 size_t IndexedDBFactory::GetConnectionCount(const GURL& origin_url) const { |
338 result.push_back(it->second); | 369 size_t count(0); |
339 } | 370 |
340 return result; | 371 std::pair<OriginDbMapIterator, OriginDbMapIterator> range = |
jsbell
2014/01/03 00:34:25
Can't this be:
return origin_dbs_.count(origin_ur
cmumford
2014/01/03 21:34:01
That would return the number of open DB's for an o
jsbell
2014/01/03 21:37:26
Oops, you're right, sorry.
| |
372 GetOpenDatabasesForOrigin(origin_url); | |
373 for (OriginDbMapIterator it = range.first; it != range.second; ++it) | |
374 count += it->second->ConnectionCount(); | |
375 | |
376 return count; | |
341 } | 377 } |
342 | 378 |
343 } // namespace content | 379 } // namespace content |
OLD | NEW |