| 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 "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" | 20 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" |
| 21 #include "url/origin.h" |
| 21 | 22 |
| 22 using content::BrowserThread; | 23 using content::BrowserThread; |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Previously, budget information was stored in the prefs. If there is any old | 27 // Previously, budget information was stored in the prefs. If there is any old |
| 27 // information still there, clear it. | 28 // information still there, clear it. |
| 28 // TODO(harkness): Remove once Chrome 56 has branched. | 29 // TODO(harkness): Remove once Chrome 56 has branched. |
| 29 void ClearBudgetDataFromPrefs(Profile* profile) { | 30 void ClearBudgetDataFromPrefs(Profile* profile) { |
| 30 profile->GetPrefs()->ClearPref(prefs::kBackgroundBudgetMap); | 31 profile->GetPrefs()->ClearPref(prefs::kBackgroundBudgetMap); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 58 case blink::mojom::BudgetOperationType::SILENT_PUSH: | 59 case blink::mojom::BudgetOperationType::SILENT_PUSH: |
| 59 return 2.0; | 60 return 2.0; |
| 60 case blink::mojom::BudgetOperationType::INVALID_OPERATION: | 61 case blink::mojom::BudgetOperationType::INVALID_OPERATION: |
| 61 return SiteEngagementScore::kMaxPoints + 1; | 62 return SiteEngagementScore::kMaxPoints + 1; |
| 62 // No default case. | 63 // No default case. |
| 63 } | 64 } |
| 64 NOTREACHED(); | 65 NOTREACHED(); |
| 65 return SiteEngagementScore::kMaxPoints + 1.0; | 66 return SiteEngagementScore::kMaxPoints + 1.0; |
| 66 } | 67 } |
| 67 | 68 |
| 68 void BudgetManager::GetBudget(const GURL& origin, | 69 void BudgetManager::GetBudget(const url::Origin& origin, |
| 69 const GetBudgetCallback& callback) { | 70 const GetBudgetCallback& callback) { |
| 70 db_.GetBudgetDetails(origin, callback); | 71 db_.GetBudgetDetails(origin, callback); |
| 71 } | 72 } |
| 72 | 73 |
| 73 void BudgetManager::Reserve(const GURL& origin, | 74 void BudgetManager::Reserve(const url::Origin& origin, |
| 74 blink::mojom::BudgetOperationType type, | 75 blink::mojom::BudgetOperationType type, |
| 75 const ReserveCallback& callback) { | 76 const ReserveCallback& callback) { |
| 76 DCHECK_EQ(origin, origin.GetOrigin()); | |
| 77 | |
| 78 db_.SpendBudget( | 77 db_.SpendBudget( |
| 79 origin, GetCost(type), | 78 origin, GetCost(type), |
| 80 base::Bind(&BudgetManager::DidReserve, weak_ptr_factory_.GetWeakPtr(), | 79 base::Bind(&BudgetManager::DidReserve, weak_ptr_factory_.GetWeakPtr(), |
| 81 origin, type, callback)); | 80 origin, type, callback)); |
| 82 } | 81 } |
| 83 | 82 |
| 84 void BudgetManager::Consume(const GURL& origin, | 83 void BudgetManager::Consume(const url::Origin& 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()); | |
| 88 bool found_reservation = false; | 86 bool found_reservation = false; |
| 89 | 87 |
| 90 // First, see if there is a reservation already. | 88 // First, see if there is a reservation already. |
| 91 auto count = reservation_map_.find(origin.spec()); | 89 auto count = reservation_map_.find(origin.host()); |
| 92 if (count != reservation_map_.end()) { | 90 if (count != reservation_map_.end()) { |
| 93 if (count->second == 1) | 91 if (count->second == 1) |
| 94 reservation_map_.erase(origin.spec()); | 92 reservation_map_.erase(origin.host()); |
| 95 else | 93 else |
| 96 reservation_map_[origin.spec()]--; | 94 reservation_map_[origin.host()]--; |
| 97 found_reservation = true; | 95 found_reservation = true; |
| 98 } | 96 } |
| 99 | 97 |
| 100 if (found_reservation) { | 98 if (found_reservation) { |
| 101 callback.Run(true); | 99 callback.Run(true); |
| 102 return; | 100 return; |
| 103 } | 101 } |
| 104 | 102 |
| 105 // If there wasn't a reservation already, try to directly consume budget. | 103 // If there wasn't a reservation already, try to directly consume budget. |
| 106 // The callback will return directly to the caller. | 104 // The callback will return directly to the caller. |
| 107 db_.SpendBudget(origin, GetCost(type), callback); | 105 db_.SpendBudget(origin, GetCost(type), callback); |
| 108 } | 106 } |
| 109 | 107 |
| 110 void BudgetManager::DidReserve(const GURL& origin, | 108 void BudgetManager::DidReserve(const url::Origin& origin, |
| 111 blink::mojom::BudgetOperationType type, | 109 blink::mojom::BudgetOperationType type, |
| 112 const ReserveCallback& callback, | 110 const ReserveCallback& callback, |
| 113 bool success) { | 111 bool success) { |
| 114 if (!success) { | 112 if (!success) { |
| 115 callback.Run(false); | 113 callback.Run(false); |
| 116 return; | 114 return; |
| 117 } | 115 } |
| 118 | 116 |
| 119 // Write the new reservation into the map. | 117 // Write the new reservation into the map. |
| 120 reservation_map_[origin.spec()]++; | 118 reservation_map_[origin.host()]++; |
| 121 callback.Run(true); | 119 callback.Run(true); |
| 122 } | 120 } |
| OLD | NEW |