| 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_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_BUDGET_SERVICE_BUDGET_MANAGER_H_ |
| 6 #define CHROME_BROWSER_BUDGET_SERVICE_BUDGET_MANAGER_H_ | 6 #define CHROME_BROWSER_BUDGET_SERVICE_BUDGET_MANAGER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 public: | 29 public: |
| 30 explicit BudgetManager(Profile* profile); | 30 explicit BudgetManager(Profile* profile); |
| 31 ~BudgetManager() override; | 31 ~BudgetManager() override; |
| 32 | 32 |
| 33 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 33 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
| 34 | 34 |
| 35 // Query for the base cost for any background processing. | 35 // Query for the base cost for any background processing. |
| 36 static double GetCost(blink::mojom::BudgetOperationType type); | 36 static double GetCost(blink::mojom::BudgetOperationType type); |
| 37 | 37 |
| 38 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; | 38 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; |
| 39 using ReserveCallback = base::Callback<void(bool success)>; | 39 using ReserveCallback = blink::mojom::BudgetService::ReserveCallback; |
| 40 using ConsumeCallback = base::Callback<void(bool success)>; | 40 using ConsumeCallback = base::Callback<void(bool success)>; |
| 41 | 41 |
| 42 // Get the budget associated with the origin. This is passed to the | 42 // Get the budget associated with the origin. This is passed to the |
| 43 // callback. Budget will be a sequence of points describing the time and | 43 // callback. Budget will be a sequence of points describing the time and |
| 44 // the budget at that time. | 44 // the budget at that time. |
| 45 void GetBudget(const GURL& origin, const GetBudgetCallback& callback); | 45 void GetBudget(const GURL& origin, const GetBudgetCallback& callback); |
| 46 | 46 |
| 47 // Spend enough budget to cover the cost of the desired action and create | 47 // Spend enough budget to cover the cost of the desired action and create |
| 48 // a reservation for that action. If this returns true to the callback, then | 48 // a reservation for that action. If this returns true to the callback, then |
| 49 // the next action will consume that reservation and not cost any budget. | 49 // the next action will consume that reservation and not cost any budget. |
| 50 void Reserve(const GURL& origin, | 50 void Reserve(const GURL& origin, |
| 51 blink::mojom::BudgetOperationType type, | 51 blink::mojom::BudgetOperationType type, |
| 52 const ReserveCallback& callback); | 52 const ReserveCallback& callback); |
| 53 | 53 |
| 54 // Spend budget, first consuming a reservation if one exists, or spend | 54 // Spend budget, first consuming a reservation if one exists, or spend |
| 55 // directly from the budget. | 55 // directly from the budget. |
| 56 void Consume(const GURL& origin, | 56 void Consume(const GURL& origin, |
| 57 blink::mojom::BudgetOperationType type, | 57 blink::mojom::BudgetOperationType type, |
| 58 const ConsumeCallback& callback); | 58 const ConsumeCallback& callback); |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 friend class BudgetManagerTest; | 61 friend class BudgetManagerTest; |
| 62 | 62 |
| 63 // Called as a callback from BudgetDatabase after it made a SpendBudget |
| 64 // decision. |
| 65 void DidConsume(const ConsumeCallback& callback, |
| 66 blink::mojom::BudgetServiceErrorType error, |
| 67 bool success); |
| 68 |
| 63 // Called as a callback from BudgetDatabase after it has made a reserve | 69 // Called as a callback from BudgetDatabase after it has made a reserve |
| 64 // decision. | 70 // decision. |
| 65 void DidReserve(const GURL& origin, | 71 void DidReserve(const GURL& origin, |
| 66 blink::mojom::BudgetOperationType type, | |
| 67 const ReserveCallback& callback, | 72 const ReserveCallback& callback, |
| 73 blink::mojom::BudgetServiceErrorType error, |
| 68 bool success); | 74 bool success); |
| 69 | 75 |
| 70 Profile* profile_; | 76 Profile* profile_; |
| 71 BudgetDatabase db_; | 77 BudgetDatabase db_; |
| 72 | 78 |
| 73 std::unordered_map<std::string, int> reservation_map_; | 79 std::unordered_map<std::string, int> reservation_map_; |
| 74 base::WeakPtrFactory<BudgetManager> weak_ptr_factory_; | 80 base::WeakPtrFactory<BudgetManager> weak_ptr_factory_; |
| 75 | 81 |
| 76 DISALLOW_COPY_AND_ASSIGN(BudgetManager); | 82 DISALLOW_COPY_AND_ASSIGN(BudgetManager); |
| 77 }; | 83 }; |
| 78 | 84 |
| 79 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_MANAGER_H_ | 85 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_MANAGER_H_ |
| OLD | NEW |