permissions_client.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2020 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_PERMISSIONS_CLIENT_H_
  5. #define COMPONENTS_PERMISSIONS_PERMISSIONS_CLIENT_H_
  6. #include "base/callback_forward.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "build/build_config.h"
  9. #include "build/chromeos_buildflags.h"
  10. #include "components/content_settings/core/common/content_settings_types.h"
  11. #include "components/permissions/permission_prompt.h"
  12. #include "components/permissions/permission_ui_selector.h"
  13. #include "components/permissions/permission_uma_util.h"
  14. #include "components/permissions/permission_util.h"
  15. #include "components/permissions/request_type.h"
  16. #include "services/metrics/public/cpp/ukm_source_id.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. #include "url/origin.h"
  19. #if BUILDFLAG(IS_ANDROID)
  20. #include "components/messages/android/message_wrapper.h"
  21. #endif
  22. class GURL;
  23. class HostContentSettingsMap;
  24. namespace content {
  25. class BrowserContext;
  26. class WebContents;
  27. } // namespace content
  28. namespace content_settings {
  29. class CookieSettings;
  30. }
  31. namespace infobars {
  32. class InfoBar;
  33. class InfoBarManager;
  34. } // namespace infobars
  35. namespace permissions {
  36. class ObjectPermissionContextBase;
  37. class PermissionActionsHistory;
  38. class PermissionDecisionAutoBlocker;
  39. class PermissionPromptAndroid;
  40. // Interface to be implemented by permissions embedder to access embedder
  41. // specific logic.
  42. class PermissionsClient {
  43. public:
  44. #if BUILDFLAG(IS_ANDROID)
  45. class PermissionMessageDelegate {
  46. public:
  47. virtual ~PermissionMessageDelegate() = default;
  48. };
  49. #endif
  50. PermissionsClient(const PermissionsClient&) = delete;
  51. PermissionsClient& operator=(const PermissionsClient&) = delete;
  52. PermissionsClient();
  53. virtual ~PermissionsClient();
  54. // Return the permissions client.
  55. static PermissionsClient* Get();
  56. // Retrieves the HostContentSettingsMap for this context. The returned pointer
  57. // has the same lifetime as |browser_context|.
  58. virtual HostContentSettingsMap* GetSettingsMap(
  59. content::BrowserContext* browser_context) = 0;
  60. // Retrieves the CookieSettings for this context.
  61. virtual scoped_refptr<content_settings::CookieSettings> GetCookieSettings(
  62. content::BrowserContext* browser_context) = 0;
  63. // Retrieves the subresource filter activation from browser website settings.
  64. virtual bool IsSubresourceFilterActivated(
  65. content::BrowserContext* browser_context,
  66. const GURL& url) = 0;
  67. virtual PermissionActionsHistory* GetPermissionActionsHistory(
  68. content::BrowserContext* browser_context) = 0;
  69. // Retrieves the PermissionDecisionAutoBlocker for this context. The returned
  70. // pointer has the same lifetime as |browser_context|.
  71. virtual PermissionDecisionAutoBlocker* GetPermissionDecisionAutoBlocker(
  72. content::BrowserContext* browser_context) = 0;
  73. // Gets the ObjectPermissionContextBase for the given type and context, which
  74. // must be a
  75. // *_CHOOSER_DATA value. May return null if the context does not exist.
  76. virtual ObjectPermissionContextBase* GetChooserContext(
  77. content::BrowserContext* browser_context,
  78. ContentSettingsType type) = 0;
  79. // Gets the embedder defined engagement score for this |origin|.
  80. virtual double GetSiteEngagementScore(
  81. content::BrowserContext* browser_context,
  82. const GURL& origin);
  83. // Determines whether some origins are "important". |origins| is an in-out
  84. // param that passes in the list of origins which need judgment as the first
  85. // item in each pair, and the determination of importance should be stored in
  86. // the second item in the pair (true meaning important).
  87. virtual void AreSitesImportant(
  88. content::BrowserContext* browser_context,
  89. std::vector<std::pair<url::Origin, bool>>* origins);
  90. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS_ASH)
  91. // Returns whether cookie deletion is allowed for |browser_context| and
  92. // |origin|.
  93. // TODO(crbug.com/1081944): Remove this method and all code depending on it
  94. // when a proper fix is landed.
  95. virtual bool IsCookieDeletionDisabled(
  96. content::BrowserContext* browser_context,
  97. const GURL& origin);
  98. #endif
  99. // Retrieves the ukm::SourceId (if any) associated with this |browser_context|
  100. // and |web_contents|. |web_contents| may be null. |callback| will be called
  101. // with the result, and may be run synchronously if the result is available
  102. // immediately.
  103. using GetUkmSourceIdCallback =
  104. base::OnceCallback<void(absl::optional<ukm::SourceId>)>;
  105. virtual void GetUkmSourceId(content::BrowserContext* browser_context,
  106. content::WebContents* web_contents,
  107. const GURL& requesting_origin,
  108. GetUkmSourceIdCallback callback);
  109. // Returns the icon ID that should be used for permissions UI for |type|. If
  110. // the embedder returns an empty IconId, the default icon for |type| will be
  111. // used.
  112. virtual IconId GetOverrideIconId(RequestType request_type);
  113. // Allows the embedder to provide a list of selectors for choosing the UI to
  114. // use for permission requests. If the embedder returns an empty list, the
  115. // normal UI will be used always. Then for each request, if none of the
  116. // returned selectors prescribe the quiet UI, the normal UI will be used.
  117. // Otherwise the quiet UI will be used. Selectors at lower indices have higher
  118. // priority when determining the quiet UI flavor.
  119. virtual std::vector<std::unique_ptr<PermissionUiSelector>>
  120. CreatePermissionUiSelectors(content::BrowserContext* browser_context);
  121. using QuietUiReason = PermissionUiSelector::QuietUiReason;
  122. // Called for each request type when a permission prompt is resolved.
  123. virtual void OnPromptResolved(
  124. content::BrowserContext* browser_context,
  125. RequestType request_type,
  126. PermissionAction action,
  127. const GURL& origin,
  128. PermissionPromptDisposition prompt_disposition,
  129. PermissionPromptDispositionReason prompt_disposition_reason,
  130. PermissionRequestGestureType gesture_type,
  131. absl::optional<QuietUiReason> quiet_ui_reason);
  132. // Returns true if user has 3 consecutive notifications permission denies,
  133. // returns false otherwise.
  134. // Returns absl::nullopt if the user is not in the adoptive activation quiet
  135. // ui dry run experiment group.
  136. virtual absl::optional<bool> HadThreeConsecutiveNotificationPermissionDenies(
  137. content::BrowserContext* browser_context);
  138. // Returns whether the |permission| has already been auto-revoked due to abuse
  139. // at least once for the given |origin|. Returns `nullopt` if permission
  140. // auto-revocation is not supported for a given permission type.
  141. virtual absl::optional<bool> HasPreviouslyAutoRevokedPermission(
  142. content::BrowserContext* browser_context,
  143. const GURL& origin,
  144. ContentSettingsType permission);
  145. // If the embedder returns an origin here, any requests matching that origin
  146. // will be approved. Requests that do not match the returned origin will
  147. // immediately be finished without granting/denying the permission.
  148. virtual absl::optional<url::Origin> GetAutoApprovalOrigin();
  149. // Allows the embedder to bypass checking the embedding origin when performing
  150. // permission availability checks. This is used for example when a permission
  151. // should only be available on secure origins. Return true to bypass embedding
  152. // origin checks for the passed in origins.
  153. virtual bool CanBypassEmbeddingOriginCheck(const GURL& requesting_origin,
  154. const GURL& embedding_origin);
  155. // Allows embedder to override the canonical origin for a permission request.
  156. // This is the origin that will be used for requesting/storing/displaying
  157. // permissions.
  158. virtual absl::optional<GURL> OverrideCanonicalOrigin(
  159. const GURL& requesting_origin,
  160. const GURL& embedding_origin);
  161. // Checks if `requesting_origin` and `embedding_origin` are the new tab page
  162. // origins.
  163. virtual bool DoURLsMatchNewTabPage(const GURL& requesting_origin,
  164. const GURL& embedding_origin);
  165. #if BUILDFLAG(IS_ANDROID)
  166. // Returns whether the given origin matches the default search engine (DSE)
  167. // origin.
  168. virtual bool IsDseOrigin(content::BrowserContext* browser_context,
  169. const url::Origin& origin);
  170. // Retrieves the InfoBarManager for the web contents. The returned
  171. // pointer has the same lifetime as |web_contents|.
  172. virtual infobars::InfoBarManager* GetInfoBarManager(
  173. content::WebContents* web_contents);
  174. // Allows the embedder to create an info bar to use as the permission prompt.
  175. // Might return null based on internal logic (e.g. |type| does not support
  176. // infobar permission prompts). The returned infobar is owned by the info bar
  177. // manager.
  178. virtual infobars::InfoBar* MaybeCreateInfoBar(
  179. content::WebContents* web_contents,
  180. ContentSettingsType type,
  181. base::WeakPtr<PermissionPromptAndroid> prompt);
  182. // Allows the embedder to create a message UI to use as the permission prompt.
  183. // Returns the pointer to the message UI if the message UI is successfully
  184. // created, nullptr otherwise, e.g. if the messages-prompt is not
  185. // supported for `type`.
  186. virtual std::unique_ptr<PermissionMessageDelegate> MaybeCreateMessageUI(
  187. content::WebContents* web_contents,
  188. ContentSettingsType type,
  189. base::WeakPtr<PermissionPromptAndroid> prompt);
  190. using PermissionsUpdatedCallback = base::OnceCallback<void(bool)>;
  191. // Prompts the user to accept system permissions for |content_settings_types|,
  192. // after they've already been denied. In Chrome, this shows an infobar.
  193. // |callback| will be run with |true| for success and |false| otherwise.
  194. virtual void RepromptForAndroidPermissions(
  195. content::WebContents* web_contents,
  196. const std::vector<ContentSettingsType>& content_settings_types,
  197. PermissionsUpdatedCallback callback);
  198. // Converts the given chromium |resource_id| (e.g. IDR_INFOBAR_TRANSLATE) to
  199. // an Android drawable resource ID. Returns 0 if a mapping wasn't found.
  200. virtual int MapToJavaDrawableId(int resource_id);
  201. #else
  202. // Creates a permission prompt.
  203. // TODO(crbug.com/1025609): Move the desktop permission prompt implementation
  204. // into //components/permissions and remove this.
  205. virtual std::unique_ptr<PermissionPrompt> CreatePrompt(
  206. content::WebContents* web_contents,
  207. PermissionPrompt::Delegate* delegate);
  208. #endif
  209. };
  210. } // namespace permissions
  211. #endif // COMPONENTS_PERMISSIONS_PERMISSIONS_CLIENT_H_