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 | |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 8 | #include <unordered_map> |
| 9 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 10 | #include "base/callback_forward.h" |
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" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 13 | #include "base/macros.h" |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 14 | #include "components/content_settings/core/browser/content_settings_observer.h" |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 15 | #include "components/content_settings/core/common/content_settings.h" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 16 | #include "components/keyed_service/core/keyed_service.h" |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame^] | 17 | #include "components/permissions/permission_context_base.h" |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 18 | #include "components/permissions/permission_request_id.h" |
Clark DuVall | 732778e | 2020-01-27 18:13:58 | [diff] [blame] | 19 | #include "components/permissions/permission_util.h" |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 20 | #include "content/public/browser/permission_controller_delegate.h" |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 21 | #include "content/public/browser/permission_type.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 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 24 | namespace content { |
| 25 | class BrowserContext; |
| 26 | } |
| 27 | |
Clark DuVall | 484c256 | 2020-01-23 22:05:09 | [diff] [blame] | 28 | namespace permissions { |
Clark DuVall | a11361ad3 | 2020-02-20 22:14:27 | [diff] [blame] | 29 | class PermissionContextBase; |
timloh | c691180 | 2017-03-01 05:37:03 | [diff] [blame] | 30 | struct PermissionResult; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 31 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 32 | class PermissionManager : public KeyedService, |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 33 | public content::PermissionControllerDelegate, |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame^] | 34 | public permissions::Observer { |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 35 | public: |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 36 | using PermissionContextMap = |
| 37 | std::unordered_map<ContentSettingsType, |
| 38 | std::unique_ptr<PermissionContextBase>, |
| 39 | ContentSettingsTypeHash>; |
| 40 | PermissionManager(content::BrowserContext* browser_context, |
| 41 | PermissionContextMap permission_contexts); |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 42 | ~PermissionManager() override; |
| 43 | |
Marc Treib | 9e4bd92 | 2017-09-25 08:32:13 | [diff] [blame] | 44 | // Converts from |url|'s actual origin to the "canonical origin" that should |
| 45 | // be used for the purpose of requesting/storing permissions. For example, the |
Raymes Khoury | b474c64 | 2018-02-28 06:16:28 | [diff] [blame] | 46 | // origin of the local NTP gets mapped to the Google base URL instead. With |
| 47 | // Permission Delegation it will transform the requesting origin into |
| 48 | // the embedding origin because all permission checks happen on the top level |
| 49 | // origin. |
| 50 | // |
| 51 | // All the public methods below, such as RequestPermission or |
| 52 | // GetPermissionStatus, take the actual origin and do the canonicalization |
| 53 | // internally. You only need to call this directly if you do something else |
| 54 | // with the origin, such as display it in the UI. |
Balazs Engedy | f39e22b | 2019-07-30 11:16:24 | [diff] [blame] | 55 | GURL GetCanonicalOrigin(ContentSettingsType permission, |
| 56 | const GURL& requesting_origin, |
Raymes Khoury | b474c64 | 2018-02-28 06:16:28 | [diff] [blame] | 57 | const GURL& embedding_origin) const; |
Marc Treib | 9e4bd92 | 2017-09-25 08:32:13 | [diff] [blame] | 58 | |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 59 | // Callers from within chrome/ should use the methods which take the |
| 60 | // ContentSettingsType enum. The methods which take PermissionType values |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 61 | // are for the content::PermissionControllerDelegate overrides and shouldn't |
| 62 | // be used from chrome/. |
timloh | 592d732 | 2017-02-23 07:23:54 | [diff] [blame] | 63 | |
Balazs Engedy | e30e961 | 2021-04-02 10:37:29 | [diff] [blame] | 64 | void RequestPermission(ContentSettingsType permission, |
| 65 | content::RenderFrameHost* render_frame_host, |
| 66 | const GURL& requesting_origin, |
| 67 | bool user_gesture, |
| 68 | base::OnceCallback<void(ContentSetting)> callback); |
| 69 | void RequestPermissions( |
timloh | 592d732 | 2017-02-23 07:23:54 | [diff] [blame] | 70 | const std::vector<ContentSettingsType>& permissions, |
| 71 | content::RenderFrameHost* render_frame_host, |
| 72 | const GURL& requesting_origin, |
| 73 | bool user_gesture, |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 74 | base::OnceCallback<void(const std::vector<ContentSetting>&)> callback); |
timloh | 592d732 | 2017-02-23 07:23:54 | [diff] [blame] | 75 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 76 | PermissionResult GetPermissionStatus(ContentSettingsType permission, |
| 77 | const GURL& requesting_origin, |
| 78 | const GURL& embedding_origin); |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 79 | |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 80 | // Returns the permission status for a given frame. This should be preferred |
| 81 | // over GetPermissionStatus as additional checks can be performed when we know |
| 82 | // the exact context the request is coming from. |
raymes | 79f22a61 | 2017-03-13 05:28:10 | [diff] [blame] | 83 | // TODO(raymes): Currently we still pass the |requesting_origin| as a separate |
| 84 | // parameter because we can't yet guarantee that it matches the last committed |
| 85 | // origin of the RenderFrameHost. See crbug.com/698985. |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 86 | PermissionResult GetPermissionStatusForFrame( |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 87 | ContentSettingsType permission, |
raymes | 79f22a61 | 2017-03-13 05:28:10 | [diff] [blame] | 88 | content::RenderFrameHost* render_frame_host, |
| 89 | const GURL& requesting_origin); |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 90 | |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 91 | // content::PermissionControllerDelegate implementation. |
Balazs Engedy | e30e961 | 2021-04-02 10:37:29 | [diff] [blame] | 92 | void RequestPermission( |
| 93 | content::PermissionType permission, |
| 94 | content::RenderFrameHost* render_frame_host, |
| 95 | const GURL& requesting_origin, |
| 96 | bool user_gesture, |
| 97 | base::OnceCallback<void(blink::mojom::PermissionStatus)> callback) |
| 98 | override; |
| 99 | void RequestPermissions( |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 100 | const std::vector<content::PermissionType>& permissions, |
| 101 | content::RenderFrameHost* render_frame_host, |
| 102 | const GURL& requesting_origin, |
benwells | fd2b155 | 2016-07-05 04:26:53 | [diff] [blame] | 103 | bool user_gesture, |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 104 | base::OnceCallback< |
| 105 | void(const std::vector<blink::mojom::PermissionStatus>&)> callback) |
leon.han | 06e5566 | 2016-03-26 17:19:42 | [diff] [blame] | 106 | override; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 107 | void ResetPermission(content::PermissionType permission, |
| 108 | const GURL& requesting_origin, |
| 109 | const GURL& embedding_origin) override; |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 110 | blink::mojom::PermissionStatus GetPermissionStatus( |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 111 | content::PermissionType permission, |
| 112 | const GURL& requesting_origin, |
| 113 | const GURL& embedding_origin) override; |
Raymes Khoury | 4ead6c3 | 2018-03-07 04:43:48 | [diff] [blame] | 114 | blink::mojom::PermissionStatus GetPermissionStatusForFrame( |
| 115 | content::PermissionType permission, |
| 116 | content::RenderFrameHost* render_frame_host, |
| 117 | const GURL& requesting_origin) override; |
Pavel Feldman | 446a91b | 2020-03-13 17:39:55 | [diff] [blame] | 118 | bool IsPermissionOverridableByDevTools( |
| 119 | content::PermissionType permission, |
| 120 | const base::Optional<url::Origin>& origin) override; |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 121 | SubscriptionId SubscribePermissionStatusChange( |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 122 | content::PermissionType permission, |
Raymes Khoury | 3ef4f6e | 2018-08-09 09:34:48 | [diff] [blame] | 123 | content::RenderFrameHost* render_frame_host, |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 124 | const GURL& requesting_origin, |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 125 | base::RepeatingCallback<void(blink::mojom::PermissionStatus)> callback) |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 126 | override; |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 127 | void UnsubscribePermissionStatusChange( |
| 128 | SubscriptionId subscription_id) override; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 129 | |
timloh | c691180 | 2017-03-01 05:37:03 | [diff] [blame] | 130 | // TODO(raymes): Rather than exposing this, use the denial reason from |
| 131 | // GetPermissionStatus in callers to determine whether a permission is |
raymes | 893dbdd60 | 2016-12-19 22:49:29 | [diff] [blame] | 132 | // denied due to the kill switch. |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 133 | bool IsPermissionKillSwitchOn(ContentSettingsType); |
raymes | 893dbdd60 | 2016-12-19 22:49:29 | [diff] [blame] | 134 | |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 135 | // For the given |origin|, overrides permissions that belong to |overrides|. |
| 136 | // These permissions are in-sync with the PermissionController. |
| 137 | void SetPermissionOverridesForDevTools( |
Pavel Feldman | 446a91b | 2020-03-13 17:39:55 | [diff] [blame] | 138 | const base::Optional<url::Origin>& origin, |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 139 | const PermissionOverrides& overrides) override; |
| 140 | void ResetPermissionOverridesForDevTools() override; |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 141 | |
Alexey Baskakov | 386f174 | 2019-09-03 04:08:47 | [diff] [blame] | 142 | // KeyedService implementation |
| 143 | void Shutdown() override; |
| 144 | |
Clark DuVall | 2c6c867 | 2020-03-18 18:41:20 | [diff] [blame] | 145 | // Helper method to convert PermissionType to ContentSettingType. |
| 146 | static ContentSettingsType PermissionTypeToContentSetting( |
| 147 | content::PermissionType permission); |
| 148 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 149 | PermissionContextBase* GetPermissionContextForTesting( |
| 150 | ContentSettingsType type); |
| 151 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 152 | private: |
raymes | 158a8c1 | 2017-07-06 02:52:59 | [diff] [blame] | 153 | friend class PermissionManagerTest; |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 154 | |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 155 | // The `PendingRequestLocalId` will be unique within the `PermissionManager` |
| 156 | // instance, thus within a `BrowserContext`, which overachieves the |
| 157 | // requirement from `PermissionRequestID` that the `RequestLocalId` be unique |
| 158 | // within each frame. |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 159 | class PendingRequest; |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 160 | using PendingRequestLocalId = PermissionRequestID::RequestLocalId; |
| 161 | using PendingRequestsMap = |
| 162 | base::IDMap<std::unique_ptr<PendingRequest>, PendingRequestLocalId>; |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 163 | |
raymes | 158a8c1 | 2017-07-06 02:52:59 | [diff] [blame] | 164 | class PermissionResponseCallback; |
| 165 | |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 166 | struct Subscription; |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 167 | using SubscriptionsMap = |
| 168 | base::IDMap<std::unique_ptr<Subscription>, SubscriptionId>; |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame^] | 169 | using SubscriptionTypeCounts = base::flat_map<ContentSettingsType, size_t>; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 170 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 171 | PermissionContextBase* GetPermissionContext(ContentSettingsType type); |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 172 | |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 173 | // Called when a permission was decided for a given PendingRequest. The |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 174 | // PendingRequest is identified by its |request_local_id| and the permission |
| 175 | // is identified by its |permission_id|. If the PendingRequest contains more |
| 176 | // than one permission, it will wait for the remaining permissions to be |
| 177 | // resolved. When all the permissions have been resolved, the PendingRequest's |
| 178 | // callback is run. |
| 179 | void OnPermissionsRequestResponseStatus( |
| 180 | PendingRequestLocalId request_local_id, |
| 181 | int permission_id, |
| 182 | ContentSetting status); |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 183 | |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame^] | 184 | // permissions::Observer: |
| 185 | void OnPermissionChanged(const ContentSettingsPattern& primary_pattern, |
| 186 | const ContentSettingsPattern& secondary_pattern, |
| 187 | ContentSettingsType content_type) override; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 188 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 189 | PermissionResult GetPermissionStatusHelper( |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 190 | ContentSettingsType permission, |
| 191 | content::RenderFrameHost* render_frame_host, |
| 192 | const GURL& requesting_origin, |
| 193 | const GURL& embedding_origin); |
| 194 | |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 195 | ContentSetting GetPermissionOverrideForDevTools( |
Rohan Pavone | 8180cba6 | 2019-08-26 20:55:09 | [diff] [blame] | 196 | const url::Origin& origin, |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 197 | ContentSettingsType permission); |
| 198 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 199 | content::BrowserContext* browser_context_; |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 200 | |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 201 | PendingRequestsMap pending_requests_; |
Balazs Engedy | e15473b | 2021-04-14 09:09:21 | [diff] [blame] | 202 | PendingRequestLocalId::Generator request_local_id_generator_; |
| 203 | |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 204 | SubscriptionsMap subscriptions_; |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 205 | SubscriptionId::Generator subscription_id_generator_; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 206 | |
James Hollyer | d281a731 | 2021-04-29 21:07:59 | [diff] [blame^] | 207 | // Tracks the number of Subscriptions in |subscriptions_| which have a |
| 208 | // certain ContentSettingsType. An entry for a given ContentSettingsType key |
| 209 | // is added on first use and never removed. This is done to utilize the |
| 210 | // flat_map's efficiency in accessing/editing items and minimize the use of |
| 211 | // the unefficient addition/removal of items. |
| 212 | SubscriptionTypeCounts subscription_type_counts_; |
| 213 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 214 | PermissionContextMap permission_contexts_; |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 215 | using ContentSettingsTypeOverrides = |
| 216 | base::flat_map<ContentSettingsType, ContentSetting>; |
| 217 | std::map<url::Origin, ContentSettingsTypeOverrides> |
| 218 | devtools_permission_overrides_; |
Pavel Feldman | 446a91b | 2020-03-13 17:39:55 | [diff] [blame] | 219 | url::Origin devtools_global_overrides_origin_; |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 220 | |
Alexey Baskakov | 386f174 | 2019-09-03 04:08:47 | [diff] [blame] | 221 | bool is_shutting_down_ = false; |
| 222 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 223 | DISALLOW_COPY_AND_ASSIGN(PermissionManager); |
| 224 | }; |
| 225 | |
Clark DuVall | 6b73c74 | 2020-03-11 19:00:15 | [diff] [blame] | 226 | } // namespace permissions |
| 227 | |
| 228 | #endif // COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_ |