Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: chrome/browser/budget_service/budget_manager.h

Issue 2272563005: Start plumbing connections from the BudgetManager to the BudgetDatabase (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix the task runner Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/keyed_service/core/keyed_service.h" 14 #include "components/keyed_service/core/keyed_service.h"
14 #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"
15 #include "url/gurl.h" 16 #include "url/gurl.h"
16 17
17 class Profile; 18 class Profile;
18 19
19 namespace base { 20 namespace base {
20 class Clock; 21 class Clock;
21 } 22 }
22 23
23 namespace user_prefs { 24 namespace user_prefs {
24 class PrefRegistrySyncable; 25 class PrefRegistrySyncable;
25 } 26 }
26 27
27 // A budget manager to help Chrome decide how much background work a service 28 // A budget manager to help Chrome decide how much background work a service
28 // worker should be able to do on behalf of the user. The budget is calculated 29 // worker should be able to do on behalf of the user. The budget is calculated
29 // based on the Site Engagment Score and is consumed when a origin does 30 // based on the Site Engagment Score and is consumed when a origin does
30 // background work on behalf of the user. 31 // background work on behalf of the user.
31 class BudgetManager : public KeyedService { 32 class BudgetManager : public KeyedService {
32 public: 33 public:
33 explicit BudgetManager(Profile* profile); 34 explicit BudgetManager(Profile* profile);
34 ~BudgetManager() override; 35 ~BudgetManager() override;
35 36
36 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 37 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
37 38
38 // Query for the base cost for any background processing. 39 // Query for the base cost for any background processing.
39 static double GetCost(blink::mojom::BudgetOperationType type); 40 static double GetCost(blink::mojom::BudgetOperationType type);
40 41
41 using GetBudgetCallback = base::Callback<void(double budget)>; 42 using GetBudgetCallback = base::Callback<void(double budget)>;
43 using ReserveCallback = base::Callback<void(bool success)>;
44 using ConsumeCallback = base::Callback<void(bool success)>;
42 45
43 // Get the budget associated with the origin. This is passed to the 46 // Get the budget associated with the origin. This is passed to the
44 // callback. Budget will be a value between 0.0 and 47 // callback. Budget will be a value between 0.0 and
45 // SiteEngagementScore::kMaxPoints. 48 // SiteEngagementScore::kMaxPoints.
46 void GetBudget(const GURL& origin, const GetBudgetCallback& callback); 49 void GetBudget(const GURL& origin, const GetBudgetCallback& callback);
47 50
48 // Store the budget associated with the origin. Budget should be a value 51 // Store the budget associated with the origin. Budget should be a value
49 // between 0.0 and SiteEngagementScore::kMaxPoints. closure will be called 52 // between 0.0 and SiteEngagementScore::kMaxPoints. closure will be called
50 // when the store completes. 53 // when the store completes.
51 void StoreBudget(const GURL& origin, 54 void StoreBudget(const GURL& origin,
52 double budget, 55 double budget,
53 const base::Closure& closure); 56 const base::Closure& closure);
54 57
58 // 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
60 // the next action will consume that reservation and not cost any budget.
61 void Reserve(const GURL& origin,
62 blink::mojom::BudgetOperationType type,
63 const ReserveCallback& callback);
64
65 // Spend budget, first consuming a reservation if one exists, or spend
66 // directly from the budget.
67 void Consume(const GURL& origin,
68 blink::mojom::BudgetOperationType type,
69 const ConsumeCallback& callback);
70
55 private: 71 private:
56 friend class BudgetManagerTest; 72 friend class BudgetManagerTest;
57 73
74 // Called as a callback from BudgetDatabase after it has made a reserve
75 // decision.
76 void DidReserve(const GURL& origin,
77 blink::mojom::BudgetOperationType type,
78 const ReserveCallback& callback,
79 bool success);
80
58 // Used to allow tests to fast forward/reverse time. 81 // Used to allow tests to fast forward/reverse time.
59 void SetClockForTesting(std::unique_ptr<base::Clock> clock); 82 void SetClockForTesting(std::unique_ptr<base::Clock> clock);
60 83
61 // The clock used to vend times. 84 // The clock used to vend times.
62 std::unique_ptr<base::Clock> clock_; 85 std::unique_ptr<base::Clock> clock_;
63 86
64 Profile* profile_; 87 Profile* profile_;
88 BudgetDatabase db_;
89
90 std::unordered_map<std::string, int> reservation_map_;
91 base::WeakPtrFactory<BudgetManager> weak_ptr_factory_;
92
65 DISALLOW_COPY_AND_ASSIGN(BudgetManager); 93 DISALLOW_COPY_AND_ASSIGN(BudgetManager);
66 }; 94 };
67 95
68 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_MANAGER_H_ 96 #endif // CHROME_BROWSER_BUDGET_SERVICE_BUDGET_MANAGER_H_
OLDNEW
« no previous file with comments | « chrome/browser/budget_service/budget_database_unittest.cc ('k') | chrome/browser/budget_service/budget_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698