| 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 |
| 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 #include "url/gurl.h" | |
| 17 | 16 |
| 18 class Profile; | 17 class Profile; |
| 19 | 18 |
| 20 namespace user_prefs { | 19 namespace user_prefs { |
| 21 class PrefRegistrySyncable; | 20 class PrefRegistrySyncable; |
| 22 } | 21 } |
| 23 | 22 |
| 23 namespace url { |
| 24 class Origin; |
| 25 } |
| 26 |
| 24 // A budget manager to help Chrome decide how much background work a service | 27 // A budget manager to help Chrome decide how much background work a service |
| 25 // worker should be able to do on behalf of the user. The budget is calculated | 28 // worker should be able to do on behalf of the user. The budget is calculated |
| 26 // based on the Site Engagment Score and is consumed when a origin does | 29 // based on the Site Engagment Score and is consumed when a origin does |
| 27 // background work on behalf of the user. | 30 // background work on behalf of the user. |
| 28 class BudgetManager : public KeyedService { | 31 class BudgetManager : public KeyedService { |
| 29 public: | 32 public: |
| 30 explicit BudgetManager(Profile* profile); | 33 explicit BudgetManager(Profile* profile); |
| 31 ~BudgetManager() override; | 34 ~BudgetManager() override; |
| 32 | 35 |
| 33 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 36 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
| 34 | 37 |
| 35 // Query for the base cost for any background processing. | 38 // Query for the base cost for any background processing. |
| 36 static double GetCost(blink::mojom::BudgetOperationType type); | 39 static double GetCost(blink::mojom::BudgetOperationType type); |
| 37 | 40 |
| 38 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; | 41 using GetBudgetCallback = blink::mojom::BudgetService::GetBudgetCallback; |
| 39 using ReserveCallback = base::Callback<void(bool success)>; | 42 using ReserveCallback = base::Callback<void(bool success)>; |
| 40 using ConsumeCallback = base::Callback<void(bool success)>; | 43 using ConsumeCallback = base::Callback<void(bool success)>; |
| 41 | 44 |
| 42 // 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 |
| 43 // 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 |
| 44 // the budget at that time. | 47 // the budget at that time. |
| 45 void GetBudget(const GURL& origin, const GetBudgetCallback& callback); | 48 void GetBudget(const url::Origin& origin, const GetBudgetCallback& callback); |
| 46 | 49 |
| 47 // 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 |
| 48 // 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 |
| 49 // 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. |
| 50 void Reserve(const GURL& origin, | 53 void Reserve(const url::Origin& origin, |
| 51 blink::mojom::BudgetOperationType type, | 54 blink::mojom::BudgetOperationType type, |
| 52 const ReserveCallback& callback); | 55 const ReserveCallback& callback); |
| 53 | 56 |
| 54 // Spend budget, first consuming a reservation if one exists, or spend | 57 // Spend budget, first consuming a reservation if one exists, or spend |
| 55 // directly from the budget. | 58 // directly from the budget. |
| 56 void Consume(const GURL& origin, | 59 void Consume(const url::Origin& origin, |
| 57 blink::mojom::BudgetOperationType type, | 60 blink::mojom::BudgetOperationType type, |
| 58 const ConsumeCallback& callback); | 61 const ConsumeCallback& callback); |
| 59 | 62 |
| 60 private: | 63 private: |
| 61 friend class BudgetManagerTest; | 64 friend class BudgetManagerTest; |
| 62 | 65 |
| 63 // Called as a callback from BudgetDatabase after it has made a reserve | 66 // Called as a callback from BudgetDatabase after it has made a reserve |
| 64 // decision. | 67 // decision. |
| 65 void DidReserve(const GURL& origin, | 68 void DidReserve(const url::Origin& origin, |
| 66 blink::mojom::BudgetOperationType type, | 69 blink::mojom::BudgetOperationType type, |
| 67 const ReserveCallback& callback, | 70 const ReserveCallback& callback, |
| 68 bool success); | 71 bool success); |
| 69 | 72 |
| 70 Profile* profile_; | 73 Profile* profile_; |
| 71 BudgetDatabase db_; | 74 BudgetDatabase db_; |
| 72 | 75 |
| 73 std::unordered_map<std::string, int> reservation_map_; | 76 std::unordered_map<std::string, int> reservation_map_; |
| 74 base::WeakPtrFactory<BudgetManager> weak_ptr_factory_; | 77 base::WeakPtrFactory<BudgetManager> weak_ptr_factory_; |
| 75 | 78 |
| 76 DISALLOW_COPY_AND_ASSIGN(BudgetManager); | 79 DISALLOW_COPY_AND_ASSIGN(BudgetManager); |
| 77 }; | 80 }; |
| 78 | 81 |
| 79 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_MANAGER_H_ | 82 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_MANAGER_H_ |
| OLD | NEW |