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 22 matching lines...) Expand all Loading... |
33 case blink::mojom::BudgetOperationType::INVALID_OPERATION: | 33 case blink::mojom::BudgetOperationType::INVALID_OPERATION: |
34 return SiteEngagementScore::kMaxPoints + 1; | 34 return SiteEngagementScore::kMaxPoints + 1; |
35 // No default case. | 35 // No default case. |
36 } | 36 } |
37 NOTREACHED(); | 37 NOTREACHED(); |
38 return SiteEngagementScore::kMaxPoints + 1.0; | 38 return SiteEngagementScore::kMaxPoints + 1.0; |
39 } | 39 } |
40 | 40 |
41 void BudgetManager::GetBudget(const url::Origin& origin, | 41 void BudgetManager::GetBudget(const url::Origin& origin, |
42 GetBudgetCallback callback) { | 42 GetBudgetCallback callback) { |
43 if (origin.unique() || !content::IsOriginSecure(origin.GetURL())) { | 43 if (origin.opaque() || !content::IsOriginSecure(origin.GetURL())) { |
44 std::move(callback).Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, | 44 std::move(callback).Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, |
45 std::vector<blink::mojom::BudgetStatePtr>()); | 45 std::vector<blink::mojom::BudgetStatePtr>()); |
46 return; | 46 return; |
47 } | 47 } |
48 db_.GetBudgetDetails(origin, base::BindOnce(&BudgetManager::DidGetBudget, | 48 db_.GetBudgetDetails(origin, base::BindOnce(&BudgetManager::DidGetBudget, |
49 weak_ptr_factory_.GetWeakPtr(), | 49 weak_ptr_factory_.GetWeakPtr(), |
50 base::Passed(&callback))); | 50 base::Passed(&callback))); |
51 } | 51 } |
52 | 52 |
53 void BudgetManager::Reserve(const url::Origin& origin, | 53 void BudgetManager::Reserve(const url::Origin& origin, |
54 blink::mojom::BudgetOperationType type, | 54 blink::mojom::BudgetOperationType type, |
55 ReserveCallback callback) { | 55 ReserveCallback callback) { |
56 if (origin.unique() || !content::IsOriginSecure(origin.GetURL())) { | 56 if (origin.opaque() || !content::IsOriginSecure(origin.GetURL())) { |
57 std::move(callback).Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, | 57 std::move(callback).Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, |
58 false /* success */); | 58 false /* success */); |
59 return; | 59 return; |
60 } | 60 } |
61 db_.SpendBudget( | 61 db_.SpendBudget( |
62 origin, GetCost(type), | 62 origin, GetCost(type), |
63 base::Bind(&BudgetManager::DidReserve, weak_ptr_factory_.GetWeakPtr(), | 63 base::Bind(&BudgetManager::DidReserve, weak_ptr_factory_.GetWeakPtr(), |
64 origin, base::Passed(&callback))); | 64 origin, base::Passed(&callback))); |
65 } | 65 } |
66 | 66 |
67 void BudgetManager::Consume(const url::Origin& origin, | 67 void BudgetManager::Consume(const url::Origin& origin, |
68 blink::mojom::BudgetOperationType type, | 68 blink::mojom::BudgetOperationType type, |
69 ConsumeCallback callback) { | 69 ConsumeCallback callback) { |
70 if (origin.unique() || !content::IsOriginSecure(origin.GetURL())) { | 70 if (origin.opaque() || !content::IsOriginSecure(origin.GetURL())) { |
71 std::move(callback).Run(false /* success */); | 71 std::move(callback).Run(false /* success */); |
72 return; | 72 return; |
73 } | 73 } |
74 | 74 |
75 bool found_reservation = false; | 75 bool found_reservation = false; |
76 | 76 |
77 // First, see if there is a reservation already. | 77 // First, see if there is a reservation already. |
78 auto count = reservation_map_.find(origin); | 78 auto count = reservation_map_.find(origin); |
79 if (count != reservation_map_.end()) { | 79 if (count != reservation_map_.end()) { |
80 if (count->second == 1) | 80 if (count->second == 1) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 blink::mojom::BudgetServiceErrorType error, | 128 blink::mojom::BudgetServiceErrorType error, |
129 bool success) { | 129 bool success) { |
130 // If the call succeeded, write the new reservation into the map. | 130 // If the call succeeded, write the new reservation into the map. |
131 if (success && error == blink::mojom::BudgetServiceErrorType::NONE) | 131 if (success && error == blink::mojom::BudgetServiceErrorType::NONE) |
132 reservation_map_[origin]++; | 132 reservation_map_[origin]++; |
133 | 133 |
134 UMA_HISTOGRAM_BOOLEAN("Blink.BudgetAPI.Reserve", success); | 134 UMA_HISTOGRAM_BOOLEAN("Blink.BudgetAPI.Reserve", success); |
135 | 135 |
136 std::move(callback).Run(error, success); | 136 std::move(callback).Run(error, success); |
137 } | 137 } |
OLD | NEW |