File: user_education_service_factory.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (139 lines) | stat: -rw-r--r-- 5,246 bytes parent folder | download | duplicates (3)
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
139
// Copyright 2021 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/user_education/user_education_service_factory.h"

#include <memory>
#include <optional>

#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "chrome/browser/headless/headless_mode_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/user_education/browser_user_education_storage_service.h"
#include "chrome/browser/user_education/user_education_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/user_education/common/session/user_education_idle_observer.h"
#include "components/user_education/common/session/user_education_idle_policy.h"
#include "components/user_education/common/session/user_education_session_manager.h"
#include "content/public/browser/browser_context.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/components/mgs/managed_guest_session_utils.h"
#endif

namespace {

// Idle observer that doesn't do anything.
class StubIdleObserver : public user_education::UserEducationIdleObserver {
 public:
  StubIdleObserver() = default;
  ~StubIdleObserver() override = default;

  // UserEducationIdleObserver:
  std::optional<base::Time> MaybeGetNewLastActiveTime() const override {
    return std::nullopt;
  }
};

}  // namespace

// These are found in chrome/browser/ui/user_education, so extern the factory
// methods to create the necessary objects.
extern std::unique_ptr<user_education::UserEducationIdleObserver>
CreatePollingIdleObserver();
extern std::unique_ptr<RecentSessionObserver> CreateRecentSessionObserver(
    Profile& profile);

UserEducationServiceFactory* UserEducationServiceFactory::GetInstance() {
  static base::NoDestructor<UserEducationServiceFactory> instance;
  return instance.get();
}

// static
UserEducationService* UserEducationServiceFactory::GetForBrowserContext(
    content::BrowserContext* profile) {
  return static_cast<UserEducationService*>(
      GetInstance()->GetServiceForBrowserContext(profile, true));
}

UserEducationServiceFactory::UserEducationServiceFactory()
    : ProfileKeyedServiceFactory(
          "UserEducationService",
          ProfileSelections::Builder()
              .WithRegular(ProfileSelection::kOriginalOnly)
              // TODO(crbug.com/40257657): Check if this service is needed in
              // Guest mode.
              .WithGuest(ProfileSelection::kOriginalOnly)
              // The service is needed by the System Profile OTR (that manages
              // the Profile Picker) to control the IPHs displayed in the
              // Profile Picker.
              .WithSystem(ProfileSelection::kOffTheRecordOnly)
              // TODO(crbug.com/41488885): Check if this service is needed for
              // Ash Internals.
              .WithAshInternals(ProfileSelection::kOriginalOnly)
              .Build()) {}

UserEducationServiceFactory::~UserEducationServiceFactory() = default;

// static
std::unique_ptr<UserEducationService>
UserEducationServiceFactory::BuildServiceInstanceForBrowserContextImpl(
    content::BrowserContext* context,
    bool disable_idle_polling) {
  Profile* const profile = Profile::FromBrowserContext(context);

  // Create the user education service.
  auto result = std::make_unique<UserEducationService>(
      std::make_unique<BrowserUserEducationStorageService>(profile),
      ProfileAllowsUserEducation(profile));

  // Set up the session manager.
  result->user_education_session_manager().Init(
      &result->user_education_storage_service(),
      disable_idle_polling ? std::make_unique<StubIdleObserver>()
                           : CreatePollingIdleObserver(),
      std::make_unique<user_education::UserEducationIdlePolicy>());

  // Possibly install a session observer. This isn't public, since it's
  // self-contained and mostly for tracking state.
  if (result->recent_session_tracker()) {
    result->recent_session_observer_ = CreateRecentSessionObserver(*profile);
    result->recent_session_observer_->Init(*result->recent_session_tracker());
  }

  return result;
}

// static
bool UserEducationServiceFactory::ProfileAllowsUserEducation(Profile* profile) {
#if BUILDFLAG(CHROME_FOR_TESTING)
  // IPH is always disabled in Chrome for Testing.
  return false;
#else
  // In order to do user education, the browser must have a UI and not be an
  // "off-the-record" or in a demo or guest mode.
  if (profile->IsIncognitoProfile() || profile->IsGuestSession() ||
      profiles::IsDemoSession() || profiles::IsChromeAppKioskSession()) {
    return false;
  }
#if BUILDFLAG(IS_CHROMEOS)
  if (chromeos::IsManagedGuestSession()) {
    return false;
  }
#endif
  if (headless::IsHeadlessMode()) {
    return false;
  }
  return true;
#endif  // BUILDFLAG(CHROME_FOR_TESTING)
}

std::unique_ptr<KeyedService>
UserEducationServiceFactory::BuildServiceInstanceForBrowserContext(
    content::BrowserContext* context) const {
  return BuildServiceInstanceForBrowserContextImpl(context,
                                                   disable_idle_polling_);
}