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/trace_event/memory_dump_manager.h" | |
| 16 #include "base/trace_event/process_memory_dump.h" | |
| 14 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 15 #include "extensions/browser/value_store/value_store_util.h" | 18 #include "extensions/browser/value_store/value_store_util.h" |
| 16 #include "third_party/leveldatabase/env_chromium.h" | 19 #include "third_party/leveldatabase/env_chromium.h" |
| 17 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 20 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| 18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 21 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 19 | 22 |
| 20 namespace util = value_store_util; | 23 namespace util = value_store_util; |
| 21 using content::BrowserThread; | 24 using content::BrowserThread; |
| 22 | 25 |
| 23 namespace { | 26 namespace { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 52 const base::FilePath& db_path) | 55 const base::FilePath& db_path) |
| 53 : db_path_(db_path), open_histogram_(nullptr) { | 56 : db_path_(db_path), open_histogram_(nullptr) { |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 57 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 55 | 58 |
| 56 // Used in lieu of UMA_HISTOGRAM_ENUMERATION because the histogram name is | 59 // Used in lieu of UMA_HISTOGRAM_ENUMERATION because the histogram name is |
| 57 // not a constant. | 60 // not a constant. |
| 58 open_histogram_ = base::LinearHistogram::FactoryGet( | 61 open_histogram_ = base::LinearHistogram::FactoryGet( |
| 59 "Extensions.Database.Open." + uma_client_name, 1, | 62 "Extensions.Database.Open." + uma_client_name, 1, |
| 60 leveldb_env::LEVELDB_STATUS_MAX, leveldb_env::LEVELDB_STATUS_MAX + 1, | 63 leveldb_env::LEVELDB_STATUS_MAX, leveldb_env::LEVELDB_STATUS_MAX + 1, |
| 61 base::Histogram::kUmaTargetedHistogramFlag); | 64 base::Histogram::kUmaTargetedHistogramFlag); |
| 65 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
|
Primiano Tucci (use gerrit)
2015/10/15 08:59:53
same here about threading and unregistration
ssid
2015/10/15 15:22:04
Done.
| |
| 66 this); | |
| 62 } | 67 } |
| 63 | 68 |
| 64 LeveldbValueStore::~LeveldbValueStore() { | 69 LeveldbValueStore::~LeveldbValueStore() { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 70 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 71 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
|
Primiano Tucci (use gerrit)
2015/10/15 08:59:53
And I guess here you meant to UNregister, not re-r
ssid
2015/10/15 15:22:04
Done.
| |
| 72 this); | |
| 66 | 73 |
| 67 // Delete the database from disk if it's empty (but only if we managed to | 74 // 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 | 75 // open it!). This is safe on destruction, assuming that we have exclusive |
| 69 // access to the database. | 76 // access to the database. |
| 70 if (db_ && IsEmpty()) | 77 if (db_ && IsEmpty()) |
| 71 DeleteDbFile(); | 78 DeleteDbFile(); |
| 72 } | 79 } |
| 73 | 80 |
| 74 size_t LeveldbValueStore::GetBytesInUse(const std::string& key) { | 81 size_t LeveldbValueStore::GetBytesInUse(const std::string& key) { |
| 75 // Let SettingsStorageQuotaEnforcer implement this. | 82 // Let SettingsStorageQuotaEnforcer implement this. |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 } | 325 } |
| 319 | 326 |
| 320 // The restore succeeded if there is no corruption error. | 327 // The restore succeeded if there is no corruption error. |
| 321 return !result->IsCorrupted(); | 328 return !result->IsCorrupted(); |
| 322 } | 329 } |
| 323 | 330 |
| 324 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { | 331 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { |
| 325 return !WriteToDb(batch).get(); | 332 return !WriteToDb(batch).get(); |
| 326 } | 333 } |
| 327 | 334 |
| 335 bool LeveldbValueStore::OnMemoryDump( | |
| 336 const base::trace_event::MemoryDumpArgs& args, | |
| 337 base::trace_event::ProcessMemoryDump* pmd) { | |
| 338 if (!db_) | |
|
Primiano Tucci (use gerrit)
2015/10/15 08:59:53
you really need to think about threading here. Wha
ssid
2015/10/15 15:22:04
Done.
| |
| 339 return true; | |
| 340 | |
| 341 std::string value; | |
| 342 uint64 size; | |
| 343 db_->GetProperty("leveldb.approximate-memory-usage", &value); | |
| 344 base::StringToUint64(value, &size); | |
| 345 | |
| 346 base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump( | |
| 347 base::StringPrintf("leveldb/value_store/%p", this)); | |
|
Primiano Tucci (use gerrit)
2015/10/15 08:59:53
why not putting the uma_client_name or the basenam
ssid
2015/10/15 15:22:04
Done.
| |
| 348 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 349 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size); | |
| 350 | |
| 351 // Memory is allocated from system allocator (malloc). | |
| 352 pmd->AddSuballocation(dump->guid(), | |
| 353 base::trace_event::MemoryDumpManager::GetInstance() | |
| 354 ->system_allocator_pool_name()); | |
| 355 return true; | |
| 356 } | |
| 357 | |
| 328 scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() { | 358 scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() { |
| 329 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 359 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 330 | 360 |
| 331 if (db_) | 361 if (db_) |
| 332 return util::NoError(); | 362 return util::NoError(); |
| 333 | 363 |
| 334 leveldb::Options options; | 364 leveldb::Options options; |
| 335 options.max_open_files = 0; // Use minimum. | 365 options.max_open_files = 0; // Use minimum. |
| 336 options.create_if_missing = true; | 366 options.create_if_missing = true; |
| 337 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 367 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 | 473 CHECK(!status.IsNotFound()); // not an error |
| 444 | 474 |
| 445 std::string message = status.ToString(); | 475 std::string message = status.ToString(); |
| 446 // The message may contain |db_path_|, which may be considered sensitive | 476 // 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. | 477 // data, and those strings are passed to the extension, so strip it out. |
| 448 base::ReplaceSubstringsAfterOffset( | 478 base::ReplaceSubstringsAfterOffset( |
| 449 &message, 0u, db_path_.AsUTF8Unsafe(), "..."); | 479 &message, 0u, db_path_.AsUTF8Unsafe(), "..."); |
| 450 | 480 |
| 451 return Error::Create(CORRUPTION, message, key.Pass()); | 481 return Error::Create(CORRUPTION, message, key.Pass()); |
| 452 } | 482 } |
| OLD | NEW |