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 <memory> | 9 #include <memory> |
| 10 #include <unordered_map> |
| 11 #include <vector> |
9 | 12 |
10 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
11 #include "base/macros.h" | 14 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
13 #include "components/leveldb_proto/proto_database.h" | 16 #include "components/leveldb_proto/proto_database.h" |
14 | 17 |
15 namespace base { | 18 namespace base { |
16 class SequencedTaskRunner; | 19 class SequencedTaskRunner; |
17 } | 20 } |
18 | 21 |
19 namespace budget_service { | 22 namespace budget_service { |
20 class Budget; | 23 class Budget; |
21 } | 24 } |
22 | 25 |
23 class GURL; | 26 class GURL; |
24 | 27 |
25 // A class used to asynchronously read and write details of the budget | 28 // A class used to asynchronously read and write details of the budget |
26 // assigned to an origin. The class uses an underlying LevelDB. | 29 // assigned to an origin. The class uses an underlying LevelDB. |
27 class BudgetDatabase { | 30 class BudgetDatabase { |
28 public: | 31 public: |
| 32 // Data structure for returing the budget decay expectations to the caller. |
| 33 using BudgetExpectation = std::list<std::pair<double, double>>; |
| 34 |
29 // Callback for the basic GetBudget call. | 35 // Callback for the basic GetBudget call. |
30 using GetValueCallback = | 36 using GetValueCallback = |
31 base::Callback<void(bool success, | 37 base::Callback<void(bool success, |
32 std::unique_ptr<budget_service::Budget>)>; | 38 std::unique_ptr<budget_service::Budget>)>; |
33 | 39 |
34 // Callback for setting a budget value. | 40 // Callback for setting a budget value. |
35 using SetValueCallback = base::Callback<void(bool success)>; | 41 using SetValueCallback = base::Callback<void(bool success)>; |
36 | 42 |
| 43 // Callback for getting a list of all budget chunks. |
| 44 using GetBudgetDetailsCallback = base::Callback< |
| 45 void(bool success, double debt, const BudgetExpectation& expectation)>; |
| 46 |
37 // The database_dir specifies the location of the budget information on | 47 // The database_dir specifies the location of the budget information on |
38 // disk. The task_runner is used by the ProtoDatabase to handle all blocking | 48 // disk. The task_runner is used by the ProtoDatabase to handle all blocking |
39 // calls and disk access. | 49 // calls and disk access. |
40 BudgetDatabase(const base::FilePath& database_dir, | 50 BudgetDatabase(const base::FilePath& database_dir, |
41 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 51 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
42 ~BudgetDatabase(); | 52 ~BudgetDatabase(); |
43 | 53 |
44 void GetValue(const GURL& origin, const GetValueCallback& callback); | 54 void GetValue(const GURL& origin, const GetValueCallback& callback); |
45 void SetValue(const GURL& origin, | 55 void SetValue(const GURL& origin, |
46 const budget_service::Budget& budget, | 56 const budget_service::Budget& budget, |
47 const SetValueCallback& callback); | 57 const SetValueCallback& callback); |
48 | 58 |
| 59 // 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 |
| 61 // those times. |
| 62 void GetBudgetDetails(const GURL& origin, |
| 63 const GetBudgetDetailsCallback& callback); |
| 64 |
49 private: | 65 private: |
| 66 // Data structure for caching budget information. |
| 67 using BudgetChunks = std::vector<std::pair<double, double>>; |
| 68 using BudgetInfo = std::pair<double, BudgetChunks>; |
| 69 |
| 70 using AddToCacheCallback = base::Callback<void(bool success)>; |
| 71 |
50 void OnDatabaseInit(bool success); | 72 void OnDatabaseInit(bool success); |
51 | 73 |
| 74 void AddToCache(const GURL& origin, |
| 75 const AddToCacheCallback& callback, |
| 76 bool success, |
| 77 std::unique_ptr<budget_service::Budget> budget); |
| 78 |
| 79 void DidGetBudget(const GURL& origin, |
| 80 const GetBudgetDetailsCallback& callback, |
| 81 bool success); |
| 82 |
52 // The database for storing budget information. | 83 // The database for storing budget information. |
53 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 84 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
54 | 85 |
| 86 // Cached data for the origins which have been loaded. |
| 87 std::unordered_map<std::string, BudgetInfo> budget_map_; |
| 88 |
55 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 89 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
56 | 90 |
57 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 91 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
58 }; | 92 }; |
59 | 93 |
60 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 94 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
OLD | NEW |