blob: ed0b5ba6f18868235b446d52c47f224b33a9304b [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"
Illia Klimov15550f752022-08-11 19:33:1022#include "content/public/browser/permission_result.h"
Rohan Pavonefaf64572019-07-30 17:50:2023#include "url/origin.h"
mlamouri4e372022015-03-29 14:51:0624
Andy Paicua6d6d852022-04-28 18:08:3625namespace blink {
26enum class PermissionType;
27}
28
Clark DuVall6b73c742020-03-11 19:00:1529namespace content {
30class BrowserContext;
Robbie McElrath8d5602a2022-04-01 17:39:1831class RenderFrameHost;
32class RenderProcessHost;
Clark DuVall6b73c742020-03-11 19:00:1533}
34
Illia Klimov770b145f2022-04-20 17:19:0935class GeolocationPermissionContextDelegateTests;
36class SubscriptionInterceptingPermissionManager;
37
Clark DuVall484c2562020-01-23 22:05:0938namespace permissions {
Clark DuValla11361ad32020-02-20 22:14:2739class PermissionContextBase;
timlohc6911802017-03-01 05:37:0340struct PermissionResult;
Illia Klimov770b145f2022-04-20 17:19:0941class PermissionManagerTest;
mlamouri4e372022015-03-29 14:51:0642
mlamouri4e372022015-03-29 14:51:0643class PermissionManager : public KeyedService,
Andrey Lushnikovf3500102018-07-16 19:55:2244 public content::PermissionControllerDelegate,
James Hollyerd281a7312021-04-29 21:07:5945 public permissions::Observer {
mlamouri4e372022015-03-29 14:51:0646 public:
Clark DuVall6b73c742020-03-11 19:00:1547 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öm09c01822021-09-20 22:43:2753
54 PermissionManager(const PermissionManager&) = delete;
55 PermissionManager& operator=(const PermissionManager&) = delete;
56
mlamouri4e372022015-03-29 14:51:0657 ~PermissionManager() override;
58
Illia Klimov770b145f2022-04-20 17:19:0959 // KeyedService implementation.
60 void Shutdown() override;
61
Illia Klimov770b145f2022-04-20 17:19:0962 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 Lushnikovf3500102018-07-16 19:55:2292 // content::PermissionControllerDelegate implementation.
Balazs Engedye30e9612021-04-02 10:37:2993 void RequestPermission(
Andy Paicua6d6d852022-04-28 18:08:3694 blink::PermissionType permission,
Balazs Engedye30e9612021-04-02 10:37:2995 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 Paicua6d6d852022-04-28 18:08:36101 const std::vector<blink::PermissionType>& permissions,
mlamouri8b5ec902015-10-24 00:52:03102 content::RenderFrameHost* render_frame_host,
103 const GURL& requesting_origin,
benwellsfd2b1552016-07-05 04:26:53104 bool user_gesture,
danakj47c8fb52019-05-02 16:34:36105 base::OnceCallback<
106 void(const std::vector<blink::mojom::PermissionStatus>&)> callback)
leon.han06e55662016-03-26 17:19:42107 override;
Andy Paicua6d6d852022-04-28 18:08:36108 void ResetPermission(blink::PermissionType permission,
mlamouri4e372022015-03-29 14:51:06109 const GURL& requesting_origin,
110 const GURL& embedding_origin) override;
Illia Klimov27239edc2022-05-11 17:14:59111 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;
mathpcc29ae52016-05-04 15:22:17118 blink::mojom::PermissionStatus GetPermissionStatus(
Andy Paicua6d6d852022-04-28 18:08:36119 blink::PermissionType permission,
mlamouri4e372022015-03-29 14:51:06120 const GURL& requesting_origin,
121 const GURL& embedding_origin) override;
Illia Klimov15550f752022-08-11 19:33:10122 content::PermissionResult GetPermissionResultForOriginWithoutContext(
123 blink::PermissionType permission,
124 const url::Origin& origin) override;
Illia Klimovf2842842022-03-22 11:33:39125 blink::mojom::PermissionStatus GetPermissionStatusForCurrentDocument(
Andy Paicua6d6d852022-04-28 18:08:36126 blink::PermissionType permission,
Illia Klimovf2842842022-03-22 11:33:39127 content::RenderFrameHost* render_frame_host) override;
Illia Klimov15550f752022-08-11 19:33:10128 content::PermissionResult GetPermissionResultForCurrentDocument(
129 blink::PermissionType permission,
130 content::RenderFrameHost* render_frame_host) override;
Robbie McElrath8d5602a2022-04-01 17:39:18131 blink::mojom::PermissionStatus GetPermissionStatusForWorker(
Andy Paicua6d6d852022-04-28 18:08:36132 blink::PermissionType permission,
Robbie McElrath8d5602a2022-04-01 17:39:18133 content::RenderProcessHost* render_process_host,
134 const GURL& worker_origin) override;
Pavel Feldman446a91b2020-03-13 17:39:55135 bool IsPermissionOverridableByDevTools(
Andy Paicua6d6d852022-04-28 18:08:36136 blink::PermissionType permission,
Anton Bikineev1156b5f2021-05-15 22:35:36137 const absl::optional<url::Origin>& origin) override;
Balazs Engedyad1489b2021-03-31 07:47:19138 SubscriptionId SubscribePermissionStatusChange(
Andy Paicua6d6d852022-04-28 18:08:36139 blink::PermissionType permission,
Robbie McElrath8d5602a2022-04-01 17:39:18140 content::RenderProcessHost* render_process_host,
Raymes Khoury3ef4f6e2018-08-09 09:34:48141 content::RenderFrameHost* render_frame_host,
mlamouri23957a22015-04-01 10:37:56142 const GURL& requesting_origin,
danakj47c8fb52019-05-02 16:34:36143 base::RepeatingCallback<void(blink::mojom::PermissionStatus)> callback)
mathpcc29ae52016-05-04 15:22:17144 override;
Balazs Engedyad1489b2021-03-31 07:47:19145 void UnsubscribePermissionStatusChange(
146 SubscriptionId subscription_id) override;
mlamouri4e372022015-03-29 14:51:06147
mlamouri8b5ec902015-10-24 00:52:03148 // Called when a permission was decided for a given PendingRequest. The
Balazs Engedye15473b2021-04-14 09:09:21149 // 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);
lalitm27583e92015-10-02 11:34:17158
James Hollyerd281a7312021-04-29 21:07:59159 // permissions::Observer:
160 void OnPermissionChanged(const ContentSettingsPattern& primary_pattern,
161 const ContentSettingsPattern& secondary_pattern,
Christian Dullweber2c4c71d2021-10-14 15:07:43162 ContentSettingsTypeSet content_type_set) override;
mlamouri23957a22015-04-01 10:37:56163
Robbie McElrath8d5602a2022-04-01 17:39:18164 // 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 Klimov15550f752022-08-11 19:33:10166 PermissionResult GetPermissionStatusInternal(
raymesf6104d492017-03-09 01:20:18167 ContentSettingsType permission,
Robbie McElrath8d5602a2022-04-01 17:39:18168 content::RenderProcessHost* render_process_host,
raymesf6104d492017-03-09 01:20:18169 content::RenderFrameHost* render_frame_host,
170 const GURL& requesting_origin,
171 const GURL& embedding_origin);
172
Pavel Feldman73b22022018-11-02 02:55:30173 ContentSetting GetPermissionOverrideForDevTools(
Rohan Pavone8180cba62019-08-26 20:55:09174 const url::Origin& origin,
Pavel Feldman73b22022018-11-02 02:55:30175 ContentSettingsType permission);
176
Illia Klimov770b145f2022-04-20 17:19:09177 // 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 Hattori0e45c022021-11-27 09:25:52185 raw_ptr<content::BrowserContext> browser_context_;
Balazs Engedye15473b2021-04-14 09:09:21186
lalitm27583e92015-10-02 11:34:17187 PendingRequestsMap pending_requests_;
Balazs Engedye15473b2021-04-14 09:09:21188 PendingRequestLocalId::Generator request_local_id_generator_;
189
mlamouri23957a22015-04-01 10:37:56190 SubscriptionsMap subscriptions_;
Balazs Engedyad1489b2021-03-31 07:47:19191 SubscriptionId::Generator subscription_id_generator_;
mlamouri4e372022015-03-29 14:51:06192
James Hollyerd281a7312021-04-29 21:07:59193 // 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 DuVall6b73c742020-03-11 19:00:15200 PermissionContextMap permission_contexts_;
Rohan Pavonefaf64572019-07-30 17:50:20201 using ContentSettingsTypeOverrides =
202 base::flat_map<ContentSettingsType, ContentSetting>;
203 std::map<url::Origin, ContentSettingsTypeOverrides>
204 devtools_permission_overrides_;
Pavel Feldman446a91b2020-03-13 17:39:55205 url::Origin devtools_global_overrides_origin_;
raymese3afee6b2016-04-18 02:00:50206
Alexey Baskakov386f1742019-09-03 04:08:47207 bool is_shutting_down_ = false;
mlamouri4e372022015-03-29 14:51:06208};
209
Clark DuVall6b73c742020-03-11 19:00:15210} // namespace permissions
211
212#endif // COMPONENTS_PERMISSIONS_PERMISSION_MANAGER_H_