Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 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 | |
Clark DuVall | 5ca4ae1 | 2020-02-19 22:25:27 | [diff] [blame] | 5 | #ifndef COMPONENTS_PERMISSIONS_NOTIFICATION_PERMISSION_UI_SELECTOR_H_ |
| 6 | #define COMPONENTS_PERMISSIONS_NOTIFICATION_PERMISSION_UI_SELECTOR_H_ |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 7 | |
Balazs Engedy | 825e89b | 2020-05-28 12:48:17 | [diff] [blame] | 8 | #include "base/callback_forward.h" |
| 9 | #include "base/optional.h" |
Clark DuVall | 484c256 | 2020-01-23 22:05:09 | [diff] [blame] | 10 | #include "components/permissions/permission_request.h" |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 11 | |
Clark DuVall | 5ca4ae1 | 2020-02-19 22:25:27 | [diff] [blame] | 12 | namespace permissions { |
| 13 | |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 14 | // The interface for implementations that decide if the quiet prompt UI should |
Balazs Engedy | 825e89b | 2020-05-28 12:48:17 | [diff] [blame] | 15 | // be used to display a notification permission |request|, whether a warning |
| 16 | // should be printed to the Dev Tools console, and the reasons for both. |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 17 | // |
| 18 | // Implementations of interface are expected to have long-lived instances that |
| 19 | // can support multiple requests, but only one at a time. |
| 20 | class NotificationPermissionUiSelector { |
| 21 | public: |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 22 | enum class QuietUiReason { |
| 23 | kEnabledInPrefs, |
| 24 | kTriggeredByCrowdDeny, |
Balazs Engedy | b968c19 | 2020-05-14 15:20:19 | [diff] [blame] | 25 | kTriggeredDueToAbusiveRequests, |
Illia Klimov | f786352 | 2020-07-03 14:54:27 | [diff] [blame] | 26 | kTriggeredDueToAbusiveContent, |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 27 | }; |
| 28 | |
Balazs Engedy | 825e89b | 2020-05-28 12:48:17 | [diff] [blame] | 29 | enum class WarningReason { |
| 30 | kAbusiveRequests, |
Illia Klimov | f786352 | 2020-07-03 14:54:27 | [diff] [blame] | 31 | kAbusiveContent, |
Balazs Engedy | 825e89b | 2020-05-28 12:48:17 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | struct Decision { |
| 35 | Decision(base::Optional<QuietUiReason> quiet_ui_reason, |
| 36 | base::Optional<WarningReason> warning_reason); |
| 37 | ~Decision(); |
| 38 | |
| 39 | Decision(const Decision&); |
| 40 | Decision& operator=(const Decision&); |
| 41 | |
| 42 | static constexpr base::Optional<QuietUiReason> UseNormalUi() { |
| 43 | return base::nullopt; |
| 44 | } |
| 45 | |
| 46 | static constexpr base::Optional<WarningReason> ShowNoWarning() { |
| 47 | return base::nullopt; |
| 48 | } |
| 49 | |
| 50 | static Decision UseNormalUiAndShowNoWarning(); |
| 51 | |
| 52 | // The reason for showing the quiet UI, or `base::nullopt` if the normal UI |
| 53 | // should be used. |
| 54 | base::Optional<QuietUiReason> quiet_ui_reason; |
| 55 | |
| 56 | // The reason for printing a warning to the console, or `base::nullopt` if |
| 57 | // no warning should be printed. |
| 58 | base::Optional<WarningReason> warning_reason; |
| 59 | }; |
| 60 | |
| 61 | using DecisionMadeCallback = base::OnceCallback<void(const Decision&)>; |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 62 | |
| 63 | virtual ~NotificationPermissionUiSelector() {} |
| 64 | |
Bret Sepulveda | e710f89 | 2020-10-08 10:40:50 | [diff] [blame^] | 65 | // Determines whether animations should be suppressed because we're very |
| 66 | // confident the user does not want notifications (e.g. they're abusive). |
| 67 | static bool ShouldSuppressAnimation(QuietUiReason reason); |
| 68 | |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 69 | // Determines the UI to use for the given |request|, and invokes |callback| |
Darwin Huang | 1620c5f | 2020-01-30 20:34:06 | [diff] [blame] | 70 | // when done, either synchronously or asynchronously. The |callback| is |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 71 | // guaranteed never to be invoked after |this| goes out of scope. Only one |
| 72 | // request is supported at a time. |
Clark DuVall | 5ca4ae1 | 2020-02-19 22:25:27 | [diff] [blame] | 73 | virtual void SelectUiToUse(PermissionRequest* request, |
Balazs Engedy | e9934acb | 2019-12-04 22:15:07 | [diff] [blame] | 74 | DecisionMadeCallback callback) = 0; |
| 75 | |
| 76 | // Cancel the pending request, if any. After this, the |callback| is |
| 77 | // guaranteed not to be invoked anymore, and another call to SelectUiToUse() |
| 78 | // can be issued. |
| 79 | virtual void Cancel() {} |
| 80 | }; |
| 81 | |
Clark DuVall | 5ca4ae1 | 2020-02-19 22:25:27 | [diff] [blame] | 82 | } // namespace permissions |
| 83 | |
| 84 | #endif // COMPONENTS_PERMISSIONS_NOTIFICATION_PERMISSION_UI_SELECTOR_H_ |