| 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 Clock; |
| 20 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
| 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 | 29 |
| 29 // 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 |
| 30 // assigned to an origin. The class uses an underlying LevelDB. | 31 // assigned to an origin. The class uses an underlying LevelDB. |
| 31 class BudgetDatabase { | 32 class BudgetDatabase { |
| 32 public: | 33 public: |
| 33 // Data structure for returing the budget decay expectations to the caller. | 34 // Data structure for returing the budget decay expectations to the caller. |
| 34 using BudgetExpectation = std::list<std::pair<double, double>>; | 35 using BudgetExpectation = std::list<std::pair<double, base::Time>>; |
| 35 | 36 |
| 36 // Callback for setting a budget value. | 37 // Callback for setting a budget value. |
| 37 using StoreBudgetCallback = base::Callback<void(bool success)>; | 38 using StoreBudgetCallback = base::Callback<void(bool success)>; |
| 38 | 39 |
| 39 // Callback for getting a list of all budget chunks. | 40 // Callback for getting a list of all budget chunks. |
| 40 using GetBudgetDetailsCallback = base::Callback< | 41 using GetBudgetDetailsCallback = base::Callback< |
| 41 void(bool success, double debt, const BudgetExpectation& expectation)>; | 42 void(bool success, double debt, const BudgetExpectation& expectation)>; |
| 42 | 43 |
| 43 // The database_dir specifies the location of the budget information on | 44 // The database_dir specifies the location of the budget information on |
| 44 // disk. The task_runner is used by the ProtoDatabase to handle all blocking | 45 // disk. The task_runner is used by the ProtoDatabase to handle all blocking |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 bool success, | 80 bool success, |
| 80 std::unique_ptr<budget_service::Budget> budget); | 81 std::unique_ptr<budget_service::Budget> budget); |
| 81 | 82 |
| 82 void DidGetBudget(const GURL& origin, | 83 void DidGetBudget(const GURL& origin, |
| 83 const GetBudgetDetailsCallback& callback, | 84 const GetBudgetDetailsCallback& callback, |
| 84 bool success); | 85 bool success); |
| 85 | 86 |
| 86 void WriteCachedValuesToDatabase(const GURL& origin, | 87 void WriteCachedValuesToDatabase(const GURL& origin, |
| 87 const StoreBudgetCallback& callback); | 88 const StoreBudgetCallback& callback); |
| 88 | 89 |
| 90 void CleanupExpiredBudget(const GURL& origin); |
| 91 |
| 89 // The database for storing budget information. | 92 // The database for storing budget information. |
| 90 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; | 93 std::unique_ptr<leveldb_proto::ProtoDatabase<budget_service::Budget>> db_; |
| 91 | 94 |
| 92 // Cached data for the origins which have been loaded. | 95 // Cached data for the origins which have been loaded. |
| 93 std::unordered_map<std::string, BudgetInfo> budget_map_; | 96 std::unordered_map<std::string, BudgetInfo> budget_map_; |
| 94 | 97 |
| 95 // The clock used to vend times. | 98 // The clock used to vend times. |
| 96 std::unique_ptr<base::Clock> clock_; | 99 std::unique_ptr<base::Clock> clock_; |
| 97 | 100 |
| 98 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; | 101 base::WeakPtrFactory<BudgetDatabase> weak_ptr_factory_; |
| 99 | 102 |
| 100 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); | 103 DISALLOW_COPY_AND_ASSIGN(BudgetDatabase); |
| 101 }; | 104 }; |
| 102 | 105 |
| 103 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ | 106 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_DATABASE_H_ |
| OLD | NEW |