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 <map> |
9 #include <memory> | 10 #include <memory> |
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 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" | 16 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 class Clock; | 19 class Clock; |
20 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
21 class Time; | 21 class Time; |
22 } | 22 } |
23 | 23 |
24 namespace budget_service { | 24 namespace budget_service { |
25 class Budget; | 25 class Budget; |
26 } | 26 } |
27 | 27 |
28 class GURL; | 28 namespace url { |
| 29 class Origin; |
| 30 } |
| 31 |
29 class Profile; | 32 class Profile; |
30 | 33 |
31 // 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 |
32 // assigned to an origin. The class uses an underlying LevelDB. | 35 // assigned to an origin. The class uses an underlying LevelDB. |
33 class BudgetDatabase { | 36 class BudgetDatabase { |
34 public: | 37 public: |
35 // Callback for setting a budget value. | 38 // Callback for setting a budget value. |
36 using StoreBudgetCallback = base::Callback<void(bool success)>; | 39 using StoreBudgetCallback = base::Callback<void(bool success)>; |
37 | 40 |
38 // Callback for getting a list of all budget chunks. | 41 // Callback for getting a list of all budget chunks. |
39 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; | 42 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; |
40 | 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 url::Origin& origin, |
| 55 const GetBudgetCallback& callback); |
52 | 56 |
53 // Spend a particular amount of budget for an origin. The callback takes | 57 // Spend a particular amount of budget for an origin. The callback takes |
54 // a boolean which indicates whether the origin had enough budget to spend. | 58 // a boolean which indicates whether the origin had enough budget to spend. |
55 // If it returns success, then the budget was deducted and the result written | 59 // If it returns success, then the budget was deducted and the result written |
56 // to the database. | 60 // to the database. |
57 void SpendBudget(const GURL& origin, | 61 void SpendBudget(const url::Origin& origin, |
58 double amount, | 62 double amount, |
59 const StoreBudgetCallback& callback); | 63 const StoreBudgetCallback& callback); |
60 | 64 |
61 private: | 65 private: |
62 friend class BudgetDatabaseTest; | 66 friend class BudgetDatabaseTest; |
63 | 67 |
64 // Used to allow tests to change time for testing. | 68 // Used to allow tests to change time for testing. |
65 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 69 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
66 | 70 |
67 // Holds information about individual pieces of awarded budget. There is a | 71 // Holds information about individual pieces of awarded budget. There is a |
(...skipping 23 matching lines...) Expand all Loading... |
91 BudgetChunks chunks; | 95 BudgetChunks chunks; |
92 | 96 |
93 DISALLOW_COPY_AND_ASSIGN(BudgetInfo); | 97 DISALLOW_COPY_AND_ASSIGN(BudgetInfo); |
94 }; | 98 }; |
95 | 99 |
96 using AddToCacheCallback = base::Callback<void(bool success)>; | 100 using AddToCacheCallback = base::Callback<void(bool success)>; |
97 using SyncCacheCallback = base::Callback<void(bool success)>; | 101 using SyncCacheCallback = base::Callback<void(bool success)>; |
98 | 102 |
99 void OnDatabaseInit(bool success); | 103 void OnDatabaseInit(bool success); |
100 | 104 |
101 bool IsCached(const GURL& origin) const; | 105 bool IsCached(const url::Origin& origin) const; |
102 | 106 |
103 double GetBudget(const GURL& origin) const; | 107 double GetBudget(const url::Origin& origin) const; |
104 | 108 |
105 void AddToCache(const GURL& origin, | 109 void AddToCache(const url::Origin& origin, |
106 const AddToCacheCallback& callback, | 110 const AddToCacheCallback& callback, |
107 bool success, | 111 bool success, |
108 std::unique_ptr<budget_service::Budget> budget); | 112 std::unique_ptr<budget_service::Budget> budget); |
109 | 113 |
110 void GetBudgetAfterSync(const GURL& origin, | 114 void GetBudgetAfterSync(const url::Origin& origin, |
111 const GetBudgetCallback& callback, | 115 const GetBudgetCallback& callback, |
112 bool success); | 116 bool success); |
113 | 117 |
114 void SpendBudgetAfterSync(const GURL& origin, | 118 void SpendBudgetAfterSync(const url::Origin& origin, |
115 double amount, | 119 double amount, |
116 const StoreBudgetCallback& callback, | 120 const StoreBudgetCallback& callback, |
117 bool success); | 121 bool success); |
118 | 122 |
119 void WriteCachedValuesToDatabase(const GURL& origin, | 123 void WriteCachedValuesToDatabase(const url::Origin& origin, |
120 const StoreBudgetCallback& callback); | 124 const StoreBudgetCallback& callback); |
121 | 125 |
122 void SyncCache(const GURL& origin, const SyncCacheCallback& callback); | 126 void SyncCache(const url::Origin& origin, const SyncCacheCallback& callback); |
123 void SyncLoadedCache(const GURL& origin, | 127 void SyncLoadedCache(const url::Origin& origin, |
124 const SyncCacheCallback& callback, | 128 const SyncCacheCallback& callback, |
125 bool success); | 129 bool success); |
126 | 130 |
127 // Add budget based on engagement with an origin. The method queries for the | 131 // 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 | 132 // 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. | 133 // was last awarded and awards a portion of the score based on that. |
130 // This only writes budget to the cache. | 134 // This only writes budget to the cache. |
131 void AddEngagementBudget(const GURL& origin); | 135 void AddEngagementBudget(const url::Origin& origin); |
132 | 136 |
133 bool CleanupExpiredBudget(const GURL& origin); | 137 bool CleanupExpiredBudget(const url::Origin& origin); |
134 | 138 |
135 Profile* profile_; | 139 Profile* profile_; |
136 | 140 |
137 // The database for storing budget information. | 141 // The database for storing budget information. |
138 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 142 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
139 | 143 |
140 // Cached data for the origins which have been loaded. | 144 // Cached data for the origins which have been loaded. |
141 std::unordered_map<std::string, BudgetInfo> budget_map_; | 145 std::map<url::Origin, BudgetInfo> budget_map_; |
142 | 146 |
143 // The clock used to vend times. | 147 // The clock used to vend times. |
144 std::unique_ptr<base::Clock> clock_; | 148 std::unique_ptr<base::Clock> clock_; |
145 | 149 |
146 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 150 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
147 | 151 |
148 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 152 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
149 }; | 153 }; |
150 | 154 |
151 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 155 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
OLD | NEW |