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

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

Issue 2272563005: Start plumbing connections from the BudgetManager to the BudgetDatabase (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix the task runner 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 <memory> 9 #include <memory>
10 #include <unordered_map> 10 #include <unordered_map>
11 11
12 #include "base/callback_forward.h" 12 #include "base/callback_forward.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "components/leveldb_proto/proto_database.h" 15 #include "components/leveldb_proto/proto_database.h"
16 16
17 namespace base { 17 namespace base {
18 class Clock; 18 class Clock;
19 class SequencedTaskRunner; 19 class SequencedTaskRunner;
20 class Time; 20 class Time;
21 } 21 }
22 22
23 namespace budget_service { 23 namespace budget_service {
24 class Budget; 24 class Budget;
25 } 25 }
26 26
27 class GURL; 27 class GURL;
28 class Profile;
28 29
29 // A class used to asynchronously read and write details of the budget 30 // A class used to asynchronously read and write details of the budget
30 // assigned to an origin. The class uses an underlying LevelDB. 31 // assigned to an origin. The class uses an underlying LevelDB.
31 class BudgetDatabase { 32 class BudgetDatabase {
32 public: 33 public:
33 // Data structure to hold information about the cumulative amount of budget 34 // Data structure to hold information about the cumulative amount of budget
34 // at a particular point in time in the future. The budget described by a 35 // at a particular point in time in the future. The budget described by a
35 // BudgetStatus represents the budget from zero or more BudgetChunk objects. 36 // BudgetStatus represents the budget from zero or more BudgetChunk objects.
36 struct BudgetStatus { 37 struct BudgetStatus {
37 BudgetStatus(double budget_at, base::Time time) 38 BudgetStatus(double budget_at, base::Time time)
(...skipping 11 matching lines...) Expand all
49 // Callback for setting a budget value. 50 // Callback for setting a budget value.
50 using StoreBudgetCallback = base::Callback<void(bool success)>; 51 using StoreBudgetCallback = base::Callback<void(bool success)>;
51 52
52 // Callback for getting a list of all budget chunks. 53 // Callback for getting a list of all budget chunks.
53 using GetBudgetDetailsCallback = 54 using GetBudgetDetailsCallback =
54 base::Callback<void(bool success, const BudgetPrediction& prediction)>; 55 base::Callback<void(bool success, const BudgetPrediction& prediction)>;
55 56
56 // The database_dir specifies the location of the budget information on 57 // The database_dir specifies the location of the budget information on
57 // 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
58 // calls and disk access. 59 // calls and disk access.
59 BudgetDatabase(const base::FilePath& database_dir, 60 BudgetDatabase(Profile* profile,
61 const base::FilePath& database_dir,
60 const scoped_refptr<base::SequencedTaskRunner>& task_runner); 62 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
61 ~BudgetDatabase(); 63 ~BudgetDatabase();
62 64
63 // Get the full budget expectation for the origin. This will return a 65 // Get the full budget expectation for the origin. This will return a
64 // sequence of time points and the expected budget at those times. 66 // sequence of time points and the expected budget at those times.
65 void GetBudgetDetails(const GURL& origin, 67 void GetBudgetDetails(const GURL& origin,
66 const GetBudgetDetailsCallback& callback); 68 const GetBudgetDetailsCallback& callback);
67 69
68 // Add budget for an origin. The caller specifies the amount, and the method
69 // adds the amount to the cache with the correct expiration. Callback is
70 // invoked only after the newly cached value is written to storage. This
71 // should only be called after the budget has been read from the database.
72 void AddBudget(const GURL& origin,
73 double amount,
74 const StoreBudgetCallback& callback);
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 70 // 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. 71 // 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 72 // 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. 73 // to the database.
89 void SpendBudget(const GURL& origin, 74 void SpendBudget(const GURL& origin,
90 double amount, 75 double amount,
91 const StoreBudgetCallback& callback); 76 const StoreBudgetCallback& callback);
92 77
93 private: 78 private:
94 friend class BudgetDatabaseTest; 79 friend class BudgetDatabaseTest;
95 80
96 // Used to allow tests to change time for testing. 81 // Used to allow tests to change time for testing.
97 void SetClockForTesting(std::unique_ptr<base::Clock> clock); 82 void SetClockForTesting(std::unique_ptr<base::Clock> clock);
98 83
(...skipping 20 matching lines...) Expand all
119 BudgetInfo(const BudgetInfo&& other); 104 BudgetInfo(const BudgetInfo&& other);
120 ~BudgetInfo(); 105 ~BudgetInfo();
121 106
122 base::Time last_engagement_award; 107 base::Time last_engagement_award;
123 BudgetChunks chunks; 108 BudgetChunks chunks;
124 109
125 DISALLOW_COPY_AND_ASSIGN(BudgetInfo); 110 DISALLOW_COPY_AND_ASSIGN(BudgetInfo);
126 }; 111 };
127 112
128 using AddToCacheCallback = base::Callback<void(bool success)>; 113 using AddToCacheCallback = base::Callback<void(bool success)>;
114 using SyncCacheCallback = base::Callback<void(bool success)>;
129 115
130 void OnDatabaseInit(bool success); 116 void OnDatabaseInit(bool success);
131 117
132 bool IsCached(const GURL& origin) const; 118 bool IsCached(const GURL& origin) const;
133 119
134 void AddToCache(const GURL& origin, 120 void AddToCache(const GURL& origin,
135 const AddToCacheCallback& callback, 121 const AddToCacheCallback& callback,
136 bool success, 122 bool success,
137 std::unique_ptr<budget_service::Budget> budget); 123 std::unique_ptr<budget_service::Budget> budget);
138 124
139 void DidGetBudget(const GURL& origin, 125 void GetBudgetAfterSync(const GURL& origin,
140 const GetBudgetDetailsCallback& callback, 126 const GetBudgetDetailsCallback& callback,
141 bool success); 127 bool success);
128
129 void SpendBudgetAfterSync(const GURL& origin,
130 double amount,
131 const StoreBudgetCallback& callback,
132 bool success);
142 133
143 void WriteCachedValuesToDatabase(const GURL& origin, 134 void WriteCachedValuesToDatabase(const GURL& origin,
144 const StoreBudgetCallback& callback); 135 const StoreBudgetCallback& callback);
145 136
146 void CleanupExpiredBudget(const GURL& origin); 137 void SyncCache(const GURL& origin, const SyncCacheCallback& callback);
138 void SyncLoadedCache(const GURL& origin,
139 const SyncCacheCallback& callback,
140 bool success);
141
142 // Add budget based on engagement with an origin. The method queries for the
143 // engagement score of the origin, and then calculates when engagement budget
144 // was last awarded and awards a portion of the score based on that.
145 // This only writes budget to the cache.
146 void AddEngagementBudget(const GURL& origin);
147
148 bool CleanupExpiredBudget(const GURL& origin);
149
150 Profile* profile_;
147 151
148 // The database for storing budget information. 152 // The database for storing budget information.
149 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; 153 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_;
150 154
151 // Cached data for the origins which have been loaded. 155 // Cached data for the origins which have been loaded.
152 std::unordered_map<std::string, BudgetInfo> budget_map_; 156 std::unordered_map<std::string, BudgetInfo> budget_map_;
153 157
154 // The clock used to vend times. 158 // The clock used to vend times.
155 std::unique_ptr<base::Clock> clock_; 159 std::unique_ptr<base::Clock> clock_;
156 160
157 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; 161 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_;
158 162
159 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); 163 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase);
160 }; 164 };
161 165
162 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ 166 #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