| 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 return SiteEngagementScore::kMaxPoints + 1.0; | 67 return SiteEngagementScore::kMaxPoints + 1.0; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void BudgetManager::GetBudget(const url::Origin& origin, | 70 void BudgetManager::GetBudget(const url::Origin& origin, |
| 71 const GetBudgetCallback& callback) { | 71 const GetBudgetCallback& callback) { |
| 72 if (origin.unique() || !content::IsOriginSecure(origin.GetURL())) { | 72 if (origin.unique() || !content::IsOriginSecure(origin.GetURL())) { |
| 73 callback.Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, | 73 callback.Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, |
| 74 std::vector<blink::mojom::BudgetStatePtr>()); | 74 std::vector<blink::mojom::BudgetStatePtr>()); |
| 75 return; | 75 return; |
| 76 } | 76 } |
| 77 db_.GetBudgetDetails(origin, callback); | 77 db_.GetBudgetDetails(origin, |
| 78 base::Bind(&BudgetManager::DidGetBudget, |
| 79 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void BudgetManager::Reserve(const url::Origin& origin, | 82 void BudgetManager::Reserve(const url::Origin& origin, |
| 81 blink::mojom::BudgetOperationType type, | 83 blink::mojom::BudgetOperationType type, |
| 82 const ReserveCallback& callback) { | 84 const ReserveCallback& callback) { |
| 83 if (origin.unique() || !content::IsOriginSecure(origin.GetURL())) { | 85 if (origin.unique() || !content::IsOriginSecure(origin.GetURL())) { |
| 84 callback.Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, | 86 callback.Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, |
| 85 false /* success */); | 87 false /* success */); |
| 86 return; | 88 return; |
| 87 } | 89 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 115 return; | 117 return; |
| 116 } | 118 } |
| 117 | 119 |
| 118 // If there wasn't a reservation already, try to directly consume budget. | 120 // If there wasn't a reservation already, try to directly consume budget. |
| 119 // The callback will return directly to the caller. | 121 // The callback will return directly to the caller. |
| 120 db_.SpendBudget(origin, GetCost(type), | 122 db_.SpendBudget(origin, GetCost(type), |
| 121 base::Bind(&BudgetManager::DidConsume, | 123 base::Bind(&BudgetManager::DidConsume, |
| 122 weak_ptr_factory_.GetWeakPtr(), callback)); | 124 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 123 } | 125 } |
| 124 | 126 |
| 127 void BudgetManager::DidGetBudget( |
| 128 const GetBudgetCallback& callback, |
| 129 const blink::mojom::BudgetServiceErrorType error, |
| 130 std::vector<blink::mojom::BudgetStatePtr> budget) { |
| 131 // If there was an error, just record a budget of 0, so the API query is still |
| 132 // counted. |
| 133 double budget_at = 0; |
| 134 if (error == blink::mojom::BudgetServiceErrorType::NONE) |
| 135 budget_at = budget[0]->budget_at; |
| 136 UMA_HISTOGRAM_COUNTS_100("Blink.BudgetAPI.QueryBudget", budget_at); |
| 137 |
| 138 callback.Run(error, std::move(budget)); |
| 139 } |
| 140 |
| 125 void BudgetManager::DidConsume(const ConsumeCallback& callback, | 141 void BudgetManager::DidConsume(const ConsumeCallback& callback, |
| 126 blink::mojom::BudgetServiceErrorType error, | 142 blink::mojom::BudgetServiceErrorType error, |
| 127 bool success) { | 143 bool success) { |
| 128 // The caller of Consume only cares whether it succeeded or failed and not | 144 // The caller of Consume only cares whether it succeeded or failed and not |
| 129 // why. So, only return a combined bool. | 145 // why. So, only return a combined bool. |
| 130 if (error != blink::mojom::BudgetServiceErrorType::NONE) { | 146 if (error != blink::mojom::BudgetServiceErrorType::NONE) { |
| 131 callback.Run(false /* success */); | 147 callback.Run(false /* success */); |
| 132 return; | 148 return; |
| 133 } | 149 } |
| 134 callback.Run(success); | 150 callback.Run(success); |
| 135 } | 151 } |
| 136 | 152 |
| 137 void BudgetManager::DidReserve(const url::Origin& origin, | 153 void BudgetManager::DidReserve(const url::Origin& origin, |
| 138 const ReserveCallback& callback, | 154 const ReserveCallback& callback, |
| 139 blink::mojom::BudgetServiceErrorType error, | 155 blink::mojom::BudgetServiceErrorType error, |
| 140 bool success) { | 156 bool success) { |
| 141 // If the call succeeded, write the new reservation into the map. | 157 // If the call succeeded, write the new reservation into the map. |
| 142 if (success && error == blink::mojom::BudgetServiceErrorType::NONE) | 158 if (success && error == blink::mojom::BudgetServiceErrorType::NONE) |
| 143 reservation_map_[origin]++; | 159 reservation_map_[origin]++; |
| 144 | 160 |
| 161 UMA_HISTOGRAM_BOOLEAN("Blink.BudgetAPI.Reserve", success); |
| 162 |
| 145 callback.Run(error, success); | 163 callback.Run(error, success); |
| 146 } | 164 } |
| OLD | NEW |