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 | |
46 void IndexedDBFactory::ReleaseDatabase( | 28 void IndexedDBFactory::ReleaseDatabase( |
47 const IndexedDBDatabase::Identifier& identifier, | 29 const IndexedDBDatabase::Identifier& identifier, |
48 bool forcedClose) { | 30 bool forcedClose) { |
49 | 31 IndexedDBDatabaseMap::iterator it = database_map_.find(identifier); |
50 DCHECK(!database_map_.find(identifier)->second->backing_store()); | 32 DCHECK(it != database_map_.end()); |
51 | 33 DCHECK(!it->second->backing_store()); |
52 RemoveDatabaseFromMaps(identifier); | 34 database_map_.erase(it); |
53 | 35 |
54 // No grace period on a forced-close, as the initiator is | 36 // No grace period on a forced-close, as the initiator is |
55 // assuming the backing store will be released once all | 37 // assuming the backing store will be released once all |
56 // connections are closed. | 38 // connections are closed. |
57 ReleaseBackingStore(identifier.first, forcedClose); | 39 ReleaseBackingStore(identifier.first, forcedClose); |
58 } | 40 } |
59 | 41 |
60 void IndexedDBFactory::ReleaseBackingStore(const GURL& origin_url, | 42 void IndexedDBFactory::ReleaseBackingStore(const GURL& origin_url, |
61 bool immediate) { | 43 bool immediate) { |
62 // Only close if this is the last reference. | 44 // Only close if this is the last reference. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // Scope so that the implicit scoped_refptr<> is freed. | 85 // Scope so that the implicit scoped_refptr<> is freed. |
104 IndexedDBBackingStoreMap::const_iterator it = | 86 IndexedDBBackingStoreMap::const_iterator it = |
105 backing_store_map_.find(origin_url); | 87 backing_store_map_.find(origin_url); |
106 DCHECK(it != backing_store_map_.end()); | 88 DCHECK(it != backing_store_map_.end()); |
107 ptr = it->second.get(); | 89 ptr = it->second.get(); |
108 } | 90 } |
109 return ptr->HasOneRef(); | 91 return ptr->HasOneRef(); |
110 } | 92 } |
111 | 93 |
112 void IndexedDBFactory::ForceClose(const GURL& origin_url) { | 94 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 | |
122 if (backing_store_map_.find(origin_url) != backing_store_map_.end()) | 95 if (backing_store_map_.find(origin_url) != backing_store_map_.end()) |
123 ReleaseBackingStore(origin_url, true /* immediate */); | 96 ReleaseBackingStore(origin_url, true /* immediate */); |
124 } | 97 } |
125 | 98 |
126 void IndexedDBFactory::ContextDestroyed() { | 99 void IndexedDBFactory::ContextDestroyed() { |
127 // Timers on backing stores hold a reference to this factory. When the | 100 // Timers on backing stores hold a reference to this factory. When the |
128 // context (which nominally owns this factory) is destroyed during thread | 101 // context (which nominally owns this factory) is destroyed during thread |
129 // termination the timers must be stopped so that this factory and the | 102 // termination the timers must be stopped so that this factory and the |
130 // stores can be disposed of. | 103 // stores can be disposed of. |
131 for (IndexedDBBackingStoreMap::iterator it = backing_store_map_.begin(); | 104 for (IndexedDBBackingStoreMap::iterator it = backing_store_map_.begin(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 if (!database) { | 176 if (!database) { |
204 callbacks->OnError(IndexedDBDatabaseError( | 177 callbacks->OnError(IndexedDBDatabaseError( |
205 blink::WebIDBDatabaseExceptionUnknownError, | 178 blink::WebIDBDatabaseExceptionUnknownError, |
206 ASCIIToUTF16( | 179 ASCIIToUTF16( |
207 "Internal error creating database backend for " | 180 "Internal error creating database backend for " |
208 "indexedDB.deleteDatabase."))); | 181 "indexedDB.deleteDatabase."))); |
209 return; | 182 return; |
210 } | 183 } |
211 | 184 |
212 database_map_[unique_identifier] = database; | 185 database_map_[unique_identifier] = database; |
213 origin_dbs_.insert(std::make_pair(origin_url, database)); | |
214 database->DeleteDatabase(callbacks); | 186 database->DeleteDatabase(callbacks); |
215 RemoveDatabaseFromMaps(unique_identifier); | 187 database_map_.erase(unique_identifier); |
216 database = NULL; | 188 database = NULL; |
217 backing_store = NULL; | 189 backing_store = NULL; |
218 ReleaseBackingStore(origin_url, false /* immediate */); | 190 ReleaseBackingStore(origin_url, false /* immediate */); |
219 } | 191 } |
220 | 192 |
221 void IndexedDBFactory::DatabaseDeleted( | 193 void IndexedDBFactory::DatabaseDeleted( |
222 const IndexedDBDatabase::Identifier& identifier) { | 194 const IndexedDBDatabase::Identifier& identifier) { |
223 // NULL after ContextDestroyed() called, and in some unit tests. | 195 // NULL after ContextDestroyed() called, and in some unit tests. |
224 if (!context_) | 196 if (!context_) |
225 return; | 197 return; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 } else { | 317 } else { |
346 database = it->second; | 318 database = it->second; |
347 } | 319 } |
348 | 320 |
349 if (data_loss != blink::WebIDBDataLossNone) | 321 if (data_loss != blink::WebIDBDataLossNone) |
350 callbacks->OnDataLoss(data_loss, data_loss_message); | 322 callbacks->OnDataLoss(data_loss, data_loss_message); |
351 | 323 |
352 database->OpenConnection( | 324 database->OpenConnection( |
353 callbacks, database_callbacks, transaction_id, version); | 325 callbacks, database_callbacks, transaction_id, version); |
354 | 326 |
355 if (!was_open && database->ConnectionCount() > 0) { | 327 if (!was_open && database->ConnectionCount() > 0) |
356 database_map_[unique_identifier] = database; | 328 database_map_[unique_identifier] = database; |
357 origin_dbs_.insert(std::make_pair(origin_url, database)); | |
358 } | |
359 } | 329 } |
360 | 330 |
361 std::pair<IndexedDBFactory::OriginDBMapIterator, | 331 std::vector<IndexedDBDatabase*> IndexedDBFactory::GetOpenDatabasesForOrigin( |
362 IndexedDBFactory::OriginDBMapIterator> | 332 const GURL& origin_url) const { |
363 IndexedDBFactory::GetOpenDatabasesForOrigin(const GURL& origin_url) const { | 333 std::vector<IndexedDBDatabase*> result; |
364 return origin_dbs_.equal_range(origin_url); | 334 for (IndexedDBDatabaseMap::const_iterator it = database_map_.begin(); |
365 } | 335 it != database_map_.end(); |
366 | 336 ++it) { |
367 size_t IndexedDBFactory::GetConnectionCount(const GURL& origin_url) const { | 337 if (it->first.first == origin_url) |
368 size_t count(0); | 338 result.push_back(it->second); |
369 | 339 } |
370 std::pair<OriginDBMapIterator, OriginDBMapIterator> range = | 340 return result; |
371 GetOpenDatabasesForOrigin(origin_url); | |
372 for (OriginDBMapIterator it = range.first; it != range.second; ++it) | |
373 count += it->second->ConnectionCount(); | |
374 | |
375 return count; | |
376 } | 341 } |
377 | 342 |
378 } // namespace content | 343 } // namespace content |
OLD | NEW |