permission_util.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2015 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_UTIL_H_
  5. #define COMPONENTS_PERMISSIONS_PERMISSION_UTIL_H_
  6. #include <string>
  7. #include "build/build_config.h"
  8. #include "components/content_settings/core/common/content_settings.h"
  9. #include "components/content_settings/core/common/content_settings_types.h"
  10. #include "components/permissions/permission_request.h"
  11. #include "third_party/blink/public/mojom/permissions/permission_status.mojom.h"
  12. namespace blink {
  13. enum class PermissionType;
  14. }
  15. namespace content {
  16. class RenderFrameHost;
  17. class RenderProcessHost;
  18. struct PermissionResult;
  19. } // namespace content
  20. class GURL;
  21. namespace permissions {
  22. struct PermissionResult;
  23. // This enum backs a UMA histogram, so it must be treated as append-only.
  24. enum class PermissionAction {
  25. GRANTED = 0,
  26. DENIED = 1,
  27. DISMISSED = 2,
  28. IGNORED = 3,
  29. REVOKED = 4,
  30. GRANTED_ONCE = 5,
  31. // Always keep this at the end.
  32. NUM,
  33. };
  34. // A utility class for permissions.
  35. class PermissionUtil {
  36. public:
  37. PermissionUtil() = delete;
  38. PermissionUtil(const PermissionUtil&) = delete;
  39. PermissionUtil& operator=(const PermissionUtil&) = delete;
  40. // Returns the permission string for the given permission.
  41. static std::string GetPermissionString(ContentSettingsType);
  42. // Returns the gesture type corresponding to whether a permission request is
  43. // made with or without a user gesture.
  44. static PermissionRequestGestureType GetGestureType(bool user_gesture);
  45. // Limited conversion of ContentSettingsType to PermissionType. Returns true
  46. // if the conversion was performed.
  47. // TODO(timloh): Try to remove this function. Mainly we need to work out how
  48. // to remove the usage in PermissionUmaUtil, which uses PermissionType as a
  49. // histogram value to count permission request metrics.
  50. static bool GetPermissionType(ContentSettingsType type,
  51. blink::PermissionType* out);
  52. // Checks whether the given ContentSettingsType is a permission. Use this
  53. // to determine whether a specific ContentSettingsType is supported by the
  54. // PermissionManager.
  55. static bool IsPermission(ContentSettingsType type);
  56. // Checks whether the given ContentSettingsType is a guard content setting,
  57. // meaning it does not support allow setting and toggles between "ask" and
  58. // "block" instead. This is primarily used for chooser-based permissions.
  59. static bool IsGuardContentSetting(ContentSettingsType type);
  60. // Checks whether the given ContentSettingsType supports one time grants.
  61. static bool CanPermissionBeAllowedOnce(ContentSettingsType type);
  62. // Returns the authoritative `embedding origin`, as a GURL, to be used for
  63. // permission decisions in `render_frame_host`.
  64. // TODO(crbug.com/1327384): Remove this method when possible.
  65. static GURL GetLastCommittedOriginAsURL(
  66. content::RenderFrameHost* render_frame_host);
  67. // Helper method to convert `PermissionType` to `ContentSettingType`.
  68. // If `PermissionType` is not supported or found, returns
  69. // ContentSettingsType::DEFAULT.
  70. static ContentSettingsType PermissionTypeToContentSettingTypeSafe(
  71. blink::PermissionType permission);
  72. // Helper method to convert `PermissionType` to `ContentSettingType`.
  73. static ContentSettingsType PermissionTypeToContentSettingType(
  74. blink::PermissionType permission);
  75. // Helper method to convert `ContentSettingType` to `PermissionType`.
  76. static blink::PermissionType ContentSettingTypeToPermissionType(
  77. ContentSettingsType permission);
  78. // Helper method to convert PermissionStatus to ContentSetting.
  79. static ContentSetting PermissionStatusToContentSetting(
  80. blink::mojom::PermissionStatus status);
  81. // Helper methods to convert ContentSetting to PermissionStatus and vice
  82. // versa.
  83. static blink::mojom::PermissionStatus ContentSettingToPermissionStatus(
  84. ContentSetting setting);
  85. static content::PermissionResult ToContentPermissionResult(
  86. PermissionResult result);
  87. static PermissionResult ToPermissionResult(content::PermissionResult result);
  88. // If an iframed document/worker inherits a different StoragePartition from
  89. // its embedder than it would use if it were a main frame, we should block
  90. // undelegated permissions. Because permissions are scoped to BrowserContext
  91. // instead of StoragePartition, without this check the aforementioned iframe
  92. // would be given undelegated permissions if the user had granted its origin
  93. // access when it was loaded as a main frame.
  94. static bool IsPermissionBlockedInPartition(
  95. ContentSettingsType permission,
  96. const GURL& requesting_origin,
  97. content::RenderProcessHost* render_process_host);
  98. // Converts from |url|'s actual origin to the "canonical origin" that should
  99. // be used for the purpose of requesting/storing permissions. For example, the
  100. // origin of the local NTP gets mapped to the Google base URL instead. With
  101. // Permission Delegation it will transform the requesting origin into
  102. // the embedding origin because all permission checks happen on the top level
  103. // origin.
  104. //
  105. // All the public methods below, such as RequestPermission or
  106. // GetPermissionStatus, take the actual origin and do the canonicalization
  107. // internally. You only need to call this directly if you do something else
  108. // with the origin, such as display it in the UI.
  109. static GURL GetCanonicalOrigin(ContentSettingsType permission,
  110. const GURL& requesting_origin,
  111. const GURL& embedding_origin);
  112. };
  113. } // namespace permissions
  114. #endif // COMPONENTS_PERMISSIONS_PERMISSION_UTIL_H_