permission_prompt.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_PROMPT_H_
  5. #define COMPONENTS_PERMISSIONS_PERMISSION_PROMPT_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "components/permissions/permission_ui_selector.h"
  10. #include "url/gurl.h"
  11. namespace content {
  12. class WebContents;
  13. }
  14. namespace permissions {
  15. enum class PermissionPromptDisposition;
  16. class PermissionRequest;
  17. // This class is the platform-independent interface through which the permission
  18. // request managers (which are one per tab) communicate to the UI surface.
  19. // When the visible tab changes, the UI code must provide an object of this type
  20. // to the manager for the visible tab.
  21. class PermissionPrompt {
  22. public:
  23. // Permission prompt behavior on tab switching.
  24. enum TabSwitchingBehavior {
  25. // The prompt should be kept as-is on tab switching (usually because it's
  26. // part of the containing tab so it will be hidden automatically when
  27. // switching from said tab)
  28. kKeepPromptAlive,
  29. // Destroy the prompt but keep the permission request pending. When the user
  30. // revisits the tab, the permission prompt is re-displayed.
  31. kDestroyPromptButKeepRequestPending,
  32. // Destroy the prompt and treat the permission request as being resolved
  33. // with the PermissionAction::IGNORED result.
  34. kDestroyPromptAndIgnoreRequest,
  35. };
  36. // The delegate will receive events caused by user action which need to
  37. // be persisted in the per-tab UI state.
  38. class Delegate {
  39. public:
  40. virtual ~Delegate() {}
  41. // These pointers should not be stored as the actual request objects may be
  42. // deleted upon navigation and so on.
  43. virtual const std::vector<PermissionRequest*>& Requests() = 0;
  44. // Get the single origin for the current set of requests.
  45. virtual GURL GetRequestingOrigin() const = 0;
  46. // Get the top-level origin currently displayed in the address bar
  47. // associated with the requests.
  48. virtual GURL GetEmbeddingOrigin() const = 0;
  49. virtual void Accept() = 0;
  50. virtual void AcceptThisTime() = 0;
  51. virtual void Deny() = 0;
  52. virtual void Dismiss() = 0;
  53. virtual void Ignore() = 0;
  54. // If |ShouldCurrentRequestUseQuietUI| return true, this will provide a
  55. // reason as to why the quiet UI needs to be used. Returns `absl::nullopt`
  56. // otherwise.
  57. virtual absl::optional<PermissionUiSelector::QuietUiReason>
  58. ReasonForUsingQuietUi() const = 0;
  59. // Notification permission requests might use a quiet UI when the
  60. // "quiet-notification-prompts" feature is enabled. This is done either
  61. // directly by the user in notifications settings, or via automatic logic
  62. // that might trigger the current request to use the quiet UI.
  63. virtual bool ShouldCurrentRequestUseQuietUI() const = 0;
  64. // If the LocationBar is not visible, there is no place to display a quiet
  65. // permission prompt. Abusive prompts will be ignored.
  66. virtual bool ShouldDropCurrentRequestIfCannotShowQuietly() const = 0;
  67. // Whether the current request has been shown to the user at least once.
  68. virtual bool WasCurrentRequestAlreadyDisplayed() = 0;
  69. // Set whether the current request should be dismissed if the current tab is
  70. // closed.
  71. virtual void SetDismissOnTabClose() = 0;
  72. // Set whether the permission prompt bubble was shown for the current
  73. // request.
  74. virtual void SetBubbleShown() = 0;
  75. // Set when the user made any decision for the currentrequest.
  76. virtual void SetDecisionTime() = 0;
  77. // Set when the user made any decision for manage settings.
  78. virtual void SetManageClicked() = 0;
  79. // Set when the user made any decision for clicking on learn more link.
  80. virtual void SetLearnMoreClicked() = 0;
  81. virtual base::WeakPtr<Delegate> GetWeakPtr() = 0;
  82. // Recreate the UI view because the UI flavor needs to change. Returns true
  83. // iff successful.
  84. virtual bool RecreateView() = 0;
  85. };
  86. typedef base::RepeatingCallback<
  87. std::unique_ptr<PermissionPrompt>(content::WebContents*, Delegate*)>
  88. Factory;
  89. // Create and display a platform specific prompt.
  90. static std::unique_ptr<PermissionPrompt> Create(
  91. content::WebContents* web_contents,
  92. Delegate* delegate);
  93. virtual ~PermissionPrompt() {}
  94. // Updates where the prompt should be anchored. ex: fullscreen toggle.
  95. virtual void UpdateAnchor() = 0;
  96. // Get the behavior of this prompt when the user switches away from the
  97. // associated tab.
  98. virtual TabSwitchingBehavior GetTabSwitchingBehavior() = 0;
  99. // Get the type of prompt UI shown for metrics.
  100. virtual PermissionPromptDisposition GetPromptDisposition() const = 0;
  101. };
  102. } // namespace permissions
  103. #endif // COMPONENTS_PERMISSIONS_PERMISSION_PROMPT_H_