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 14 matching lines...) Expand all Loading... | |
25 class Budget; | 25 class Budget; |
26 } | 26 } |
27 | 27 |
28 class GURL; | 28 class GURL; |
29 class Profile; | 29 class Profile; |
30 | 30 |
31 // A class used to asynchronously read and write details of the budget | 31 // A class used to asynchronously read and write details of the budget |
32 // assigned to an origin. The class uses an underlying LevelDB. | 32 // assigned to an origin. The class uses an underlying LevelDB. |
33 class BudgetDatabase { | 33 class BudgetDatabase { |
34 public: | 34 public: |
35 // Callback for setting a budget value. | |
36 using StoreBudgetCallback = base::Callback<void(bool success)>; | |
37 | |
38 // Callback for getting a list of all budget chunks. | 35 // Callback for getting a list of all budget chunks. |
39 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; | 36 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; |
40 | 37 |
38 // Callback for SpendBudget. This will be invoked only after the spend has | |
39 // been written to the database. It takes two values, an error type which | |
40 // indicates if there was a particular error, and a bool that indicates | |
41 // whether the origin had budget to spend. | |
42 using SpendBudgetCallback = blink::mojom::BudgetService::ReserveCallback; | |
Peter Beverloo
2016/09/08 17:16:06
We should probably just redefine the callback form
harkness
2016/09/13 13:41:28
Done.
| |
43 | |
41 // The database_dir specifies the location of the budget information on | 44 // The database_dir specifies the location of the budget information on |
42 // disk. The task_runner is used by the ProtoDatabase to handle all blocking | 45 // disk. The task_runner is used by the ProtoDatabase to handle all blocking |
43 // calls and disk access. | 46 // calls and disk access. |
44 BudgetDatabase(Profile* profile, | 47 BudgetDatabase(Profile* profile, |
45 const base::FilePath& database_dir, | 48 const base::FilePath& database_dir, |
46 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 49 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
47 ~BudgetDatabase(); | 50 ~BudgetDatabase(); |
48 | 51 |
49 // Get the full budget expectation for the origin. This will return a | 52 // Get the full budget expectation for the origin. This will return a |
50 // sequence of time points and the expected budget at those times. | 53 // sequence of time points and the expected budget at those times. |
51 void GetBudgetDetails(const GURL& origin, const GetBudgetCallback& callback); | 54 void GetBudgetDetails(const GURL& origin, const GetBudgetCallback& callback); |
52 | 55 |
53 // Spend a particular amount of budget for an origin. The callback takes | 56 // Spend a particular amount of budget for an origin. The callback indicates |
54 // a boolean which indicates whether the origin had enough budget to spend. | 57 // whether there was an error and if the origin had enough budget. |
55 // If it returns success, then the budget was deducted and the result written | |
56 // to the database. | |
57 void SpendBudget(const GURL& origin, | 58 void SpendBudget(const GURL& origin, |
58 double amount, | 59 double amount, |
59 const StoreBudgetCallback& callback); | 60 const SpendBudgetCallback& callback); |
60 | 61 |
61 private: | 62 private: |
62 friend class BudgetDatabaseTest; | 63 friend class BudgetDatabaseTest; |
63 | 64 |
64 // Used to allow tests to change time for testing. | 65 // Used to allow tests to change time for testing. |
65 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 66 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
66 | 67 |
67 // Holds information about individual pieces of awarded budget. There is a | 68 // Holds information about individual pieces of awarded budget. There is a |
68 // one-to-one mapping of these to the chunks in the underlying database. | 69 // one-to-one mapping of these to the chunks in the underlying database. |
69 struct BudgetChunk { | 70 struct BudgetChunk { |
(...skipping 16 matching lines...) Expand all Loading... | |
86 BudgetInfo(); | 87 BudgetInfo(); |
87 BudgetInfo(const BudgetInfo&& other); | 88 BudgetInfo(const BudgetInfo&& other); |
88 ~BudgetInfo(); | 89 ~BudgetInfo(); |
89 | 90 |
90 base::Time last_engagement_award; | 91 base::Time last_engagement_award; |
91 BudgetChunks chunks; | 92 BudgetChunks chunks; |
92 | 93 |
93 DISALLOW_COPY_AND_ASSIGN(BudgetInfo); | 94 DISALLOW_COPY_AND_ASSIGN(BudgetInfo); |
94 }; | 95 }; |
95 | 96 |
96 using AddToCacheCallback = base::Callback<void(bool success)>; | 97 // Callback for writing budget values to the database. |
97 using SyncCacheCallback = base::Callback<void(bool success)>; | 98 using StoreBudgetCallback = base::Callback<void(bool success)>; |
99 | |
100 using CacheCallback = base::Callback<void(bool success)>; | |
98 | 101 |
99 void OnDatabaseInit(bool success); | 102 void OnDatabaseInit(bool success); |
100 | 103 |
101 bool IsCached(const GURL& origin) const; | 104 bool IsCached(const GURL& origin) const; |
102 | 105 |
103 double GetBudget(const GURL& origin) const; | 106 double GetBudget(const GURL& origin) const; |
104 | 107 |
105 void AddToCache(const GURL& origin, | 108 void AddToCache(const GURL& origin, |
106 const AddToCacheCallback& callback, | 109 const CacheCallback& callback, |
107 bool success, | 110 bool success, |
108 std::unique_ptr<budget_service::Budget> budget); | 111 std::unique_ptr<budget_service::Budget> budget); |
109 | 112 |
110 void GetBudgetAfterSync(const GURL& origin, | 113 void GetBudgetAfterSync(const GURL& origin, |
111 const GetBudgetCallback& callback, | 114 const GetBudgetCallback& callback, |
112 bool success); | 115 bool success); |
113 | 116 |
114 void SpendBudgetAfterSync(const GURL& origin, | 117 void SpendBudgetAfterSync(const GURL& origin, |
115 double amount, | 118 double amount, |
116 const StoreBudgetCallback& callback, | 119 const SpendBudgetCallback& callback, |
117 bool success); | 120 bool success); |
118 | 121 |
122 void SpendBudgetAfterWrite(const SpendBudgetCallback& callback, bool success); | |
123 | |
119 void WriteCachedValuesToDatabase(const GURL& origin, | 124 void WriteCachedValuesToDatabase(const GURL& origin, |
120 const StoreBudgetCallback& callback); | 125 const StoreBudgetCallback& callback); |
121 | 126 |
122 void SyncCache(const GURL& origin, const SyncCacheCallback& callback); | 127 void SyncCache(const GURL& origin, const CacheCallback& callback); |
123 void SyncLoadedCache(const GURL& origin, | 128 void SyncLoadedCache(const GURL& origin, |
124 const SyncCacheCallback& callback, | 129 const CacheCallback& callback, |
125 bool success); | 130 bool success); |
126 | 131 |
127 // Add budget based on engagement with an origin. The method queries for the | 132 // Add budget based on engagement with an origin. The method queries for the |
128 // engagement score of the origin, and then calculates when engagement budget | 133 // engagement score of the origin, and then calculates when engagement budget |
129 // was last awarded and awards a portion of the score based on that. | 134 // was last awarded and awards a portion of the score based on that. |
130 // This only writes budget to the cache. | 135 // This only writes budget to the cache. |
131 void AddEngagementBudget(const GURL& origin); | 136 void AddEngagementBudget(const GURL& origin); |
132 | 137 |
133 bool CleanupExpiredBudget(const GURL& origin); | 138 bool CleanupExpiredBudget(const GURL& origin); |
134 | 139 |
135 Profile* profile_; | 140 Profile* profile_; |
136 | 141 |
137 // The database for storing budget information. | 142 // The database for storing budget information. |
138 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 143 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
139 | 144 |
140 // Cached data for the origins which have been loaded. | 145 // Cached data for the origins which have been loaded. |
141 std::unordered_map<std::string, BudgetInfo> budget_map_; | 146 std::unordered_map<std::string, BudgetInfo> budget_map_; |
142 | 147 |
143 // The clock used to vend times. | 148 // The clock used to vend times. |
144 std::unique_ptr<base::Clock> clock_; | 149 std::unique_ptr<base::Clock> clock_; |
145 | 150 |
146 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 151 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
147 | 152 |
148 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 153 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
149 }; | 154 }; |
150 | 155 |
151 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 156 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
OLD | NEW |