File: safe_browsing_handler.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 (137 lines) | stat: -rw-r--r-- 5,726 bytes parent folder | download | duplicates (6)
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
// 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/new_tab_page/modules/safe_browsing/safe_browsing_handler.h"

#include "base/metrics/field_trial_params.h"
#include "base/time/time.h"
#include "chrome/browser/new_tab_page/modules/safe_browsing/safe_browsing_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/safe_browsing_metrics_collector_factory.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/core/browser/safe_browsing_metrics_collector.h"
#include "components/safe_browsing/core/common/safe_browsing_policy_handler.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/search/ntp_features.h"

using ::safe_browsing::IsEnhancedProtectionEnabled;
using ::safe_browsing::SafeBrowsingMetricsCollectorFactory;
using ::safe_browsing::SafeBrowsingPolicyHandler;

namespace ntp {
namespace {
// Get end time for last cooldown. Returns epoch + cooldown_period if no
// cooldown happened.
base::Time GetLastCooldownEndTime(PrefService* pref_service) {
  base::Time cooldown_start = base::Time::FromDeltaSinceWindowsEpoch(
      base::Seconds(pref_service->GetInt64(
          prefs::kSafeBrowsingModuleLastCooldownStartAt)));
  // The field param is passed as a double, to allow manual testing using a
  // fraction of a day as cooldown.
  double cooldown_days = base::GetFieldTrialParamByFeatureAsDouble(
      ntp_features::kNtpSafeBrowsingModule,
      ntp_features::kNtpSafeBrowsingModuleCooldownPeriodDaysParam,
      /*default_value=*/30.0);
  base::TimeDelta cooldown_period = base::Seconds(static_cast<int>(
      cooldown_days * base::Time::kHoursPerDay * base::Time::kSecondsPerHour));
  return cooldown_start + cooldown_period;
}

void SetModuleCooldownPrefs(PrefService* pref_service,
                            int64_t cooldown_start_time,
                            int module_shown_count) {
  pref_service->SetInt64(prefs::kSafeBrowsingModuleLastCooldownStartAt,
                         cooldown_start_time);
  pref_service->SetInteger(prefs::kSafeBrowsingModuleShownCount,
                           module_shown_count);
}
}  // namespace

SafeBrowsingHandler::SafeBrowsingHandler(
    mojo::PendingReceiver<ntp::safe_browsing::mojom::SafeBrowsingHandler>
        handler,
    Profile* profile)
    : handler_(this, std::move(handler)),
      metrics_collector_(
          SafeBrowsingMetricsCollectorFactory::GetForProfile(profile)),
      pref_service_(profile->GetPrefs()),
      saved_last_cooldown_start_time_(0),
      saved_module_shown_count_(0) {}

SafeBrowsingHandler::~SafeBrowsingHandler() = default;

void SafeBrowsingHandler::CanShowModule(CanShowModuleCallback callback) {
  bool managed =
      SafeBrowsingPolicyHandler::IsSafeBrowsingProtectionLevelSetByPolicy(
          pref_service_);
  bool already_enabled = IsEnhancedProtectionEnabled(*pref_service_);
  bool module_already_opened =
      pref_service_->GetBoolean(prefs::kSafeBrowsingModuleOpened);
  base::Time cooldown_end = GetLastCooldownEndTime(pref_service_);

  // Do not show the module if Safe Browsing protection level is controlled by
  // policy, or is already enabled, if the user has clicked on the module button
  // earlier, or if the user is in cooldown.
  if (managed || already_enabled || module_already_opened ||
      (base::Time::Now() < cooldown_end)) {
    std::move(callback).Run(false);
    return;
  }

  std::optional<base::Time> latest_event_time =
      metrics_collector_->GetLatestSecuritySensitiveEventTimestamp();
  // Do not show if there is no security sensitive event after the latest
  // cooldown.
  if (!latest_event_time.has_value() || latest_event_time < cooldown_end) {
    std::move(callback).Run(false);
    return;
  }

  int module_shown_count =
      pref_service_->GetInteger(prefs::kSafeBrowsingModuleShownCount);
  int module_shown_count_max = base::GetFieldTrialParamByFeatureAsInt(
      ntp_features::kNtpSafeBrowsingModule,
      ntp_features::kNtpSafeBrowsingModuleCountMaxParam,
      /*default_value=*/5);

  // Start cooldown if module has been shown module_shown_count_max times.
  if (module_shown_count + 1 >= module_shown_count_max) {
    SetModuleCooldownPrefs(
        pref_service_, base::Time::Now().ToDeltaSinceWindowsEpoch().InSeconds(),
        /*module_shown_count=*/0);
  } else {
    pref_service_->SetInteger(prefs::kSafeBrowsingModuleShownCount,
                              module_shown_count + 1);
  }
  std::move(callback).Run(true);
}

void SafeBrowsingHandler::ProcessModuleClick() {
  pref_service_->SetBoolean(prefs::kSafeBrowsingModuleOpened, true);
}

void SafeBrowsingHandler::DismissModule() {
  saved_last_cooldown_start_time_ =
      pref_service_->GetInt64(prefs::kSafeBrowsingModuleLastCooldownStartAt);
  saved_module_shown_count_ =
      pref_service_->GetInteger(prefs::kSafeBrowsingModuleShownCount);
  SetModuleCooldownPrefs(
      pref_service_, base::Time::Now().ToDeltaSinceWindowsEpoch().InSeconds(),
      /*module_shown_count=*/0);
}

void SafeBrowsingHandler::RestoreModule() {
  SetModuleCooldownPrefs(pref_service_, saved_last_cooldown_start_time_,
                         saved_module_shown_count_);
}

// static
void SafeBrowsingHandler::RegisterProfilePrefs(PrefRegistrySimple* registry) {
  registry->RegisterIntegerPref(prefs::kSafeBrowsingModuleShownCount, 0);
  registry->RegisterInt64Pref(prefs::kSafeBrowsingModuleLastCooldownStartAt, 0);
  registry->RegisterBooleanPref(prefs::kSafeBrowsingModuleOpened, false);
}

}  // namespace ntp