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