permission_request.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2014 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_REQUEST_H_
  5. #define COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "build/build_config.h"
  9. #include "components/content_settings/core/common/content_settings.h"
  10. #include "components/content_settings/core/common/content_settings_types.h"
  11. #include "components/permissions/permission_request_enums.h"
  12. #include "components/permissions/request_type.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "url/gurl.h"
  15. namespace permissions {
  16. enum class RequestType;
  17. // Describes the interface a feature making permission requests should
  18. // implement. A class of this type is registered with the permission request
  19. // manager to receive updates about the result of the permissions request
  20. // from the bubble or infobar. It should live until it is unregistered or until
  21. // RequestFinished is called.
  22. // Note that no particular guarantees are made about what exact UI surface
  23. // is presented to the user. The delegate may be coalesced with other bubble
  24. // requests, or depending on the situation, not shown at all.
  25. class PermissionRequest {
  26. public:
  27. // If `result` is CONTENT_SETTING_ALLOW, the permission was granted by the
  28. // user. If it's CONTENT_SETTING_BLOCK, the permission was blocked by the
  29. // user. If it's CONTENT_SETTING_DEFAULT, the permission was ignored or
  30. // dismissed without an explicit decision. No other ContentSetting value will
  31. // be passed into this callback.
  32. // If `is_one_time` is true, the decision will last until all tabs of
  33. // `requesting_origin_` are closed or navigated away from.
  34. using PermissionDecidedCallback =
  35. base::OnceCallback<void(ContentSetting /*result*/, bool /*is_one_time*/)>;
  36. // `permission_decided_callback` is called when the permission request is
  37. // resolved by the user (see comment on PermissionDecidedCallback above).
  38. // `delete_callback` is called when the permission request is no longer needed
  39. // by the permission system. Therefore, it is safe to delete `this` inside
  40. // `delete_callback`. It will always be called eventually by the permission
  41. // system.
  42. // `delete_callback` may be called before `permission_decided_callback`, for
  43. // example if the tab is closed without user interaction. In this case, the
  44. // javascript promise from the requesting origin will not be resolved.
  45. PermissionRequest(const GURL& requesting_origin,
  46. RequestType request_type,
  47. bool has_gesture,
  48. PermissionDecidedCallback permission_decided_callback,
  49. base::OnceClosure delete_callback);
  50. PermissionRequest(const PermissionRequest&) = delete;
  51. PermissionRequest& operator=(const PermissionRequest&) = delete;
  52. virtual ~PermissionRequest();
  53. GURL requesting_origin() const { return requesting_origin_; }
  54. RequestType request_type() const { return request_type_; }
  55. // Whether |this| and |other_request| are duplicates and therefore don't both
  56. // need to be shown in the UI.
  57. virtual bool IsDuplicateOf(PermissionRequest* other_request) const;
  58. #if BUILDFLAG(IS_ANDROID)
  59. // Returns prompt text appropriate for displaying in an Android dialog.
  60. virtual std::u16string GetDialogMessageText() const;
  61. #endif
  62. #if !BUILDFLAG(IS_ANDROID)
  63. // Returns prompt icon appropriate for displaying on the chip button in the
  64. // location bar.
  65. IconId GetIconForChip();
  66. // Returns prompt icon appropriate for displaying on the quiet chip button in
  67. // the location bar.
  68. IconId GetBlockedIconForChip();
  69. // Returns prompt text appropriate for displaying on the chip button in the
  70. // location bar.
  71. absl::optional<std::u16string> GetRequestChipText() const;
  72. // Returns prompt text appropriate for displaying on the quiet chip button in
  73. // the location bar.
  74. absl::optional<std::u16string> GetQuietChipText() const;
  75. // Returns prompt text appropriate for displaying under the dialog title
  76. // "[domain] wants to:".
  77. virtual std::u16string GetMessageTextFragment() const;
  78. #endif
  79. // Called when the user has granted the requested permission.
  80. // If |is_one_time| is true the permission will last until all tabs of
  81. // |origin| are closed or navigated away from, and then the permission will
  82. // automatically expire after 1 day.
  83. void PermissionGranted(bool is_one_time);
  84. // Called when the user has denied the requested permission.
  85. void PermissionDenied();
  86. // Called when the user has cancelled the permission request. This
  87. // corresponds to a denial, but is segregated in case the context needs to
  88. // be able to distinguish between an active refusal or an implicit refusal.
  89. void Cancelled();
  90. // The UI this request was associated with was answered by the user.
  91. // It is safe for the request to be deleted at this point -- it will receive
  92. // no further message from the permission request system. This method will
  93. // eventually be called on every request which is not unregistered.
  94. // It is ok to call this method without actually resolving the request via
  95. // PermissionGranted(), PermissionDenied() or Canceled(). However, it will not
  96. // resolve the javascript promise from the requesting origin.
  97. void RequestFinished();
  98. // Used to record UMA for whether requests are associated with a user gesture.
  99. // To keep things simple this metric is only recorded for the most popular
  100. // request types.
  101. PermissionRequestGestureType GetGestureType() const;
  102. // Used on Android to determine what Android OS permissions are needed for
  103. // this permission request.
  104. ContentSettingsType GetContentSettingsType() const;
  105. private:
  106. // The origin on whose behalf this permission request is being made.
  107. GURL requesting_origin_;
  108. // The type of this request.
  109. RequestType request_type_;
  110. // Whether the request was associated with a user gesture.
  111. bool has_gesture_;
  112. // Called once a decision is made about the permission.
  113. PermissionDecidedCallback permission_decided_callback_;
  114. // Called when the request is no longer in use so it can be deleted by the
  115. // caller.
  116. base::OnceClosure delete_callback_;
  117. };
  118. } // namespace permissions
  119. #endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_