blob: 9f99970001df542d277a091a16ff72783ebfc5ff [file] [log] [blame]
mlamouri4e372022015-03-29 14:51:061// 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 DuVall6b73c742020-03-11 19:00:155#ifndef COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_
6#define COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_
mlamouri4e372022015-03-29 14:51:067
Lei Zhang998100f2021-06-25 17:58:198#include <map>
raymese3afee6b2016-04-18 02:00:509#include <unordered_map>
10
mlamouri4e372022015-03-29 14:51:0611#include "base/callback_forward.h"
James Hollyerd281a7312021-04-29 21:07:5912#include "base/containers/flat_map.h"
Brett Wilsonf976d3f2017-08-18 17:23:3913#include "base/containers/id_map.h"
Keishi Hattori0e45c022021-11-27 09:25:5214#include "base/memory/raw_ptr.h"
mlamouri23957a22015-04-01 10:37:5615#include "components/content_settings/core/browser/content_settings_observer.h"
lalitm27583e92015-10-02 11:34:1716#include "components/content_settings/core/common/content_settings.h"
mlamouri4e372022015-03-29 14:51:0617#include "components/keyed_service/core/keyed_service.h"
James Hollyerd281a7312021-04-29 21:07:5918#include "components/permissions/permission_context_base.h"
Balazs Engedye15473b2021-04-14 09:09:2119#include "components/permissions/permission_request_id.h"
Clark DuVall732778e2020-01-27 18:13:5820#include "components/permissions/permission_util.h"
Andrey Lushnikovf3500102018-07-16 19:55:2221#include "content/public/browser/permission_controller_delegate.h"
Pavel Feldman73b22022018-11-02 02:55:3022#include "content/public/browser/permission_type.h"
Rohan Pavonefaf64572019-07-30 17:50:2023#include "url/origin.h"
mlamouri4e372022015-03-29 14:51:0624
Clark DuVall6b73c742020-03-11 19:00:1525namespace content {
26class BrowserContext;
Robbie McElrath8d5602a2022-04-01 17:39:1827class RenderFrameHost;
28class RenderProcessHost;
Clark DuVall6b73c742020-03-11 19:00:1529}
30
Illia Klimov770b145f2022-04-20 17:19:0931class GeolocationPermissionContextDelegateTests;
32class SubscriptionInterceptingPermissionManager;
33
Clark DuVall484c2562020-01-23 22:05:0934namespace permissions {
Clark DuValla11361ad32020-02-20 22:14:2735class PermissionContextBase;
timlohc6911802017-03-01 05:37:0336struct PermissionResult;
Illia Klimov770b145f2022-04-20 17:19:0937class PermissionManagerTest;
mlamouri4e372022015-03-29 14:51:0638
mlamouri4e372022015-03-29 14:51:0639class PermissionManager : public KeyedService,
Andrey Lushnikovf3500102018-07-16 19:55:2240 public content::PermissionControllerDelegate,
James Hollyerd281a7312021-04-29 21:07:5941 public permissions::Observer {
mlamouri4e372022015-03-29 14:51:0642 public:
Clark DuVall6b73c742020-03-11 19:00:1543 using PermissionContextMap =
44 std::unordered_map<ContentSettingsType,
45 std::unique_ptr<PermissionContextBase>,
46 ContentSettingsTypeHash>;
47 PermissionManager(content::BrowserContext* browser_context,
48 PermissionContextMap permission_contexts);
Peter Boström09c01822021-09-20 22:43:2749
50 PermissionManager(const PermissionManager&) = delete;
51 PermissionManager& operator=(const PermissionManager&) = delete;
52
mlamouri4e372022015-03-29 14:51:0653 ~PermissionManager() override;
54
Marc Treib9e4bd922017-09-25 08:32:1355 // Converts from |url|'s actual origin to the "canonical origin" that should
56 // be used for the purpose of requesting/storing permissions. For example, the
Raymes Khouryb474c642018-02-28 06:16:2857 // origin of the local NTP gets mapped to the Google base URL instead. With
58 // Permission Delegation it will transform the requesting origin into
59 // the embedding origin because all permission checks happen on the top level
60 // origin.
61 //
62 // All the public methods below, such as RequestPermission or
63 // GetPermissionStatus, take the actual origin and do the canonicalization
64 // internally. You only need to call this directly if you do something else
65 // with the origin, such as display it in the UI.
Balazs Engedyf39e22b2019-07-30 11:16:2466 GURL GetCanonicalOrigin(ContentSettingsType permission,
67 const GURL& requesting_origin,
Raymes Khouryb474c642018-02-28 06:16:2868 const GURL& embedding_origin) const;
Marc Treib9e4bd922017-09-25 08:32:1369
Illia Klimov770b145f2022-04-20 17:19:0970 // This method is deprecated. Use `GetPermissionStatusForCurrentDocument`
71 // instead or `GetPermissionStatusForDisplayOnSettingsUI`.
72 PermissionResult GetPermissionStatusDeprecated(ContentSettingsType permission,
73 const GURL& requesting_origin,
74 const GURL& embedding_origin);
75
76 // Returns the permission status for a given `permission` and displayed,
77 // top-level `origin`. This should be used only for displaying on the
78 // browser's native UI (PageInfo, Settings, etc.). This method does not take
79 // context specific restrictions (e.g. permission policy) into consideration.
80 PermissionResult GetPermissionStatusForDisplayOnSettingsUI(
81 ContentSettingsType permission,
82 const GURL& origin);
83
84 // Returns the status for the given `permission` on behalf of the last
85 // committed document in `render_frame_host`, also performing additional
86 // checks such as Permission Policy.
87 PermissionResult GetPermissionStatusForCurrentDocument(
88 ContentSettingsType permission,
89 content::RenderFrameHost* render_frame_host);
90
91 // KeyedService implementation.
92 void Shutdown() override;
93
94 // Requests the given `permission` on behalf of the last committed document in
95 // `render_frame_host`, also performing additional checks such as Permission
96 // Policy.
97 void RequestPermissionsFromCurrentDocument(
98 const std::vector<ContentSettingsType>& permissions,
99 content::RenderFrameHost* render_frame_host,
100 bool user_gesture,
101 base::OnceCallback<void(const std::vector<ContentSetting>&)> callback);
102
103 PermissionContextBase* GetPermissionContextForTesting(
104 ContentSettingsType type);
105
106 PermissionContextMap& PermissionContextsForTesting() {
107 return permission_contexts_;
108 }
109
110 private:
111 friend class PermissionManagerTest;
112 friend class ::GeolocationPermissionContextDelegateTests;
113 friend class ::SubscriptionInterceptingPermissionManager;
114
115 // The `PendingRequestLocalId` will be unique within the `PermissionManager`
116 // instance, thus within a `BrowserContext`, which overachieves the
117 // requirement from `PermissionRequestID` that the `RequestLocalId` be unique
118 // within each frame.
119 class PendingRequest;
120 using PendingRequestLocalId = PermissionRequestID::RequestLocalId;
121 using PendingRequestsMap =
122 base::IDMap<std::unique_ptr<PendingRequest>, PendingRequestLocalId>;
123
124 class PermissionResponseCallback;
125
126 struct Subscription;
127 using SubscriptionsMap =
128 base::IDMap<std::unique_ptr<Subscription>, SubscriptionId>;
129 using SubscriptionTypeCounts = base::flat_map<ContentSettingsType, size_t>;
130
131 PermissionContextBase* GetPermissionContext(ContentSettingsType type);
132
timloh9a180ad2017-02-20 07:15:23133 // Callers from within chrome/ should use the methods which take the
134 // ContentSettingsType enum. The methods which take PermissionType values
Andrey Lushnikovf3500102018-07-16 19:55:22135 // are for the content::PermissionControllerDelegate overrides and shouldn't
136 // be used from chrome/.
Illia Klimovfa8fa512021-07-14 20:36:34137 // Deprecated. Use `RequestPermissionFromCurrentDocument` instead.
Balazs Engedye30e9612021-04-02 10:37:29138 void RequestPermission(ContentSettingsType permission,
139 content::RenderFrameHost* render_frame_host,
140 const GURL& requesting_origin,
141 bool user_gesture,
142 base::OnceCallback<void(ContentSetting)> callback);
Illia Klimov770b145f2022-04-20 17:19:09143
Illia Klimovfa8fa512021-07-14 20:36:34144 // Deprecated. Use `RequestPermissionsFromCurrentDocument` instead.
Balazs Engedye30e9612021-04-02 10:37:29145 void RequestPermissions(
timloh592d7322017-02-23 07:23:54146 const std::vector<ContentSettingsType>& permissions,
147 content::RenderFrameHost* render_frame_host,
148 const GURL& requesting_origin,
149 bool user_gesture,
danakj47c8fb52019-05-02 16:34:36150 base::OnceCallback<void(const std::vector<ContentSetting>&)> callback);
Illia Klimovfa8fa512021-07-14 20:36:34151 void RequestPermissionFromCurrentDocument(
152 ContentSettingsType permission,
153 content::RenderFrameHost* render_frame_host,
154 bool user_gesture,
155 base::OnceCallback<void(ContentSetting)> callback);
raymesf6104d492017-03-09 01:20:18156 // Returns the permission status for a given frame. This should be preferred
157 // over GetPermissionStatus as additional checks can be performed when we know
158 // the exact context the request is coming from.
raymes79f22a612017-03-13 05:28:10159 // TODO(raymes): Currently we still pass the |requesting_origin| as a separate
160 // parameter because we can't yet guarantee that it matches the last committed
161 // origin of the RenderFrameHost. See crbug.com/698985.
Illia Klimovfa8fa512021-07-14 20:36:34162 // Deprecated. Use `GetPermissionStatusForCurrentDocument` instead.
Clark DuVall6b73c742020-03-11 19:00:15163 PermissionResult GetPermissionStatusForFrame(
raymesf6104d492017-03-09 01:20:18164 ContentSettingsType permission,
raymes79f22a612017-03-13 05:28:10165 content::RenderFrameHost* render_frame_host,
166 const GURL& requesting_origin);
raymesf6104d492017-03-09 01:20:18167
Robbie McElrath8d5602a2022-04-01 17:39:18168 // Returns the status of the given `permission` for a worker on `origin`
169 // running in the renderer corresponding to `render_process_host`.
170 PermissionResult GetPermissionStatusForWorker(
171 ContentSettingsType permission,
172 content::RenderProcessHost* render_process_host,
173 const url::Origin& worker_origin);
174
Andrey Lushnikovf3500102018-07-16 19:55:22175 // content::PermissionControllerDelegate implementation.
Balazs Engedye30e9612021-04-02 10:37:29176 void RequestPermission(
177 content::PermissionType permission,
178 content::RenderFrameHost* render_frame_host,
179 const GURL& requesting_origin,
180 bool user_gesture,
181 base::OnceCallback<void(blink::mojom::PermissionStatus)> callback)
182 override;
183 void RequestPermissions(
mlamouri8b5ec902015-10-24 00:52:03184 const std::vector<content::PermissionType>& permissions,
185 content::RenderFrameHost* render_frame_host,
186 const GURL& requesting_origin,
benwellsfd2b1552016-07-05 04:26:53187 bool user_gesture,
danakj47c8fb52019-05-02 16:34:36188 base::OnceCallback<
189 void(const std::vector<blink::mojom::PermissionStatus>&)> callback)
leon.han06e55662016-03-26 17:19:42190 override;
mlamouri4e372022015-03-29 14:51:06191 void ResetPermission(content::PermissionType permission,
192 const GURL& requesting_origin,
193 const GURL& embedding_origin) override;
mathpcc29ae52016-05-04 15:22:17194 blink::mojom::PermissionStatus GetPermissionStatus(
mlamouri4e372022015-03-29 14:51:06195 content::PermissionType permission,
196 const GURL& requesting_origin,
197 const GURL& embedding_origin) override;
Raymes Khoury4ead6c32018-03-07 04:43:48198 blink::mojom::PermissionStatus GetPermissionStatusForFrame(
199 content::PermissionType permission,
200 content::RenderFrameHost* render_frame_host,
201 const GURL& requesting_origin) override;
Illia Klimovf2842842022-03-22 11:33:39202 blink::mojom::PermissionStatus GetPermissionStatusForCurrentDocument(
203 content::PermissionType permission,
204 content::RenderFrameHost* render_frame_host) override;
Robbie McElrath8d5602a2022-04-01 17:39:18205 blink::mojom::PermissionStatus GetPermissionStatusForWorker(
206 content::PermissionType permission,
207 content::RenderProcessHost* render_process_host,
208 const GURL& worker_origin) override;
Pavel Feldman446a91b2020-03-13 17:39:55209 bool IsPermissionOverridableByDevTools(
210 content::PermissionType permission,
Anton Bikineev1156b5f2021-05-15 22:35:36211 const absl::optional<url::Origin>& origin) override;
Balazs Engedyad1489b2021-03-31 07:47:19212 SubscriptionId SubscribePermissionStatusChange(
mlamouri23957a22015-04-01 10:37:56213 content::PermissionType permission,
Robbie McElrath8d5602a2022-04-01 17:39:18214 content::RenderProcessHost* render_process_host,
Raymes Khoury3ef4f6e2018-08-09 09:34:48215 content::RenderFrameHost* render_frame_host,
mlamouri23957a22015-04-01 10:37:56216 const GURL& requesting_origin,
danakj47c8fb52019-05-02 16:34:36217 base::RepeatingCallback<void(blink::mojom::PermissionStatus)> callback)
mathpcc29ae52016-05-04 15:22:17218 override;
Balazs Engedyad1489b2021-03-31 07:47:19219 void UnsubscribePermissionStatusChange(
220 SubscriptionId subscription_id) override;
mlamouri4e372022015-03-29 14:51:06221
mlamouri8b5ec902015-10-24 00:52:03222 // Called when a permission was decided for a given PendingRequest. The
Balazs Engedye15473b2021-04-14 09:09:21223 // PendingRequest is identified by its |request_local_id| and the permission
224 // is identified by its |permission_id|. If the PendingRequest contains more
225 // than one permission, it will wait for the remaining permissions to be
226 // resolved. When all the permissions have been resolved, the PendingRequest's
227 // callback is run.
228 void OnPermissionsRequestResponseStatus(
229 PendingRequestLocalId request_local_id,
230 int permission_id,
231 ContentSetting status);
lalitm27583e92015-10-02 11:34:17232
James Hollyerd281a7312021-04-29 21:07:59233 // permissions::Observer:
234 void OnPermissionChanged(const ContentSettingsPattern& primary_pattern,
235 const ContentSettingsPattern& secondary_pattern,
Christian Dullweber2c4c71d2021-10-14 15:07:43236 ContentSettingsTypeSet content_type_set) override;
mlamouri23957a22015-04-01 10:37:56237
Robbie McElrath8d5602a2022-04-01 17:39:18238 // Only one of |render_process_host| and |render_frame_host| should be set,
239 // or neither. RenderProcessHost will be inferred from |render_frame_host|.
Clark DuVall6b73c742020-03-11 19:00:15240 PermissionResult GetPermissionStatusHelper(
raymesf6104d492017-03-09 01:20:18241 ContentSettingsType permission,
Robbie McElrath8d5602a2022-04-01 17:39:18242 content::RenderProcessHost* render_process_host,
raymesf6104d492017-03-09 01:20:18243 content::RenderFrameHost* render_frame_host,
244 const GURL& requesting_origin,
245 const GURL& embedding_origin);
246
Pavel Feldman73b22022018-11-02 02:55:30247 ContentSetting GetPermissionOverrideForDevTools(
Rohan Pavone8180cba62019-08-26 20:55:09248 const url::Origin& origin,
Pavel Feldman73b22022018-11-02 02:55:30249 ContentSettingsType permission);
250
Illia Klimov770b145f2022-04-20 17:19:09251 // content::PermissionControllerDelegate implementation.
252 // For the given |origin|, overrides permissions that belong to |overrides|.
253 // These permissions are in-sync with the PermissionController.
254 void SetPermissionOverridesForDevTools(
255 const absl::optional<url::Origin>& origin,
256 const PermissionOverrides& overrides) override;
257 void ResetPermissionOverridesForDevTools() override;
258
Keishi Hattori0e45c022021-11-27 09:25:52259 raw_ptr<content::BrowserContext> browser_context_;
Balazs Engedye15473b2021-04-14 09:09:21260
lalitm27583e92015-10-02 11:34:17261 PendingRequestsMap pending_requests_;
Balazs Engedye15473b2021-04-14 09:09:21262 PendingRequestLocalId::Generator request_local_id_generator_;
263
mlamouri23957a22015-04-01 10:37:56264 SubscriptionsMap subscriptions_;
Balazs Engedyad1489b2021-03-31 07:47:19265 SubscriptionId::Generator subscription_id_generator_;
mlamouri4e372022015-03-29 14:51:06266
James Hollyerd281a7312021-04-29 21:07:59267 // Tracks the number of Subscriptions in |subscriptions_| which have a
268 // certain ContentSettingsType. An entry for a given ContentSettingsType key
269 // is added on first use and never removed. This is done to utilize the
270 // flat_map's efficiency in accessing/editing items and minimize the use of
271 // the unefficient addition/removal of items.
272 SubscriptionTypeCounts subscription_type_counts_;
273
Clark DuVall6b73c742020-03-11 19:00:15274 PermissionContextMap permission_contexts_;
Rohan Pavonefaf64572019-07-30 17:50:20275 using ContentSettingsTypeOverrides =
276 base::flat_map<ContentSettingsType, ContentSetting>;
277 std::map<url::Origin, ContentSettingsTypeOverrides>
278 devtools_permission_overrides_;
Pavel Feldman446a91b2020-03-13 17:39:55279 url::Origin devtools_global_overrides_origin_;
raymese3afee6b2016-04-18 02:00:50280
Alexey Baskakov386f1742019-09-03 04:08:47281 bool is_shutting_down_ = false;
mlamouri4e372022015-03-29 14:51:06282};
283
Clark DuVall6b73c742020-03-11 19:00:15284} // namespace permissions
285
286#endif // COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_