1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PERMISSIONS_PERMISSION_ACTIONS_HISTORY_H_
6 #define COMPONENTS_PERMISSIONS_PERMISSION_ACTIONS_HISTORY_H_
8 #include "base/memory/raw_ptr.h"
9 #include "base/notreached.h"
10 #include "base/time/time.h"
11 #include "components/keyed_service/core/keyed_service.h"
12 #include "components/permissions/permission_request_enums.h"
13 #include "components/permissions/permission_uma_util.h"
14 #include "components/permissions/permission_util.h"
15 #include "components/permissions/prediction_service/prediction_request_features.h"
16 #include "components/pref_registry/pref_registry_syncable.h"
18 enum class PermissionAction;
19 enum class RequestType;
22 namespace user_prefs {
23 class PrefRegistrySyncable;
26 namespace permissions {
27 // This class records and stores all actions taken on permission prompts. It
28 // also has utility functions to interact with the history.
29 class PermissionActionsHistory : public KeyedService {
32 PermissionAction action;
35 bool operator==(const Entry that) const {
36 return std::tie(this->action, this->time) ==
37 std::tie(that.action, that.time);
39 bool operator!=(const Entry that) const { return !(*this == that); }
42 enum class EntryFilter {
44 WANT_LOUD_PROMPTS_ONLY,
45 WANT_QUIET_PROMPTS_ONLY,
48 explicit PermissionActionsHistory(PrefService* pref_service);
50 PermissionActionsHistory(const PermissionActionsHistory&) = delete;
51 PermissionActionsHistory& operator=(const PermissionActionsHistory&) = delete;
53 ~PermissionActionsHistory() override = default;
55 // Get the history of recorded actions that happened after a particular time.
56 // Optionally a permission request type can be specified which will only
57 // return actions of that type.
58 std::vector<Entry> GetHistory(const base::Time& begin,
59 EntryFilter entry_filter);
60 std::vector<Entry> GetHistory(const base::Time& begin,
62 EntryFilter entry_filter);
64 // Record that a particular action has occurred at this time.
65 // `base::Time::Now()` is used to retrieve the current time.
66 void RecordAction(PermissionAction action,
68 PermissionPromptDisposition prompt_disposition);
70 // Delete logs of past user interactions. To be called when clearing
72 void ClearHistory(const base::Time& delete_begin,
73 const base::Time& delete_end);
75 PrefService* GetPrefServiceForTesting();
77 static void FillInActionCounts(
78 PredictionRequestFeatures::ActionCounts* counts,
79 const std::vector<PermissionActionsHistory::Entry>& permission_actions);
81 // Registers the preferences related to blocklisting in the given PrefService.
82 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
85 std::vector<Entry> GetHistoryInternal(const base::Time& begin,
86 const std::string& key,
87 EntryFilter entry_filter);
89 raw_ptr<PrefService> pref_service_;
92 } // namespace permissions
93 #endif // COMPONENTS_PERMISSIONS_PERMISSION_ACTIONS_HISTORY_H_