Chromium Code Reviews| 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 "chrome/browser/budget_service/budget.pb.h" | 7 #include "chrome/browser/budget_service/budget.pb.h" | 
| 8 #include "components/leveldb_proto/proto_database_impl.h" | 8 #include "components/leveldb_proto/proto_database_impl.h" | 
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" | 
| 10 #include "url/gurl.h" | 10 #include "url/gurl.h" | 
| 11 | 11 | 
| 12 using content::BrowserThread; | |
| 13 | |
| 12 namespace { | 14 namespace { | 
| 13 | 15 | 
| 14 // UMA are logged for the database with this string as part of the name. | 16 // UMA are logged for the database with this string as part of the name. | 
| 15 // They will be LevelDB.*.BackgroundBudgetService. Changes here should be | 17 // They will be LevelDB.*.BackgroundBudgetService. Changes here should be | 
| 16 // synchronized with histograms.xml. | 18 // synchronized with histograms.xml. | 
| 17 const char kDatabaseUMAName[] = "BackgroundBudgetService"; | 19 const char kDatabaseUMAName[] = "BackgroundBudgetService"; | 
| 18 | 20 | 
| 19 } // namespace | 21 } // namespace | 
| 20 | 22 | 
| 21 BudgetDatabase::BudgetDatabase( | 23 BudgetDatabase::BudgetDatabase( | 
| 22 const base::FilePath& database_dir, | 24 const base::FilePath& database_dir, | 
| 23 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 25 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 
| 24 : db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>( | 26 : db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>( | 
| 25 task_runner)), | 27 task_runner)), | 
| 26 weak_ptr_factory_(this) { | 28 weak_ptr_factory_(this) { | 
| 27 db_->Init(kDatabaseUMAName, database_dir, | 29 db_->Init(kDatabaseUMAName, database_dir, | 
| 28 base::Bind(&BudgetDatabase::OnDatabaseInit, | 30 base::Bind(&BudgetDatabase::OnDatabaseInit, | 
| 29 weak_ptr_factory_.GetWeakPtr())); | 31 weak_ptr_factory_.GetWeakPtr())); | 
| 30 } | 32 } | 
| 31 | 33 | 
| 32 BudgetDatabase::~BudgetDatabase() {} | 34 BudgetDatabase::~BudgetDatabase() {} | 
| 33 | 35 | 
| 34 void BudgetDatabase::OnDatabaseInit(bool success) { | 36 // TODO(harkness): Either remove this method or add caching. | 
| 35 // TODO(harkness): Consider caching the budget database now? | |
| 36 } | |
| 37 | |
| 38 void BudgetDatabase::GetValue(const GURL& origin, | 37 void BudgetDatabase::GetValue(const GURL& origin, | 
| 39 const GetValueCallback& callback) { | 38 const GetValueCallback& callback) { | 
| 40 DCHECK_EQ(origin.GetOrigin(), origin); | 39 DCHECK_EQ(origin.GetOrigin(), origin); | 
| 41 db_->GetEntry(origin.spec(), callback); | 40 db_->GetEntry(origin.spec(), callback); | 
| 42 } | 41 } | 
| 43 | 42 | 
| 44 void BudgetDatabase::SetValue(const GURL& origin, | 43 void BudgetDatabase::SetValue(const GURL& origin, | 
| 45 const budget_service::Budget& budget, | 44 const budget_service::Budget& budget, | 
| 46 const SetValueCallback& callback) { | 45 const SetValueCallback& callback) { | 
| 47 DCHECK_EQ(origin.GetOrigin(), origin); | 46 DCHECK_EQ(origin.GetOrigin(), origin); | 
| 48 | 47 | 
| 48 // TODO(harkness) Either remove this method or add the updated value to | |
| 49 // the cache. | |
| 
 
Peter Beverloo
2016/07/26 17:26:21
I think we decided on this one, so remove the ambi
 
harkness
2016/07/27 10:59:03
Done.
 
 | |
| 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( | |
| 72 BrowserThread::UI, FROM_HERE, | |
| 73 base::Bind(&BudgetDatabase::DidGetBudget, | |
| 74 weak_ptr_factory_.GetWeakPtr(), origin, callback, true)); | |
| 
 
Peter Beverloo
2016/07/26 17:26:20
true -> true /* success */
 
harkness
2016/07/27 10:59:03
Done.
 
 | |
| 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. | |
| 103 BudgetDatabase::BudgetInfo& info = budget_map_[origin.spec()]; | |
| 104 info.second.clear(); | |
| 105 info.first = budget_proto->debt(); | |
| 106 | |
| 107 // Convert from the proto format to an STL format which is better for | |
| 108 // removing things from the front of the list. | |
| 109 for (int i = 0; i < budget_proto->budget_size(); i++) { | |
| 110 const budget_service::BudgetChunk& chunk = budget_proto->budget(i); | |
| 111 info.second.push_back( | |
| 112 std::pair<double, double>(chunk.amount(), chunk.expiration())); | |
| 113 } | |
| 114 | |
| 115 callback.Run(success); | |
| 116 } | |
| 117 | |
| 118 void BudgetDatabase::DidGetBudget(const GURL& origin, | |
| 119 const GetBudgetDetailsCallback& callback, | |
| 120 bool success) { | |
| 121 // If the database wasn't able to read the information, return the | |
| 122 // failure and an empty BudgetExpectation. | |
| 123 if (!success) { | |
| 124 callback.Run(success, 0, BudgetExpectation()); | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 // Otherwise, build up the BudgetExpection. This is different from the format | |
| 129 // in which the cache stores the data. The cache stores chunks of budget and | |
| 130 // when that budget expires. The BudgetExpectation describes a set of times | |
| 131 // and the budget at those times. | |
| 132 const BudgetInfo& info = budget_map_[origin.spec()]; | |
| 133 BudgetExpectation expectation; | |
| 134 double total = 0; | |
| 135 | |
| 136 // Starting with the chunks that expire the farthest in the future, build up | |
| 137 // the budget expectations for those future times. | |
| 138 for (BudgetChunks::const_reverse_iterator iter = info.second.rbegin(); | |
| 
 
Peter Beverloo
2016/07/26 17:26:20
const auto&
 
harkness
2016/07/27 10:59:02
I believe there are issues with const auto& in thi
 
Peter Beverloo
2016/07/27 11:21:40
You're right re: const, because we're incrementing
 
 | |
| 139 iter != info.second.rend(); iter++) { | |
| 140 expectation.push_front(std::make_pair(total, iter->second)); | |
| 141 total += iter->first; | |
| 142 } | |
| 143 | |
| 144 // Always add one entry at the front of the list for the total budget right | |
| 145 // now. | |
| 146 expectation.push_front(std::make_pair(total, 0)); | |
| 147 | |
| 148 callback.Run(success, info.first, expectation); | |
| 149 } | |
| OLD | NEW |