Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / components / permissions / permission_ui_selector.h
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.
4
5 #ifndef COMPONENTS_PERMISSIONS_PERMISSION_UI_SELECTOR_H_
6 #define COMPONENTS_PERMISSIONS_PERMISSION_UI_SELECTOR_H_
7
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"
12
13 namespace permissions {
14
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.
18 //
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 {
22  public:
23   enum class QuietUiReason {
24     kEnabledInPrefs,
25     kTriggeredByCrowdDeny,
26     kTriggeredDueToAbusiveRequests,
27     kTriggeredDueToAbusiveContent,
28     kPredictedVeryUnlikelyGrant,
29   };
30
31   enum class WarningReason {
32     kAbusiveRequests,
33     kAbusiveContent,
34   };
35
36   struct Decision {
37     Decision(absl::optional<QuietUiReason> quiet_ui_reason,
38              absl::optional<WarningReason> warning_reason);
39     ~Decision();
40
41     Decision(const Decision&);
42     Decision& operator=(const Decision&);
43
44     static constexpr absl::optional<QuietUiReason> UseNormalUi() {
45       return absl::nullopt;
46     }
47
48     static constexpr absl::optional<WarningReason> ShowNoWarning() {
49       return absl::nullopt;
50     }
51
52     static Decision UseNormalUiAndShowNoWarning();
53
54     // The reason for showing the quiet UI, or `absl::nullopt` if the normal UI
55     // should be used.
56     absl::optional<QuietUiReason> quiet_ui_reason;
57
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;
61   };
62
63   using DecisionMadeCallback = base::OnceCallback<void(const Decision&)>;
64
65   virtual ~PermissionUiSelector() {}
66
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);
70
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;
77
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
81   // simply be a no-op.
82   virtual void Cancel() {}
83
84   virtual bool IsPermissionRequestSupported(RequestType request_type) = 0;
85
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();
91 };
92
93 }  // namespace permissions
94
95 #endif  // COMPONENTS_PERMISSIONS_PERMISSION_UI_SELECTOR_H_