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