permission_context_base.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_CONTEXT_BASE_H_
  5. #define COMPONENTS_PERMISSIONS_PERMISSION_CONTEXT_BASE_H_
  6. #include <memory>
  7. #include <unordered_map>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "build/build_config.h"
  13. #include "components/content_settings/core/browser/content_settings_observer.h"
  14. #include "components/content_settings/core/common/content_settings.h"
  15. #include "components/content_settings/core/common/content_settings_types.h"
  16. #include "components/keyed_service/core/keyed_service.h"
  17. #include "components/permissions/permission_request.h"
  18. #include "components/permissions/permission_result.h"
  19. #include "third_party/blink/public/mojom/permissions_policy/permissions_policy_feature.mojom-forward.h"
  20. class GURL;
  21. namespace permissions {
  22. class PermissionRequestID;
  23. }
  24. namespace content {
  25. class BrowserContext;
  26. class RenderFrameHost;
  27. class WebContents;
  28. } // namespace content
  29. namespace permissions {
  30. class Observer : public base::CheckedObserver {
  31. public:
  32. virtual void OnPermissionChanged(
  33. const ContentSettingsPattern& primary_pattern,
  34. const ContentSettingsPattern& secondary_pattern,
  35. ContentSettingsTypeSet content_type_set) = 0;
  36. };
  37. using BrowserPermissionCallback = base::OnceCallback<void(ContentSetting)>;
  38. // This base class contains common operations for granting permissions.
  39. // It offers the following functionality:
  40. // - Creates a permission request when needed.
  41. // - If accepted/denied the permission is saved in content settings for
  42. // future uses (for the domain that requested it).
  43. // - If dismissed the permission is not saved but it's considered denied for
  44. // this one request
  45. // - In any case the BrowserPermissionCallback is executed once a decision
  46. // about the permission is made by the user.
  47. // The bare minimum you need to create a new permission request is
  48. // - Define your new permission in the ContentSettingsType enum.
  49. // - Create a class that inherits from PermissionContextBase and passes the
  50. // new permission.
  51. // - Edit the PermissionRequest methods to add the new text.
  52. // - Hit several asserts for the missing plumbing and fix them :)
  53. // After this you can override several other methods to customize behavior,
  54. // in particular it is advised to override UpdateTabContext in order to manage
  55. // the permission from the omnibox.
  56. // It is mandatory to override IsRestrictedToSecureOrigin.
  57. // See midi_permission_context.h/cc or push_permission_context.cc/h for some
  58. // examples.
  59. class PermissionContextBase : public KeyedService,
  60. public content_settings::Observer {
  61. public:
  62. PermissionContextBase(
  63. content::BrowserContext* browser_context,
  64. ContentSettingsType content_settings_type,
  65. blink::mojom::PermissionsPolicyFeature permissions_policy_feature);
  66. ~PermissionContextBase() override;
  67. // A field trial used to enable the global permissions kill switch.
  68. // This is public so permissions that don't yet inherit from
  69. // PermissionContextBase can use it.
  70. static const char kPermissionsKillSwitchFieldStudy[];
  71. // The field trial param to enable the global permissions kill switch.
  72. // This is public so permissions that don't yet inherit from
  73. // PermissionContextBase can use it.
  74. static const char kPermissionsKillSwitchBlockedValue[];
  75. // |callback| is called upon resolution of the request, but not if a prompt
  76. // is shown and ignored.
  77. virtual void RequestPermission(const PermissionRequestID& id,
  78. const GURL& requesting_frame,
  79. bool user_gesture,
  80. BrowserPermissionCallback callback);
  81. // Returns whether the permission has been granted, denied etc.
  82. // |render_frame_host| may be nullptr if the call is coming from a context
  83. // other than a specific frame.
  84. PermissionResult GetPermissionStatus(
  85. content::RenderFrameHost* render_frame_host,
  86. const GURL& requesting_origin,
  87. const GURL& embedding_origin) const;
  88. // Returns whether the permission is usable by requesting/embedding origins.
  89. bool IsPermissionAvailableToOrigins(const GURL& requesting_origin,
  90. const GURL& embedding_origin) const;
  91. // Update |result| with any modifications based on the device state. For
  92. // example, if |result| is ALLOW but Chrome does not have the relevant
  93. // permission at the device level, but will prompt the user, return ASK.
  94. virtual PermissionResult UpdatePermissionStatusWithDeviceStatus(
  95. PermissionResult result,
  96. const GURL& requesting_origin,
  97. const GURL& embedding_origin) const;
  98. // Resets the permission to its default value.
  99. virtual void ResetPermission(const GURL& requesting_origin,
  100. const GURL& embedding_origin);
  101. // Whether the kill switch has been enabled for this permission.
  102. // public for permissions that do not use RequestPermission, like
  103. // camera and microphone, and for testing.
  104. bool IsPermissionKillSwitchOn() const;
  105. void AddObserver(permissions::Observer* permission_observer);
  106. void RemoveObserver(permissions::Observer* permission_observer);
  107. protected:
  108. virtual ContentSetting GetPermissionStatusInternal(
  109. content::RenderFrameHost* render_frame_host,
  110. const GURL& requesting_origin,
  111. const GURL& embedding_origin) const;
  112. // Called if generic checks (existing content setting, embargo, etc.) fail to
  113. // resolve a permission request. The default implementation prompts the user.
  114. virtual void DecidePermission(const PermissionRequestID& id,
  115. const GURL& requesting_origin,
  116. const GURL& embedding_origin,
  117. bool user_gesture,
  118. BrowserPermissionCallback callback);
  119. // Updates stored content setting if persist is set, updates tab indicators
  120. // and runs the callback to finish the request.
  121. virtual void NotifyPermissionSet(const PermissionRequestID& id,
  122. const GURL& requesting_origin,
  123. const GURL& embedding_origin,
  124. BrowserPermissionCallback callback,
  125. bool persist,
  126. ContentSetting content_setting,
  127. bool is_one_time);
  128. // Implementors can override this method to update the icons on the
  129. // url bar with the result of the new permission.
  130. virtual void UpdateTabContext(const PermissionRequestID& id,
  131. const GURL& requesting_origin,
  132. bool allowed) {}
  133. // Returns the browser context associated with this permission context.
  134. content::BrowserContext* browser_context() const;
  135. // Store the decided permission as a content setting.
  136. // virtual since the permission might be stored with different restrictions
  137. // (for example for desktop notifications).
  138. virtual void UpdateContentSetting(const GURL& requesting_origin,
  139. const GURL& embedding_origin,
  140. ContentSetting content_setting,
  141. bool is_one_time);
  142. // Whether the permission should be restricted to secure origins.
  143. virtual bool IsRestrictedToSecureOrigins() const = 0;
  144. // Called by PermissionDecided when the user has made a permission decision.
  145. // Subclasses may override this method to perform context-specific logic
  146. // before the content setting is changed and the permission callback is run.
  147. virtual void UserMadePermissionDecision(const PermissionRequestID& id,
  148. const GURL& requesting_origin,
  149. const GURL& embedding_origin,
  150. ContentSetting content_setting);
  151. // content_settings::Observer:
  152. void OnContentSettingChanged(
  153. const ContentSettingsPattern& primary_pattern,
  154. const ContentSettingsPattern& secondary_pattern,
  155. ContentSettingsTypeSet content_type_set) override;
  156. // Implementors can override this method to use a different PermissionRequest
  157. // implementation.
  158. virtual std::unique_ptr<PermissionRequest> CreatePermissionRequest(
  159. const GURL& request_origin,
  160. ContentSettingsType content_settings_type,
  161. bool has_gesture,
  162. content::WebContents* web_contents,
  163. PermissionRequest::PermissionDecidedCallback permission_decided_callback,
  164. base::OnceClosure delete_callback) const;
  165. ContentSettingsType content_settings_type() const {
  166. return content_settings_type_;
  167. }
  168. base::ObserverList<permissions::Observer> permission_observers_;
  169. // Set by subclasses to inform the base class that they will handle adding
  170. // and removing themselves as observers to the HostContentSettingsMap.
  171. bool content_setting_observer_registered_by_subclass_ = false;
  172. private:
  173. friend class PermissionContextBaseTests;
  174. bool PermissionAllowedByPermissionsPolicy(
  175. content::RenderFrameHost* rfh) const;
  176. // Called when a request is no longer used so it can be cleaned up.
  177. void CleanUpRequest(const PermissionRequestID& id);
  178. // This is the callback for PermissionRequest and is called once the user
  179. // allows/blocks/dismisses a permission prompt.
  180. void PermissionDecided(const PermissionRequestID& id,
  181. const GURL& requesting_origin,
  182. const GURL& embedding_origin,
  183. BrowserPermissionCallback callback,
  184. ContentSetting content_setting,
  185. bool is_one_time);
  186. raw_ptr<content::BrowserContext> browser_context_;
  187. const ContentSettingsType content_settings_type_;
  188. const blink::mojom::PermissionsPolicyFeature permissions_policy_feature_;
  189. std::unordered_map<std::string, std::unique_ptr<PermissionRequest>>
  190. pending_requests_;
  191. // Must be the last member, to ensure that it will be
  192. // destroyed first, which will invalidate weak pointers
  193. base::WeakPtrFactory<PermissionContextBase> weak_factory_{this};
  194. };
  195. } // namespace permissions
  196. #endif // COMPONENTS_PERMISSIONS_PERMISSION_CONTEXT_BASE_H_