File: announcement_notification_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (274 lines) | stat: -rw-r--r-- 9,398 bytes parent folder | download | duplicates (7)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2020 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/updates/announcement_notification/announcement_notification_service.h"

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"    // nogncheck
#include "chrome/browser/profiles/profile_attributes_storage.h"  // nogncheck
#include "chrome/browser/profiles/profile_manager.h"             // nogncheck
#include "chrome/grit/generated_resources.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

// Default value for Finch parameter |kVersion|.
const int kInvalidVersion = -1;

// Whether the announcement notification is shown.
static bool notification_shown = false;

}  // namespace

class AnnouncementNotificationServiceImpl
    : public AnnouncementNotificationService {
 public:
  AnnouncementNotificationServiceImpl(Profile* profile,
                                      PrefService* pref_service,
                                      std::unique_ptr<Delegate> delegate,
                                      base::Clock* clock)
      : profile_(profile),
        pref_service_(pref_service),
        delegate_(std::move(delegate)),
        clock_(clock),
        skip_first_run_(true),
        skip_new_profile_(false),
        skip_display_(false),
        remote_version_(kInvalidVersion),
        require_signout_(false),
        show_one_(false) {
    DCHECK(delegate_);
    DCHECK(profile_);

    if (!IsFeatureEnabled())
      return;

    // By default do nothing in first run.
    skip_first_run_ = base::GetFieldTrialParamByFeatureAsBool(
        kAnnouncementNotification, kSkipFirstRun, true);
    skip_new_profile_ = base::GetFieldTrialParamByFeatureAsBool(
        kAnnouncementNotification, kSkipNewProfile, false);
    skip_display_ = base::GetFieldTrialParamByFeatureAsBool(
        kAnnouncementNotification, kSkipDisplay, false);
    remote_version_ = base::GetFieldTrialParamByFeatureAsInt(
        kAnnouncementNotification, kVersion, kInvalidVersion);
    require_signout_ = base::GetFieldTrialParamByFeatureAsBool(
        kAnnouncementNotification, kRequireSignout, false);
    show_one_ = base::GetFieldTrialParamByFeatureAsBool(
        kAnnouncementNotification, kShowOneAllProfiles, false);
    remote_url_ = base::GetFieldTrialParamValueByFeature(
        kAnnouncementNotification, kAnnouncementUrl);

    bool success = base::Time::FromUTCString(
        base::GetFieldTrialParamValueByFeature(kAnnouncementNotification,
                                               kSkipFirstRunAfterTime)
            .c_str(),
        &skip_first_run_after_);
    if (!success)
      skip_first_run_after_ = base::Time();
  }

  AnnouncementNotificationServiceImpl(
      const AnnouncementNotificationServiceImpl&) = delete;
  AnnouncementNotificationServiceImpl& operator=(
      const AnnouncementNotificationServiceImpl&) = delete;

  ~AnnouncementNotificationServiceImpl() override = default;

 private:
  // AnnouncementNotificationService implementation.
  void MaybeShowNotification() override {
    // Finch config may not be delivered on first run. Records the first run
    // timestamp to check whether we want to skip the notification.
    RecordFirstRunIfNeeded();

    if (!IsFeatureEnabled())
      return;

    // No valid version Finch parameter.
    if (!IsVersionValid(remote_version_))
      return;

    // Update the version preference. This happens earilier than checks in
    // ShowNotification.
    int current_version = pref_service_->GetInteger(kCurrentVersionPrefName);
    pref_service_->SetInteger(kCurrentVersionPrefName, remote_version_);

    if (remote_version_ > current_version) {
      OnNewVersion();
    }
  }

  void RecordFirstRunIfNeeded() {
    if (!delegate_->IsFirstRun())
      return;

    pref_service_->SetTime(kAnnouncementFirstRunTimePrefName, clock_->Now());
  }

  bool IsFeatureEnabled() const {
    return base::FeatureList::IsEnabled(kAnnouncementNotification);
  }

  bool IsVersionValid(int version) const { return version >= 0; }

  void OnNewVersion() {
    if (ShouldSkipDueToFirstRun())
      return;

    if (skip_display_)
      return;

    // Skip new profile if needed.
    if (skip_new_profile_ && profile_->IsNewProfile())
      return;

    // Require signed out but the user signed in.
    if (require_signout_ && IsUserSignIn())
      return;

    // Check if we only want to show once.
    if (show_one_ && notification_shown)
      return;

    // Check profile type, some types can't create new navigation.
    if (!CanOpenAnnouncement(profile_))
      return;
    ShowNotification();
  }

  void ShowNotification() {
    notification_shown = true;
    delegate_->ShowNotification();
  }

  // Returns whether the notification should be skipped based on first run
  // controls defined in Finch.
  bool ShouldSkipDueToFirstRun() const {
    // Don't show notification for first run if Finch parameter specified
    // "skip_first_run" to true.
    if (delegate_->IsFirstRun() && skip_first_run_)
      return true;

    // Finch parameters is not guaranteed to receive by Chrome on first run.
    // Don't show notification if first run happens after the timestamp
    // specified in Finch parameter "skip_first_run_after_time".
    base::Time first_run_time =
        pref_service_->GetTime(kAnnouncementFirstRunTimePrefName);
    if (!first_run_time.is_null() && !skip_first_run_after_.is_null() &&
        first_run_time >= skip_first_run_after_) {
      return true;
    }

    // Notice that the first run can happen before
    // kAnnouncementFirstRunTimePrefName was added to the code base . In this
    // case, we do want to show the announcement.
    return false;
  }

  bool IsUserSignIn() {
    DCHECK(g_browser_process);
    // No browser process, assume the user is not signed in.
    if (!g_browser_process)
      return false;

    ProfileAttributesStorage& storage =
        g_browser_process->profile_manager()->GetProfileAttributesStorage();

    // Can't find the profile path, assume the user is not signed in.
    DCHECK(profile_);
    ProfileAttributesEntry* entry =
        storage.GetProfileAttributesWithPath(profile_->GetPath());
    return entry && entry->GetSigninState() != SigninState::kNotSignedIn;
  }

  raw_ptr<Profile, DanglingUntriaged> profile_;
  raw_ptr<PrefService, DanglingUntriaged> pref_service_;
  std::unique_ptr<Delegate> delegate_;
  raw_ptr<base::Clock> clock_;

  // Whether to skip first Chrome launch. Parsed from Finch.
  bool skip_first_run_;

  // Whether to skip new profile. Parsed from Finch.
  bool skip_new_profile_;

  // Don't show notification if true. Parsed from Finch.
  bool skip_display_;

  // The new notification version. Parsed from Finch.
  int remote_version_;

  // Whether only show notification to signed out users. Parsed from Finch.
  bool require_signout_;

  // Whether only show one notification in each Chrome launch. Parsed from
  // Finch.
  bool show_one_;

  // The announcement URL parsed from Finch. If empty then we use the default
  // URL.
  std::string remote_url_;

  // If first run happens after this time, then notification should not show.
  base::Time skip_first_run_after_;

  base::WeakPtrFactory<AnnouncementNotificationServiceImpl> weak_ptr_factory_{
      this};
};

BASE_FEATURE(kAnnouncementNotification,
             "AnnouncementNotificationService",
             base::FEATURE_DISABLED_BY_DEFAULT);

// static
void AnnouncementNotificationService::RegisterProfilePrefs(
    PrefRegistrySimple* registry) {
  DCHECK(registry);
  registry->RegisterIntegerPref(kCurrentVersionPrefName, kInvalidVersion);
  registry->RegisterTimePref(kAnnouncementFirstRunTimePrefName, base::Time());
}

// static
std::unique_ptr<AnnouncementNotificationService>
AnnouncementNotificationService::Create(Profile* profile,
                                        PrefService* pref_service,
                                        std::unique_ptr<Delegate> delegate,
                                        base::Clock* clock) {
  return std::make_unique<AnnouncementNotificationServiceImpl>(
      profile, pref_service, std::move(delegate), clock);
}

// static
GURL AnnouncementNotificationService::GetAnnouncementURL() {
  std::string remote_url = base::GetFieldTrialParamValueByFeature(
      kAnnouncementNotification, kAnnouncementUrl);
  // Fallback to default URL if |remote_url| is empty.
  std::string url = remote_url.empty()
                        ? l10n_util::GetStringUTF8(IDS_TOS_NOTIFICATION_LINK)
                        : remote_url;
  return GURL(url);
}

// static
bool AnnouncementNotificationService::CanOpenAnnouncement(Profile* profile) {
  DCHECK(profile);
  if (!profile)
    return false;

  return !profile->IsGuestSession() && !profile->IsSystemProfile();
}

AnnouncementNotificationService::AnnouncementNotificationService() = default;

AnnouncementNotificationService::~AnnouncementNotificationService() = default;