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_
|