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 #include "url/gurl.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // UMA are logged for the database with this string as part of the name. |
| 15 // They will be LevelDB.*.BackgroundBudgetService. Changes here should be |
| 16 // synchronized with histograms.xml. |
| 17 const char kDatabaseUMAName[] = "BackgroundBudgetService"; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 BudgetDatabase::BudgetDatabase( |
| 22 const base::FilePath& database_dir, |
| 23 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 24 : db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>( |
| 25 task_runner)), |
| 26 weak_ptr_factory_(this) { |
| 27 db_->Init(kDatabaseUMAName, database_dir, |
| 28 base::Bind(&BudgetDatabase::OnDatabaseInit, |
| 29 weak_ptr_factory_.GetWeakPtr())); |
| 30 } |
| 31 |
| 32 BudgetDatabase::~BudgetDatabase() {} |
| 33 |
| 34 void BudgetDatabase::OnDatabaseInit(bool success) { |
| 35 // TODO(harkness): Consider caching the budget database now? |
| 36 } |
| 37 |
| 38 void BudgetDatabase::GetValue(const GURL& origin, |
| 39 const GetValueCallback& callback) { |
| 40 DCHECK_EQ(origin.GetOrigin(), origin); |
| 41 db_->GetEntry(origin.spec(), callback); |
| 42 } |
| 43 |
| 44 void BudgetDatabase::SetValue(const GURL& origin, |
| 45 const budget_service::Budget& budget, |
| 46 const SetValueCallback& callback) { |
| 47 DCHECK_EQ(origin.GetOrigin(), origin); |
| 48 |
| 49 // Build structures to hold the updated values. |
| 50 std::unique_ptr< |
| 51 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> |
| 52 entries(new leveldb_proto::ProtoDatabase< |
| 53 budget_service::Budget>::KeyEntryVector()); |
| 54 entries->push_back(std::make_pair(origin.spec(), budget)); |
| 55 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 56 new std::vector<std::string>()); |
| 57 |
| 58 // Send the updates to the database. |
| 59 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); |
| 60 } |
OLD | NEW |