mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 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 | |
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 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 11 | #include "base/callback_forward.h" |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame] | 12 | #include "base/containers/flat_map.h" |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 13 | #include "base/containers/id_map.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" |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 19 | #include "components/permissions/permission_request_id.h" |
Clark DuVall | 732778e | 2020-01-27 18:13:58 | [diff] [blame] | 20 | #include "components/permissions/permission_util.h" |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 21 | #include "content/public/browser/permission_controller_delegate.h" |
Illia Klimov | 15550f75 | 2022-08-11 19:33:10 | [diff] [blame^] | 22 | #include "content/public/browser/permission_result.h" |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 23 | #include "url/origin.h" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 24 | |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 25 | namespace blink { |
| 26 | enum class PermissionType; |
| 27 | } |
| 28 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 29 | namespace content { |
| 30 | class BrowserContext; |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 31 | class RenderFrameHost; |
| 32 | class RenderProcessHost; |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 33 | } |
| 34 | |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 35 | class GeolocationPermissionContextDelegateTests; |
| 36 | class SubscriptionInterceptingPermissionManager; |
| 37 | |
Clark DuVall | 484c256 | 2020-01-23 22:05:09 | [diff] [blame] | 38 | namespace permissions { |
Clark DuVall | a11361ad3 | 2020-02-20 22:14:27 | [diff] [blame] | 39 | class PermissionContextBase; |
timloh | c691180 | 2017-03-01 05:37:03 | [diff] [blame] | 40 | struct PermissionResult; |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 41 | class PermissionManagerTest; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 42 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 43 | class PermissionManager : public KeyedService, |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 44 | public content::PermissionControllerDelegate, |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame] | 45 | public permissions::Observer { |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 46 | public: |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 47 | using PermissionContextMap = |
| 48 | std::unordered_map<ContentSettingsType, |
| 49 | std::unique_ptr<PermissionContextBase>, |
| 50 | ContentSettingsTypeHash>; |
| 51 | PermissionManager(content::BrowserContext* browser_context, |
| 52 | PermissionContextMap permission_contexts); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 53 | |
| 54 | PermissionManager(const PermissionManager&) = delete; |
| 55 | PermissionManager& operator=(const PermissionManager&) = delete; |
| 56 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 57 | ~PermissionManager() override; |
| 58 | |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 59 | // KeyedService implementation. |
| 60 | void Shutdown() override; |
| 61 | |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 62 | PermissionContextBase* GetPermissionContextForTesting( |
| 63 | ContentSettingsType type); |
| 64 | |
| 65 | PermissionContextMap& PermissionContextsForTesting() { |
| 66 | return permission_contexts_; |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | friend class PermissionManagerTest; |
| 71 | friend class ::GeolocationPermissionContextDelegateTests; |
| 72 | friend class ::SubscriptionInterceptingPermissionManager; |
| 73 | |
| 74 | // The `PendingRequestLocalId` will be unique within the `PermissionManager` |
| 75 | // instance, thus within a `BrowserContext`, which overachieves the |
| 76 | // requirement from `PermissionRequestID` that the `RequestLocalId` be unique |
| 77 | // within each frame. |
| 78 | class PendingRequest; |
| 79 | using PendingRequestLocalId = PermissionRequestID::RequestLocalId; |
| 80 | using PendingRequestsMap = |
| 81 | base::IDMap<std::unique_ptr<PendingRequest>, PendingRequestLocalId>; |
| 82 | |
| 83 | class PermissionResponseCallback; |
| 84 | |
| 85 | struct Subscription; |
| 86 | using SubscriptionsMap = |
| 87 | base::IDMap<std::unique_ptr<Subscription>, SubscriptionId>; |
| 88 | using SubscriptionTypeCounts = base::flat_map<ContentSettingsType, size_t>; |
| 89 | |
| 90 | PermissionContextBase* GetPermissionContext(ContentSettingsType type); |
| 91 | |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 92 | // content::PermissionControllerDelegate implementation. |
Balazs Engedy | e30e961 | 2021-04-02 10:37:29 | [diff] [blame] | 93 | void RequestPermission( |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 94 | blink::PermissionType permission, |
Balazs Engedy | e30e961 | 2021-04-02 10:37:29 | [diff] [blame] | 95 | content::RenderFrameHost* render_frame_host, |
| 96 | const GURL& requesting_origin, |
| 97 | bool user_gesture, |
| 98 | base::OnceCallback<void(blink::mojom::PermissionStatus)> callback) |
| 99 | override; |
| 100 | void RequestPermissions( |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 101 | const std::vector<blink::PermissionType>& permissions, |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 102 | content::RenderFrameHost* render_frame_host, |
| 103 | const GURL& requesting_origin, |
benwells | fd2b155 | 2016-07-05 04:26:53 | [diff] [blame] | 104 | bool user_gesture, |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 105 | base::OnceCallback< |
| 106 | void(const std::vector<blink::mojom::PermissionStatus>&)> callback) |
leon.han | 06e5566 | 2016-03-26 17:19:42 | [diff] [blame] | 107 | override; |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 108 | void ResetPermission(blink::PermissionType permission, |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 109 | const GURL& requesting_origin, |
| 110 | const GURL& embedding_origin) override; |
Illia Klimov | 27239edc | 2022-05-11 17:14:59 | [diff] [blame] | 111 | void RequestPermissionsFromCurrentDocument( |
| 112 | const std::vector<blink::PermissionType>& permissions, |
| 113 | content::RenderFrameHost* render_frame_host, |
| 114 | bool user_gesture, |
| 115 | base::OnceCallback< |
| 116 | void(const std::vector<blink::mojom::PermissionStatus>&)> callback) |
| 117 | override; |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 118 | blink::mojom::PermissionStatus GetPermissionStatus( |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 119 | blink::PermissionType permission, |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 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( |
| 123 | blink::PermissionType permission, |
| 124 | const url::Origin& origin) override; |
Illia Klimov | f284284 | 2022-03-22 11:33:39 | [diff] [blame] | 125 | blink::mojom::PermissionStatus GetPermissionStatusForCurrentDocument( |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 126 | blink::PermissionType permission, |
Illia Klimov | f284284 | 2022-03-22 11:33:39 | [diff] [blame] | 127 | content::RenderFrameHost* render_frame_host) override; |
Illia Klimov | 15550f75 | 2022-08-11 19:33:10 | [diff] [blame^] | 128 | content::PermissionResult GetPermissionResultForCurrentDocument( |
| 129 | blink::PermissionType permission, |
| 130 | content::RenderFrameHost* render_frame_host) override; |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 131 | blink::mojom::PermissionStatus GetPermissionStatusForWorker( |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 132 | blink::PermissionType permission, |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 133 | content::RenderProcessHost* render_process_host, |
| 134 | const GURL& worker_origin) override; |
Pavel Feldman | 446a91b | 2020-03-13 17:39:55 | [diff] [blame] | 135 | bool IsPermissionOverridableByDevTools( |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 136 | blink::PermissionType permission, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 137 | const absl::optional<url::Origin>& origin) override; |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 138 | SubscriptionId SubscribePermissionStatusChange( |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 139 | blink::PermissionType permission, |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 140 | content::RenderProcessHost* render_process_host, |
Raymes Khoury | 3ef4f6e | 2018-08-09 09:34:48 | [diff] [blame] | 141 | content::RenderFrameHost* render_frame_host, |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 142 | const GURL& requesting_origin, |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 143 | base::RepeatingCallback<void(blink::mojom::PermissionStatus)> callback) |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 144 | override; |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 145 | void UnsubscribePermissionStatusChange( |
| 146 | SubscriptionId subscription_id) override; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 147 | |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 148 | // Called when a permission was decided for a given PendingRequest. The |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 149 | // PendingRequest is identified by its |request_local_id| and the permission |
| 150 | // is identified by its |permission_id|. If the PendingRequest contains more |
| 151 | // than one permission, it will wait for the remaining permissions to be |
| 152 | // resolved. When all the permissions have been resolved, the PendingRequest's |
| 153 | // callback is run. |
| 154 | void OnPermissionsRequestResponseStatus( |
| 155 | PendingRequestLocalId request_local_id, |
| 156 | int permission_id, |
| 157 | ContentSetting status); |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 158 | |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame] | 159 | // permissions::Observer: |
| 160 | void OnPermissionChanged(const ContentSettingsPattern& primary_pattern, |
| 161 | const ContentSettingsPattern& secondary_pattern, |
Christian Dullweber | 2c4c71d | 2021-10-14 15:07:43 | [diff] [blame] | 162 | ContentSettingsTypeSet content_type_set) override; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 163 | |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 164 | // Only one of |render_process_host| and |render_frame_host| should be set, |
| 165 | // or neither. RenderProcessHost will be inferred from |render_frame_host|. |
Illia Klimov | 15550f75 | 2022-08-11 19:33:10 | [diff] [blame^] | 166 | PermissionResult GetPermissionStatusInternal( |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 167 | ContentSettingsType permission, |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 168 | content::RenderProcessHost* render_process_host, |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 169 | content::RenderFrameHost* render_frame_host, |
| 170 | const GURL& requesting_origin, |
| 171 | const GURL& embedding_origin); |
| 172 | |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 173 | ContentSetting GetPermissionOverrideForDevTools( |
Rohan Pavone | 8180cba6 | 2019-08-26 20:55:09 | [diff] [blame] | 174 | const url::Origin& origin, |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 175 | ContentSettingsType permission); |
| 176 | |
Illia Klimov | 770b145f | 2022-04-20 17:19:09 | [diff] [blame] | 177 | // content::PermissionControllerDelegate implementation. |
| 178 | // For the given |origin|, overrides permissions that belong to |overrides|. |
| 179 | // These permissions are in-sync with the PermissionController. |
| 180 | void SetPermissionOverridesForDevTools( |
| 181 | const absl::optional<url::Origin>& origin, |
| 182 | const PermissionOverrides& overrides) override; |
| 183 | void ResetPermissionOverridesForDevTools() override; |
| 184 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 185 | raw_ptr<content::BrowserContext> browser_context_; |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 186 | |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 187 | PendingRequestsMap pending_requests_; |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 188 | PendingRequestLocalId::Generator request_local_id_generator_; |
| 189 | |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 190 | SubscriptionsMap subscriptions_; |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 191 | SubscriptionId::Generator subscription_id_generator_; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 192 | |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame] | 193 | // Tracks the number of Subscriptions in |subscriptions_| which have a |
| 194 | // certain ContentSettingsType. An entry for a given ContentSettingsType key |
| 195 | // is added on first use and never removed. This is done to utilize the |
| 196 | // flat_map's efficiency in accessing/editing items and minimize the use of |
| 197 | // the unefficient addition/removal of items. |
| 198 | SubscriptionTypeCounts subscription_type_counts_; |
| 199 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 200 | PermissionContextMap permission_contexts_; |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 201 | using ContentSettingsTypeOverrides = |
| 202 | base::flat_map<ContentSettingsType, ContentSetting>; |
| 203 | std::map<url::Origin, ContentSettingsTypeOverrides> |
| 204 | devtools_permission_overrides_; |
Pavel Feldman | 446a91b | 2020-03-13 17:39:55 | [diff] [blame] | 205 | url::Origin devtools_global_overrides_origin_; |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 206 | |
Alexey Baskakov | 386f174 | 2019-09-03 04:08:47 | [diff] [blame] | 207 | bool is_shutting_down_ = false; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 208 | }; |
| 209 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 210 | } // namespace permissions |
| 211 | |
| 212 | #endif // COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_ |