permission_ui_selector.h 3.7 KB

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