OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/budget_service/budget_database.h" | 5 #include "chrome/browser/budget_service/budget_database.h" |
6 | 6 |
| 7 #include "base/containers/adapters.h" |
7 #include "chrome/browser/budget_service/budget.pb.h" | 8 #include "chrome/browser/budget_service/budget.pb.h" |
8 #include "components/leveldb_proto/proto_database_impl.h" | 9 #include "components/leveldb_proto/proto_database_impl.h" |
9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
10 #include "url/gurl.h" | 11 #include "url/gurl.h" |
11 | 12 |
| 13 using content::BrowserThread; |
| 14 |
12 namespace { | 15 namespace { |
13 | 16 |
14 // UMA are logged for the database with this string as part of the name. | 17 // UMA are logged for the database with this string as part of the name. |
15 // They will be LevelDB.*.BackgroundBudgetService. Changes here should be | 18 // They will be LevelDB.*.BackgroundBudgetService. Changes here should be |
16 // synchronized with histograms.xml. | 19 // synchronized with histograms.xml. |
17 const char kDatabaseUMAName[] = "BackgroundBudgetService"; | 20 const char kDatabaseUMAName[] = "BackgroundBudgetService"; |
18 | 21 |
19 } // namespace | 22 } // namespace |
20 | 23 |
21 BudgetDatabase::BudgetDatabase( | 24 BudgetDatabase::BudgetDatabase( |
22 const base::FilePath& database_dir, | 25 const base::FilePath& database_dir, |
23 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 26 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
24 : db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>( | 27 : db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>( |
25 task_runner)), | 28 task_runner)), |
26 weak_ptr_factory_(this) { | 29 weak_ptr_factory_(this) { |
27 db_->Init(kDatabaseUMAName, database_dir, | 30 db_->Init(kDatabaseUMAName, database_dir, |
28 base::Bind(&BudgetDatabase::OnDatabaseInit, | 31 base::Bind(&BudgetDatabase::OnDatabaseInit, |
29 weak_ptr_factory_.GetWeakPtr())); | 32 weak_ptr_factory_.GetWeakPtr())); |
30 } | 33 } |
31 | 34 |
32 BudgetDatabase::~BudgetDatabase() {} | 35 BudgetDatabase::~BudgetDatabase() {} |
33 | 36 |
34 void BudgetDatabase::OnDatabaseInit(bool success) { | 37 // TODO(harkness): Remove this method once the replacement is available. |
35 // TODO(harkness): Consider caching the budget database now? | |
36 } | |
37 | |
38 void BudgetDatabase::GetValue(const GURL& origin, | 38 void BudgetDatabase::GetValue(const GURL& origin, |
39 const GetValueCallback& callback) { | 39 const GetValueCallback& callback) { |
40 DCHECK_EQ(origin.GetOrigin(), origin); | 40 DCHECK_EQ(origin.GetOrigin(), origin); |
41 db_->GetEntry(origin.spec(), callback); | 41 db_->GetEntry(origin.spec(), callback); |
42 } | 42 } |
43 | 43 |
44 void BudgetDatabase::SetValue(const GURL& origin, | 44 void BudgetDatabase::SetValue(const GURL& origin, |
45 const budget_service::Budget& budget, | 45 const budget_service::Budget& budget, |
46 const SetValueCallback& callback) { | 46 const SetValueCallback& callback) { |
47 DCHECK_EQ(origin.GetOrigin(), origin); | 47 DCHECK_EQ(origin.GetOrigin(), origin); |
48 | 48 |
| 49 // TODO(harkness) Remove this method once the replacement is available. |
| 50 |
49 // Build structures to hold the updated values. | 51 // Build structures to hold the updated values. |
50 std::unique_ptr< | 52 std::unique_ptr< |
51 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> | 53 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> |
52 entries(new leveldb_proto::ProtoDatabase< | 54 entries(new leveldb_proto::ProtoDatabase< |
53 budget_service::Budget>::KeyEntryVector()); | 55 budget_service::Budget>::KeyEntryVector()); |
54 entries->push_back(std::make_pair(origin.spec(), budget)); | 56 entries->push_back(std::make_pair(origin.spec(), budget)); |
55 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 57 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
56 new std::vector<std::string>()); | 58 new std::vector<std::string>()); |
57 | 59 |
58 // Send the updates to the database. | 60 // Send the updates to the database. |
59 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); | 61 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); |
60 } | 62 } |
| 63 |
| 64 void BudgetDatabase::GetBudgetDetails( |
| 65 const GURL& origin, |
| 66 const GetBudgetDetailsCallback& callback) { |
| 67 DCHECK_EQ(origin.GetOrigin(), origin); |
| 68 |
| 69 // If this origin is already in the cache, immediately return the data. |
| 70 if (budget_map_.find(origin.spec()) != budget_map_.end()) { |
| 71 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 72 base::Bind(&BudgetDatabase::DidGetBudget, |
| 73 weak_ptr_factory_.GetWeakPtr(), origin, |
| 74 callback, true /* success */)); |
| 75 return; |
| 76 } |
| 77 |
| 78 // Otherwise, query for the data, add it to the cache, then return the result. |
| 79 AddToCacheCallback cache_callback = |
| 80 base::Bind(&BudgetDatabase::DidGetBudget, weak_ptr_factory_.GetWeakPtr(), |
| 81 origin, callback); |
| 82 db_->GetEntry(origin.spec(), base::Bind(&BudgetDatabase::AddToCache, |
| 83 weak_ptr_factory_.GetWeakPtr(), |
| 84 origin, cache_callback)); |
| 85 } |
| 86 |
| 87 void BudgetDatabase::OnDatabaseInit(bool success) { |
| 88 // TODO(harkness): Consider caching the budget database now? |
| 89 } |
| 90 |
| 91 void BudgetDatabase::AddToCache( |
| 92 const GURL& origin, |
| 93 const AddToCacheCallback& callback, |
| 94 bool success, |
| 95 std::unique_ptr<budget_service::Budget> budget_proto) { |
| 96 // If the database read failed, there's nothing to add to the cache. |
| 97 if (!success) { |
| 98 callback.Run(success); |
| 99 return; |
| 100 } |
| 101 |
| 102 // Add the data to the cache, converting from the proto format to an STL |
| 103 // format which is better for removing things from the list. |
| 104 BudgetChunks chunks; |
| 105 for (const auto& chunk : budget_proto->budget()) |
| 106 chunks.push_back(std::make_pair(chunk.amount(), chunk.expiration())); |
| 107 |
| 108 budget_map_[origin.spec()] = |
| 109 std::make_pair(budget_proto->debt(), std::move(chunks)); |
| 110 |
| 111 callback.Run(success); |
| 112 } |
| 113 |
| 114 void BudgetDatabase::DidGetBudget(const GURL& origin, |
| 115 const GetBudgetDetailsCallback& callback, |
| 116 bool success) { |
| 117 // If the database wasn't able to read the information, return the |
| 118 // failure and an empty BudgetExpectation. |
| 119 if (!success) { |
| 120 callback.Run(success, 0, BudgetExpectation()); |
| 121 return; |
| 122 } |
| 123 |
| 124 // Otherwise, build up the BudgetExpection. This is different from the format |
| 125 // in which the cache stores the data. The cache stores chunks of budget and |
| 126 // when that budget expires. The BudgetExpectation describes a set of times |
| 127 // and the budget at those times. |
| 128 const BudgetInfo& info = budget_map_[origin.spec()]; |
| 129 BudgetExpectation expectation; |
| 130 double total = 0; |
| 131 |
| 132 // Starting with the chunks that expire the farthest in the future, build up |
| 133 // the budget expectations for those future times. |
| 134 for (const auto& chunk : base::Reversed(info.second)) { |
| 135 expectation.push_front(std::make_pair(total, chunk.second)); |
| 136 total += chunk.first; |
| 137 } |
| 138 |
| 139 // Always add one entry at the front of the list for the total budget right |
| 140 // now. |
| 141 expectation.push_front(std::make_pair(total, 0)); |
| 142 |
| 143 callback.Run(true /* success */, info.first, expectation); |
| 144 } |
OLD | NEW |