Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/value_store/leveldb_value_store.h" | 5 #include "extensions/browser/value_store/leveldb_value_store.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/thread_task_runner_handle.h" | |
| 16 #include "base/trace_event/memory_dump_manager.h" | |
| 17 #include "base/trace_event/process_memory_dump.h" | |
| 14 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 15 #include "extensions/browser/value_store/value_store_util.h" | 19 #include "extensions/browser/value_store/value_store_util.h" |
| 16 #include "third_party/leveldatabase/env_chromium.h" | 20 #include "third_party/leveldatabase/env_chromium.h" |
| 17 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 21 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| 18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 22 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 19 | 23 |
| 20 namespace util = value_store_util; | 24 namespace util = value_store_util; |
| 21 using content::BrowserThread; | 25 using content::BrowserThread; |
| 22 | 26 |
| 23 namespace { | 27 namespace { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 52 const base::FilePath& db_path) | 56 const base::FilePath& db_path) |
| 53 : db_path_(db_path), open_histogram_(nullptr) { | 57 : db_path_(db_path), open_histogram_(nullptr) { |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 58 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 55 | 59 |
| 56 // Used in lieu of UMA_HISTOGRAM_ENUMERATION because the histogram name is | 60 // Used in lieu of UMA_HISTOGRAM_ENUMERATION because the histogram name is |
| 57 // not a constant. | 61 // not a constant. |
| 58 open_histogram_ = base::LinearHistogram::FactoryGet( | 62 open_histogram_ = base::LinearHistogram::FactoryGet( |
| 59 "Extensions.Database.Open." + uma_client_name, 1, | 63 "Extensions.Database.Open." + uma_client_name, 1, |
| 60 leveldb_env::LEVELDB_STATUS_MAX, leveldb_env::LEVELDB_STATUS_MAX + 1, | 64 leveldb_env::LEVELDB_STATUS_MAX, leveldb_env::LEVELDB_STATUS_MAX + 1, |
| 61 base::Histogram::kUmaTargetedHistogramFlag); | 65 base::Histogram::kUmaTargetedHistogramFlag); |
| 66 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
| 67 this, base::ThreadTaskRunnerHandle::Get()); | |
| 62 } | 68 } |
| 63 | 69 |
| 64 LeveldbValueStore::~LeveldbValueStore() { | 70 LeveldbValueStore::~LeveldbValueStore() { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 71 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 72 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | |
| 73 this); | |
| 66 | 74 |
| 67 // Delete the database from disk if it's empty (but only if we managed to | 75 // Delete the database from disk if it's empty (but only if we managed to |
| 68 // open it!). This is safe on destruction, assuming that we have exclusive | 76 // open it!). This is safe on destruction, assuming that we have exclusive |
| 69 // access to the database. | 77 // access to the database. |
| 70 if (db_ && IsEmpty()) | 78 if (db_ && IsEmpty()) |
| 71 DeleteDbFile(); | 79 DeleteDbFile(); |
| 72 } | 80 } |
| 73 | 81 |
| 74 size_t LeveldbValueStore::GetBytesInUse(const std::string& key) { | 82 size_t LeveldbValueStore::GetBytesInUse(const std::string& key) { |
| 75 // Let SettingsStorageQuotaEnforcer implement this. | 83 // Let SettingsStorageQuotaEnforcer implement this. |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 } | 326 } |
| 319 | 327 |
| 320 // The restore succeeded if there is no corruption error. | 328 // The restore succeeded if there is no corruption error. |
| 321 return !result->IsCorrupted(); | 329 return !result->IsCorrupted(); |
| 322 } | 330 } |
| 323 | 331 |
| 324 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { | 332 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { |
| 325 return !WriteToDb(batch).get(); | 333 return !WriteToDb(batch).get(); |
| 326 } | 334 } |
| 327 | 335 |
| 336 bool LeveldbValueStore::OnMemoryDump( | |
| 337 const base::trace_event::MemoryDumpArgs& args, | |
| 338 base::trace_event::ProcessMemoryDump* pmd) { | |
| 339 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 340 if (!db_) | |
| 341 return true; | |
| 342 | |
| 343 std::string value; | |
| 344 uint64 size; | |
| 345 bool res = db_->GetProperty("leveldb.approximate-memory-usage", &value); | |
| 346 DCHECK(res); | |
| 347 base::StringToUint64(value, &size); | |
| 348 | |
| 349 std::string name; | |
| 350 base::ReplaceChars(open_histogram_->histogram_name(), ".", "_", &name); | |
|
Primiano Tucci (use gerrit)
2015/10/15 15:27:56
this should not needed anymore, I removed the awkw
ssid
2015/10/15 15:36:27
Done.
| |
| 351 base::trace_event::MemoryAllocatorDump* dump = | |
| 352 pmd->CreateAllocatorDump("leveldb/value_store/" + name); | |
| 353 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 354 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size); | |
| 355 | |
| 356 // Memory is allocated from system allocator (malloc). | |
| 357 pmd->AddSuballocation(dump->guid(), | |
| 358 base::trace_event::MemoryDumpManager::GetInstance() | |
| 359 ->system_allocator_pool_name()); | |
| 360 return true; | |
| 361 } | |
| 362 | |
| 328 scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() { | 363 scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() { |
| 329 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 364 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 330 | 365 |
| 331 if (db_) | 366 if (db_) |
| 332 return util::NoError(); | 367 return util::NoError(); |
| 333 | 368 |
| 334 leveldb::Options options; | 369 leveldb::Options options; |
| 335 options.max_open_files = 0; // Use minimum. | 370 options.max_open_files = 0; // Use minimum. |
| 336 options.create_if_missing = true; | 371 options.create_if_missing = true; |
| 337 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 372 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 CHECK(!status.IsNotFound()); // not an error | 478 CHECK(!status.IsNotFound()); // not an error |
| 444 | 479 |
| 445 std::string message = status.ToString(); | 480 std::string message = status.ToString(); |
| 446 // The message may contain |db_path_|, which may be considered sensitive | 481 // The message may contain |db_path_|, which may be considered sensitive |
| 447 // data, and those strings are passed to the extension, so strip it out. | 482 // data, and those strings are passed to the extension, so strip it out. |
| 448 base::ReplaceSubstringsAfterOffset( | 483 base::ReplaceSubstringsAfterOffset( |
| 449 &message, 0u, db_path_.AsUTF8Unsafe(), "..."); | 484 &message, 0u, db_path_.AsUTF8Unsafe(), "..."); |
| 450 | 485 |
| 451 return Error::Create(CORRUPTION, message, key.Pass()); | 486 return Error::Create(CORRUPTION, message, key.Pass()); |
| 452 } | 487 } |
| OLD | NEW |