| 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 #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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 } | 67 } |
| 68 | 68 |
| 69 void BudgetManager::GetBudget(const url::Origin& origin, | 69 void BudgetManager::GetBudget(const url::Origin& origin, |
| 70 const GetBudgetCallback& callback) { | 70 const GetBudgetCallback& callback) { |
| 71 db_.GetBudgetDetails(origin, callback); | 71 db_.GetBudgetDetails(origin, callback); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void BudgetManager::Reserve(const url::Origin& origin, | 74 void BudgetManager::Reserve(const url::Origin& origin, |
| 75 blink::mojom::BudgetOperationType type, | 75 blink::mojom::BudgetOperationType type, |
| 76 const ReserveCallback& callback) { | 76 const ReserveCallback& callback) { |
| 77 db_.SpendBudget( | 77 db_.SpendBudget(origin, GetCost(type), |
| 78 origin, GetCost(type), | 78 base::Bind(&BudgetManager::DidReserve, |
| 79 base::Bind(&BudgetManager::DidReserve, weak_ptr_factory_.GetWeakPtr(), | 79 weak_ptr_factory_.GetWeakPtr(), origin, callback)); |
| 80 origin, type, callback)); | |
| 81 } | 80 } |
| 82 | 81 |
| 83 void BudgetManager::Consume(const url::Origin& origin, | 82 void BudgetManager::Consume(const url::Origin& origin, |
| 84 blink::mojom::BudgetOperationType type, | 83 blink::mojom::BudgetOperationType type, |
| 85 const ConsumeCallback& callback) { | 84 const ConsumeCallback& callback) { |
| 86 bool found_reservation = false; | 85 bool found_reservation = false; |
| 87 | 86 |
| 88 // First, see if there is a reservation already. | 87 // First, see if there is a reservation already. |
| 89 auto count = reservation_map_.find(origin.host()); | 88 auto count = reservation_map_.find(origin); |
| 90 if (count != reservation_map_.end()) { | 89 if (count != reservation_map_.end()) { |
| 91 if (count->second == 1) | 90 if (count->second == 1) |
| 92 reservation_map_.erase(origin.host()); | 91 reservation_map_.erase(origin); |
| 93 else | 92 else |
| 94 reservation_map_[origin.host()]--; | 93 reservation_map_[origin]--; |
| 95 found_reservation = true; | 94 found_reservation = true; |
| 96 } | 95 } |
| 97 | 96 |
| 98 if (found_reservation) { | 97 if (found_reservation) { |
| 99 callback.Run(true); | 98 callback.Run(true); |
| 100 return; | 99 return; |
| 101 } | 100 } |
| 102 | 101 |
| 103 // If there wasn't a reservation already, try to directly consume budget. | 102 // If there wasn't a reservation already, try to directly consume budget. |
| 104 // The callback will return directly to the caller. | 103 // The callback will return directly to the caller. |
| 105 db_.SpendBudget(origin, GetCost(type), callback); | 104 db_.SpendBudget(origin, GetCost(type), |
| 105 base::Bind(&BudgetManager::DidConsume, |
| 106 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 107 } |
| 108 |
| 109 void BudgetManager::DidConsume(const ConsumeCallback& callback, |
| 110 blink::mojom::BudgetServiceErrorType error, |
| 111 bool success) { |
| 112 // The caller of Consume only cares whether it succeeded or failed and not |
| 113 // why. So, only return a combined bool. |
| 114 if (error != blink::mojom::BudgetServiceErrorType::NONE) { |
| 115 callback.Run(false /* success */); |
| 116 return; |
| 117 } |
| 118 callback.Run(success); |
| 106 } | 119 } |
| 107 | 120 |
| 108 void BudgetManager::DidReserve(const url::Origin& origin, | 121 void BudgetManager::DidReserve(const url::Origin& origin, |
| 109 blink::mojom::BudgetOperationType type, | |
| 110 const ReserveCallback& callback, | 122 const ReserveCallback& callback, |
| 123 blink::mojom::BudgetServiceErrorType error, |
| 111 bool success) { | 124 bool success) { |
| 112 if (!success) { | 125 // If the call succeeded, write the new reservation into the map. |
| 113 callback.Run(false); | 126 if (success && error == blink::mojom::BudgetServiceErrorType::NONE) |
| 114 return; | 127 reservation_map_[origin]++; |
| 115 } | |
| 116 | 128 |
| 117 // Write the new reservation into the map. | 129 callback.Run(error, success); |
| 118 reservation_map_[origin.host()]++; | |
| 119 callback.Run(true); | |
| 120 } | 130 } |
| OLD | NEW |