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