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 SequencedTaskRunner; | 20 class SequencedTaskRunner; |
20 } | 21 } |
21 | 22 |
22 namespace budget_service { | 23 namespace budget_service { |
23 class Budget; | 24 class Budget; |
24 } | 25 } |
25 | 26 |
26 class GURL; | 27 class GURL; |
27 | 28 |
28 // A class used to asynchronously read and write details of the budget | 29 // A class used to asynchronously read and write details of the budget |
29 // assigned to an origin. The class uses an underlying LevelDB. | 30 // assigned to an origin. The class uses an underlying LevelDB. |
30 class BudgetDatabase { | 31 class BudgetDatabase { |
31 public: | 32 public: |
32 // Data structure for returing the budget decay expectations to the caller. | 33 // Data structure for returing the budget decay expectations to the caller. |
33 using BudgetExpectation = std::list<std::pair<double, double>>; | 34 using BudgetExpectation = std::list<std::pair<double, double>>; |
34 | 35 |
35 // Callback for the basic GetBudget call. | |
36 using GetValueCallback = | |
37 base::Callback<void(bool success, | |
38 std::unique_ptr<budget_service::Budget>)>; | |
39 | |
40 // Callback for setting a budget value. | 36 // Callback for setting a budget value. |
41 using SetValueCallback = base::Callback<void(bool success)>; | 37 using StoreBudgetCallback = base::Callback<void(bool success)>; |
42 | 38 |
43 // Callback for getting a list of all budget chunks. | 39 // Callback for getting a list of all budget chunks. |
44 using GetBudgetDetailsCallback = base::Callback< | 40 using GetBudgetDetailsCallback = base::Callback< |
45 void(bool success, double debt, const BudgetExpectation& expectation)>; | 41 void(bool success, double debt, const BudgetExpectation& expectation)>; |
46 | 42 |
47 // The database_dir specifies the location of the budget information on | 43 // The database_dir specifies the location of the budget information on |
48 // disk. The task_runner is used by the ProtoDatabase to handle all blocking | 44 // disk. The task_runner is used by the ProtoDatabase to handle all blocking |
49 // calls and disk access. | 45 // calls and disk access. |
50 BudgetDatabase(const base::FilePath& database_dir, | 46 BudgetDatabase(const base::FilePath& database_dir, |
51 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 47 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
52 ~BudgetDatabase(); | 48 ~BudgetDatabase(); |
53 | 49 |
54 void GetValue(const GURL& origin, const GetValueCallback& callback); | |
55 void SetValue(const GURL& origin, | |
56 const budget_service::Budget& budget, | |
57 const SetValueCallback& callback); | |
58 | |
59 // Get the full budget expectation for the origin. This will return any | 50 // Get the full budget expectation for the origin. This will return any |
60 // debt as well as a sequence of time points and the expected budget at | 51 // debt as well as a sequence of time points and the expected budget at |
61 // those times. | 52 // those times. |
62 void GetBudgetDetails(const GURL& origin, | 53 void GetBudgetDetails(const GURL& origin, |
63 const GetBudgetDetailsCallback& callback); | 54 const GetBudgetDetailsCallback& callback); |
64 | 55 |
| 56 // 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 // invoked only after the newly cached value is written to storage. |
| 59 void AddBudget(const GURL& origin, |
| 60 double amount, |
| 61 const StoreBudgetCallback& callback); |
| 62 |
65 private: | 63 private: |
| 64 friend class BudgetDatabaseTest; |
| 65 |
| 66 // Used to allow tests to change time for testing. |
| 67 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
| 68 |
66 // Data structure for caching budget information. | 69 // Data structure for caching budget information. |
67 using BudgetChunks = std::vector<std::pair<double, double>>; | 70 using BudgetChunks = std::vector<std::pair<double, double>>; |
68 using BudgetInfo = std::pair<double, BudgetChunks>; | 71 using BudgetInfo = std::pair<double, BudgetChunks>; |
69 | 72 |
70 using AddToCacheCallback = base::Callback<void(bool success)>; | 73 using AddToCacheCallback = base::Callback<void(bool success)>; |
71 | 74 |
72 void OnDatabaseInit(bool success); | 75 void OnDatabaseInit(bool success); |
73 | 76 |
74 void AddToCache(const GURL& origin, | 77 void AddToCache(const GURL& origin, |
75 const AddToCacheCallback& callback, | 78 const AddToCacheCallback& callback, |
76 bool success, | 79 bool success, |
77 std::unique_ptr<budget_service::Budget> budget); | 80 std::unique_ptr<budget_service::Budget> budget); |
78 | 81 |
79 void DidGetBudget(const GURL& origin, | 82 void DidGetBudget(const GURL& origin, |
80 const GetBudgetDetailsCallback& callback, | 83 const GetBudgetDetailsCallback& callback, |
81 bool success); | 84 bool success); |
82 | 85 |
| 86 void WriteCachedValuesToDatabase(const GURL& origin, |
| 87 const StoreBudgetCallback& callback); |
| 88 |
83 // The database for storing budget information. | 89 // The database for storing budget information. |
84 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 90 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
85 | 91 |
86 // Cached data for the origins which have been loaded. | 92 // Cached data for the origins which have been loaded. |
87 std::unordered_map<std::string, BudgetInfo> budget_map_; | 93 std::unordered_map<std::string, BudgetInfo> budget_map_; |
88 | 94 |
| 95 // The clock used to vend times. |
| 96 std::unique_ptr<base::Clock> clock_; |
| 97 |
89 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 98 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
90 | 99 |
91 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 100 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
92 }; | 101 }; |
93 | 102 |
94 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 103 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
OLD | NEW |