| 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" | |
| 17 #include "components/pref_registry/pref_registry_syncable.h" | |
| 18 #include "components/prefs/pref_service.h" | |
| 19 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/common/origin_util.h" | 17 #include "content/public/common/origin_util.h" |
| 21 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" | 18 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" |
| 22 #include "url/origin.h" | 19 #include "url/origin.h" |
| 23 | 20 |
| 24 using content::BrowserThread; | 21 using content::BrowserThread; |
| 25 | 22 |
| 26 namespace { | |
| 27 | |
| 28 // Previously, budget information was stored in the prefs. If there is any old | |
| 29 // information still there, clear it. | |
| 30 // TODO(harkness): Remove once Chrome 56 has branched. | |
| 31 void ClearBudgetDataFromPrefs(Profile* profile) { | |
| 32 profile->GetPrefs()->ClearPref(prefs::kBackgroundBudgetMap); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 BudgetManager::BudgetManager(Profile* profile) | 23 BudgetManager::BudgetManager(Profile* profile) |
| 38 : profile_(profile), | 24 : profile_(profile), |
| 39 db_(profile, | 25 db_(profile, |
| 40 profile->GetPath().Append(FILE_PATH_LITERAL("BudgetDatabase")), | 26 profile->GetPath().Append(FILE_PATH_LITERAL("BudgetDatabase")), |
| 41 BrowserThread::GetBlockingPool() | 27 BrowserThread::GetBlockingPool() |
| 42 ->GetSequencedTaskRunnerWithShutdownBehavior( | 28 ->GetSequencedTaskRunnerWithShutdownBehavior( |
| 43 base::SequencedWorkerPool::GetSequenceToken(), | 29 base::SequencedWorkerPool::GetSequenceToken(), |
| 44 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | 30 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), |
| 45 weak_ptr_factory_(this) { | 31 weak_ptr_factory_(this) { |
| 46 ClearBudgetDataFromPrefs(profile); | |
| 47 } | 32 } |
| 48 | 33 |
| 49 BudgetManager::~BudgetManager() {} | 34 BudgetManager::~BudgetManager() {} |
| 50 | 35 |
| 51 // static | 36 // static |
| 52 void BudgetManager::RegisterProfilePrefs( | |
| 53 user_prefs::PrefRegistrySyncable* registry) { | |
| 54 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | |
| 55 } | |
| 56 | |
| 57 // static | |
| 58 double BudgetManager::GetCost(blink::mojom::BudgetOperationType type) { | 37 double BudgetManager::GetCost(blink::mojom::BudgetOperationType type) { |
| 59 switch (type) { | 38 switch (type) { |
| 60 case blink::mojom::BudgetOperationType::SILENT_PUSH: | 39 case blink::mojom::BudgetOperationType::SILENT_PUSH: |
| 61 return 2.0; | 40 return 2.0; |
| 62 case blink::mojom::BudgetOperationType::INVALID_OPERATION: | 41 case blink::mojom::BudgetOperationType::INVALID_OPERATION: |
| 63 return SiteEngagementScore::kMaxPoints + 1; | 42 return SiteEngagementScore::kMaxPoints + 1; |
| 64 // No default case. | 43 // No default case. |
| 65 } | 44 } |
| 66 NOTREACHED(); | 45 NOTREACHED(); |
| 67 return SiteEngagementScore::kMaxPoints + 1.0; | 46 return SiteEngagementScore::kMaxPoints + 1.0; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 blink::mojom::BudgetServiceErrorType error, | 134 blink::mojom::BudgetServiceErrorType error, |
| 156 bool success) { | 135 bool success) { |
| 157 // If the call succeeded, write the new reservation into the map. | 136 // If the call succeeded, write the new reservation into the map. |
| 158 if (success && error == blink::mojom::BudgetServiceErrorType::NONE) | 137 if (success && error == blink::mojom::BudgetServiceErrorType::NONE) |
| 159 reservation_map_[origin]++; | 138 reservation_map_[origin]++; |
| 160 | 139 |
| 161 UMA_HISTOGRAM_BOOLEAN("Blink.BudgetAPI.Reserve", success); | 140 UMA_HISTOGRAM_BOOLEAN("Blink.BudgetAPI.Reserve", success); |
| 162 | 141 |
| 163 callback.Run(error, success); | 142 callback.Run(error, success); |
| 164 } | 143 } |
| OLD | NEW |