blob: 3bcac1cd04659ac6036e00621971be7b26ac87a5 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2015 The Chromium Authors
mlamouri4e372022015-03-29 14:51:062// 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
James Hollyerd281a7312021-04-29 21:07:5911#include "base/containers/flat_map.h"
Brett Wilsonf976d3f2017-08-18 17:23:3912#include "base/containers/id_map.h"
Avi Drissman12be0312023-01-11 09:16:0913#include "base/functional/callback_forward.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"
Andy Paicu1b5011a2022-09-20 15:44:5819#include "components/permissions/permission_decision_auto_blocker.h"
Balazs Engedye15473b2021-04-14 09:09:2120#include "components/permissions/permission_request_id.h"
Clark DuVall732778e2020-01-27 18:13:5821#include "components/permissions/permission_util.h"
Yifan Luo006519d92024-08-22 16:11:3422#include "content/public/browser/permission_controller.h"
Andrey Lushnikovf3500102018-07-16 19:55:2223#include "content/public/browser/permission_controller_delegate.h"
Illia Klimov15550f752022-08-11 19:33:1024#include "content/public/browser/permission_result.h"
Rohan Pavonefaf64572019-07-30 17:50:2025#include "url/origin.h"
mlamouri4e372022015-03-29 14:51:0626
Illia Klimov2c6138b2023-08-14 09:39:2527using PermissionStatus = blink::mojom::PermissionStatus;
28
Andy Paicua6d6d852022-04-28 18:08:3629namespace blink {
30enum class PermissionType;
31}
32
Clark DuVall6b73c742020-03-11 19:00:1533namespace content {
34class BrowserContext;
Robbie McElrath8d5602a2022-04-01 17:39:1835class RenderFrameHost;
36class RenderProcessHost;
Thomas Nguyen583f07e2023-07-19 17:45:3737class WebContents;
Andy Paicu0a6d4b502023-08-29 15:13:0938struct PermissionRequestDescription;
Miyoung Shindf9aee9d2024-01-17 16:03:1339struct PermissionResult;
Clark DuVall6b73c742020-03-11 19:00:1540}
41
Illia Klimov770b145f2022-04-20 17:19:0942class GeolocationPermissionContextDelegateTests;
43class SubscriptionInterceptingPermissionManager;
44
Clark DuVall484c2562020-01-23 22:05:0945namespace permissions {
Clark DuValla11361ad32020-02-20 22:14:2746class PermissionContextBase;
Illia Klimov770b145f2022-04-20 17:19:0947class PermissionManagerTest;
mlamouri4e372022015-03-29 14:51:0648
mlamouri4e372022015-03-29 14:51:0649class PermissionManager : public KeyedService,
Andrey Lushnikovf3500102018-07-16 19:55:2250 public content::PermissionControllerDelegate,
Andy Paicu1b5011a2022-09-20 15:44:5851 public permissions::Observer,
52 public PermissionDecisionAutoBlocker::Observer {
mlamouri4e372022015-03-29 14:51:0653 public:
Clark DuVall6b73c742020-03-11 19:00:1554 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öm09c01822021-09-20 22:43:2760
61 PermissionManager(const PermissionManager&) = delete;
62 PermissionManager& operator=(const PermissionManager&) = delete;
63
mlamouri4e372022015-03-29 14:51:0664 ~PermissionManager() override;
65
Illia Klimov770b145f2022-04-20 17:19:0966 // KeyedService implementation.
67 void Shutdown() override;
68
Andy Paicu1b5011a2022-09-20 15:44:5869 // PermissionDecisionAutoBlocker::Observer
70 void OnEmbargoStarted(const GURL& origin,
71 ContentSettingsType content_setting) override;
72
Illia Klimov770b145f2022-04-20 17:19:0973 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 Klimov770b145f2022-04-20 17:19:0996 using SubscriptionTypeCounts = base::flat_map<ContentSettingsType, size_t>;
97
98 PermissionContextBase* GetPermissionContext(ContentSettingsType type);
99
Andrey Lushnikovf3500102018-07-16 19:55:22100 // content::PermissionControllerDelegate implementation.
Balazs Engedye30e9612021-04-02 10:37:29101 void RequestPermissions(
mlamouri8b5ec902015-10-24 00:52:03102 content::RenderFrameHost* render_frame_host,
Andy Paicu0a6d4b502023-08-29 15:13:09103 const content::PermissionRequestDescription& request_description,
Illia Klimov2c6138b2023-08-14 09:39:25104 base::OnceCallback<void(const std::vector<PermissionStatus>&)> callback)
leon.han06e55662016-03-26 17:19:42105 override;
Matt Reichhoff56f62282022-09-12 20:36:00106 void RequestPermissionsInternal(
Matt Reichhoff56f62282022-09-12 20:36:00107 content::RenderFrameHost* render_frame_host,
Andy Paicu0a6d4b502023-08-29 15:13:09108 const content::PermissionRequestDescription& request_description,
Illia Klimov2c6138b2023-08-14 09:39:25109 base::OnceCallback<void(const std::vector<PermissionStatus>&)> callback);
Andy Paicua6d6d852022-04-28 18:08:36110 void ResetPermission(blink::PermissionType permission,
mlamouri4e372022015-03-29 14:51:06111 const GURL& requesting_origin,
112 const GURL& embedding_origin) override;
Illia Klimov27239edc2022-05-11 17:14:59113 void RequestPermissionsFromCurrentDocument(
Illia Klimov27239edc2022-05-11 17:14:59114 content::RenderFrameHost* render_frame_host,
Andy Paicu0a6d4b502023-08-29 15:13:09115 const content::PermissionRequestDescription& request_description,
Illia Klimov2c6138b2023-08-14 09:39:25116 base::OnceCallback<void(const std::vector<PermissionStatus>&)> callback)
Illia Klimov27239edc2022-05-11 17:14:59117 override;
Florian Jacky8373f042025-04-16 14:20:53118 PermissionStatus GetPermissionStatus(
119 const blink::mojom::PermissionDescriptorPtr& permission_descriptor,
120 const GURL& requesting_origin,
121 const GURL& embedding_origin) override;
Illia Klimov15550f752022-08-11 19:33:10122 content::PermissionResult GetPermissionResultForOriginWithoutContext(
Florian Jacky8373f042025-04-16 14:20:53123 const blink::mojom::PermissionDescriptorPtr& permission_descriptor,
Christian Dullwebera475f4a2023-08-07 17:04:13124 const url::Origin& requesting_origin,
125 const url::Origin& embedding_origin) override;
Illia Klimov2c6138b2023-08-14 09:39:25126 PermissionStatus GetPermissionStatusForCurrentDocument(
Florian Jacky8373f042025-04-16 14:20:53127 const blink::mojom::PermissionDescriptorPtr& permission_descriptor,
Thomas Nguyen235933322024-05-27 14:52:11128 content::RenderFrameHost* render_frame_host,
129 bool should_include_device_status) override;
Illia Klimov15550f752022-08-11 19:33:10130 content::PermissionResult GetPermissionResultForCurrentDocument(
Florian Jacky8373f042025-04-16 14:20:53131 const blink::mojom::PermissionDescriptorPtr& permission_descriptor,
Thomas Nguyen235933322024-05-27 14:52:11132 content::RenderFrameHost* render_frame_host,
133 bool should_include_device_status) override;
Illia Klimov2c6138b2023-08-14 09:39:25134 PermissionStatus GetPermissionStatusForWorker(
Florian Jacky8373f042025-04-16 14:20:53135 const blink::mojom::PermissionDescriptorPtr& permission_descriptor,
Robbie McElrath8d5602a2022-04-01 17:39:18136 content::RenderProcessHost* render_process_host,
137 const GURL& worker_origin) override;
Illia Klimov2c6138b2023-08-14 09:39:25138 PermissionStatus GetPermissionStatusForEmbeddedRequester(
Florian Jacky8373f042025-04-16 14:20:53139 const blink::mojom::PermissionDescriptorPtr& permission_descriptor,
Illia Klimov20badfdf2023-03-16 19:53:10140 content::RenderFrameHost* render_frame_host,
141 const url::Origin& requesting_origin) override;
Illia Klimov9af8089132022-10-07 16:42:30142 bool IsPermissionOverridable(
Andy Paicua6d6d852022-04-28 18:08:36143 blink::PermissionType permission,
Arthur Sonzognic571efb2024-01-26 20:26:18144 const std::optional<url::Origin>& origin) override;
Yifan Luo006519d92024-08-22 16:11:34145 void OnPermissionStatusChangeSubscriptionAdded(
146 content::PermissionController::SubscriptionId subscription_id) override;
Balazs Engedy33b441e2023-12-12 18:53:42147 void UnsubscribeFromPermissionStatusChange(
Yifan Luo006519d92024-08-22 16:11:34148 content::PermissionController::SubscriptionId subscription_id) override;
Arthur Sonzognic571efb2024-01-26 20:26:18149 std::optional<gfx::Rect> GetExclusionAreaBoundsInScreen(
Thomas Nguyen583f07e2023-07-19 17:45:37150 content::WebContents* web_contents) const override;
mlamouri4e372022015-03-29 14:51:06151
mlamouri8b5ec902015-10-24 00:52:03152 // Called when a permission was decided for a given PendingRequest. The
Balazs Engedye15473b2021-04-14 09:09:21153 // 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);
lalitm27583e92015-10-02 11:34:17162
James Hollyerd281a7312021-04-29 21:07:59163 // permissions::Observer:
164 void OnPermissionChanged(const ContentSettingsPattern& primary_pattern,
165 const ContentSettingsPattern& secondary_pattern,
Christian Dullweber2c4c71d2021-10-14 15:07:43166 ContentSettingsTypeSet content_type_set) override;
mlamouri23957a22015-04-01 10:37:56167
Robbie McElrath8d5602a2022-04-01 17:39:18168 // 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 Klimov2c6138b2023-08-14 09:39:25170 content::PermissionResult GetPermissionStatusInternal(
Florian Jacky8373f042025-04-16 14:20:53171 const blink::mojom::PermissionDescriptorPtr& permission_descriptor,
Robbie McElrath8d5602a2022-04-01 17:39:18172 content::RenderProcessHost* render_process_host,
raymesf6104d492017-03-09 01:20:18173 content::RenderFrameHost* render_frame_host,
174 const GURL& requesting_origin,
Andy Paicu9d70da42024-05-10 22:24:39175 const GURL& embedding_origin,
176 bool should_include_device_status);
raymesf6104d492017-03-09 01:20:18177
Keishi Hattori0e45c022021-11-27 09:25:52178 raw_ptr<content::BrowserContext> browser_context_;
Balazs Engedye15473b2021-04-14 09:09:21179
lalitm27583e92015-10-02 11:34:17180 PendingRequestsMap pending_requests_;
Balazs Engedye15473b2021-04-14 09:09:21181 PendingRequestLocalId::Generator request_local_id_generator_;
182
James Hollyerd281a7312021-04-29 21:07:59183 // 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 DuVall6b73c742020-03-11 19:00:15190 PermissionContextMap permission_contexts_;
raymese3afee6b2016-04-18 02:00:50191
Alexey Baskakov386f1742019-09-03 04:08:47192 bool is_shutting_down_ = false;
Thomas Nguyen3d9dd372024-09-23 13:44:48193
194 base::WeakPtrFactory<PermissionManager> weak_factory_{this};
mlamouri4e372022015-03-29 14:51:06195};
196
Clark DuVall6b73c742020-03-11 19:00:15197} // namespace permissions
198
199#endif // COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_