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" | |
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 caching budget information. | |
32 typedef std::list<std::pair<double, double>> BudgetChunks; | |
Peter Beverloo
2016/07/22 16:14:52
std::vector? I don't think any of the list-over-ve
harkness
2016/07/26 16:42:07
It doesn't really matter for my purposes. Access w
Peter Beverloo
2016/07/26 17:26:20
std::vector is more popular in Chromium code by an
harkness
2016/07/27 10:59:02
I changed BudgetChunks to be a vector, but because
| |
33 typedef std::pair<double, BudgetChunks> BudgetInfo; | |
Peter Beverloo
2016/07/22 16:14:52
It looks like both BudgetChunks and BudgetInfo can
harkness
2016/07/26 16:42:07
Ah yes, I'd planned to talk to you about this. You
Peter Beverloo
2016/07/26 17:26:20
No, there are at most three blocks, one of each {p
harkness
2016/07/27 10:59:02
Done.
| |
34 | |
35 // Data structure for returing the budget decay expectations to the caller. | |
36 typedef std::list<std::pair<double, double>> BudgetExpectation; | |
Peter Beverloo
2016/07/22 16:14:52
All three of these should be using `using`, as you
harkness
2016/07/26 16:42:08
Done.
| |
37 | |
29 // Callback for the basic GetBudget call. | 38 // Callback for the basic GetBudget call. |
30 using GetValueCallback = | 39 using GetValueCallback = |
31 base::Callback<void(bool success, | 40 base::Callback<void(bool success, |
32 std::unique_ptr<budget_service::Budget>)>; | 41 std::unique_ptr<budget_service::Budget>)>; |
33 | 42 |
34 // Callback for setting a budget value. | 43 // Callback for setting a budget value. |
35 using SetValueCallback = base::Callback<void(bool success)>; | 44 using SetValueCallback = base::Callback<void(bool success)>; |
36 | 45 |
46 // Callback for getting a list of all budget chunks. | |
47 using GetBudgetDetailsCallback = base::Callback< | |
48 void(bool success, double debt, const BudgetExpectation& expectation)>; | |
49 | |
50 using AddToCacheCallback = base::Callback<void(bool success)>; | |
Peter Beverloo
2016/07/22 16:14:52
private
harkness
2016/07/26 16:42:07
Done.
| |
51 | |
37 // The database_dir specifies the location of the budget information on | 52 // 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 | 53 // disk. The task_runner is used by the ProtoDatabase to handle all blocking |
39 // calls and disk access. | 54 // calls and disk access. |
40 BudgetDatabase(const base::FilePath& database_dir, | 55 BudgetDatabase(const base::FilePath& database_dir, |
41 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 56 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
42 ~BudgetDatabase(); | 57 ~BudgetDatabase(); |
43 | 58 |
44 void GetValue(const GURL& origin, const GetValueCallback& callback); | 59 void GetValue(const GURL& origin, const GetValueCallback& callback); |
45 void SetValue(const GURL& origin, | 60 void SetValue(const GURL& origin, |
46 const budget_service::Budget& budget, | 61 const budget_service::Budget& budget, |
47 const SetValueCallback& callback); | 62 const SetValueCallback& callback); |
48 | 63 |
64 // Get the full budget expectation for the origin. This will return any | |
65 // debt as well as a sequence of time points and the expected budget at | |
66 // those times. | |
67 void GetBudgetDetails(const GURL& origin, | |
68 const GetBudgetDetailsCallback& callback); | |
69 | |
49 private: | 70 private: |
50 void OnDatabaseInit(bool success); | 71 void OnDatabaseInit(bool success); |
51 | 72 |
73 void AddToCache(const GURL& origin, | |
74 const AddToCacheCallback& callback, | |
75 bool success, | |
76 std::unique_ptr<budget_service::Budget> budget); | |
77 | |
78 void ReturnBudgetDetails(const GURL& origin, | |
79 const GetBudgetDetailsCallback& callback, | |
80 bool success); | |
81 | |
52 // The database for storing budget information. | 82 // The database for storing budget information. |
53 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 83 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
54 | 84 |
85 // Cached data for the origins which have been loaded. | |
86 base::hash_map<std::string, BudgetInfo> budget_map_; | |
Peter Beverloo
2016/07/22 16:14:52
base::hash_map<> is deprecated, please use std::un
harkness
2016/07/26 16:42:08
Done.
| |
87 | |
55 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 88 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
56 | 89 |
57 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 90 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
58 }; | 91 }; |
59 | 92 |
60 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 93 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
OLD | NEW |