heavy_ad_service.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/heavy_ad_intervention/heavy_ad_service.h"
  5. #include "base/feature_list.h"
  6. #include "base/files/file_path.h"
  7. #include "base/memory/scoped_refptr.h"
  8. #include "base/metrics/field_trial_params.h"
  9. #include "base/task/sequenced_task_runner.h"
  10. #include "base/task/thread_pool.h"
  11. #include "base/time/default_clock.h"
  12. #include "components/blocklist/opt_out_blocklist/sql/opt_out_store_sql.h"
  13. #include "components/heavy_ad_intervention/heavy_ad_blocklist.h"
  14. #include "components/heavy_ad_intervention/heavy_ad_features.h"
  15. #include "content/public/browser/browser_task_traits.h"
  16. #include "content/public/browser/browser_thread.h"
  17. namespace heavy_ad_intervention {
  18. namespace {
  19. const base::FilePath::CharType kHeavyAdInterventionOptOutDBFilename[] =
  20. FILE_PATH_LITERAL("heavy_ad_intervention_opt_out.db");
  21. } // namespace
  22. // Whether an opt out store should be used or not.
  23. bool HeavyAdOptOutStoreDisabled() {
  24. return base::GetFieldTrialParamByFeatureAsBool(
  25. features::kHeavyAdPrivacyMitigations, "OptOutStoreDisabled", false);
  26. }
  27. HeavyAdService::HeavyAdService() {
  28. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  29. }
  30. HeavyAdService::~HeavyAdService() {
  31. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  32. }
  33. void HeavyAdService::Initialize(const base::FilePath& profile_path) {
  34. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  35. DCHECK(!profile_path.empty());
  36. if (!base::FeatureList::IsEnabled(features::kHeavyAdPrivacyMitigations))
  37. return;
  38. std::unique_ptr<blocklist::OptOutStoreSQL> opt_out_store;
  39. if (!HeavyAdOptOutStoreDisabled()) {
  40. // Get the background thread to run SQLite on.
  41. scoped_refptr<base::SequencedTaskRunner> background_task_runner =
  42. base::ThreadPool::CreateSequencedTaskRunner(
  43. {base::MayBlock(), base::TaskPriority::BEST_EFFORT});
  44. opt_out_store = std::make_unique<blocklist::OptOutStoreSQL>(
  45. content::GetUIThreadTaskRunner({}), background_task_runner,
  46. profile_path.Append(kHeavyAdInterventionOptOutDBFilename));
  47. }
  48. heavy_ad_blocklist_ = std::make_unique<HeavyAdBlocklist>(
  49. std::move(opt_out_store), base::DefaultClock::GetInstance(), this);
  50. }
  51. void HeavyAdService::InitializeOffTheRecord() {
  52. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  53. if (!base::FeatureList::IsEnabled(features::kHeavyAdPrivacyMitigations))
  54. return;
  55. // Providing a null out_out_store which sets up the blocklist in-memory only.
  56. heavy_ad_blocklist_ = std::make_unique<HeavyAdBlocklist>(
  57. nullptr /* opt_out_store */, base::DefaultClock::GetInstance(), this);
  58. }
  59. void HeavyAdService::NotifyOnBlocklistLoaded(
  60. base::OnceClosure on_blocklist_loaded_callback) {
  61. if (blocklist_is_loaded_) {
  62. std::move(on_blocklist_loaded_callback).Run();
  63. return;
  64. }
  65. on_blocklist_loaded_callback_ = std::move(on_blocklist_loaded_callback);
  66. }
  67. void HeavyAdService::NotifyOnBlocklistCleared(
  68. base::OnceClosure on_blocklist_cleared_callback) {
  69. on_blocklist_cleared_callback_ = std::move(on_blocklist_cleared_callback);
  70. }
  71. void HeavyAdService::OnLoadingStateChanged(bool is_loaded) {
  72. blocklist_is_loaded_ = is_loaded;
  73. if (blocklist_is_loaded_ && !on_blocklist_loaded_callback_.is_null())
  74. std::move(on_blocklist_loaded_callback_).Run();
  75. }
  76. void HeavyAdService::OnBlocklistCleared(base::Time time) {
  77. if (!on_blocklist_cleared_callback_.is_null())
  78. std::move(on_blocklist_cleared_callback_).Run();
  79. }
  80. } // namespace heavy_ad_intervention