Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(465)

Side by Side Diff: chrome/browser/budget_service/budget_database.cc

Issue 2173833002: Expand the functionality of the BudgetDatabase. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More code review cleanup Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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): 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, 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) Remove this method once the replacement is available.
49
49 // Build structures to hold the updated values. 50 // Build structures to hold the updated values.
50 std::unique_ptr< 51 std::unique_ptr<
51 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> 52 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector>
52 entries(new leveldb_proto::ProtoDatabase< 53 entries(new leveldb_proto::ProtoDatabase<
53 budget_service::Budget>::KeyEntryVector()); 54 budget_service::Budget>::KeyEntryVector());
54 entries->push_back(std::make_pair(origin.spec(), budget)); 55 entries->push_back(std::make_pair(origin.spec(), budget));
55 std::unique_ptr<std::vector<std::string>> keys_to_remove( 56 std::unique_ptr<std::vector<std::string>> keys_to_remove(
56 new std::vector<std::string>()); 57 new std::vector<std::string>());
57 58
58 // Send the updates to the database. 59 // Send the updates to the database.
59 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); 60 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback);
60 } 61 }
62
63 void BudgetDatabase::GetBudgetDetails(
64 const GURL& origin,
65 const GetBudgetDetailsCallback& callback) {
66 DCHECK_EQ(origin.GetOrigin(), origin);
67
68 // If this origin is already in the cache, immediately return the data.
69 if (budget_map_.find(origin.spec()) != budget_map_.end()) {
70 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
71 base::Bind(&BudgetDatabase::DidGetBudget,
72 weak_ptr_factory_.GetWeakPtr(), origin,
73 callback, true /* success */));
74 return;
75 }
76
77 // Otherwise, query for the data, add it to the cache, then return the result.
78 AddToCacheCallback cache_callback =
79 base::Bind(&BudgetDatabase::DidGetBudget, weak_ptr_factory_.GetWeakPtr(),
80 origin, callback);
81 db_->GetEntry(origin.spec(), base::Bind(&BudgetDatabase::AddToCache,
82 weak_ptr_factory_.GetWeakPtr(),
83 origin, cache_callback));
84 }
85
86 void BudgetDatabase::OnDatabaseInit(bool success) {
87 // TODO(harkness): Consider caching the budget database now?
88 }
89
90 void BudgetDatabase::AddToCache(
91 const GURL& origin,
92 const AddToCacheCallback& callback,
93 bool success,
94 std::unique_ptr<budget_service::Budget> budget_proto) {
95 // If the database read failed, there's nothing to add to the cache.
96 if (!success) {
97 callback.Run(success);
98 return;
99 }
100
101 // Add the data to the cache, converting from the proto format to an STL
102 // format which is better for removing things from the list.
103 BudgetChunks chunks;
104 for (const auto& chunk : budget_proto->budget())
105 chunks.push_back(std::make_pair(chunk.amount(), chunk.expiration()));
106
107 budget_map_[origin.spec()] =
108 std::make_pair(budget_proto->debt(), std::move(chunks));
109
110 callback.Run(success);
111 }
112
113 void BudgetDatabase::DidGetBudget(const GURL& origin,
114 const GetBudgetDetailsCallback& callback,
115 bool success) {
116 // If the database wasn't able to read the information, return the
117 // failure and an empty BudgetExpectation.
118 if (!success) {
119 callback.Run(success, 0, BudgetExpectation());
120 return;
121 }
122
123 // Otherwise, build up the BudgetExpection. This is different from the format
124 // in which the cache stores the data. The cache stores chunks of budget and
125 // when that budget expires. The BudgetExpectation describes a set of times
126 // and the budget at those times.
127 const BudgetInfo& info = budget_map_[origin.spec()];
128 BudgetExpectation expectation;
129 double total = 0;
130
131 // Starting with the chunks that expire the farthest in the future, build up
132 // the budget expectations for those future times.
133 for (BudgetChunks::const_reverse_iterator iter = info.second.rbegin();
134 iter != info.second.rend(); iter++) {
135 expectation.push_front(std::make_pair(total, iter->second));
136 total += iter->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(success, info.first, expectation);
Peter Beverloo 2016/07/27 11:21:40 nit: success -> true /* success */ Might be a wee
harkness 2016/07/27 12:23:41 Done.
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698