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<OriginDBMap::iterator, OriginDBMap::iterator> range = |
| 36 origin_dbs_.equal_range(database->identifier().first); |
| 37 DCHECK(range.first != range.second); |
| 38 for (OriginDBMap::iterator 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 while (range.first != range.second) { |
| 117 IndexedDBDatabase* db = range.first->second; |
| 118 ++range.first; |
| 119 db->ForceClose(); |
| 120 } |
| 121 |
95 if (backing_store_map_.find(origin_url) != backing_store_map_.end()) | 122 if (backing_store_map_.find(origin_url) != backing_store_map_.end()) |
96 ReleaseBackingStore(origin_url, true /* immediate */); | 123 ReleaseBackingStore(origin_url, true /* immediate */); |
97 } | 124 } |
98 | 125 |
99 void IndexedDBFactory::ContextDestroyed() { | 126 void IndexedDBFactory::ContextDestroyed() { |
100 // Timers on backing stores hold a reference to this factory. When the | 127 // Timers on backing stores hold a reference to this factory. When the |
101 // context (which nominally owns this factory) is destroyed during thread | 128 // context (which nominally owns this factory) is destroyed during thread |
102 // termination the timers must be stopped so that this factory and the | 129 // termination the timers must be stopped so that this factory and the |
103 // stores can be disposed of. | 130 // stores can be disposed of. |
104 for (IndexedDBBackingStoreMap::iterator it = backing_store_map_.begin(); | 131 for (IndexedDBBackingStoreMap::iterator it = backing_store_map_.begin(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 if (!database) { | 203 if (!database) { |
177 callbacks->OnError(IndexedDBDatabaseError( | 204 callbacks->OnError(IndexedDBDatabaseError( |
178 blink::WebIDBDatabaseExceptionUnknownError, | 205 blink::WebIDBDatabaseExceptionUnknownError, |
179 ASCIIToUTF16( | 206 ASCIIToUTF16( |
180 "Internal error creating database backend for " | 207 "Internal error creating database backend for " |
181 "indexedDB.deleteDatabase."))); | 208 "indexedDB.deleteDatabase."))); |
182 return; | 209 return; |
183 } | 210 } |
184 | 211 |
185 database_map_[unique_identifier] = database; | 212 database_map_[unique_identifier] = database; |
| 213 origin_dbs_.insert(std::make_pair(origin_url, database)); |
186 database->DeleteDatabase(callbacks); | 214 database->DeleteDatabase(callbacks); |
187 database_map_.erase(unique_identifier); | 215 RemoveDatabaseFromMaps(unique_identifier); |
188 database = NULL; | 216 database = NULL; |
189 backing_store = NULL; | 217 backing_store = NULL; |
190 ReleaseBackingStore(origin_url, false /* immediate */); | 218 ReleaseBackingStore(origin_url, false /* immediate */); |
191 } | 219 } |
192 | 220 |
193 void IndexedDBFactory::DatabaseDeleted( | 221 void IndexedDBFactory::DatabaseDeleted( |
194 const IndexedDBDatabase::Identifier& identifier) { | 222 const IndexedDBDatabase::Identifier& identifier) { |
195 // NULL after ContextDestroyed() called, and in some unit tests. | 223 // NULL after ContextDestroyed() called, and in some unit tests. |
196 if (!context_) | 224 if (!context_) |
197 return; | 225 return; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 } else { | 345 } else { |
318 database = it->second; | 346 database = it->second; |
319 } | 347 } |
320 | 348 |
321 if (data_loss != blink::WebIDBDataLossNone) | 349 if (data_loss != blink::WebIDBDataLossNone) |
322 callbacks->OnDataLoss(data_loss, data_loss_message); | 350 callbacks->OnDataLoss(data_loss, data_loss_message); |
323 | 351 |
324 database->OpenConnection( | 352 database->OpenConnection( |
325 callbacks, database_callbacks, transaction_id, version); | 353 callbacks, database_callbacks, transaction_id, version); |
326 | 354 |
327 if (!was_open && database->ConnectionCount() > 0) | 355 if (!was_open && database->ConnectionCount() > 0) { |
328 database_map_[unique_identifier] = database; | 356 database_map_[unique_identifier] = database; |
| 357 origin_dbs_.insert(std::make_pair(origin_url, database)); |
| 358 } |
329 } | 359 } |
330 | 360 |
331 std::vector<IndexedDBDatabase*> IndexedDBFactory::GetOpenDatabasesForOrigin( | 361 std::pair<IndexedDBFactory::OriginDBMapIterator, |
332 const GURL& origin_url) const { | 362 IndexedDBFactory::OriginDBMapIterator> |
333 std::vector<IndexedDBDatabase*> result; | 363 IndexedDBFactory::GetOpenDatabasesForOrigin(const GURL& origin_url) const { |
334 for (IndexedDBDatabaseMap::const_iterator it = database_map_.begin(); | 364 return origin_dbs_.equal_range(origin_url); |
335 it != database_map_.end(); | 365 } |
336 ++it) { | 366 |
337 if (it->first.first == origin_url) | 367 size_t IndexedDBFactory::GetConnectionCount(const GURL& origin_url) const { |
338 result.push_back(it->second); | 368 size_t count(0); |
339 } | 369 |
340 return result; | 370 std::pair<OriginDBMapIterator, OriginDBMapIterator> range = |
| 371 GetOpenDatabasesForOrigin(origin_url); |
| 372 for (OriginDBMapIterator it = range.first; it != range.second; ++it) |
| 373 count += it->second->ConnectionCount(); |
| 374 |
| 375 return count; |
341 } | 376 } |
342 | 377 |
343 } // namespace content | 378 } // namespace content |
OLD | NEW |