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" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "chrome/browser/engagement/site_engagement_score.h" | 14 #include "chrome/browser/engagement/site_engagement_score.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
17 #include "components/pref_registry/pref_registry_syncable.h" | 17 #include "components/pref_registry/pref_registry_syncable.h" |
18 #include "components/prefs/pref_service.h" | 18 #include "components/prefs/pref_service.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/common/origin_util.h" |
20 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" | 21 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" |
21 #include "url/origin.h" | 22 #include "url/origin.h" |
22 | 23 |
23 using content::BrowserThread; | 24 using content::BrowserThread; |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 // Previously, budget information was stored in the prefs. If there is any old | 28 // Previously, budget information was stored in the prefs. If there is any old |
28 // information still there, clear it. | 29 // information still there, clear it. |
29 // TODO(harkness): Remove once Chrome 56 has branched. | 30 // TODO(harkness): Remove once Chrome 56 has branched. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 case blink::mojom::BudgetOperationType::INVALID_OPERATION: | 62 case blink::mojom::BudgetOperationType::INVALID_OPERATION: |
62 return SiteEngagementScore::kMaxPoints + 1; | 63 return SiteEngagementScore::kMaxPoints + 1; |
63 // No default case. | 64 // No default case. |
64 } | 65 } |
65 NOTREACHED(); | 66 NOTREACHED(); |
66 return SiteEngagementScore::kMaxPoints + 1.0; | 67 return SiteEngagementScore::kMaxPoints + 1.0; |
67 } | 68 } |
68 | 69 |
69 void BudgetManager::GetBudget(const url::Origin& origin, | 70 void BudgetManager::GetBudget(const url::Origin& origin, |
70 const GetBudgetCallback& callback) { | 71 const GetBudgetCallback& callback) { |
| 72 if (origin.unique() || !content::IsOriginSecure(GURL(origin.Serialize()))) { |
| 73 callback.Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, |
| 74 mojo::Array<blink::mojom::BudgetStatePtr>()); |
| 75 return; |
| 76 } |
71 db_.GetBudgetDetails(origin, callback); | 77 db_.GetBudgetDetails(origin, callback); |
72 } | 78 } |
73 | 79 |
74 void BudgetManager::Reserve(const url::Origin& origin, | 80 void BudgetManager::Reserve(const url::Origin& origin, |
75 blink::mojom::BudgetOperationType type, | 81 blink::mojom::BudgetOperationType type, |
76 const ReserveCallback& callback) { | 82 const ReserveCallback& callback) { |
| 83 if (origin.unique() || !content::IsOriginSecure(GURL(origin.Serialize()))) { |
| 84 callback.Run(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, |
| 85 false /* success */); |
| 86 return; |
| 87 } |
77 db_.SpendBudget(origin, GetCost(type), | 88 db_.SpendBudget(origin, GetCost(type), |
78 base::Bind(&BudgetManager::DidReserve, | 89 base::Bind(&BudgetManager::DidReserve, |
79 weak_ptr_factory_.GetWeakPtr(), origin, callback)); | 90 weak_ptr_factory_.GetWeakPtr(), origin, callback)); |
80 } | 91 } |
81 | 92 |
82 void BudgetManager::Consume(const url::Origin& origin, | 93 void BudgetManager::Consume(const url::Origin& origin, |
83 blink::mojom::BudgetOperationType type, | 94 blink::mojom::BudgetOperationType type, |
84 const ConsumeCallback& callback) { | 95 const ConsumeCallback& callback) { |
| 96 if (origin.unique() || !content::IsOriginSecure(GURL(origin.Serialize()))) { |
| 97 callback.Run(false /* success */); |
| 98 return; |
| 99 } |
| 100 |
85 bool found_reservation = false; | 101 bool found_reservation = false; |
86 | 102 |
87 // First, see if there is a reservation already. | 103 // First, see if there is a reservation already. |
88 auto count = reservation_map_.find(origin); | 104 auto count = reservation_map_.find(origin); |
89 if (count != reservation_map_.end()) { | 105 if (count != reservation_map_.end()) { |
90 if (count->second == 1) | 106 if (count->second == 1) |
91 reservation_map_.erase(origin); | 107 reservation_map_.erase(origin); |
92 else | 108 else |
93 reservation_map_[origin]--; | 109 reservation_map_[origin]--; |
94 found_reservation = true; | 110 found_reservation = true; |
(...skipping 26 matching lines...) Expand all Loading... |
121 void BudgetManager::DidReserve(const url::Origin& origin, | 137 void BudgetManager::DidReserve(const url::Origin& origin, |
122 const ReserveCallback& callback, | 138 const ReserveCallback& callback, |
123 blink::mojom::BudgetServiceErrorType error, | 139 blink::mojom::BudgetServiceErrorType error, |
124 bool success) { | 140 bool success) { |
125 // If the call succeeded, write the new reservation into the map. | 141 // If the call succeeded, write the new reservation into the map. |
126 if (success && error == blink::mojom::BudgetServiceErrorType::NONE) | 142 if (success && error == blink::mojom::BudgetServiceErrorType::NONE) |
127 reservation_map_[origin]++; | 143 reservation_map_[origin]++; |
128 | 144 |
129 callback.Run(error, success); | 145 callback.Run(error, success); |
130 } | 146 } |
OLD | NEW |