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

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

Issue 2255813002: AddEngagementBudget and SpendBudget added to BudgetDatabase. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 #ifndef CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ 5 #ifndef CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_
6 #define CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ 6 #define CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_
7 7
8 #include <list> 8 #include <list>
9 #include <memory> 9 #include <memory>
10 #include <unordered_map> 10 #include <unordered_map>
11 #include <vector>
12 11
13 #include "base/callback_forward.h" 12 #include "base/callback_forward.h"
14 #include "base/macros.h" 13 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
16 #include "components/leveldb_proto/proto_database.h" 15 #include "components/leveldb_proto/proto_database.h"
17 16
18 namespace base { 17 namespace base {
19 class Clock; 18 class Clock;
20 class SequencedTaskRunner; 19 class SequencedTaskRunner;
21 class Time; 20 class Time;
(...skipping 15 matching lines...) Expand all
37 struct BudgetStatus { 36 struct BudgetStatus {
38 BudgetStatus(double budget_at, base::Time time) 37 BudgetStatus(double budget_at, base::Time time)
39 : budget_at(budget_at), time(time) {} 38 : budget_at(budget_at), time(time) {}
40 BudgetStatus(const BudgetStatus& other) 39 BudgetStatus(const BudgetStatus& other)
41 : budget_at(other.budget_at), time(other.time) {} 40 : budget_at(other.budget_at), time(other.time) {}
42 41
43 double budget_at; 42 double budget_at;
44 base::Time time; 43 base::Time time;
45 }; 44 };
46 45
47 // Data structure for returing the budget decay expectations to the caller. 46 // Data structure for returing the budget decay prediction to the caller.
48 using BudgetExpectation = std::list<BudgetStatus>; 47 using BudgetPrediction = std::list<BudgetStatus>;
49 48
50 // Callback for setting a budget value. 49 // Callback for setting a budget value.
51 using StoreBudgetCallback = base::Callback<void(bool success)>; 50 using StoreBudgetCallback = base::Callback<void(bool success)>;
52 51
53 // Callback for getting a list of all budget chunks. 52 // Callback for getting a list of all budget chunks.
54 using GetBudgetDetailsCallback = 53 using GetBudgetDetailsCallback =
55 base::Callback<void(bool success, const BudgetExpectation& expectation)>; 54 base::Callback<void(bool success, const BudgetPrediction& prediction)>;
56 55
57 // The database_dir specifies the location of the budget information on 56 // The database_dir specifies the location of the budget information on
58 // disk. The task_runner is used by the ProtoDatabase to handle all blocking 57 // disk. The task_runner is used by the ProtoDatabase to handle all blocking
59 // calls and disk access. 58 // calls and disk access.
60 BudgetDatabase(const base::FilePath& database_dir, 59 BudgetDatabase(const base::FilePath& database_dir,
61 const scoped_refptr<base::SequencedTaskRunner>& task_runner); 60 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
62 ~BudgetDatabase(); 61 ~BudgetDatabase();
63 62
64 // Get the full budget expectation for the origin. This will return a 63 // Get the full budget expectation for the origin. This will return a
65 // sequence of time points and the expected budget at those times. 64 // sequence of time points and the expected budget at those times.
66 void GetBudgetDetails(const GURL& origin, 65 void GetBudgetDetails(const GURL& origin,
67 const GetBudgetDetailsCallback& callback); 66 const GetBudgetDetailsCallback& callback);
68 67
69 // Add budget for an origin. The caller specifies the amount, and the method 68 // Add budget for an origin. The caller specifies the amount, and the method
70 // adds the amount to the cache with the correct expiration. Callback is 69 // adds the amount to the cache with the correct expiration. Callback is
71 // invoked only after the newly cached value is written to storage. This 70 // invoked only after the newly cached value is written to storage. This
72 // should only be called after the budget has been read from the database. 71 // should only be called after the budget has been read from the database.
73 void AddBudget(const GURL& origin, 72 void AddBudget(const GURL& origin,
74 double amount, 73 double amount,
75 const StoreBudgetCallback& callback); 74 const StoreBudgetCallback& callback);
76 75
76 // Add budget based on engagement with an origin. The caller specifies the
77 // engagement score of the origin, and the method calculates when engagement
78 // budget was last awarded and awards a portion of the score based on that.
79 // Callback is invoked after the value is written to storage. This should
80 // only be called after the budget has been read from the database.
81 void AddEngagementBudget(const GURL& origin,
82 double score,
83 const StoreBudgetCallback& callback);
84
85 // Spend a particular amount of budget for an origin. The callback takes
86 // a boolean which indicates whether the origin had enough budget to spend.
87 // If it returns success, then the budget was deducted and the result written
88 // to the database. This should only be called after the budget has been read.
89 void SpendBudget(const GURL& origin,
90 double amount,
91 const StoreBudgetCallback& callback);
92
77 private: 93 private:
78 friend class BudgetDatabaseTest; 94 friend class BudgetDatabaseTest;
79 95
80 // Used to allow tests to change time for testing. 96 // Used to allow tests to change time for testing.
81 void SetClockForTesting(std::unique_ptr<base::Clock> clock); 97 void SetClockForTesting(std::unique_ptr<base::Clock> clock);
82 98
83 // Holds information about individual pieces of awarded budget. There is a 99 // Holds information about individual pieces of awarded budget. There is a
84 // one-to-one mapping of these to the chunks in the underlying database. 100 // one-to-one mapping of these to the chunks in the underlying database.
85 struct BudgetChunk { 101 struct BudgetChunk {
86 BudgetChunk(double amount, base::Time expiration) 102 BudgetChunk(double amount, base::Time expiration)
87 : amount(amount), expiration(expiration) {} 103 : amount(amount), expiration(expiration) {}
88 BudgetChunk(const BudgetChunk& other) 104 BudgetChunk(const BudgetChunk& other)
89 : amount(other.amount), expiration(other.expiration) {} 105 : amount(other.amount), expiration(other.expiration) {}
90 106
91 double amount; 107 double amount;
92 base::Time expiration; 108 base::Time expiration;
93 }; 109 };
94 110
95 // Data structure for caching budget information. 111 // Data structure for caching budget information.
96 using BudgetChunks = std::vector<BudgetChunk>; 112 using BudgetChunks = std::list<BudgetChunk>;
113
114 // Holds information about the overall budget for a site. This includes the
115 // time the budget was last incremented, as well as a list of budget chunks
116 // which have been awarded.
117 struct BudgetInfo {
118 BudgetInfo();
119 BudgetInfo(const BudgetInfo&& other);
120 ~BudgetInfo();
121
122 base::Time last_engagement_award;
123 BudgetChunks chunks;
124
125 DISALLOW_COPY_AND_ASSIGN(BudgetInfo);
126 };
97 127
98 using AddToCacheCallback = base::Callback<void(bool success)>; 128 using AddToCacheCallback = base::Callback<void(bool success)>;
99 129
100 void OnDatabaseInit(bool success); 130 void OnDatabaseInit(bool success);
101 131
102 bool IsCached(const GURL& origin) const; 132 bool IsCached(const GURL& origin) const;
103 133
104 void AddToCache(const GURL& origin, 134 void AddToCache(const GURL& origin,
105 const AddToCacheCallback& callback, 135 const AddToCacheCallback& callback,
106 bool success, 136 bool success,
107 std::unique_ptr<budget_service::Budget> budget); 137 std::unique_ptr<budget_service::Budget> budget);
108 138
109 void DidGetBudget(const GURL& origin, 139 void DidGetBudget(const GURL& origin,
110 const GetBudgetDetailsCallback& callback, 140 const GetBudgetDetailsCallback& callback,
111 bool success); 141 bool success);
112 142
113 void WriteCachedValuesToDatabase(const GURL& origin, 143 void WriteCachedValuesToDatabase(const GURL& origin,
114 const StoreBudgetCallback& callback); 144 const StoreBudgetCallback& callback);
115 145
116 void CleanupExpiredBudget(const GURL& origin); 146 void CleanupExpiredBudget(const GURL& origin);
117 147
118 // The database for storing budget information. 148 // The database for storing budget information.
119 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; 149 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_;
120 150
121 // Cached data for the origins which have been loaded. 151 // Cached data for the origins which have been loaded.
122 std::unordered_map<std::string, BudgetChunks> budget_map_; 152 std::unordered_map<std::string, BudgetInfo> budget_map_;
123 153
124 // The clock used to vend times. 154 // The clock used to vend times.
125 std::unique_ptr<base::Clock> clock_; 155 std::unique_ptr<base::Clock> clock_;
126 156
127 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; 157 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_;
128 158
129 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); 159 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase);
130 }; 160 };
131 161
132 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ 162 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_
OLDNEW
« no previous file with comments | « chrome/browser/budget_service/budget.proto ('k') | chrome/browser/budget_service/budget_database.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698