1 // Copyright 2019 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_UI_SELECTOR_H_
6 #define COMPONENTS_PERMISSIONS_PERMISSION_UI_SELECTOR_H_
8 #include "base/functional/callback_forward.h"
9 #include "components/permissions/permission_request.h"
10 #include "components/permissions/permission_uma_util.h"
11 #include "third_party/abseil-cpp/absl/types/optional.h"
13 namespace permissions {
15 // The interface for implementations that decide if the quiet prompt UI should
16 // be used to display a permission |request|, whether a warning should be
17 // printed to the Dev Tools console, and the reasons for both.
19 // Implementations of interface are expected to have long-lived instances that
20 // can support multiple requests, but only one at a time.
21 class PermissionUiSelector {
23 enum class QuietUiReason {
25 kTriggeredByCrowdDeny,
26 kTriggeredDueToAbusiveRequests,
27 kTriggeredDueToAbusiveContent,
28 kServicePredictedVeryUnlikelyGrant,
29 kOnDevicePredictedVeryUnlikelyGrant,
30 kTriggeredDueToDisruptiveBehavior,
33 enum class WarningReason {
40 Decision(absl::optional<QuietUiReason> quiet_ui_reason,
41 absl::optional<WarningReason> warning_reason);
44 Decision(const Decision&);
45 Decision& operator=(const Decision&);
47 static constexpr absl::optional<QuietUiReason> UseNormalUi() {
51 static constexpr absl::optional<WarningReason> ShowNoWarning() {
55 static Decision UseNormalUiAndShowNoWarning();
57 // The reason for showing the quiet UI, or `absl::nullopt` if the normal UI
59 absl::optional<QuietUiReason> quiet_ui_reason;
61 // The reason for printing a warning to the console, or `absl::nullopt` if
62 // no warning should be printed.
63 absl::optional<WarningReason> warning_reason;
66 using DecisionMadeCallback = base::OnceCallback<void(const Decision&)>;
68 virtual ~PermissionUiSelector() {}
70 // Determines whether animations should be suppressed because we're very
71 // confident the user does not want notifications (e.g. they're abusive).
72 static bool ShouldSuppressAnimation(absl::optional<QuietUiReason> reason);
74 // Determines the UI to use for the given |request|, and invokes |callback|
75 // when done, either synchronously or asynchronously. The |callback| is
76 // guaranteed never to be invoked after |this| goes out of scope. Only one
77 // request is supported at a time.
78 virtual void SelectUiToUse(PermissionRequest* request,
79 DecisionMadeCallback callback) = 0;
81 // Cancel the pending request, if any. After this, the |callback| is
82 // guaranteed not to be invoked anymore, and another call to SelectUiToUse()
83 // can be issued. Can be called when there is no pending request which will
85 virtual void Cancel() {}
87 virtual bool IsPermissionRequestSupported(RequestType request_type) = 0;
89 // Will return the selector's discretized prediction value, if any is
90 // applicable to be recorded in UKMs. This is specific only to a selector that
91 // makes use of the Web Permission Predictions Service to make decisions.
92 virtual absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
93 PredictedGrantLikelihoodForUKM();
95 // Will return if the selector's decision was heldback. Currently only the
96 // Web Prediction Service selector supports holdbacks.
97 virtual absl::optional<bool> WasSelectorDecisionHeldback();
100 } // namespace permissions
102 #endif // COMPONENTS_PERMISSIONS_PERMISSION_UI_SELECTOR_H_