1 // Copyright 2022 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_HATS_TRIGGER_HELPER_H_
6 #define COMPONENTS_PERMISSIONS_PERMISSION_HATS_TRIGGER_HELPER_H_
11 #include "components/keyed_service/core/keyed_service.h"
12 #include "components/permissions/permission_util.h"
13 #include "constants.h"
15 namespace user_prefs {
16 class PrefRegistrySyncable;
17 } // namespace user_prefs
20 namespace permissions {
22 constexpr char kTrueStr[] = "true";
23 constexpr char kFalseStr[] = "false";
25 constexpr char kOnPromptAppearing[] = "OnPromptAppearing";
26 constexpr char kOnPromptResolved[] = "OnPromptResolved";
28 // A static class that handles permission HaTS survey trigger configuration and
30 class PermissionHatsTriggerHelper {
31 // Key-value mapping type for a HaTS survey's product specific bits data.
32 typedef std::map<std::string, bool> SurveyBitsData;
34 // Key-value mapping type for HaTS survey's product specific string data.
35 typedef std::map<std::string, std::string> SurveyStringData;
38 enum OneTimePermissionPromptsDecidedBucket {
43 BUCKET_11_20, // 11-20
47 struct PromptParametersForHaTS {
48 PromptParametersForHaTS(
49 permissions::RequestType request_type,
50 absl::optional<permissions::PermissionAction> action,
51 permissions::PermissionPromptDisposition prompt_disposition,
52 permissions::PermissionPromptDispositionReason
53 prompt_disposition_reason,
54 permissions::PermissionRequestGestureType gesture_type,
55 const std::string& channel,
56 const std::string& survey_display_time,
57 absl::optional<base::TimeDelta> prompt_display_duration,
58 OneTimePermissionPromptsDecidedBucket one_time_prompts_decided_bucket,
59 absl::optional<GURL> gurl);
60 PromptParametersForHaTS(const PromptParametersForHaTS& other);
61 ~PromptParametersForHaTS();
63 permissions::RequestType request_type;
64 absl::optional<permissions::PermissionAction> action;
65 permissions::PermissionPromptDisposition prompt_disposition;
66 permissions::PermissionPromptDispositionReason prompt_disposition_reason;
67 permissions::PermissionRequestGestureType gesture_type;
69 std::string survey_display_time;
70 absl::optional<base::TimeDelta> prompt_display_duration;
71 OneTimePermissionPromptsDecidedBucket one_time_prompts_decided_bucket;
75 struct SurveyProductSpecificData {
77 ~SurveyProductSpecificData();
79 static SurveyProductSpecificData PopulateFrom(
80 PromptParametersForHaTS prompt_parameters);
82 const SurveyBitsData survey_bits_data;
83 const SurveyStringData survey_string_data;
86 explicit SurveyProductSpecificData(SurveyBitsData survey_bits_data,
87 SurveyStringData survey_string_data);
90 ~PermissionHatsTriggerHelper() = delete;
91 PermissionHatsTriggerHelper(const PermissionHatsTriggerHelper&) = delete;
92 PermissionHatsTriggerHelper& operator=(const PermissionHatsTriggerHelper&) =
95 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
97 static bool ArePromptTriggerCriteriaSatisfied(
98 PromptParametersForHaTS prompt_parameters,
99 const std::string& trigger_name_base);
101 static OneTimePermissionPromptsDecidedBucket GetOneTimePromptsDecidedBucket(
102 PrefService* pref_service);
104 // Increments the count representing the one time permission prompts seen by
106 static void IncrementOneTimePermissionPromptsDecidedIfApplicable(
107 ContentSettingsType type,
108 PrefService* profile);
110 // Bucketing used to categorize users by how many one time permission they
112 static std::string GetOneTimePromptsDecidedBucketString(
113 OneTimePermissionPromptsDecidedBucket bucket);
115 // Returns a vector containing pairs of <trigger_name, trigger_id>
116 // The trigger_name is a unique name used by the HaTS service integration, and
117 // the trigger_id is an ID that specifies a survey in the Listnr backend.
118 static std::vector<std::pair<std::string, std::string>>&
119 GetPermissionPromptTriggerIdPairs(const std::string& trigger_name_base);
121 // Returns the trigger name and probability corresponding to a specific
122 // request type. Returns empty value if there is a configuration error or the
123 // passed request type is not configured.
124 static absl::optional<std::pair<std::string, double>>
125 GetPermissionPromptTriggerNameAndProbabilityForRequestType(
126 const std::string& trigger_name_base,
127 const std::string& request_type);
129 static void SetIsTest();
132 } // namespace permissions
134 #endif // COMPONENTS_PERMISSIONS_PERMISSION_HATS_TRIGGER_HELPER_H_