File: safety_hub_handler.h

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 (287 lines) | stat: -rw-r--r-- 12,203 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
// 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.

#ifndef CHROME_BROWSER_UI_WEBUI_SETTINGS_SAFETY_HUB_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_SETTINGS_SAFETY_HUB_HANDLER_H_

#include <set>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/time/clock.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/safety_hub/extensions_result.h"
#include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h"
#include "components/content_settings/core/common/content_settings_constraints.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "extensions/browser/extension_prefs_observer.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/extension_id.h"
#include "url/origin.h"

namespace extensions {
class ExtensionPrefs;
class ExtensionRegistry;
}  // namespace extensions

// The state of Safe Browsing settings.
enum class SafeBrowsingState {
  kEnabledEnhanced = 0,
  kEnabledStandard = 1,
  kDisabledByAdmin = 2,
  kDisabledByExtension = 3,
  kDisabledByUser = 4,
  // New enum values must go above here.
  kMaxValue = kDisabledByUser,
};

/**
 * This handler deals with the permission-related operations on the site
 * settings page.
 */

class SafetyHubHandler : public settings::SettingsPageUIHandler,
                         public extensions::ExtensionPrefsObserver,
                         public extensions::ExtensionRegistryObserver {
 public:
  enum class SafetyHubModule {
    kExtensions,
    kNotifications,
    kPasswords,
    kSafeBrowsing,
    kUnusedSitePermissions,
    kVersion
  };

  explicit SafetyHubHandler(Profile* profile);

  ~SafetyHubHandler() override;

  static std::unique_ptr<SafetyHubHandler> GetForProfile(Profile* profile);

  // Testing functions to manipulate the handlers CWSInfoService and the
  // triggering extension dictionary.
  void SetTriggeringExtensionForTesting(std::string extension_id);
  void ClearExtensionResultsForTesting();

 private:
  friend class SafetyHubHandlerTest;
  friend class SafetyHubHandlerUnusedPermissionRevocationDisabledTest;
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           PopulateUnusedSitePermissionsData);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           HandleAllowPermissionsAgainForUnusedSite);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           HandleAcknowledgeRevokedUnusedSitePermissionsList);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           PopulateAbusiveAndUnusedSitePermissionsData);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           HandleAllowPermissionsAgainForAbusiveSite);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           HandleAllowPermissionsAgainForAbusiveAndUnusedSite);
  FRIEND_TEST_ALL_PREFIXES(
      SafetyHubHandlerTest,
      HandleAcknowledgeRevokedAbusiveAndUnusedSitePermissionsList);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           HandleIgnoreOriginsForNotificationPermissionReview);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           HandleBlockNotificationPermissionForOrigins);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           HandleAllowNotificationPermissionForOrigins);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           HandleResetNotificationPermissionForOrigins);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           PopulateNotificationPermissionReviewData);
  FRIEND_TEST_ALL_PREFIXES(
      SafetyHubHandlerTest,
      HandleUndoIgnoreOriginsForNotificationPermissionReview);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           SendNotificationPermissionReviewList_FeatureEnabled);
  FRIEND_TEST_ALL_PREFIXES(
      SafetyHubHandlerTest,
      SendNotificationPermissionReviewList_FeatureDisabled);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest, RevokeAllContentSettingTypes);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerParameterizedTest,
                           PasswordCardState);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest, PasswordCardCheckTime);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest, VersionCardUpToDate);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest, VersionCardOutOfDate);
  FRIEND_TEST_ALL_PREFIXES(SafetyHubHandlerTest,
                           ExtensionPrefAndInitialization);
  FRIEND_TEST_ALL_PREFIXES(
      SafetyHubHandlerUnusedPermissionRevocationDisabledTest,
      PopulateSitePermissionsData);
  FRIEND_TEST_ALL_PREFIXES(
      SafetyHubHandlerUnusedPermissionRevocationDisabledTest,
      HandleAllowPermissionsAgainForSite);
  FRIEND_TEST_ALL_PREFIXES(
      SafetyHubHandlerUnusedPermissionRevocationDisabledTest,
      HandleAcknowledgeRevokedSitePermissionsList);

  // SettingsPageUIHandler implementation.
  void OnJavascriptAllowed() override;
  void OnJavascriptDisallowed() override;

  // WebUIMessageHandler implementation.
  void RegisterMessages() override;

  // Returns the list of revoked permissions to be used in
  // "Unused site permissions" module.
  void HandleGetRevokedUnusedSitePermissionsList(const base::Value::List& args);

  // Re-grant the revoked permissions and remove the given origin from the
  // revoked permissions list.
  void HandleAllowPermissionsAgainForUnusedSite(const base::Value::List& args);

  // Reverse the changes made by |HandleAllowPermissionsAgainForUnusedSite| for
  // the given |UnusedSitePermission| object.
  void HandleUndoAllowPermissionsAgainForUnusedSite(
      const base::Value::List& args);

  // Clear the list of revoked permissions so they are not shown again.
  // Permission settings themselves are not affected by this.
  void HandleAcknowledgeRevokedUnusedSitePermissionsList(
      const base::Value::List& args);

  // Reverse the changes made by
  // |HandleAcknowledgeRevokedUnusedSitePermissionsList| for the given list of
  // |UnusedSitePermission| objects. List of revoked
  // permissions is repopulated. Permission settings are not changed.
  void HandleUndoAcknowledgeRevokedUnusedSitePermissionsList(
      const base::Value::List& args);

  // Returns the list of revoked permissions that belongs to origins which
  // haven't been visited recently.
  // TODO(crbug.com/40267370): Get list of revoked permissions from the unused
  // site permission service instead.
  base::Value::List PopulateUnusedSitePermissionsData();

  // Sends the list of unused site permissions to review to the WebUI.
  void SendUnusedSitePermissionsReviewList();

  // Returns the list of notification permissions that needs to be reviewed.
  void HandleGetNotificationPermissionReviewList(const base::Value::List& args);

  // Handles ignoring origins for the review notification permissions feature.
  void HandleIgnoreOriginsForNotificationPermissionReview(
      const base::Value::List& args);

  // Handles resetting a notification permission for given origins.
  void HandleResetNotificationPermissionForOrigins(
      const base::Value::List& args);

  // Handles blocking notification permissions for multiple origins.
  void HandleBlockNotificationPermissionForOrigins(
      const base::Value::List& args);

  // Handles allowing notification permissions for multiple origins.
  void HandleAllowNotificationPermissionForOrigins(
      const base::Value::List& args);

  // Handles reverting the action of ignoring origins for review notification
  // permissions feature by removing them from the notification permission
  // verification blocklist.
  void HandleUndoIgnoreOriginsForNotificationPermissionReview(
      const base::Value::List& args);

  // Handles dismissing the active menu notification for Safety Hub.
  void HandleDismissActiveMenuNotification(const base::Value::List& args);

  // Handles dismissing the menu notifications for the password module.
  void HandleDismissPasswordMenuNotification(const base::Value::List& args);

  // Handles dismissing the menu notifications for the extensions module.
  void HandleDismissExtensionsMenuNotification(const base::Value::List& args);

  // Returns the data for Safe Browsing card.
  void HandleGetSafeBrowsingCardData(const base::Value::List& args);

  // Returns the data for the password card.
  void HandleGetPasswordCardData(const base::Value::List& args);

  // Returns the data for the version card.
  void HandleGetVersionCardData(const base::Value::List& args);

  // Fetches data for the version card to return data to the UI.
  base::Value::Dict GetVersionCardData();

  // Returns the data for Safety Hub entry point.
  void HandleGetSafetyHubEntryPointData(const base::Value::List& args);

  // Returns true if Safety Hub has recommendations.
  void HandleGetSafetyHubHasRecommendations(const base::Value::List& args);

  // Returns the subheader for Safety Hub entry point in settings.
  void HandleGetSafetyHubEntryPointSubheader(const base::Value::List& args);

  // Sends the list of notification permissions to review to the WebUI.
  void SendNotificationPermissionReviewList();

  // Creates the safety hub results object which tracks triggering
  // extensions.
  void InitSafetyHubExtensionResults();

  // Returns the set of Safety Hub modules which require the user's attention.
  std::set<SafetyHubModule> GetSafetyHubModulesWithRecommendations();

  // Calculate the number of extensions that need to be reviewed by the
  // user.
  void HandleGetNumberOfExtensionsThatNeedReview(const base::Value::List& args);

  // Return the number of extensions that should be reviewed by the user.
  // There are currently three triggers the `SafetyHubHandler` tracks:
  // -- Extension Malware Violation
  // -- Extension Policy Violation
  // -- Extension Unpublished by the developer
  // -- Extension that is considerard unwanted
  // -- Extension that is considerard offstore
  // When first called it will create the `extension_sh_result_` object.
  int GetNumberOfExtensionsThatNeedReview();

  // Let listeners know that the number of extensions that need review may
  // have changed.
  void UpdateNumberOfExtensionsThatNeedReview(
      int num_extension_need_review_before,
      int num_extension_need_review_after);

  // ExtensionPrefsObserver implementation to track changes to extensions.
  void OnExtensionPrefsUpdated(const std::string& extension_id) override;
  void OnExtensionPrefsWillBeDestroyed(
      extensions::ExtensionPrefs* prefs) override;

  // ExtensionRegistryObserver implementation to track changes to extensions.
  void OnExtensionUninstalled(content::BrowserContext* browser_context,
                              const extensions::Extension* extension,
                              extensions::UninstallReason reason) override;
  void OnShutdown(extensions::ExtensionRegistry* registry) override;

  // Record a visit to the Safety Hub page.
  void HandleRecordSafetyHubVisit(const base::Value::List& args);

  // Record an interaction with one of the Safety Hub modules.
  void HandleRecordSafetyHubInteraction(const base::Value::List& args);

  // The `extension_sh_result_` contains the needed information about how
  // many extensions should be reviewed by the user.
  std::unique_ptr<SafetyHubExtensionsResult> extension_sh_result_;

  const raw_ptr<Profile, DanglingUntriaged> profile_;

  raw_ptr<base::Clock> clock_;

  void SetClockForTesting(base::Clock* clock);

  // Listen to the extension prefs and the extension registry for when
  // prefs are unloaded or changed or an extension is removed.
  base::ScopedObservation<extensions::ExtensionRegistry,
                          extensions::ExtensionRegistryObserver>
      extension_registry_observation_{this};
  base::ScopedObservation<extensions::ExtensionPrefs,
                          extensions::ExtensionPrefsObserver>
      prefs_observation_{this};
  base::WeakPtrFactory<SafetyHubHandler> weak_ptr_factory_{this};
};

#endif  // CHROME_BROWSER_UI_WEBUI_SETTINGS_SAFETY_HUB_HANDLER_H_