Chromium Code Reviews| 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 void AddBudget(const GURL& origin, | |
|
Peter Beverloo
2016/08/01 17:33:09
+docs
harkness
2016/08/03 11:17:48
Done.
| |
| 57 double amount, | |
| 58 const StoreBudgetCallback& callback); | |
| 59 | |
| 65 private: | 60 private: |
| 61 friend class BudgetDatabaseTest; | |
| 62 | |
| 63 // Used to allow tests to change time for testing. | |
| 64 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | |
| 65 | |
| 66 // Data structure for caching budget information. | 66 // Data structure for caching budget information. |
| 67 using BudgetChunks = std::vector<std::pair<double, double>>; | 67 using BudgetChunks = std::list<std::pair<double, double>>; |
|
Peter Beverloo
2016/08/01 17:33:09
Merge error or deliberate?
harkness
2016/08/03 11:17:48
oops, merge error.
| |
| 68 using BudgetInfo = std::pair<double, BudgetChunks>; | 68 using BudgetInfo = std::pair<double, BudgetChunks>; |
| 69 | 69 |
| 70 using AddToCacheCallback = base::Callback<void(bool success)>; | 70 using AddToCacheCallback = base::Callback<void(bool success)>; |
| 71 | 71 |
| 72 void OnDatabaseInit(bool success); | 72 void OnDatabaseInit(bool success); |
| 73 | 73 |
| 74 void AddToCache(const GURL& origin, | 74 void AddToCache(const GURL& origin, |
| 75 const AddToCacheCallback& callback, | 75 const AddToCacheCallback& callback, |
| 76 bool success, | 76 bool success, |
| 77 std::unique_ptr<budget_service::Budget> budget); | 77 std::unique_ptr<budget_service::Budget> budget); |
| 78 | 78 |
| 79 void DidGetBudget(const GURL& origin, | 79 void DidGetBudget(const GURL& origin, |
| 80 const GetBudgetDetailsCallback& callback, | 80 const GetBudgetDetailsCallback& callback, |
| 81 bool success); | 81 bool success); |
| 82 | 82 |
| 83 void WriteCachedValuesToDatabase(const GURL& origin, | |
| 84 const StoreBudgetCallback& callback); | |
| 85 | |
| 83 // The database for storing budget information. | 86 // The database for storing budget information. |
| 84 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 87 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
| 85 | 88 |
| 86 // Cached data for the origins which have been loaded. | 89 // Cached data for the origins which have been loaded. |
| 87 std::unordered_map<std::string, BudgetInfo> budget_map_; | 90 std::unordered_map<std::string, BudgetInfo> budget_map_; |
| 88 | 91 |
| 92 // The clock used to vend times. | |
| 93 std::unique_ptr<base::Clock> clock_; | |
| 94 | |
| 89 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 95 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
| 90 | 96 |
| 91 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 97 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
| 92 }; | 98 }; |
| 93 | 99 |
| 94 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 100 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
| OLD | NEW |