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 #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> |
(...skipping 27 matching lines...) Expand all Loading... | |
38 BudgetStatus(double budget_at, base::Time time) | 38 BudgetStatus(double budget_at, base::Time time) |
39 : budget_at(budget_at), time(time) {} | 39 : budget_at(budget_at), time(time) {} |
40 BudgetStatus(const BudgetStatus& other) | 40 BudgetStatus(const BudgetStatus& other) |
41 : budget_at(other.budget_at), time(other.time) {} | 41 : budget_at(other.budget_at), time(other.time) {} |
42 | 42 |
43 double budget_at; | 43 double budget_at; |
44 base::Time time; | 44 base::Time time; |
45 }; | 45 }; |
46 | 46 |
47 // Data structure for returing the budget decay expectations to the caller. | 47 // Data structure for returing the budget decay expectations to the caller. |
48 using BudgetExpectation = std::list<BudgetStatus>; | 48 using BudgetExpectation = std::list<BudgetStatus>; |
johnme
2016/08/19 18:48:36
Nit: Can we rename this to BudgetPrediction (in a
harkness
2016/08/22 13:46:26
Done.
| |
49 | 49 |
50 // Callback for setting a budget value. | 50 // Callback for setting a budget value. |
51 using StoreBudgetCallback = base::Callback<void(bool success)>; | 51 using StoreBudgetCallback = base::Callback<void(bool success)>; |
52 | 52 |
53 // Callback for getting a list of all budget chunks. | 53 // Callback for getting a list of all budget chunks. |
54 using GetBudgetDetailsCallback = | 54 using GetBudgetDetailsCallback = |
55 base::Callback<void(bool success, const BudgetExpectation& expectation)>; | 55 base::Callback<void(bool success, const BudgetExpectation& expectation)>; |
56 | 56 |
57 // The database_dir specifies the location of the budget information on | 57 // 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 | 58 // disk. The task_runner is used by the ProtoDatabase to handle all blocking |
59 // calls and disk access. | 59 // calls and disk access. |
60 BudgetDatabase(const base::FilePath& database_dir, | 60 BudgetDatabase(const base::FilePath& database_dir, |
61 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 61 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
62 ~BudgetDatabase(); | 62 ~BudgetDatabase(); |
63 | 63 |
64 // Get the full budget expectation for the origin. This will return a | 64 // Get the full budget expectation for the origin. This will return a |
65 // sequence of time points and the expected budget at those times. | 65 // sequence of time points and the expected budget at those times. |
66 void GetBudgetDetails(const GURL& origin, | 66 void GetBudgetDetails(const GURL& origin, |
67 const GetBudgetDetailsCallback& callback); | 67 const GetBudgetDetailsCallback& callback); |
68 | 68 |
69 // Add budget for an origin. The caller specifies the amount, and the method | 69 // 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 | 70 // 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 | 71 // 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. | 72 // should only be called after the budget has been read from the database. |
73 void AddBudget(const GURL& origin, | 73 void AddBudget(const GURL& origin, |
johnme
2016/08/19 18:48:36
What's the use case for adding non-engagement budg
harkness
2016/08/22 13:46:26
There are a few of these. One would be a bootstrap
| |
74 double amount, | 74 double amount, |
75 const StoreBudgetCallback& callback); | 75 const StoreBudgetCallback& callback); |
76 | 76 |
77 // Add budget based on engagement with an origin. The caller specifies the | |
78 // engagement score of the origin, and the method calculates when engagement | |
79 // budget was last awarded and awards a portion of the score based on that. | |
80 // Callback is invoked after the value is written to storage. This should | |
81 // only be called after the budget has been read from the database. | |
82 void AddEngagementBudget(const GURL& origin, | |
83 double engagementScore, | |
84 const StoreBudgetCallback& callback); | |
85 | |
86 // Spend a particular amount of budget for an origin. The callback specifies | |
johnme
2016/08/19 18:48:36
Nit: s/specifies/takes/
harkness
2016/08/22 13:46:26
Done.
| |
87 // a boolean which indicates whether the origin had enough budget to spend. | |
88 // If it returns success, then the budget was deducted and the result written | |
89 // to the database. This should only be called after the budget has been read. | |
johnme
2016/08/19 18:48:36
It seems a slightly awkward constraint that "This
harkness
2016/08/22 13:46:26
Peter and I had a long discussion about how much w
johnme
2016/08/22 14:50:31
Doesn't that just mean you'll have to add equivale
| |
90 void SpendBudget(const GURL& origin, | |
91 double amount, | |
92 const StoreBudgetCallback& callback); | |
93 | |
77 private: | 94 private: |
78 friend class BudgetDatabaseTest; | 95 friend class BudgetDatabaseTest; |
79 | 96 |
80 // Used to allow tests to change time for testing. | 97 // Used to allow tests to change time for testing. |
81 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 98 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
82 | 99 |
83 // Holds information about individual pieces of awarded budget. There is a | 100 // 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. | 101 // one-to-one mapping of these to the chunks in the underlying database. |
85 struct BudgetChunk { | 102 struct BudgetChunk { |
86 BudgetChunk(double amount, base::Time expiration) | 103 BudgetChunk(double amount, base::Time expiration) |
87 : amount(amount), expiration(expiration) {} | 104 : amount(amount), expiration(expiration) {} |
88 BudgetChunk(const BudgetChunk& other) | 105 BudgetChunk(const BudgetChunk& other) |
89 : amount(other.amount), expiration(other.expiration) {} | 106 : amount(other.amount), expiration(other.expiration) {} |
90 | 107 |
91 double amount; | 108 double amount; |
92 base::Time expiration; | 109 base::Time expiration; |
93 }; | 110 }; |
94 | 111 |
95 // Data structure for caching budget information. | 112 // Data structure for caching budget information. |
96 using BudgetChunks = std::vector<BudgetChunk>; | 113 using BudgetChunks = std::vector<BudgetChunk>; |
97 | 114 |
115 // Holds information about the overall budget for a site. This includes the | |
116 // time the budget was last incremented, as well as a list of budget chunks | |
117 // which have been awarded. | |
118 struct BudgetInfo { | |
119 BudgetInfo(); | |
120 BudgetInfo(const BudgetInfo& other); | |
121 ~BudgetInfo(); | |
122 | |
123 base::Time last_engagement_award; | |
124 BudgetChunks chunks; | |
125 }; | |
126 | |
98 using AddToCacheCallback = base::Callback<void(bool success)>; | 127 using AddToCacheCallback = base::Callback<void(bool success)>; |
99 | 128 |
100 void OnDatabaseInit(bool success); | 129 void OnDatabaseInit(bool success); |
101 | 130 |
102 bool IsCached(const GURL& origin) const; | 131 bool IsCached(const GURL& origin) const; |
103 | 132 |
104 void AddToCache(const GURL& origin, | 133 void AddToCache(const GURL& origin, |
105 const AddToCacheCallback& callback, | 134 const AddToCacheCallback& callback, |
106 bool success, | 135 bool success, |
107 std::unique_ptr<budget_service::Budget> budget); | 136 std::unique_ptr<budget_service::Budget> budget); |
108 | 137 |
109 void DidGetBudget(const GURL& origin, | 138 void DidGetBudget(const GURL& origin, |
110 const GetBudgetDetailsCallback& callback, | 139 const GetBudgetDetailsCallback& callback, |
111 bool success); | 140 bool success); |
112 | 141 |
113 void WriteCachedValuesToDatabase(const GURL& origin, | 142 void WriteCachedValuesToDatabase(const GURL& origin, |
114 const StoreBudgetCallback& callback); | 143 const StoreBudgetCallback& callback); |
115 | 144 |
116 void CleanupExpiredBudget(const GURL& origin); | 145 void CleanupExpiredBudget(const GURL& origin); |
117 | 146 |
118 // The database for storing budget information. | 147 // The database for storing budget information. |
119 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 148 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
120 | 149 |
121 // Cached data for the origins which have been loaded. | 150 // Cached data for the origins which have been loaded. |
122 std::unordered_map<std::string, BudgetChunks> budget_map_; | 151 std::unordered_map<std::string, BudgetInfo> budget_map_; |
123 | 152 |
124 // The clock used to vend times. | 153 // The clock used to vend times. |
125 std::unique_ptr<base::Clock> clock_; | 154 std::unique_ptr<base::Clock> clock_; |
126 | 155 |
127 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 156 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
128 | 157 |
129 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 158 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
130 }; | 159 }; |
131 | 160 |
132 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 161 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
OLD | NEW |