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

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

Issue 2326523003: Connect Mojo budget_service to BudgetManager implementation of Reserve. (Closed)
Patch Set: Fixed issue dependency. Created 4 years, 3 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
« no previous file with comments | « no previous file | chrome/browser/budget_service/budget_database.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 17 matching lines...) Expand all
28 namespace url { 28 namespace url {
29 class Origin; 29 class Origin;
30 } 30 }
31 31
32 class Profile; 32 class Profile;
33 33
34 // A class used to asynchronously read and write details of the budget 34 // A class used to asynchronously read and write details of the budget
35 // assigned to an origin. The class uses an underlying LevelDB. 35 // assigned to an origin. The class uses an underlying LevelDB.
36 class BudgetDatabase { 36 class BudgetDatabase {
37 public: 37 public:
38 // Callback for setting a budget value.
39 using StoreBudgetCallback = base::Callback<void(bool success)>;
40
41 // Callback for getting a list of all budget chunks. 38 // Callback for getting a list of all budget chunks.
42 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; 39 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback;
43 40
41 // This is invoked only after the spend has been written to the database.
42 using SpendBudgetCallback =
43 base::Callback<void(blink::mojom::BudgetServiceErrorType error_type,
44 bool success)>;
45
44 // The database_dir specifies the location of the budget information on 46 // The database_dir specifies the location of the budget information on
45 // disk. The task_runner is used by the ProtoDatabase to handle all blocking 47 // disk. The task_runner is used by the ProtoDatabase to handle all blocking
46 // calls and disk access. 48 // calls and disk access.
47 BudgetDatabase(Profile* profile, 49 BudgetDatabase(Profile* profile,
48 const base::FilePath& database_dir, 50 const base::FilePath& database_dir,
49 const scoped_refptr<base::SequencedTaskRunner>& task_runner); 51 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
50 ~BudgetDatabase(); 52 ~BudgetDatabase();
51 53
52 // Get the full budget expectation for the origin. This will return a 54 // Get the full budget expectation for the origin. This will return a
53 // sequence of time points and the expected budget at those times. 55 // sequence of time points and the expected budget at those times.
54 void GetBudgetDetails(const url::Origin& origin, 56 void GetBudgetDetails(const url::Origin& origin,
55 const GetBudgetCallback& callback); 57 const GetBudgetCallback& callback);
56 58
57 // Spend a particular amount of budget for an origin. The callback takes 59 // Spend a particular amount of budget for an origin. The callback indicates
58 // a boolean which indicates whether the origin had enough budget to spend. 60 // whether there was an error and if the origin had enough budget.
59 // If it returns success, then the budget was deducted and the result written
60 // to the database.
61 void SpendBudget(const url::Origin& origin, 61 void SpendBudget(const url::Origin& origin,
62 double amount, 62 double amount,
63 const StoreBudgetCallback& callback); 63 const SpendBudgetCallback& callback);
64 64
65 private: 65 private:
66 friend class BudgetDatabaseTest; 66 friend class BudgetDatabaseTest;
67 67
68 // Used to allow tests to change time for testing. 68 // Used to allow tests to change time for testing.
69 void SetClockForTesting(std::unique_ptr<base::Clock> clock); 69 void SetClockForTesting(std::unique_ptr<base::Clock> clock);
70 70
71 // Holds information about individual pieces of awarded budget. There is a 71 // Holds information about individual pieces of awarded budget. There is a
72 // one-to-one mapping of these to the chunks in the underlying database. 72 // one-to-one mapping of these to the chunks in the underlying database.
73 struct BudgetChunk { 73 struct BudgetChunk {
(...skipping 16 matching lines...) Expand all
90 BudgetInfo(); 90 BudgetInfo();
91 BudgetInfo(const BudgetInfo&& other); 91 BudgetInfo(const BudgetInfo&& other);
92 ~BudgetInfo(); 92 ~BudgetInfo();
93 93
94 base::Time last_engagement_award; 94 base::Time last_engagement_award;
95 BudgetChunks chunks; 95 BudgetChunks chunks;
96 96
97 DISALLOW_COPY_AND_ASSIGN(BudgetInfo); 97 DISALLOW_COPY_AND_ASSIGN(BudgetInfo);
98 }; 98 };
99 99
100 using AddToCacheCallback = base::Callback<void(bool success)>; 100 // Callback for writing budget values to the database.
101 using SyncCacheCallback = base::Callback<void(bool success)>; 101 using StoreBudgetCallback = base::Callback<void(bool success)>;
102
103 using CacheCallback = base::Callback<void(bool success)>;
102 104
103 void OnDatabaseInit(bool success); 105 void OnDatabaseInit(bool success);
104 106
105 bool IsCached(const url::Origin& origin) const; 107 bool IsCached(const url::Origin& origin) const;
106 108
107 double GetBudget(const url::Origin& origin) const; 109 double GetBudget(const url::Origin& origin) const;
108 110
109 void AddToCache(const url::Origin& origin, 111 void AddToCache(const url::Origin& origin,
110 const AddToCacheCallback& callback, 112 const CacheCallback& callback,
111 bool success, 113 bool success,
112 std::unique_ptr<budget_service::Budget> budget); 114 std::unique_ptr<budget_service::Budget> budget);
113 115
114 void GetBudgetAfterSync(const url::Origin& origin, 116 void GetBudgetAfterSync(const url::Origin& origin,
115 const GetBudgetCallback& callback, 117 const GetBudgetCallback& callback,
116 bool success); 118 bool success);
117 119
118 void SpendBudgetAfterSync(const url::Origin& origin, 120 void SpendBudgetAfterSync(const url::Origin& origin,
119 double amount, 121 double amount,
120 const StoreBudgetCallback& callback, 122 const SpendBudgetCallback& callback,
121 bool success); 123 bool success);
122 124
125 void SpendBudgetAfterWrite(const SpendBudgetCallback& callback, bool success);
126
123 void WriteCachedValuesToDatabase(const url::Origin& origin, 127 void WriteCachedValuesToDatabase(const url::Origin& origin,
124 const StoreBudgetCallback& callback); 128 const StoreBudgetCallback& callback);
125 129
126 void SyncCache(const url::Origin& origin, const SyncCacheCallback& callback); 130 void SyncCache(const url::Origin& origin, const CacheCallback& callback);
127 void SyncLoadedCache(const url::Origin& origin, 131 void SyncLoadedCache(const url::Origin& origin,
128 const SyncCacheCallback& callback, 132 const CacheCallback& callback,
129 bool success); 133 bool success);
130 134
131 // Add budget based on engagement with an origin. The method queries for the 135 // Add budget based on engagement with an origin. The method queries for the
132 // engagement score of the origin, and then calculates when engagement budget 136 // engagement score of the origin, and then calculates when engagement budget
133 // was last awarded and awards a portion of the score based on that. 137 // was last awarded and awards a portion of the score based on that.
134 // This only writes budget to the cache. 138 // This only writes budget to the cache.
135 void AddEngagementBudget(const url::Origin& origin); 139 void AddEngagementBudget(const url::Origin& origin);
136 140
137 bool CleanupExpiredBudget(const url::Origin& origin); 141 bool CleanupExpiredBudget(const url::Origin& origin);
138 142
139 Profile* profile_; 143 Profile* profile_;
140 144
141 // The database for storing budget information. 145 // The database for storing budget information.
142 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; 146 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_;
143 147
144 // Cached data for the origins which have been loaded. 148 // Cached data for the origins which have been loaded.
145 std::map<url::Origin, BudgetInfo> budget_map_; 149 std::map<url::Origin, BudgetInfo> budget_map_;
146 150
147 // The clock used to vend times. 151 // The clock used to vend times.
148 std::unique_ptr<base::Clock> clock_; 152 std::unique_ptr<base::Clock> clock_;
149 153
150 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; 154 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_;
151 155
152 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); 156 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase);
153 }; 157 };
154 158
155 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ 159 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/budget_service/budget_database.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698