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; | |
cmumford
2015/10/15 18:35:44
Should you be returning false here?
ssid
2015/11/04 11:41:49
This has to return true for temporary failures and
| |
342 | |
343 std::string value; | |
344 uint64 size; | |
345 bool res = db_->GetProperty("leveldb.approximate-memory-usage", &value); | |
346 DCHECK(res); | |
cmumford
2015/10/15 18:35:44
What about returning false if GetProperty returns
ssid
2015/11/04 11:41:49
The DCHECK is placed so that if in future the api
| |
347 base::StringToUint64(value, &size); | |
cmumford
2015/10/15 18:35:44
I guess not super important to check this, but mig
ssid
2015/11/04 11:41:49
Done.
| |
348 | |
349 base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump( | |
cmumford
2015/10/15 18:35:44
You could make this "auto" if you want - but perfe
ssid
2015/11/04 11:41:49
Done.
| |
350 "leveldb/value_store/" + open_histogram_->histogram_name()); | |
351 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
352 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size); | |
353 | |
354 // Memory is allocated from system allocator (malloc). | |
355 pmd->AddSuballocation(dump->guid(), | |
356 base::trace_event::MemoryDumpManager::GetInstance() | |
357 ->system_allocator_pool_name()); | |
358 return true; | |
359 } | |
360 | |
328 scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() { | 361 scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() { |
329 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 362 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
330 | 363 |
331 if (db_) | 364 if (db_) |
332 return util::NoError(); | 365 return util::NoError(); |
333 | 366 |
334 leveldb::Options options; | 367 leveldb::Options options; |
335 options.max_open_files = 0; // Use minimum. | 368 options.max_open_files = 0; // Use minimum. |
336 options.create_if_missing = true; | 369 options.create_if_missing = true; |
337 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 370 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 | 476 CHECK(!status.IsNotFound()); // not an error |
444 | 477 |
445 std::string message = status.ToString(); | 478 std::string message = status.ToString(); |
446 // The message may contain |db_path_|, which may be considered sensitive | 479 // 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. | 480 // data, and those strings are passed to the extension, so strip it out. |
448 base::ReplaceSubstringsAfterOffset( | 481 base::ReplaceSubstringsAfterOffset( |
449 &message, 0u, db_path_.AsUTF8Unsafe(), "..."); | 482 &message, 0u, db_path_.AsUTF8Unsafe(), "..."); |
450 | 483 |
451 return Error::Create(CORRUPTION, message, key.Pass()); | 484 return Error::Create(CORRUPTION, message, key.Pass()); |
452 } | 485 } |
OLD | NEW |