| 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 | 11 |
| 12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "components/leveldb_proto/proto_database.h" | 15 #include "components/leveldb_proto/proto_database.h" |
| 16 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 class Clock; | 19 class Clock; |
| 19 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
| 20 class Time; | 21 class Time; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace budget_service { | 24 namespace budget_service { |
| 24 class Budget; | 25 class Budget; |
| 25 } | 26 } |
| 26 | 27 |
| 27 class GURL; | 28 class GURL; |
| 28 class Profile; | 29 class Profile; |
| 29 | 30 |
| 30 // A class used to asynchronously read and write details of the budget | 31 // A class used to asynchronously read and write details of the budget |
| 31 // assigned to an origin. The class uses an underlying LevelDB. | 32 // assigned to an origin. The class uses an underlying LevelDB. |
| 32 class BudgetDatabase { | 33 class BudgetDatabase { |
| 33 public: | 34 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 | |
| 47 // Data structure for returing the budget decay prediction to the caller. | |
| 48 using BudgetPrediction = std::list<BudgetStatus>; | |
| 49 | |
| 50 // Callback for setting a budget value. | 35 // Callback for setting a budget value. |
| 51 using StoreBudgetCallback = base::Callback<void(bool success)>; | 36 using StoreBudgetCallback = base::Callback<void(bool success)>; |
| 52 | 37 |
| 53 // Callback for getting a list of all budget chunks. | 38 // Callback for getting a list of all budget chunks. |
| 54 using GetBudgetDetailsCallback = | 39 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; |
| 55 base::Callback<void(bool success, const BudgetPrediction& prediction)>; | |
| 56 | 40 |
| 57 // The database_dir specifies the location of the budget information on | 41 // The database_dir specifies the location of the budget information on |
| 58 // disk. The task_runner is used by the ProtoDatabase to handle all blocking | 42 // disk. The task_runner is used by the ProtoDatabase to handle all blocking |
| 59 // calls and disk access. | 43 // calls and disk access. |
| 60 BudgetDatabase(Profile* profile, | 44 BudgetDatabase(Profile* profile, |
| 61 const base::FilePath& database_dir, | 45 const base::FilePath& database_dir, |
| 62 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 46 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 63 ~BudgetDatabase(); | 47 ~BudgetDatabase(); |
| 64 | 48 |
| 65 // Get the full budget expectation for the origin. This will return a | 49 // Get the full budget expectation for the origin. This will return a |
| 66 // sequence of time points and the expected budget at those times. | 50 // sequence of time points and the expected budget at those times. |
| 67 void GetBudgetDetails(const GURL& origin, | 51 void GetBudgetDetails(const GURL& origin, const GetBudgetCallback& callback); |
| 68 const GetBudgetDetailsCallback& callback); | |
| 69 | 52 |
| 70 // Spend a particular amount of budget for an origin. The callback takes | 53 // Spend a particular amount of budget for an origin. The callback takes |
| 71 // a boolean which indicates whether the origin had enough budget to spend. | 54 // a boolean which indicates whether the origin had enough budget to spend. |
| 72 // If it returns success, then the budget was deducted and the result written | 55 // If it returns success, then the budget was deducted and the result written |
| 73 // to the database. | 56 // to the database. |
| 74 void SpendBudget(const GURL& origin, | 57 void SpendBudget(const GURL& origin, |
| 75 double amount, | 58 double amount, |
| 76 const StoreBudgetCallback& callback); | 59 const StoreBudgetCallback& callback); |
| 77 | 60 |
| 78 private: | 61 private: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 DISALLOW_COPY_AND_ASSIGN(BudgetInfo); | 93 DISALLOW_COPY_AND_ASSIGN(BudgetInfo); |
| 111 }; | 94 }; |
| 112 | 95 |
| 113 using AddToCacheCallback = base::Callback<void(bool success)>; | 96 using AddToCacheCallback = base::Callback<void(bool success)>; |
| 114 using SyncCacheCallback = base::Callback<void(bool success)>; | 97 using SyncCacheCallback = base::Callback<void(bool success)>; |
| 115 | 98 |
| 116 void OnDatabaseInit(bool success); | 99 void OnDatabaseInit(bool success); |
| 117 | 100 |
| 118 bool IsCached(const GURL& origin) const; | 101 bool IsCached(const GURL& origin) const; |
| 119 | 102 |
| 103 double GetBudget(const GURL& origin); |
| 104 |
| 120 void AddToCache(const GURL& origin, | 105 void AddToCache(const GURL& origin, |
| 121 const AddToCacheCallback& callback, | 106 const AddToCacheCallback& callback, |
| 122 bool success, | 107 bool success, |
| 123 std::unique_ptr<budget_service::Budget> budget); | 108 std::unique_ptr<budget_service::Budget> budget); |
| 124 | 109 |
| 125 void GetBudgetAfterSync(const GURL& origin, | 110 void GetBudgetAfterSync(const GURL& origin, |
| 126 const GetBudgetDetailsCallback& callback, | 111 const GetBudgetCallback& callback, |
| 127 bool success); | 112 bool success); |
| 128 | 113 |
| 129 void SpendBudgetAfterSync(const GURL& origin, | 114 void SpendBudgetAfterSync(const GURL& origin, |
| 130 double amount, | 115 double amount, |
| 131 const StoreBudgetCallback& callback, | 116 const StoreBudgetCallback& callback, |
| 132 bool success); | 117 bool success); |
| 133 | 118 |
| 134 void WriteCachedValuesToDatabase(const GURL& origin, | 119 void WriteCachedValuesToDatabase(const GURL& origin, |
| 135 const StoreBudgetCallback& callback); | 120 const StoreBudgetCallback& callback); |
| 136 | 121 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 157 | 142 |
| 158 // The clock used to vend times. | 143 // The clock used to vend times. |
| 159 std::unique_ptr<base::Clock> clock_; | 144 std::unique_ptr<base::Clock> clock_; |
| 160 | 145 |
| 161 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 146 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
| 162 | 147 |
| 163 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 148 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
| 164 }; | 149 }; |
| 165 | 150 |
| 166 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 151 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
| OLD | NEW |