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> |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 namespace budget_service { | 24 namespace budget_service { |
| 25 class Budget; | 25 class Budget; |
| 26 } | 26 } |
| 27 | 27 |
| 28 class GURL; | 28 class GURL; |
| 29 | 29 |
| 30 // A class used to asynchronously read and write details of the budget | 30 // A class used to asynchronously read and write details of the budget |
| 31 // assigned to an origin. The class uses an underlying LevelDB. | 31 // assigned to an origin. The class uses an underlying LevelDB. |
| 32 class BudgetDatabase { | 32 class BudgetDatabase { |
| 33 public: | 33 public: |
| 34 // Data structure to hold information about the budget at particular points in | |
| 35 // time in the future. | |
| 36 struct BudgetStatus { | |
| 37 BudgetStatus(double budget_at, base::Time time) | |
| 38 : budget_at(budget_at), time(time) {} | |
| 39 BudgetStatus(const BudgetStatus& other) | |
| 40 : budget_at(other.budget_at), time(other.time) {} | |
| 41 | |
| 42 double budget_at; | |
| 43 base::Time time; | |
| 44 }; | |
| 45 | |
| 34 // Data structure for returing the budget decay expectations to the caller. | 46 // Data structure for returing the budget decay expectations to the caller. |
| 35 using BudgetExpectation = std::list<std::pair<double, base::Time>>; | 47 using BudgetExpectation = std::list<BudgetStatus>; |
| 36 | 48 |
| 37 // Callback for setting a budget value. | 49 // Callback for setting a budget value. |
| 38 using StoreBudgetCallback = base::Callback<void(bool success)>; | 50 using StoreBudgetCallback = base::Callback<void(bool success)>; |
| 39 | 51 |
| 40 // Callback for getting a list of all budget chunks. | 52 // Callback for getting a list of all budget chunks. |
| 41 using GetBudgetDetailsCallback = base::Callback< | 53 using GetBudgetDetailsCallback = |
| 42 void(bool success, double debt, const BudgetExpectation& expectation)>; | 54 base::Callback<void(bool success, const BudgetExpectation& expectation)>; |
| 43 | 55 |
| 44 // The database_dir specifies the location of the budget information on | 56 // The database_dir specifies the location of the budget information on |
| 45 // disk. The task_runner is used by the ProtoDatabase to handle all blocking | 57 // disk. The task_runner is used by the ProtoDatabase to handle all blocking |
| 46 // calls and disk access. | 58 // calls and disk access. |
| 47 BudgetDatabase(const base::FilePath& database_dir, | 59 BudgetDatabase(const base::FilePath& database_dir, |
| 48 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 60 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 49 ~BudgetDatabase(); | 61 ~BudgetDatabase(); |
| 50 | 62 |
| 51 // Get the full budget expectation for the origin. This will return any | 63 // Get the full budget expectation for the origin. This will return a |
| 52 // debt as well as a sequence of time points and the expected budget at | 64 // sequence of time points and the expected budget at those times. |
| 53 // those times. | |
| 54 void GetBudgetDetails(const GURL& origin, | 65 void GetBudgetDetails(const GURL& origin, |
| 55 const GetBudgetDetailsCallback& callback); | 66 const GetBudgetDetailsCallback& callback); |
| 56 | 67 |
| 57 // Add budget for an origin. The caller specifies the amount, and the method | 68 // Add budget for an origin. The caller specifies the amount, and the method |
| 58 // adds the amount to the cache with the correct expiration. Callback is | 69 // adds the amount to the cache with the correct expiration. Callback is |
| 59 // invoked only after the newly cached value is written to storage. This | 70 // invoked only after the newly cached value is written to storage. This |
| 60 // should only be called after the budget has been read from the database. | 71 // should only be called after the budget has been read from the database. |
| 61 void AddBudget(const GURL& origin, | 72 void AddBudget(const GURL& origin, |
| 62 double amount, | 73 double amount, |
| 63 const StoreBudgetCallback& callback); | 74 const StoreBudgetCallback& callback); |
| 64 | 75 |
| 65 private: | 76 private: |
| 66 friend class BudgetDatabaseTest; | 77 friend class BudgetDatabaseTest; |
| 67 | 78 |
| 68 // Used to allow tests to change time for testing. | 79 // Used to allow tests to change time for testing. |
| 69 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 80 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
| 70 | 81 |
| 82 // Holds information about individual pieces of awarded budget. There is a | |
| 83 // one-to-one mapping of these to the chunks in the underlying database. | |
| 84 struct BudgetChunk { | |
|
Peter Beverloo
2016/08/10 15:36:45
I think it would be good to be very explicit in do
harkness
2016/08/10 16:41:55
Agreed. I expanded the description of BudgetStatus
| |
| 85 BudgetChunk(double amount, base::Time expiration) | |
| 86 : amount(amount), expiration(expiration) {} | |
| 87 BudgetChunk(const BudgetChunk& other) | |
| 88 : amount(other.amount), expiration(other.expiration) {} | |
| 89 | |
| 90 double amount; | |
| 91 base::Time expiration; | |
| 92 }; | |
| 93 | |
| 71 // Data structure for caching budget information. | 94 // Data structure for caching budget information. |
| 72 using BudgetChunks = std::vector<std::pair<double, base::Time>>; | 95 using BudgetChunks = std::vector<BudgetChunk>; |
| 73 using BudgetInfo = std::pair<double, BudgetChunks>; | |
| 74 | 96 |
| 75 using AddToCacheCallback = base::Callback<void(bool success)>; | 97 using AddToCacheCallback = base::Callback<void(bool success)>; |
| 76 | 98 |
| 77 void OnDatabaseInit(bool success); | 99 void OnDatabaseInit(bool success); |
| 78 | 100 |
| 79 bool IsCached(const GURL& origin) const; | 101 bool IsCached(const GURL& origin) const; |
| 80 | 102 |
| 81 void AddToCache(const GURL& origin, | 103 void AddToCache(const GURL& origin, |
| 82 const AddToCacheCallback& callback, | 104 const AddToCacheCallback& callback, |
| 83 bool success, | 105 bool success, |
| 84 std::unique_ptr<budget_service::Budget> budget); | 106 std::unique_ptr<budget_service::Budget> budget); |
| 85 | 107 |
| 86 void DidGetBudget(const GURL& origin, | 108 void DidGetBudget(const GURL& origin, |
| 87 const GetBudgetDetailsCallback& callback, | 109 const GetBudgetDetailsCallback& callback, |
| 88 bool success); | 110 bool success); |
| 89 | 111 |
| 90 void WriteCachedValuesToDatabase(const GURL& origin, | 112 void WriteCachedValuesToDatabase(const GURL& origin, |
| 91 const StoreBudgetCallback& callback); | 113 const StoreBudgetCallback& callback); |
| 92 | 114 |
| 93 void CleanupExpiredBudget(const GURL& origin); | 115 void CleanupExpiredBudget(const GURL& origin); |
| 94 | 116 |
| 95 // The database for storing budget information. | 117 // The database for storing budget information. |
| 96 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 118 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
| 97 | 119 |
| 98 // Cached data for the origins which have been loaded. | 120 // Cached data for the origins which have been loaded. |
| 99 std::unordered_map<std::string, BudgetInfo> budget_map_; | 121 std::unordered_map<std::string, BudgetChunks> budget_map_; |
| 100 | 122 |
| 101 // The clock used to vend times. | 123 // The clock used to vend times. |
| 102 std::unique_ptr<base::Clock> clock_; | 124 std::unique_ptr<base::Clock> clock_; |
| 103 | 125 |
| 104 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 126 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
| 105 | 127 |
| 106 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 128 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
| 107 }; | 129 }; |
| 108 | 130 |
| 109 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 131 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
| OLD | NEW |