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> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "components/leveldb_proto/proto_database.h" | 16 #include "components/leveldb_proto/proto_database.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 } | 22 } |
22 | 23 |
23 namespace budget_service { | 24 namespace budget_service { |
24 class Budget; | 25 class Budget; |
25 } | 26 } |
26 | 27 |
27 class GURL; | 28 class GURL; |
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 for returing the budget decay expectations to the caller. | 34 // Data structure for returing the budget decay expectations to the caller. |
34 using BudgetExpectation = std::list<std::pair<double, double>>; | 35 using BudgetExpectation = std::list<std::pair<double, base::Time>>; |
35 | 36 |
36 // Callback for setting a budget value. | 37 // Callback for setting a budget value. |
37 using StoreBudgetCallback = base::Callback<void(bool success)>; | 38 using StoreBudgetCallback = base::Callback<void(bool success)>; |
38 | 39 |
39 // Callback for getting a list of all budget chunks. | 40 // Callback for getting a list of all budget chunks. |
40 using GetBudgetDetailsCallback = base::Callback< | 41 using GetBudgetDetailsCallback = base::Callback< |
41 void(bool success, double debt, const BudgetExpectation& expectation)>; | 42 void(bool success, double debt, const BudgetExpectation& expectation)>; |
42 | 43 |
43 // The database_dir specifies the location of the budget information on | 44 // The database_dir specifies the location of the budget information on |
44 // 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 |
45 // calls and disk access. | 46 // calls and disk access. |
46 BudgetDatabase(const base::FilePath& database_dir, | 47 BudgetDatabase(const base::FilePath& database_dir, |
47 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 48 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
48 ~BudgetDatabase(); | 49 ~BudgetDatabase(); |
49 | 50 |
50 // Get the full budget expectation for the origin. This will return any | 51 // Get the full budget expectation for the origin. This will return any |
51 // debt as well as a sequence of time points and the expected budget at | 52 // debt as well as a sequence of time points and the expected budget at |
52 // those times. | 53 // those times. |
53 void GetBudgetDetails(const GURL& origin, | 54 void GetBudgetDetails(const GURL& origin, |
54 const GetBudgetDetailsCallback& callback); | 55 const GetBudgetDetailsCallback& callback); |
55 | 56 |
56 // Add budget for an origin. The caller specifies the amount, and the method | 57 // Add budget for an origin. The caller specifies the amount, and the method |
57 // adds the amount to the cache with the correct expiration. Callback is | 58 // adds the amount to the cache with the correct expiration. Callback is |
58 // invoked only after the newly cached value is written to storage. | 59 // invoked only after the newly cached value is written to storage. This |
| 60 // should only be called after the budget has been read from the database. |
59 void AddBudget(const GURL& origin, | 61 void AddBudget(const GURL& origin, |
60 double amount, | 62 double amount, |
61 const StoreBudgetCallback& callback); | 63 const StoreBudgetCallback& callback); |
62 | 64 |
63 private: | 65 private: |
64 friend class BudgetDatabaseTest; | 66 friend class BudgetDatabaseTest; |
65 | 67 |
66 // Used to allow tests to change time for testing. | 68 // Used to allow tests to change time for testing. |
67 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 69 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
68 | 70 |
69 // Data structure for caching budget information. | 71 // Data structure for caching budget information. |
70 using BudgetChunks = std::vector<std::pair<double, double>>; | 72 using BudgetChunks = std::vector<std::pair<double, base::Time>>; |
71 using BudgetInfo = std::pair<double, BudgetChunks>; | 73 using BudgetInfo = std::pair<double, BudgetChunks>; |
72 | 74 |
73 using AddToCacheCallback = base::Callback<void(bool success)>; | 75 using AddToCacheCallback = base::Callback<void(bool success)>; |
74 | 76 |
75 void OnDatabaseInit(bool success); | 77 void OnDatabaseInit(bool success); |
76 | 78 |
| 79 bool IsCached(const GURL& origin) const; |
| 80 |
77 void AddToCache(const GURL& origin, | 81 void AddToCache(const GURL& origin, |
78 const AddToCacheCallback& callback, | 82 const AddToCacheCallback& callback, |
79 bool success, | 83 bool success, |
80 std::unique_ptr<budget_service::Budget> budget); | 84 std::unique_ptr<budget_service::Budget> budget); |
81 | 85 |
82 void DidGetBudget(const GURL& origin, | 86 void DidGetBudget(const GURL& origin, |
83 const GetBudgetDetailsCallback& callback, | 87 const GetBudgetDetailsCallback& callback, |
84 bool success); | 88 bool success); |
85 | 89 |
86 void WriteCachedValuesToDatabase(const GURL& origin, | 90 void WriteCachedValuesToDatabase(const GURL& origin, |
87 const StoreBudgetCallback& callback); | 91 const StoreBudgetCallback& callback); |
88 | 92 |
| 93 void CleanupExpiredBudget(const GURL& origin); |
| 94 |
89 // The database for storing budget information. | 95 // The database for storing budget information. |
90 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 96 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
91 | 97 |
92 // Cached data for the origins which have been loaded. | 98 // Cached data for the origins which have been loaded. |
93 std::unordered_map<std::string, BudgetInfo> budget_map_; | 99 std::unordered_map<std::string, BudgetInfo> budget_map_; |
94 | 100 |
95 // The clock used to vend times. | 101 // The clock used to vend times. |
96 std::unique_ptr<base::Clock> clock_; | 102 std::unique_ptr<base::Clock> clock_; |
97 | 103 |
98 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 104 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
99 | 105 |
100 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 106 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
101 }; | 107 }; |
102 | 108 |
103 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 109 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
OLD | NEW |