Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 5 | #ifndef COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_ |
| 6 | #define COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_ |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 7 | |
Lei Zhang | 998100f | 2021-06-25 17:58:19 | [diff] [blame] | 8 | #include <map> |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 9 | #include <unordered_map> |
| 10 | |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame] | 11 | #include "base/containers/flat_map.h" |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 12 | #include "base/containers/id_map.h" |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 13 | #include "base/functional/callback_forward.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 15 | #include "components/content_settings/core/browser/content_settings_observer.h" |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 16 | #include "components/content_settings/core/common/content_settings.h" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 17 | #include "components/keyed_service/core/keyed_service.h" |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame] | 18 | #include "components/permissions/permission_context_base.h" |
Andy Paicu | 1b5011a | 2022-09-20 15:44:58 | [diff] [blame] | 19 | #include "components/permissions/permission_decision_auto_blocker.h" |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 20 | #include "components/permissions/permission_request_id.h" |
Clark DuVall | 732778e | 2020-01-27 18:13:58 | [diff] [blame] | 21 | #include "components/permissions/permission_util.h" |
Yifan Luo | 006519d9 | 2024-08-22 16:11:34 | [diff] [blame] | 22 | #include "content/public/browser/permission_controller.h" |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 23 | #include "content/public/browser/permission_controller_delegate.h" |
Illia Klimov | 15550f75 | 2022-08-11 19:33:10 | [diff] [blame] | 24 | #include "content/public/browser/permission_result.h" |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 25 | #include "url/origin.h" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 26 | |
Illia Klimov | 2c6138b | 2023-08-14 09:39:25 | [diff] [blame] | 27 | using PermissionStatus = blink::mojom::PermissionStatus; |
| 28 | |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 29 | namespace blink { |
| 30 | enum class PermissionType; |
| 31 | } |
| 32 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 33 | namespace content { |
| 34 | class BrowserContext; |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 35 | class RenderFrameHost; |
| 36 | class RenderProcessHost; |
Thomas Nguyen | 583f07e | 2023-07-19 17:45:37 | [diff] [blame] | 37 | class WebContents; |
Andy Paicu | 0a6d4b50 | 2023-08-29 15:13:09 | [diff] [blame] | 38 | struct PermissionRequestDescription; |
Miyoung Shin | df9aee9d | 2024-01-17 16:03:13 | [diff] [blame] | 39 | struct PermissionResult; |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 40 | } |
| 41 | |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 42 | class GeolocationPermissionContextDelegateTests; |
| 43 | class SubscriptionInterceptingPermissionManager; |
| 44 | |
Clark DuVall | 484c256 | 2020-01-23 22:05:09 | [diff] [blame] | 45 | namespace permissions { |
Clark DuVall | a11361ad3 | 2020-02-20 22:14:27 | [diff] [blame] | 46 | class PermissionContextBase; |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 47 | class PermissionManagerTest; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 48 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 49 | class PermissionManager : public KeyedService, |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 50 | public content::PermissionControllerDelegate, |
Andy Paicu | 1b5011a | 2022-09-20 15:44:58 | [diff] [blame] | 51 | public permissions::Observer, |
| 52 | public PermissionDecisionAutoBlocker::Observer { |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 53 | public: |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 54 | using PermissionContextMap = |
| 55 | std::unordered_map<ContentSettingsType, |
| 56 | std::unique_ptr<PermissionContextBase>, |
| 57 | ContentSettingsTypeHash>; |
| 58 | PermissionManager(content::BrowserContext* browser_context, |
| 59 | PermissionContextMap permission_contexts); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 60 | |
| 61 | PermissionManager(const PermissionManager&) = delete; |
| 62 | PermissionManager& operator=(const PermissionManager&) = delete; |
| 63 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 64 | ~PermissionManager() override; |
| 65 | |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 66 | // KeyedService implementation. |
| 67 | void Shutdown() override; |
| 68 | |
Andy Paicu | 1b5011a | 2022-09-20 15:44:58 | [diff] [blame] | 69 | // PermissionDecisionAutoBlocker::Observer |
| 70 | void OnEmbargoStarted(const GURL& origin, |
| 71 | ContentSettingsType content_setting) override; |
| 72 | |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 73 | PermissionContextBase* GetPermissionContextForTesting( |
| 74 | ContentSettingsType type); |
| 75 | |
| 76 | PermissionContextMap& PermissionContextsForTesting() { |
| 77 | return permission_contexts_; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | friend class PermissionManagerTest; |
| 82 | friend class ::GeolocationPermissionContextDelegateTests; |
| 83 | friend class ::SubscriptionInterceptingPermissionManager; |
| 84 | |
| 85 | // The `PendingRequestLocalId` will be unique within the `PermissionManager` |
| 86 | // instance, thus within a `BrowserContext`, which overachieves the |
| 87 | // requirement from `PermissionRequestID` that the `RequestLocalId` be unique |
| 88 | // within each frame. |
| 89 | class PendingRequest; |
| 90 | using PendingRequestLocalId = PermissionRequestID::RequestLocalId; |
| 91 | using PendingRequestsMap = |
| 92 | base::IDMap<std::unique_ptr<PendingRequest>, PendingRequestLocalId>; |
| 93 | |
| 94 | class PermissionResponseCallback; |
| 95 | |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 96 | using SubscriptionTypeCounts = base::flat_map<ContentSettingsType, size_t>; |
| 97 | |
| 98 | PermissionContextBase* GetPermissionContext(ContentSettingsType type); |
| 99 | |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 100 | // content::PermissionControllerDelegate implementation. |
Balazs Engedy | e30e961 | 2021-04-02 10:37:29 | [diff] [blame] | 101 | void RequestPermissions( |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 102 | content::RenderFrameHost* render_frame_host, |
Andy Paicu | 0a6d4b50 | 2023-08-29 15:13:09 | [diff] [blame] | 103 | const content::PermissionRequestDescription& request_description, |
Illia Klimov | 2c6138b | 2023-08-14 09:39:25 | [diff] [blame] | 104 | base::OnceCallback<void(const std::vector<PermissionStatus>&)> callback) |
leon.han | 06e5566 | 2016-03-26 17:19:42 | [diff] [blame] | 105 | override; |
Matt Reichhoff | 56f6228 | 2022-09-12 20:36:00 | [diff] [blame] | 106 | void RequestPermissionsInternal( |
Matt Reichhoff | 56f6228 | 2022-09-12 20:36:00 | [diff] [blame] | 107 | content::RenderFrameHost* render_frame_host, |
Andy Paicu | 0a6d4b50 | 2023-08-29 15:13:09 | [diff] [blame] | 108 | const content::PermissionRequestDescription& request_description, |
Illia Klimov | 2c6138b | 2023-08-14 09:39:25 | [diff] [blame] | 109 | base::OnceCallback<void(const std::vector<PermissionStatus>&)> callback); |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 110 | void ResetPermission(blink::PermissionType permission, |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 111 | const GURL& requesting_origin, |
| 112 | const GURL& embedding_origin) override; |
Illia Klimov | 27239edc | 2022-05-11 17:14:59 | [diff] [blame] | 113 | void RequestPermissionsFromCurrentDocument( |
Illia Klimov | 27239edc | 2022-05-11 17:14:59 | [diff] [blame] | 114 | content::RenderFrameHost* render_frame_host, |
Andy Paicu | 0a6d4b50 | 2023-08-29 15:13:09 | [diff] [blame] | 115 | const content::PermissionRequestDescription& request_description, |
Illia Klimov | 2c6138b | 2023-08-14 09:39:25 | [diff] [blame] | 116 | base::OnceCallback<void(const std::vector<PermissionStatus>&)> callback) |
Illia Klimov | 27239edc | 2022-05-11 17:14:59 | [diff] [blame] | 117 | override; |
Florian Jacky | 8373f04 | 2025-04-16 14:20:53 | [diff] [blame] | 118 | PermissionStatus GetPermissionStatus( |
| 119 | const blink::mojom::PermissionDescriptorPtr& permission_descriptor, |
| 120 | const GURL& requesting_origin, |
| 121 | const GURL& embedding_origin) override; |
Illia Klimov | 15550f75 | 2022-08-11 19:33:10 | [diff] [blame] | 122 | content::PermissionResult GetPermissionResultForOriginWithoutContext( |
Florian Jacky | 8373f04 | 2025-04-16 14:20:53 | [diff] [blame] | 123 | const blink::mojom::PermissionDescriptorPtr& permission_descriptor, |
Christian Dullweber | a475f4a | 2023-08-07 17:04:13 | [diff] [blame] | 124 | const url::Origin& requesting_origin, |
| 125 | const url::Origin& embedding_origin) override; |
Illia Klimov | 2c6138b | 2023-08-14 09:39:25 | [diff] [blame] | 126 | PermissionStatus GetPermissionStatusForCurrentDocument( |
Florian Jacky | 8373f04 | 2025-04-16 14:20:53 | [diff] [blame] | 127 | const blink::mojom::PermissionDescriptorPtr& permission_descriptor, |
Thomas Nguyen | 23593332 | 2024-05-27 14:52:11 | [diff] [blame] | 128 | content::RenderFrameHost* render_frame_host, |
| 129 | bool should_include_device_status) override; |
Illia Klimov | 15550f75 | 2022-08-11 19:33:10 | [diff] [blame] | 130 | content::PermissionResult GetPermissionResultForCurrentDocument( |
Florian Jacky | 8373f04 | 2025-04-16 14:20:53 | [diff] [blame] | 131 | const blink::mojom::PermissionDescriptorPtr& permission_descriptor, |
Thomas Nguyen | 23593332 | 2024-05-27 14:52:11 | [diff] [blame] | 132 | content::RenderFrameHost* render_frame_host, |
| 133 | bool should_include_device_status) override; |
Illia Klimov | 2c6138b | 2023-08-14 09:39:25 | [diff] [blame] | 134 | PermissionStatus GetPermissionStatusForWorker( |
Florian Jacky | 8373f04 | 2025-04-16 14:20:53 | [diff] [blame] | 135 | const blink::mojom::PermissionDescriptorPtr& permission_descriptor, |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 136 | content::RenderProcessHost* render_process_host, |
| 137 | const GURL& worker_origin) override; |
Illia Klimov | 2c6138b | 2023-08-14 09:39:25 | [diff] [blame] | 138 | PermissionStatus GetPermissionStatusForEmbeddedRequester( |
Florian Jacky | 8373f04 | 2025-04-16 14:20:53 | [diff] [blame] | 139 | const blink::mojom::PermissionDescriptorPtr& permission_descriptor, |
Illia Klimov | 20badfdf | 2023-03-16 19:53:10 | [diff] [blame] | 140 | content::RenderFrameHost* render_frame_host, |
| 141 | const url::Origin& requesting_origin) override; |
Illia Klimov | 9af808913 | 2022-10-07 16:42:30 | [diff] [blame] | 142 | bool IsPermissionOverridable( |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 143 | blink::PermissionType permission, |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 144 | const std::optional<url::Origin>& origin) override; |
Yifan Luo | 006519d9 | 2024-08-22 16:11:34 | [diff] [blame] | 145 | void OnPermissionStatusChangeSubscriptionAdded( |
| 146 | content::PermissionController::SubscriptionId subscription_id) override; |
Balazs Engedy | 33b441e | 2023-12-12 18:53:42 | [diff] [blame] | 147 | void UnsubscribeFromPermissionStatusChange( |
Yifan Luo | 006519d9 | 2024-08-22 16:11:34 | [diff] [blame] | 148 | content::PermissionController::SubscriptionId subscription_id) override; |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 149 | std::optional<gfx::Rect> GetExclusionAreaBoundsInScreen( |
Thomas Nguyen | 583f07e | 2023-07-19 17:45:37 | [diff] [blame] | 150 | content::WebContents* web_contents) const override; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 151 | |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 152 | // Called when a permission was decided for a given PendingRequest. The |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 153 | // PendingRequest is identified by its |request_local_id| and the permission |
| 154 | // is identified by its |permission_id|. If the PendingRequest contains more |
| 155 | // than one permission, it will wait for the remaining permissions to be |
| 156 | // resolved. When all the permissions have been resolved, the PendingRequest's |
| 157 | // callback is run. |
| 158 | void OnPermissionsRequestResponseStatus( |
| 159 | PendingRequestLocalId request_local_id, |
| 160 | int permission_id, |
| 161 | ContentSetting status); |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 162 | |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame] | 163 | // permissions::Observer: |
| 164 | void OnPermissionChanged(const ContentSettingsPattern& primary_pattern, |
| 165 | const ContentSettingsPattern& secondary_pattern, |
Christian Dullweber | 2c4c71d | 2021-10-14 15:07:43 | [diff] [blame] | 166 | ContentSettingsTypeSet content_type_set) override; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 167 | |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 168 | // Only one of |render_process_host| and |render_frame_host| should be set, |
| 169 | // or neither. RenderProcessHost will be inferred from |render_frame_host|. |
Illia Klimov | 2c6138b | 2023-08-14 09:39:25 | [diff] [blame] | 170 | content::PermissionResult GetPermissionStatusInternal( |
Florian Jacky | 8373f04 | 2025-04-16 14:20:53 | [diff] [blame] | 171 | const blink::mojom::PermissionDescriptorPtr& permission_descriptor, |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 172 | content::RenderProcessHost* render_process_host, |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 173 | content::RenderFrameHost* render_frame_host, |
| 174 | const GURL& requesting_origin, |
Andy Paicu | 9d70da4 | 2024-05-10 22:24:39 | [diff] [blame] | 175 | const GURL& embedding_origin, |
| 176 | bool should_include_device_status); |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 177 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 178 | raw_ptr<content::BrowserContext> browser_context_; |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 179 | |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 180 | PendingRequestsMap pending_requests_; |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 181 | PendingRequestLocalId::Generator request_local_id_generator_; |
| 182 | |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame] | 183 | // Tracks the number of Subscriptions in |subscriptions_| which have a |
| 184 | // certain ContentSettingsType. An entry for a given ContentSettingsType key |
| 185 | // is added on first use and never removed. This is done to utilize the |
| 186 | // flat_map's efficiency in accessing/editing items and minimize the use of |
| 187 | // the unefficient addition/removal of items. |
| 188 | SubscriptionTypeCounts subscription_type_counts_; |
| 189 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 190 | PermissionContextMap permission_contexts_; |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 191 | |
Alexey Baskakov | 386f174 | 2019-09-03 04:08:47 | [diff] [blame] | 192 | bool is_shutting_down_ = false; |
Thomas Nguyen | 3d9dd37 | 2024-09-23 13:44:48 | [diff] [blame] | 193 | |
| 194 | base::WeakPtrFactory<PermissionManager> weak_factory_{this}; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 195 | }; |
| 196 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 197 | } // namespace permissions |
| 198 | |
| 199 | #endif // COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_ |