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_impl.h" | 5 #include "content/browser/indexed_db/indexed_db_factory_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 } | 168 } |
169 } | 169 } |
170 | 170 |
171 void IndexedDBFactoryImpl::GetDatabaseNames( | 171 void IndexedDBFactoryImpl::GetDatabaseNames( |
172 scoped_refptr<IndexedDBCallbacks> callbacks, | 172 scoped_refptr<IndexedDBCallbacks> callbacks, |
173 const Origin& origin, | 173 const Origin& origin, |
174 const base::FilePath& data_directory, | 174 const base::FilePath& data_directory, |
175 net::URLRequestContext* request_context) { | 175 net::URLRequestContext* request_context) { |
176 IDB_TRACE("IndexedDBFactoryImpl::GetDatabaseNames"); | 176 IDB_TRACE("IndexedDBFactoryImpl::GetDatabaseNames"); |
177 // TODO(dgrogan): Plumb data_loss back to script eventually? | 177 // TODO(dgrogan): Plumb data_loss back to script eventually? |
178 blink::WebIDBDataLoss data_loss; | 178 IndexedDBDataLossInfo data_loss_info; |
179 std::string data_loss_message; | |
180 bool disk_full; | 179 bool disk_full; |
181 leveldb::Status s; | 180 leveldb::Status s; |
182 // TODO(cmumford): Handle this error | 181 // TODO(cmumford): Handle this error |
183 scoped_refptr<IndexedDBBackingStore> backing_store = | 182 scoped_refptr<IndexedDBBackingStore> backing_store = OpenBackingStore( |
184 OpenBackingStore(origin, data_directory, request_context, &data_loss, | 183 origin, data_directory, request_context, &data_loss_info, &disk_full, &s); |
185 &data_loss_message, &disk_full, &s); | |
186 if (!backing_store.get()) { | 184 if (!backing_store.get()) { |
187 callbacks->OnError( | 185 callbacks->OnError( |
188 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, | 186 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, |
189 "Internal error opening backing store for " | 187 "Internal error opening backing store for " |
190 "indexedDB.webkitGetDatabaseNames.")); | 188 "indexedDB.webkitGetDatabaseNames.")); |
191 return; | 189 return; |
192 } | 190 } |
193 | 191 |
194 std::vector<base::string16> names = backing_store->GetDatabaseNames(&s); | 192 std::vector<base::string16> names = backing_store->GetDatabaseNames(&s); |
195 if (!s.ok()) { | 193 if (!s.ok()) { |
(...skipping 22 matching lines...) Expand all Loading... |
218 IndexedDBDatabase::Identifier unique_identifier(origin, name); | 216 IndexedDBDatabase::Identifier unique_identifier(origin, name); |
219 const auto& it = database_map_.find(unique_identifier); | 217 const auto& it = database_map_.find(unique_identifier); |
220 if (it != database_map_.end()) { | 218 if (it != database_map_.end()) { |
221 // If there are any connections to the database, directly delete the | 219 // If there are any connections to the database, directly delete the |
222 // database. | 220 // database. |
223 it->second->DeleteDatabase(callbacks); | 221 it->second->DeleteDatabase(callbacks); |
224 return; | 222 return; |
225 } | 223 } |
226 | 224 |
227 // TODO(dgrogan): Plumb data_loss back to script eventually? | 225 // TODO(dgrogan): Plumb data_loss back to script eventually? |
228 blink::WebIDBDataLoss data_loss; | 226 IndexedDBDataLossInfo data_loss_info; |
229 std::string data_loss_message; | |
230 bool disk_full = false; | 227 bool disk_full = false; |
231 leveldb::Status s; | 228 leveldb::Status s; |
232 scoped_refptr<IndexedDBBackingStore> backing_store = | 229 scoped_refptr<IndexedDBBackingStore> backing_store = OpenBackingStore( |
233 OpenBackingStore(origin, data_directory, request_context, &data_loss, | 230 origin, data_directory, request_context, &data_loss_info, &disk_full, &s); |
234 &data_loss_message, &disk_full, &s); | |
235 if (!backing_store.get()) { | 231 if (!backing_store.get()) { |
236 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError, | 232 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError, |
237 ASCIIToUTF16( | 233 ASCIIToUTF16( |
238 "Internal error opening backing store " | 234 "Internal error opening backing store " |
239 "for indexedDB.deleteDatabase.")); | 235 "for indexedDB.deleteDatabase.")); |
240 callbacks->OnError(error); | 236 callbacks->OnError(error); |
241 if (s.IsCorruption()) { | 237 if (s.IsCorruption()) { |
242 HandleBackingStoreCorruption(origin, error); | 238 HandleBackingStoreCorruption(origin, error); |
243 } | 239 } |
244 return; | 240 return; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 if (it == backing_store_map_.end()) | 335 if (it == backing_store_map_.end()) |
340 return false; | 336 return false; |
341 return it->second->close_timer()->IsRunning(); | 337 return it->second->close_timer()->IsRunning(); |
342 } | 338 } |
343 | 339 |
344 scoped_refptr<IndexedDBBackingStore> | 340 scoped_refptr<IndexedDBBackingStore> |
345 IndexedDBFactoryImpl::OpenBackingStoreHelper( | 341 IndexedDBFactoryImpl::OpenBackingStoreHelper( |
346 const Origin& origin, | 342 const Origin& origin, |
347 const base::FilePath& data_directory, | 343 const base::FilePath& data_directory, |
348 net::URLRequestContext* request_context, | 344 net::URLRequestContext* request_context, |
349 blink::WebIDBDataLoss* data_loss, | 345 IndexedDBDataLossInfo* data_loss_info, |
350 std::string* data_loss_message, | |
351 bool* disk_full, | 346 bool* disk_full, |
352 bool first_time, | 347 bool first_time, |
353 leveldb::Status* status) { | 348 leveldb::Status* status) { |
354 return IndexedDBBackingStore::Open( | 349 return IndexedDBBackingStore::Open( |
355 this, origin, data_directory, request_context, data_loss, | 350 this, origin, data_directory, request_context, data_loss_info, disk_full, |
356 data_loss_message, disk_full, context_->TaskRunner(), first_time, status); | 351 context_->TaskRunner(), first_time, status); |
357 } | 352 } |
358 | 353 |
359 scoped_refptr<IndexedDBBackingStore> IndexedDBFactoryImpl::OpenBackingStore( | 354 scoped_refptr<IndexedDBBackingStore> IndexedDBFactoryImpl::OpenBackingStore( |
360 const Origin& origin, | 355 const Origin& origin, |
361 const base::FilePath& data_directory, | 356 const base::FilePath& data_directory, |
362 net::URLRequestContext* request_context, | 357 net::URLRequestContext* request_context, |
363 blink::WebIDBDataLoss* data_loss, | 358 IndexedDBDataLossInfo* data_loss_info, |
364 std::string* data_loss_message, | |
365 bool* disk_full, | 359 bool* disk_full, |
366 leveldb::Status* status) { | 360 leveldb::Status* status) { |
367 const bool open_in_memory = data_directory.empty(); | 361 const bool open_in_memory = data_directory.empty(); |
368 | 362 |
369 const auto& it2 = backing_store_map_.find(origin); | 363 const auto& it2 = backing_store_map_.find(origin); |
370 if (it2 != backing_store_map_.end()) { | 364 if (it2 != backing_store_map_.end()) { |
371 it2->second->close_timer()->Stop(); | 365 it2->second->close_timer()->Stop(); |
372 return it2->second; | 366 return it2->second; |
373 } | 367 } |
374 | 368 |
375 scoped_refptr<IndexedDBBackingStore> backing_store; | 369 scoped_refptr<IndexedDBBackingStore> backing_store; |
376 bool first_time = false; | 370 bool first_time = false; |
377 if (open_in_memory) { | 371 if (open_in_memory) { |
378 backing_store = IndexedDBBackingStore::OpenInMemory( | 372 backing_store = IndexedDBBackingStore::OpenInMemory( |
379 origin, context_->TaskRunner(), status); | 373 origin, context_->TaskRunner(), status); |
380 } else { | 374 } else { |
381 first_time = !backends_opened_since_boot_.count(origin); | 375 first_time = !backends_opened_since_boot_.count(origin); |
382 | 376 |
383 backing_store = OpenBackingStoreHelper( | 377 backing_store = |
384 origin, data_directory, request_context, data_loss, data_loss_message, | 378 OpenBackingStoreHelper(origin, data_directory, request_context, |
385 disk_full, first_time, status); | 379 data_loss_info, disk_full, first_time, status); |
386 } | 380 } |
387 | 381 |
388 if (backing_store.get()) { | 382 if (backing_store.get()) { |
389 if (first_time) | 383 if (first_time) |
390 backends_opened_since_boot_.insert(origin); | 384 backends_opened_since_boot_.insert(origin); |
391 backing_store_map_[origin] = backing_store; | 385 backing_store_map_[origin] = backing_store; |
392 // If an in-memory database, bind lifetime to this factory instance. | 386 // If an in-memory database, bind lifetime to this factory instance. |
393 if (open_in_memory) | 387 if (open_in_memory) |
394 session_only_backing_stores_.insert(backing_store); | 388 session_only_backing_stores_.insert(backing_store); |
395 | 389 |
396 // All backing stores associated with this factory should be of the same | 390 // All backing stores associated with this factory should be of the same |
397 // type. | 391 // type. |
398 DCHECK_NE(session_only_backing_stores_.empty(), open_in_memory); | 392 DCHECK_NE(session_only_backing_stores_.empty(), open_in_memory); |
399 | 393 |
400 return backing_store; | 394 return backing_store; |
401 } | 395 } |
402 | 396 |
403 return 0; | 397 return 0; |
404 } | 398 } |
405 | 399 |
406 void IndexedDBFactoryImpl::Open(const base::string16& name, | 400 void IndexedDBFactoryImpl::Open(const base::string16& name, |
407 const IndexedDBPendingConnection& connection, | 401 const IndexedDBPendingConnection& connection, |
408 net::URLRequestContext* request_context, | 402 net::URLRequestContext* request_context, |
409 const Origin& origin, | 403 const Origin& origin, |
410 const base::FilePath& data_directory) { | 404 const base::FilePath& data_directory) { |
411 IDB_TRACE("IndexedDBFactoryImpl::Open"); | 405 IDB_TRACE("IndexedDBFactoryImpl::Open"); |
412 scoped_refptr<IndexedDBDatabase> database; | 406 scoped_refptr<IndexedDBDatabase> database; |
413 IndexedDBDatabase::Identifier unique_identifier(origin, name); | 407 IndexedDBDatabase::Identifier unique_identifier(origin, name); |
414 const auto& it = database_map_.find(unique_identifier); | 408 const auto& it = database_map_.find(unique_identifier); |
415 blink::WebIDBDataLoss data_loss = blink::WebIDBDataLossNone; | 409 IndexedDBDataLossInfo data_loss_info; |
416 std::string data_loss_message; | |
417 bool disk_full = false; | 410 bool disk_full = false; |
418 bool was_open = (it != database_map_.end()); | 411 bool was_open = (it != database_map_.end()); |
419 if (!was_open) { | 412 if (!was_open) { |
420 leveldb::Status s; | 413 leveldb::Status s; |
421 scoped_refptr<IndexedDBBackingStore> backing_store = | 414 scoped_refptr<IndexedDBBackingStore> backing_store = |
422 OpenBackingStore(origin, data_directory, request_context, &data_loss, | 415 OpenBackingStore(origin, data_directory, request_context, |
423 &data_loss_message, &disk_full, &s); | 416 &data_loss_info, &disk_full, &s); |
424 if (!backing_store.get()) { | 417 if (!backing_store.get()) { |
425 if (disk_full) { | 418 if (disk_full) { |
426 connection.callbacks->OnError( | 419 connection.callbacks->OnError( |
427 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionQuotaError, | 420 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionQuotaError, |
428 ASCIIToUTF16( | 421 ASCIIToUTF16( |
429 "Encountered full disk while opening " | 422 "Encountered full disk while opening " |
430 "backing store for indexedDB.open."))); | 423 "backing store for indexedDB.open."))); |
431 return; | 424 return; |
432 } | 425 } |
433 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError, | 426 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError, |
(...skipping 20 matching lines...) Expand all Loading... |
454 if (s.IsCorruption()) { | 447 if (s.IsCorruption()) { |
455 backing_store = NULL; // Closes the LevelDB so that it can be deleted | 448 backing_store = NULL; // Closes the LevelDB so that it can be deleted |
456 HandleBackingStoreCorruption(origin, error); | 449 HandleBackingStoreCorruption(origin, error); |
457 } | 450 } |
458 return; | 451 return; |
459 } | 452 } |
460 } else { | 453 } else { |
461 database = it->second; | 454 database = it->second; |
462 } | 455 } |
463 | 456 |
464 if (data_loss != blink::WebIDBDataLossNone) | 457 if (data_loss_info.status != blink::WebIDBDataLossNone) |
465 connection.callbacks->OnDataLoss(data_loss, data_loss_message); | 458 connection.callbacks->OnDataLoss(data_loss_info); |
466 | 459 |
467 database->OpenConnection(connection); | 460 database->OpenConnection(connection); |
468 | 461 |
469 if (!was_open && database->ConnectionCount() > 0) { | 462 if (!was_open && database->ConnectionCount() > 0) { |
470 database_map_[unique_identifier] = database.get(); | 463 database_map_[unique_identifier] = database.get(); |
471 origin_dbs_.insert(std::make_pair(origin, database.get())); | 464 origin_dbs_.insert(std::make_pair(origin, database.get())); |
472 } | 465 } |
473 } | 466 } |
474 | 467 |
475 std::pair<IndexedDBFactoryImpl::OriginDBMapIterator, | 468 std::pair<IndexedDBFactoryImpl::OriginDBMapIterator, |
476 IndexedDBFactoryImpl::OriginDBMapIterator> | 469 IndexedDBFactoryImpl::OriginDBMapIterator> |
477 IndexedDBFactoryImpl::GetOpenDatabasesForOrigin(const Origin& origin) const { | 470 IndexedDBFactoryImpl::GetOpenDatabasesForOrigin(const Origin& origin) const { |
478 return origin_dbs_.equal_range(origin); | 471 return origin_dbs_.equal_range(origin); |
479 } | 472 } |
480 | 473 |
481 size_t IndexedDBFactoryImpl::GetConnectionCount(const Origin& origin) const { | 474 size_t IndexedDBFactoryImpl::GetConnectionCount(const Origin& origin) const { |
482 size_t count(0); | 475 size_t count(0); |
483 | 476 |
484 OriginDBs range = GetOpenDatabasesForOrigin(origin); | 477 OriginDBs range = GetOpenDatabasesForOrigin(origin); |
485 for (OriginDBMapIterator it = range.first; it != range.second; ++it) | 478 for (OriginDBMapIterator it = range.first; it != range.second; ++it) |
486 count += it->second->ConnectionCount(); | 479 count += it->second->ConnectionCount(); |
487 | 480 |
488 return count; | 481 return count; |
489 } | 482 } |
490 | 483 |
491 } // namespace content | 484 } // namespace content |
OLD | NEW |