File: browsing_topics_service_factory.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (138 lines) | stat: -rw-r--r-- 5,787 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/browsing_topics/browsing_topics_service_factory.h"

#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/browsing_topics/annotator.h"
#include "components/browsing_topics/browsing_topics_service.h"
#include "components/browsing_topics/browsing_topics_service_impl.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/history/core/browser/history_service.h"
#include "components/keyed_service/core/service_access_type.h"
#include "components/optimization_guide/core/optimization_guide_model_provider.h"
#include "components/optimization_guide/machine_learning_tflite_buildflags.h"
#include "components/optimization_guide/proto/page_topics_model_metadata.pb.h"
#include "components/privacy_sandbox/privacy_sandbox_settings.h"
#include "content/public/browser/browsing_topics_site_data_manager.h"
#include "content/public/browser/storage_partition.h"
#include "services/network/public/cpp/features.h"
#include "third_party/blink/public/common/features.h"

#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
#include "components/browsing_topics/annotator_impl.h"
#else
#include "components/browsing_topics/annotator_noop.h"
#endif

namespace browsing_topics {

// static
BrowsingTopicsService* BrowsingTopicsServiceFactory::GetForProfile(
    Profile* profile) {
  return static_cast<BrowsingTopicsService*>(
      GetInstance()->GetServiceForBrowserContext(profile, /*create=*/true));
}

// static
BrowsingTopicsServiceFactory* BrowsingTopicsServiceFactory::GetInstance() {
  static base::NoDestructor<BrowsingTopicsServiceFactory> factory;
  return factory.get();
}

BrowsingTopicsServiceFactory::BrowsingTopicsServiceFactory()
    : ProfileKeyedServiceFactory(
          "BrowsingTopicsService",
          ProfileSelections::Builder()
              .WithRegular(ProfileSelection::kOriginalOnly)
              // TODO(crbug.com/40257657): Check if this service is needed in
              // Guest mode.
              .WithGuest(ProfileSelection::kOriginalOnly)
              // TODO(crbug.com/41488885): Check if this service is needed for
              // Ash Internals.
              .WithAshInternals(ProfileSelection::kOriginalOnly)
              .Build()) {
  DependsOn(PrivacySandboxSettingsFactory::GetInstance());
  DependsOn(HistoryServiceFactory::GetInstance());
  DependsOn(OptimizationGuideKeyedServiceFactory::GetInstance());
}

BrowsingTopicsServiceFactory::~BrowsingTopicsServiceFactory() = default;

std::unique_ptr<KeyedService>
BrowsingTopicsServiceFactory::BuildServiceInstanceForBrowserContext(
    content::BrowserContext* context) const {
  if (!base::FeatureList::IsEnabled(network::features::kBrowsingTopics)) {
    return nullptr;
  }

  Profile* profile = Profile::FromBrowserContext(context);

  privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings =
      PrivacySandboxSettingsFactory::GetForProfile(profile);
  if (!privacy_sandbox_settings)
    return nullptr;

  history::HistoryService* history_service =
      HistoryServiceFactory::GetForProfile(profile,
                                           ServiceAccessType::IMPLICIT_ACCESS);
  if (!history_service)
    return nullptr;

  content::BrowsingTopicsSiteDataManager* site_data_manager =
      context->GetDefaultStoragePartition()->GetBrowsingTopicsSiteDataManager();
  if (!site_data_manager)
    return nullptr;

  OptimizationGuideKeyedService* opt_guide_service =
      OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
  if (!opt_guide_service) {
    return nullptr;
  }

  optimization_guide::proto::Any any_metadata;
  any_metadata.set_type_url(
      "type.googleapis.com/"
      "google.internal.chrome.optimizationguide.v1.PageTopicsModelMetadata");
  optimization_guide::proto::PageTopicsModelMetadata model_metadata;
  // The current version the client supports for the topics model. This
  // should be incremented any time there is a client code change to how the
  // topics model works that needs to be side-channeled to the server.
  model_metadata.set_version(2);
  model_metadata.set_taxonomy_version(
      blink::features::kBrowsingTopicsTaxonomyVersion.Get());
  model_metadata.SerializeToString(any_metadata.mutable_value());

#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
  std::unique_ptr<Annotator> annotator = std::make_unique<AnnotatorImpl>(
      /*model_provider=*/opt_guide_service,
      base::ThreadPool::CreateSequencedTaskRunner(
          {base::MayBlock(), base::TaskPriority::BEST_EFFORT}),
      any_metadata);
#else
  std::unique_ptr<Annotator> annotator = std::make_unique<AnnotatorNoOp>();
#endif

  return std::make_unique<BrowsingTopicsServiceImpl>(
      profile->GetPath(), privacy_sandbox_settings, history_service,
      site_data_manager, std::move(annotator),
      base::BindRepeating(
          content_settings::PageSpecificContentSettings::TopicAccessed));
}

bool BrowsingTopicsServiceFactory::ServiceIsCreatedWithBrowserContext() const {
  // The `BrowsingTopicsService` needs to be created with Profile, as it needs
  // to schedule the topics calculation right away, and it might also need to
  // handle some data deletion on startup.
  return true;
}

}  // namespace browsing_topics