OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/budget_service/budget_database.h" |
| 6 |
| 7 #include "chrome/browser/budget_service/budget.pb.h" |
| 8 #include "components/leveldb_proto/proto_database_impl.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 // UMA are logged for the database with this string as part of the name. |
| 14 // They will be LevelDB.*.BackgroundBudgetService. Changes here should be |
| 15 // synchronized with histograms.xml. |
| 16 const char kDatabaseUMAName[] = "BackgroundBudgetService"; |
| 17 |
| 18 } // namespace |
| 19 |
| 20 BudgetDatabase::BudgetDatabase( |
| 21 const base::FilePath& database_dir, |
| 22 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 23 : db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>( |
| 24 task_runner)), |
| 25 weak_ptr_factory_(this) { |
| 26 db_->Init(kDatabaseUMAName, database_dir, |
| 27 base::Bind(&BudgetDatabase::OnDatabaseInit, |
| 28 weak_ptr_factory_.GetWeakPtr())); |
| 29 } |
| 30 |
| 31 BudgetDatabase::~BudgetDatabase() {} |
| 32 |
| 33 void BudgetDatabase::OnDatabaseInit(bool success) { |
| 34 // TODO(harkness): Consider caching the budget database now? |
| 35 } |
| 36 |
| 37 void BudgetDatabase::GetValue(const GURL& origin, |
| 38 const GetValueCallback& callback) { |
| 39 DCHECK_EQ(origin.GetOrigin(), origin); |
| 40 db_->GetEntry(origin.spec(), callback); |
| 41 } |
| 42 |
| 43 void BudgetDatabase::SetValue(const GURL& origin, |
| 44 const budget_service::Budget& budget, |
| 45 const SetValueCallback& callback) { |
| 46 DCHECK_EQ(origin.GetOrigin(), origin); |
| 47 |
| 48 // Build structures to hold the updated values. |
| 49 std::unique_ptr< |
| 50 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> |
| 51 entries(new leveldb_proto::ProtoDatabase< |
| 52 budget_service::Budget>::KeyEntryVector()); |
| 53 entries->push_back(std::make_pair(origin.spec(), budget)); |
| 54 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 55 new std::vector<std::string>()); |
| 56 |
| 57 // Send the updates to the database. |
| 58 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); |
| 59 } |
OLD | NEW |