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

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

Issue 2326523003: Connect Mojo budget_service to BudgetManager implementation of Reserve. (Closed)
Patch Set: 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 #include "chrome/browser/budget_service/budget_manager.h" 5 #include "chrome/browser/budget_service/budget_manager.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 void BudgetManager::GetBudget(const GURL& origin, 68 void BudgetManager::GetBudget(const GURL& origin,
69 const GetBudgetCallback& callback) { 69 const GetBudgetCallback& callback) {
70 db_.GetBudgetDetails(origin, callback); 70 db_.GetBudgetDetails(origin, callback);
71 } 71 }
72 72
73 void BudgetManager::Reserve(const GURL& origin, 73 void BudgetManager::Reserve(const GURL& origin,
74 blink::mojom::BudgetOperationType type, 74 blink::mojom::BudgetOperationType type,
75 const ReserveCallback& callback) { 75 const ReserveCallback& callback) {
76 DCHECK_EQ(origin, origin.GetOrigin()); 76 DCHECK_EQ(origin, origin.GetOrigin());
77 77
78 db_.SpendBudget( 78 db_.SpendBudget(origin, GetCost(type),
79 origin, GetCost(type), 79 base::Bind(&BudgetManager::DidReserve,
80 base::Bind(&BudgetManager::DidReserve, weak_ptr_factory_.GetWeakPtr(), 80 weak_ptr_factory_.GetWeakPtr(), origin, callback));
81 origin, type, callback));
82 } 81 }
83 82
84 void BudgetManager::Consume(const GURL& origin, 83 void BudgetManager::Consume(const GURL& origin,
85 blink::mojom::BudgetOperationType type, 84 blink::mojom::BudgetOperationType type,
86 const ConsumeCallback& callback) { 85 const ConsumeCallback& callback) {
87 DCHECK_EQ(origin, origin.GetOrigin()); 86 DCHECK_EQ(origin, origin.GetOrigin());
88 bool found_reservation = false; 87 bool found_reservation = false;
89 88
90 // First, see if there is a reservation already. 89 // First, see if there is a reservation already.
91 auto count = reservation_map_.find(origin.spec()); 90 auto count = reservation_map_.find(origin.spec());
92 if (count != reservation_map_.end()) { 91 if (count != reservation_map_.end()) {
93 if (count->second == 1) 92 if (count->second == 1)
94 reservation_map_.erase(origin.spec()); 93 reservation_map_.erase(origin.spec());
95 else 94 else
96 reservation_map_[origin.spec()]--; 95 reservation_map_[origin.spec()]--;
97 found_reservation = true; 96 found_reservation = true;
98 } 97 }
99 98
100 if (found_reservation) { 99 if (found_reservation) {
101 callback.Run(true); 100 callback.Run(true);
102 return; 101 return;
103 } 102 }
104 103
105 // If there wasn't a reservation already, try to directly consume budget. 104 // If there wasn't a reservation already, try to directly consume budget.
106 // The callback will return directly to the caller. 105 // The callback will return directly to the caller.
107 db_.SpendBudget(origin, GetCost(type), callback); 106 db_.SpendBudget(origin, GetCost(type),
107 base::Bind(&BudgetManager::DidConsume,
108 weak_ptr_factory_.GetWeakPtr(), callback));
109 }
110
111 void BudgetManager::DidConsume(const ConsumeCallback& callback,
112 blink::mojom::BudgetServiceErrorType error,
113 bool success) {
114 // The caller of Consume only cares whether it succeeded or failed and not
115 // why. So, only return a combined bool.
116 if (error != blink::mojom::BudgetServiceErrorType::NONE || !success) {
Peter Beverloo 2016/09/08 17:16:06 Let's remove "!success" from this conditional, so
harkness 2016/09/13 13:41:29 Good point. Updated.
117 callback.Run(false /* success */);
118 return;
119 }
120 callback.Run(success);
108 } 121 }
109 122
110 void BudgetManager::DidReserve(const GURL& origin, 123 void BudgetManager::DidReserve(const GURL& origin,
111 blink::mojom::BudgetOperationType type,
112 const ReserveCallback& callback, 124 const ReserveCallback& callback,
125 blink::mojom::BudgetServiceErrorType error,
113 bool success) { 126 bool success) {
114 if (!success) { 127 // If the call succeeded, write the new reservation into the map.
115 callback.Run(false); 128 if (success && error == blink::mojom::BudgetServiceErrorType::NONE)
116 return; 129 reservation_map_[origin.spec()]++;
117 }
118 130
119 // Write the new reservation into the map. 131 callback.Run(error, success);
120 reservation_map_[origin.spec()]++;
121 callback.Run(true);
122 } 132 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698