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, "LeveldbValueStore", 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 |
| 341 // Return true so that the provider is not disabled. |
| 342 if (!db_) |
| 343 return true; |
| 344 |
| 345 std::string value; |
| 346 uint64 size; |
| 347 bool res = db_->GetProperty("leveldb.approximate-memory-usage", &value); |
| 348 DCHECK(res); |
| 349 res = base::StringToUint64(value, &size); |
| 350 DCHECK(res); |
| 351 |
| 352 auto dump = pmd->CreateAllocatorDump( |
| 353 base::StringPrintf("leveldb/value_store/%s/%p", |
| 354 open_histogram_->histogram_name().c_str(), this)); |
| 355 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 356 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size); |
| 357 |
| 358 // Memory is allocated from system allocator (malloc). |
| 359 const char* system_allocator_name = |
| 360 base::trace_event::MemoryDumpManager::GetInstance() |
| 361 ->system_allocator_pool_name(); |
| 362 if (system_allocator_name) |
| 363 pmd->AddSuballocation(dump->guid(), system_allocator_name); |
| 364 |
| 365 return true; |
| 366 } |
| 367 |
328 scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() { | 368 scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() { |
329 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 369 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
330 | 370 |
331 if (db_) | 371 if (db_) |
332 return util::NoError(); | 372 return util::NoError(); |
333 | 373 |
334 leveldb::Options options; | 374 leveldb::Options options; |
335 options.max_open_files = 0; // Use minimum. | 375 options.max_open_files = 0; // Use minimum. |
336 options.create_if_missing = true; | 376 options.create_if_missing = true; |
337 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 377 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 | 483 CHECK(!status.IsNotFound()); // not an error |
444 | 484 |
445 std::string message = status.ToString(); | 485 std::string message = status.ToString(); |
446 // The message may contain |db_path_|, which may be considered sensitive | 486 // 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. | 487 // data, and those strings are passed to the extension, so strip it out. |
448 base::ReplaceSubstringsAfterOffset( | 488 base::ReplaceSubstringsAfterOffset( |
449 &message, 0u, db_path_.AsUTF8Unsafe(), "..."); | 489 &message, 0u, db_path_.AsUTF8Unsafe(), "..."); |
450 | 490 |
451 return Error::Create(CORRUPTION, message, key.Pass()); | 491 return Error::Create(CORRUPTION, message, key.Pass()); |
452 } | 492 } |
OLD | NEW |