File: menu_notification_service.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 (167 lines) | stat: -rw-r--r-- 6,131 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
// Copyright 2023 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_SAFETY_HUB_MENU_NOTIFICATION_SERVICE_H_
#define CHROME_BROWSER_UI_SAFETY_HUB_MENU_NOTIFICATION_SERVICE_H_

#include <list>
#include <map>
#include <memory>
#include <optional>

#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/ui/safety_hub/menu_notification.h"
#include "chrome/browser/ui/safety_hub/notification_permission_review_service.h"
#include "chrome/browser/ui/safety_hub/revoked_permissions_service.h"
#include "chrome/browser/ui/safety_hub/safety_hub_constants.h"
#include "chrome/browser/ui/safety_hub/safety_hub_service.h"
#include "components/keyed_service/core/keyed_service.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/extensions/cws_info_service.h"
#include "chrome/browser/ui/safety_hub/password_status_check_service.h"
#include "chrome/browser/ui/safety_hub/safety_hub_hats_service.h"
#endif  // BUILDFLAG(IS_ANDROID)

struct MenuNotificationEntry {
  int command = 0;
  std::u16string label;
  safety_hub::SafetyHubModuleType module;
};

namespace {

enum MenuNotificationPriority {
  LOW = 0,
  MEDIUM,
  HIGH,
};

struct SafetyHubModuleInfoElement {
  SafetyHubModuleInfoElement();
  ~SafetyHubModuleInfoElement();
  SafetyHubModuleInfoElement(
      MenuNotificationPriority priority,
      base::TimeDelta interval,
      base::RepeatingCallback<
          std::optional<std::unique_ptr<SafetyHubService::Result>>()>
          result_getter,
      std::unique_ptr<SafetyHubMenuNotification> notification);

  MenuNotificationPriority priority;
  base::TimeDelta interval;
  base::RepeatingCallback<
      std::optional<std::unique_ptr<SafetyHubService::Result>>()>
      result_getter;
  std::unique_ptr<SafetyHubMenuNotification> notification;
};

using ResultMap = std::map<safety_hub::SafetyHubModuleType,
                           std::unique_ptr<SafetyHubService::Result>>;

}  // namespace

// This class manages the notifications that should be shown when a user opens
// the three-dot menu. It will collect the latest results from all the Safety
// Hub service and subsequently update the notifications. Based on priority and
// prior showing of notification, it will determine which notification that
// should be shown.
class SafetyHubMenuNotificationService : public KeyedService {
 public:
  explicit SafetyHubMenuNotificationService(
      PrefService* pref_service,
      RevokedPermissionsService* revoked_permissions_service,
      NotificationPermissionsReviewService* notification_permissions_service,
#if !BUILDFLAG(IS_ANDROID)
      PasswordStatusCheckService* password_check_service,
      SafetyHubHatsService* safety_hub_hats_service,
#endif  // BUILDFLAG(IS_ANDROID)
      Profile* profile);
  SafetyHubMenuNotificationService(const SafetyHubMenuNotificationService&) =
      delete;
  SafetyHubMenuNotificationService& operator=(
      const SafetyHubMenuNotificationService&) = delete;

  ~SafetyHubMenuNotificationService() override;

  // Returns the CommandID and notification string that should be shown in the
  // three-dot menu. When no notification should be shown, std::nullopt will be
  // returned.
  std::optional<MenuNotificationEntry> GetNotificationToShow();

  // Dismisses all the active menu notifications.
  void DismissActiveNotification();

  // Dismisses the active menu notification of the specified module.
  void DismissActiveNotificationOfModule(
      safety_hub::SafetyHubModuleType module);

  void UpdateResultGetterForTesting(
      safety_hub::SafetyHubModuleType type,
      base::RepeatingCallback<
          std::optional<std::unique_ptr<SafetyHubService::Result>>()>
          result_getter);

#if !BUILDFLAG(IS_ANDROID)
  void MaybeTriggerControlSurvey() const;
#endif  // !BUILDFLAG(IS_ANDROID)

 private:
  // Gets the latest result from each Safety Hub service. Will return
  // std::nullopt when there is no result from one of the services.
  std::optional<ResultMap> GetResultsFromAllModules();

  // Stores the notifications (which should have their results updated) as a
  // dict in the prefs.
  void SaveNotificationsToPrefs() const;

  // Creates a notification from the provided dictionary, for the specified
  // Safety Hub service type.
  std::unique_ptr<SafetyHubMenuNotification> GetNotificationFromDict(
      const base::Value::Dict& dict,
      safety_hub::SafetyHubModuleType& type) const;

  // Sets the relevant, static meta information for the three-dot menu
  // (priority, interval, and method to retrieve the relevant result) for a
  // specific type of Safety Hub module provided the dictionary that stores the
  // notifications.
  void SetInfoElement(
      safety_hub::SafetyHubModuleType type,
      MenuNotificationPriority priority,
      base::TimeDelta interval,
      base::RepeatingCallback<
          std::optional<std::unique_ptr<SafetyHubService::Result>>()>
          result_getter,
      const base::Value::Dict& stored_notifications);

  // Called when the pref for Safe Browsing has been updated.
  void OnSafeBrowsingPrefUpdate();

  // Returns if any safety hub notification has been shown in the menu so far.
  bool HasAnyNotificationBeenShown() const;

  // Holds the mapping from module type to pref name.
  std::map<safety_hub::SafetyHubModuleType, const char*> pref_dict_key_map_;

  // Preference service that persists the notifications.
  raw_ptr<PrefService> pref_service_;

  // A map that captures the meta information about menu notifications for each
  // Safety Hub module.
  std::map<safety_hub::SafetyHubModuleType,
           std::unique_ptr<SafetyHubModuleInfoElement>>
      module_info_map_;

  // Registrar to record the pref changes to Safe Browsing.
  PrefChangeRegistrar registrar_;

#if !BUILDFLAG(IS_ANDROID)
  // Safety Hub Hats service to trigger surveys.
  raw_ptr<SafetyHubHatsService> safety_hub_hats_service_;
#endif  // !BUILDFLAG(IS_ANDROID)
};

#endif  // CHROME_BROWSER_UI_SAFETY_HUB_MENU_NOTIFICATION_SERVICE_H_