File: safe_browsing_pref_change_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 (268 lines) | stat: -rw-r--r-- 9,716 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 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/safe_browsing/safe_browsing_pref_change_handler.h"

#include "base/feature_list.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "content/public/browser/web_contents.h"

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || \
    BUILDFLAG(IS_MAC)
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/toasts/api/toast_id.h"
#include "chrome/browser/ui/toasts/toast_controller.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#endif

#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#endif

namespace safe_browsing {

SafeBrowsingPrefChangeHandler::SafeBrowsingPrefChangeHandler(Profile* profile)
    : profile_(profile) {
  DCHECK(profile);
#if BUILDFLAG(IS_ANDROID)
  retry_handler_ = std::make_unique<MessageRetryHandler>(
      profile_, prefs::kSafeBrowsingSyncedEnhancedProtectionRetryState,
      prefs::kSafeBrowsingSyncedEnhancedProtectionNextRetryTimestamp,
      kRetryAttemptStartupDelay, kRetryNextAttemptDelay, kWaitingPeriodInterval,
      base::BindOnce(&SafeBrowsingPrefChangeHandler::RetryStateCallback,
                     weak_ptr_factory_.GetWeakPtr()),
      "SafeBrowsing.EnhancedProtection.ShouldRetryOutcome",
      prefs::kSafeBrowsingSyncedEnhancedProtectionUpdateTimestamp,
      prefs::kEnhancedProtectionEnabledViaTailoredSecurity);
  retry_handler_->StartRetryTimer();
#endif
}

SafeBrowsingPrefChangeHandler::~SafeBrowsingPrefChangeHandler() {
#if BUILDFLAG(IS_ANDROID)
  RemoveTabModelListObserver();
  RemoveTabModelObserver();
#endif
}

// TODO(crbug.com/378888301): Add tests for Chrome Toast and Android modal
// logic.
void SafeBrowsingPrefChangeHandler::
    MaybeShowEnhancedProtectionSettingChangeNotification() {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || \
    BUILDFLAG(IS_MAC)
  if (!profile_ ||
      !base::FeatureList::IsEnabled(safe_browsing::kEsbAsASyncedSetting)) {
    return;
  }
  Browser* const browser = chrome::FindBrowserWithProfile(profile_);
  if (!browser) {
    return;
  }
  ToastController* const controller =
      browser->browser_window_features()->toast_controller();
  if (!controller) {
    return;
  }
  // We need to handle the toast for the security settings page differently:
  // 1. If the user has turned on ESB and is on the security page, we show
  // the toast but without the action button that directs users to the
  // settings page.
  // 2. If the user has turned off ESB and is on the security page, we do
  // not show a toast at all.
  TabStripModel* tab_strip_model = browser->GetTabStripModel();
  content::WebContents* web_contents = tab_strip_model->GetActiveWebContents();
  bool is_security_page =
      web_contents ? web_contents->GetLastCommittedURL().spec().starts_with(
                         "chrome://settings/security")
                   : false;

  // Extract the enhanced protection pref value.
  bool is_enhanced_enabled = IsEnhancedProtectionEnabled(*profile_->GetPrefs());

  // The enhanced protection setting has been updated. To reflect this
  // change, we will show toasts to the user, taking into account both the
  // new setting value and whether they are currently on the settings page.
  if (is_enhanced_enabled) {
    // When the user is currently on the security settings page, show a
    // toast without the action button to go to the settings page.
    // Otherwise, we should a button that takes user to the settings page to
    // change the enhanced protection settings.
    controller->MaybeShowToast(
        ToastParams(is_security_page ? ToastId::kSyncEsbOnWithoutActionButton
                                     : ToastId::kSyncEsbOn));
  } else if (!is_security_page) {
    // Toast messages are not displayed on the security page when a user
    // disables a security setting. This applies whether the user disables
    // the setting on the current device or the change is synced from
    // another device.
    controller->MaybeShowToast(ToastParams(ToastId::kSyncEsbOff));
  }
#endif

// TODO(crbug.com/397966486): Add tests in the android test file.
#if BUILDFLAG(IS_ANDROID)
  if (!base::FeatureList::IsEnabled(safe_browsing::kEsbAsASyncedSetting) ||
      !profile_) {
    return;
  }
  content::WebContents* web_contents = nullptr;
  for (const TabModel* tab_model : TabModelList::models()) {
    if (tab_model->GetProfile() != profile_) {
      continue;
    }
    int tab_count = tab_model->GetTabCount();
    for (int i = 0; i < tab_count; i++) {
      web_contents = tab_model->GetWebContentsAt(i);
      if (web_contents) {
        break;
      }
    }
  }

  if (!web_contents) {
    // Instantiate the retry handler here, if it hasn't been already
    profile_->GetPrefs()->SetInteger(
        prefs::kSafeBrowsingSyncedEnhancedProtectionRetryState,
        static_cast<int>(MessageRetryHandler::RetryState::RETRY_NEEDED));
    if (!retry_handler_) {
      retry_handler_ = std::make_unique<MessageRetryHandler>(
          profile_, prefs::kSafeBrowsingSyncedEnhancedProtectionRetryState,
          prefs::kSafeBrowsingSyncedEnhancedProtectionNextRetryTimestamp,
          kRetryAttemptStartupDelay, kRetryNextAttemptDelay,
          kWaitingPeriodInterval,
          base::BindOnce(&SafeBrowsingPrefChangeHandler::RetryStateCallback,
                         weak_ptr_factory_.GetWeakPtr()),
          "SafeBrowsing.EnhancedProtection.ShouldRetryOutcome",
          prefs::kSafeBrowsingSyncedEnhancedProtectionUpdateTimestamp,
          prefs::kEnhancedProtectionEnabledViaTailoredSecurity);
      retry_handler_->StartRetryTimer();
    }
    RegisterObserver();
  } else {
    // Do not show the notification modal if the user set the setting locally on
    // this device.
    if (profile_->GetPrefs()->GetBoolean(
            prefs::kSafeBrowsingSyncedEnhancedProtectionSetLocally)) {
      retry_handler_->SaveRetryState(
          MessageRetryHandler::RetryState::NO_RETRY_NEEDED);
      return;
    }
    // Extract the enhanced protection pref value.
    bool is_enhanced_enabled =
        IsEnhancedProtectionEnabled(*profile_->GetPrefs());
    message_ = std::make_unique<TailoredSecurityConsentedModalAndroid>(
        web_contents, is_enhanced_enabled,
        base::BindOnce(
            &SafeBrowsingPrefChangeHandler::ConsentedMessageDismissed,
            weak_ptr_factory_.GetWeakPtr()),
        /*is_requested_by_synced_esb=*/true);
    if (retry_handler_) {
      retry_handler_->SaveRetryState(
          MessageRetryHandler::RetryState::NO_RETRY_NEEDED);
    }
  }
#endif
}

#if BUILDFLAG(IS_ANDROID)
void SafeBrowsingPrefChangeHandler::RetryStateCallback() {
  profile_->GetPrefs()->SetInteger(
      prefs::kSafeBrowsingSyncedEnhancedProtectionRetryState,
      static_cast<int>(MessageRetryHandler::RetryState::RETRY_NEEDED));
  MaybeShowEnhancedProtectionSettingChangeNotification();
}

void SafeBrowsingPrefChangeHandler::SetTabModelForTesting(TabModel* tab_model) {
  observed_tab_model_ = tab_model;
}

bool SafeBrowsingPrefChangeHandler::IsObservingTabModelListForTesting() const {
  return observing_tab_model_list_;
}

bool SafeBrowsingPrefChangeHandler::IsObservingTabModelForTesting() const {
  return observed_tab_model_ != nullptr;
}

void SafeBrowsingPrefChangeHandler::DidAddTab(TabAndroid* tab,
                                              TabModel::TabLaunchType type) {
  RemoveTabModelObserver();
  RemoveTabModelListObserver();
  // Get the Profile from the TabAndroid
  if (!tab || !tab->web_contents()) {
    return;
  }
  RetryStateCallback();
}

void SafeBrowsingPrefChangeHandler::OnTabModelAdded(TabModel* tab_model) {
  if (observed_tab_model_) {
    return;
  }
  if (TabModelList::models().empty()) {
    return;
  }
  AddTabModelObserver();
}

void SafeBrowsingPrefChangeHandler::OnTabModelRemoved(TabModel* tab_model) {
  RemoveTabModelObserver();
}

void SafeBrowsingPrefChangeHandler::RegisterObserver() {
  AddTabModelListObserver();
  AddTabModelObserver();
}

void SafeBrowsingPrefChangeHandler::AddTabModelListObserver() {
  if (observing_tab_model_list_) {
    return;
  }
  TabModelList::AddObserver(this);
  observing_tab_model_list_ = true;
}

void SafeBrowsingPrefChangeHandler::AddTabModelObserver() {
  if (observed_tab_model_) {
    return;
  }
  for (TabModel* tab_model : TabModelList::models()) {
    if (tab_model->GetProfile() != profile_) {
      continue;
    }
    tab_model->AddObserver(this);
    // Saving the tab_model so we can stop observing the tab model after a
    // tabmodel is added.
    observed_tab_model_ = tab_model;
    return;
  }
}

void SafeBrowsingPrefChangeHandler::RemoveTabModelListObserver() {
  observing_tab_model_list_ = false;
  TabModelList::RemoveObserver(this);
}

void SafeBrowsingPrefChangeHandler::RemoveTabModelObserver() {
  if (!observed_tab_model_) {
    return;
  }
  observed_tab_model_->RemoveObserver(this);
  observed_tab_model_ = nullptr;
}

void SafeBrowsingPrefChangeHandler::ConsentedMessageDismissed() {
  message_.reset();
}
#endif
}  // namespace safe_browsing