1 // Copyright 2019 The Chromium Authors. All rights reserved.
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/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 kPredictedVeryUnlikelyGrant,
31 enum class WarningReason {
37 Decision(absl::optional<QuietUiReason> quiet_ui_reason,
38 absl::optional<WarningReason> warning_reason);
41 Decision(const Decision&);
42 Decision& operator=(const Decision&);
44 static constexpr absl::optional<QuietUiReason> UseNormalUi() {
48 static constexpr absl::optional<WarningReason> ShowNoWarning() {
52 static Decision UseNormalUiAndShowNoWarning();
54 // The reason for showing the quiet UI, or `absl::nullopt` if the normal UI
56 absl::optional<QuietUiReason> quiet_ui_reason;
58 // The reason for printing a warning to the console, or `absl::nullopt` if
59 // no warning should be printed.
60 absl::optional<WarningReason> warning_reason;
63 using DecisionMadeCallback = base::OnceCallback<void(const Decision&)>;
65 virtual ~PermissionUiSelector() {}
67 // Determines whether animations should be suppressed because we're very
68 // confident the user does not want notifications (e.g. they're abusive).
69 static bool ShouldSuppressAnimation(absl::optional<QuietUiReason> reason);
71 // Determines the UI to use for the given |request|, and invokes |callback|
72 // when done, either synchronously or asynchronously. The |callback| is
73 // guaranteed never to be invoked after |this| goes out of scope. Only one
74 // request is supported at a time.
75 virtual void SelectUiToUse(PermissionRequest* request,
76 DecisionMadeCallback callback) = 0;
78 // Cancel the pending request, if any. After this, the |callback| is
79 // guaranteed not to be invoked anymore, and another call to SelectUiToUse()
80 // can be issued. Can be called when there is no pending request which will
82 virtual void Cancel() {}
84 virtual bool IsPermissionRequestSupported(RequestType request_type) = 0;
86 // Will return the selector's discretized prediction value, if any is
87 // applicable to be recorded in UKMs. This is specific only to a selector that
88 // makes use of the Web Permission Predictions Service to make decisions.
89 virtual absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
90 PredictedGrantLikelihoodForUKM();
93 } // namespace permissions
95 #endif // COMPONENTS_PERMISSIONS_PERMISSION_UI_SELECTOR_H_