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