| OLD | NEW |
| 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/dom_storage/session_storage_database.h" | 5 #include "content/browser/dom_storage/session_storage_database.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | 7 #include <inttypes.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 uint64_t size; | 350 uint64_t size; |
| 351 bool res = base::StringToUint64(db_memory_usage, &size); | 351 bool res = base::StringToUint64(db_memory_usage, &size); |
| 352 DCHECK(res); | 352 DCHECK(res); |
| 353 | 353 |
| 354 auto* mad = pmd->CreateAllocatorDump( | 354 auto* mad = pmd->CreateAllocatorDump( |
| 355 base::StringPrintf("dom_storage/session_storage_0x%" PRIXPTR, | 355 base::StringPrintf("dom_storage/session_storage_0x%" PRIXPTR, |
| 356 reinterpret_cast<uintptr_t>(this))); | 356 reinterpret_cast<uintptr_t>(this))); |
| 357 mad->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 357 mad->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 358 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size); | 358 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size); |
| 359 | 359 |
| 360 // Memory is allocated from system allocator (malloc). | 360 // All leveldb databases are already dumped by leveldb_env::DBTracker. Add |
| 361 const char* system_allocator_name = | 361 // an edge to avoid double counting. |
| 362 base::trace_event::MemoryDumpManager::GetInstance() | 362 pmd->AddSuballocation(mad->guid(), |
| 363 ->system_allocator_pool_name(); | 363 leveldb_env::DBTracker::GetMemoryDumpName(db_.get())); |
| 364 if (system_allocator_name) | |
| 365 pmd->AddSuballocation(mad->guid(), system_allocator_name); | |
| 366 } | 364 } |
| 367 | 365 |
| 368 bool SessionStorageDatabase::LazyOpen(bool create_if_needed) { | 366 bool SessionStorageDatabase::LazyOpen(bool create_if_needed) { |
| 369 base::AutoLock auto_lock(db_lock_); | 367 base::AutoLock auto_lock(db_lock_); |
| 370 if (db_error_ || is_inconsistent_) { | 368 if (db_error_ || is_inconsistent_) { |
| 371 // Don't try to open a database that we know has failed already. | 369 // Don't try to open a database that we know has failed already. |
| 372 return false; | 370 return false; |
| 373 } | 371 } |
| 374 if (IsOpen()) | 372 if (IsOpen()) |
| 375 return true; | 373 return true; |
| 376 | 374 |
| 377 if (!create_if_needed && | 375 if (!create_if_needed && |
| 378 (!base::PathExists(file_path_) || base::IsDirectoryEmpty(file_path_))) { | 376 (!base::PathExists(file_path_) || base::IsDirectoryEmpty(file_path_))) { |
| 379 // If the directory doesn't exist already and we haven't been asked to | 377 // If the directory doesn't exist already and we haven't been asked to |
| 380 // create a file on disk, then we don't bother opening the database. This | 378 // create a file on disk, then we don't bother opening the database. This |
| 381 // means we wait until we absolutely need to put something onto disk before | 379 // means we wait until we absolutely need to put something onto disk before |
| 382 // we do so. | 380 // we do so. |
| 383 return false; | 381 return false; |
| 384 } | 382 } |
| 385 | 383 |
| 386 leveldb::DB* db; | 384 leveldb::Status s = TryToOpen(&db_); |
| 387 leveldb::Status s = TryToOpen(&db); | |
| 388 if (!s.ok()) { | 385 if (!s.ok()) { |
| 389 LOG(WARNING) << "Failed to open leveldb in " << file_path_.value() | 386 LOG(WARNING) << "Failed to open leveldb in " << file_path_.value() |
| 390 << ", error: " << s.ToString(); | 387 << ", error: " << s.ToString(); |
| 391 DCHECK(db == NULL); | |
| 392 | 388 |
| 393 // Clear the directory and try again. | 389 // Clear the directory and try again. |
| 394 base::DeleteFile(file_path_, true); | 390 base::DeleteFile(file_path_, true); |
| 395 s = TryToOpen(&db); | 391 s = TryToOpen(&db_); |
| 396 if (!s.ok()) { | 392 if (!s.ok()) { |
| 397 LOG(WARNING) << "Failed to open leveldb in " << file_path_.value() | 393 LOG(WARNING) << "Failed to open leveldb in " << file_path_.value() |
| 398 << ", error: " << s.ToString(); | 394 << ", error: " << s.ToString(); |
| 399 if (s.IsNotFound()) { | 395 if (s.IsNotFound()) { |
| 400 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, | 396 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, |
| 401 SESSION_STORAGE_UMA_RECREATE_NOT_FOUND, | 397 SESSION_STORAGE_UMA_RECREATE_NOT_FOUND, |
| 402 SESSION_STORAGE_UMA_MAX); | 398 SESSION_STORAGE_UMA_MAX); |
| 403 } else if (s.IsNotSupportedError()) { | 399 } else if (s.IsNotSupportedError()) { |
| 404 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, | 400 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, |
| 405 SESSION_STORAGE_UMA_RECREATE_NOT_SUPPORTED, | 401 SESSION_STORAGE_UMA_RECREATE_NOT_SUPPORTED, |
| 406 SESSION_STORAGE_UMA_MAX); | 402 SESSION_STORAGE_UMA_MAX); |
| 407 } else if (s.IsCorruption()) { | 403 } else if (s.IsCorruption()) { |
| 408 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, | 404 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, |
| 409 SESSION_STORAGE_UMA_RECREATE_CORRUPTION, | 405 SESSION_STORAGE_UMA_RECREATE_CORRUPTION, |
| 410 SESSION_STORAGE_UMA_MAX); | 406 SESSION_STORAGE_UMA_MAX); |
| 411 } else if (s.IsInvalidArgument()) { | 407 } else if (s.IsInvalidArgument()) { |
| 412 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, | 408 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, |
| 413 SESSION_STORAGE_UMA_RECREATE_INVALID_ARGUMENT, | 409 SESSION_STORAGE_UMA_RECREATE_INVALID_ARGUMENT, |
| 414 SESSION_STORAGE_UMA_MAX); | 410 SESSION_STORAGE_UMA_MAX); |
| 415 } else if (s.IsIOError()) { | 411 } else if (s.IsIOError()) { |
| 416 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, | 412 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, |
| 417 SESSION_STORAGE_UMA_RECREATE_IO_ERROR, | 413 SESSION_STORAGE_UMA_RECREATE_IO_ERROR, |
| 418 SESSION_STORAGE_UMA_MAX); | 414 SESSION_STORAGE_UMA_MAX); |
| 419 } else { | 415 } else { |
| 420 NOTREACHED(); | 416 NOTREACHED(); |
| 421 } | 417 } |
| 422 | 418 |
| 423 DCHECK(db == NULL); | |
| 424 db_error_ = true; | 419 db_error_ = true; |
| 425 return false; | 420 return false; |
| 426 } | 421 } |
| 427 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, | 422 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, |
| 428 SESSION_STORAGE_UMA_RECREATED, | 423 SESSION_STORAGE_UMA_RECREATED, |
| 429 SESSION_STORAGE_UMA_MAX); | 424 SESSION_STORAGE_UMA_MAX); |
| 430 } else { | 425 } else { |
| 431 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, | 426 UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name, |
| 432 SESSION_STORAGE_UMA_SUCCESS, | 427 SESSION_STORAGE_UMA_SUCCESS, |
| 433 SESSION_STORAGE_UMA_MAX); | 428 SESSION_STORAGE_UMA_MAX); |
| 434 } | 429 } |
| 435 db_.reset(db); | |
| 436 return true; | 430 return true; |
| 437 } | 431 } |
| 438 | 432 |
| 439 leveldb::Status SessionStorageDatabase::TryToOpen(leveldb::DB** db) { | 433 leveldb::Status SessionStorageDatabase::TryToOpen( |
| 434 std::unique_ptr<leveldb::DB>* db) { |
| 440 leveldb::Options options; | 435 leveldb::Options options; |
| 441 // The directory exists but a valid leveldb database might not exist inside it | 436 // The directory exists but a valid leveldb database might not exist inside it |
| 442 // (e.g., a subset of the needed files might be missing). Handle this | 437 // (e.g., a subset of the needed files might be missing). Handle this |
| 443 // situation gracefully by creating the database now. | 438 // situation gracefully by creating the database now. |
| 444 options.max_open_files = 0; // Use minimum. | 439 options.max_open_files = 0; // Use minimum. |
| 445 options.create_if_missing = true; | 440 options.create_if_missing = true; |
| 446 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 441 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 447 // Default write_buffer_size is 4 MB but that might leave a 3.999 | 442 // Default write_buffer_size is 4 MB but that might leave a 3.999 |
| 448 // memory allocation in RAM from a log file recovery. | 443 // memory allocation in RAM from a log file recovery. |
| 449 options.write_buffer_size = 64 * 1024; | 444 options.write_buffer_size = 64 * 1024; |
| 450 return leveldb::DB::Open(options, file_path_.AsUTF8Unsafe(), db); | 445 return leveldb_env::OpenDB(options, file_path_.AsUTF8Unsafe(), db); |
| 451 } | 446 } |
| 452 | 447 |
| 453 bool SessionStorageDatabase::IsOpen() const { | 448 bool SessionStorageDatabase::IsOpen() const { |
| 454 return db_.get() != NULL; | 449 return db_.get() != NULL; |
| 455 } | 450 } |
| 456 | 451 |
| 457 bool SessionStorageDatabase::CallerErrorCheck(bool ok) const { | 452 bool SessionStorageDatabase::CallerErrorCheck(bool ok) const { |
| 458 DCHECK(ok); | 453 DCHECK(ok); |
| 459 return ok; | 454 return ok; |
| 460 } | 455 } |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 std::string SessionStorageDatabase::MapKey(const std::string& map_id, | 785 std::string SessionStorageDatabase::MapKey(const std::string& map_id, |
| 791 const std::string& key) { | 786 const std::string& key) { |
| 792 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); | 787 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); |
| 793 } | 788 } |
| 794 | 789 |
| 795 const char* SessionStorageDatabase::NextMapIdKey() { | 790 const char* SessionStorageDatabase::NextMapIdKey() { |
| 796 return "next-map-id"; | 791 return "next-map-id"; |
| 797 } | 792 } |
| 798 | 793 |
| 799 } // namespace content | 794 } // namespace content |
| OLD | NEW |