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