blob: a3b90b9be6638a91769d7a5337174f51f437b4cd [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
5#ifndef CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_
6#define CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_
7
raymese3afee6b2016-04-18 02:00:508#include <unordered_map>
9
mlamouri4e372022015-03-29 14:51:0610#include "base/callback_forward.h"
Brett Wilsonf976d3f2017-08-18 17:23:3911#include "base/containers/id_map.h"
mlamouri4e372022015-03-29 14:51:0612#include "base/macros.h"
raymes7c1c35a22016-06-23 00:20:5313#include "chrome/browser/permissions/permission_util.h"
mlamouri23957a22015-04-01 10:37:5614#include "components/content_settings/core/browser/content_settings_observer.h"
lalitm27583e92015-10-02 11:34:1715#include "components/content_settings/core/common/content_settings.h"
mlamouri4e372022015-03-29 14:51:0616#include "components/keyed_service/core/keyed_service.h"
Andrey Lushnikovf3500102018-07-16 19:55:2217#include "content/public/browser/permission_controller_delegate.h"
Pavel Feldman73b22022018-11-02 02:55:3018#include "content/public/browser/permission_type.h"
Rohan Pavonefaf64572019-07-30 17:50:2019#include "url/origin.h"
mlamouri4e372022015-03-29 14:51:0620
raymese3afee6b2016-04-18 02:00:5021class PermissionContextBase;
timlohc6911802017-03-01 05:37:0322struct PermissionResult;
mlamouri4e372022015-03-29 14:51:0623class Profile;
24
mlamouri4e372022015-03-29 14:51:0625class PermissionManager : public KeyedService,
Andrey Lushnikovf3500102018-07-16 19:55:2226 public content::PermissionControllerDelegate,
mlamouri23957a22015-04-01 10:37:5627 public content_settings::Observer {
mlamouri4e372022015-03-29 14:51:0628 public:
raymese3afee6b2016-04-18 02:00:5029 static PermissionManager* Get(Profile* profile);
30
mlamouri4e372022015-03-29 14:51:0631 explicit PermissionManager(Profile* profile);
32 ~PermissionManager() override;
33
Marc Treib9e4bd922017-09-25 08:32:1334 // Converts from |url|'s actual origin to the "canonical origin" that should
35 // be used for the purpose of requesting/storing permissions. For example, the
Raymes Khouryb474c642018-02-28 06:16:2836 // origin of the local NTP gets mapped to the Google base URL instead. With
37 // Permission Delegation it will transform the requesting origin into
38 // the embedding origin because all permission checks happen on the top level
39 // origin.
40 //
41 // All the public methods below, such as RequestPermission or
42 // GetPermissionStatus, take the actual origin and do the canonicalization
43 // internally. You only need to call this directly if you do something else
44 // with the origin, such as display it in the UI.
Balazs Engedyf39e22b2019-07-30 11:16:2445 GURL GetCanonicalOrigin(ContentSettingsType permission,
46 const GURL& requesting_origin,
Raymes Khouryb474c642018-02-28 06:16:2847 const GURL& embedding_origin) const;
Marc Treib9e4bd922017-09-25 08:32:1348
timloh9a180ad2017-02-20 07:15:2349 // Callers from within chrome/ should use the methods which take the
50 // ContentSettingsType enum. The methods which take PermissionType values
Andrey Lushnikovf3500102018-07-16 19:55:2251 // are for the content::PermissionControllerDelegate overrides and shouldn't
52 // be used from chrome/.
timloh592d7322017-02-23 07:23:5453
timlohc6911802017-03-01 05:37:0354 int RequestPermission(ContentSettingsType permission,
55 content::RenderFrameHost* render_frame_host,
56 const GURL& requesting_origin,
57 bool user_gesture,
danakj47c8fb52019-05-02 16:34:3658 base::OnceCallback<void(ContentSetting)> callback);
timloh592d7322017-02-23 07:23:5459 int RequestPermissions(
60 const std::vector<ContentSettingsType>& permissions,
61 content::RenderFrameHost* render_frame_host,
62 const GURL& requesting_origin,
63 bool user_gesture,
danakj47c8fb52019-05-02 16:34:3664 base::OnceCallback<void(const std::vector<ContentSetting>&)> callback);
timloh592d7322017-02-23 07:23:5465
timlohc6911802017-03-01 05:37:0366 PermissionResult GetPermissionStatus(ContentSettingsType permission,
67 const GURL& requesting_origin,
68 const GURL& embedding_origin);
timloh9a180ad2017-02-20 07:15:2369
raymesf6104d492017-03-09 01:20:1870 // Returns the permission status for a given frame. This should be preferred
71 // over GetPermissionStatus as additional checks can be performed when we know
72 // the exact context the request is coming from.
raymes79f22a612017-03-13 05:28:1073 // TODO(raymes): Currently we still pass the |requesting_origin| as a separate
74 // parameter because we can't yet guarantee that it matches the last committed
75 // origin of the RenderFrameHost. See crbug.com/698985.
raymesf6104d492017-03-09 01:20:1876 PermissionResult GetPermissionStatusForFrame(
77 ContentSettingsType permission,
raymes79f22a612017-03-13 05:28:1078 content::RenderFrameHost* render_frame_host,
79 const GURL& requesting_origin);
raymesf6104d492017-03-09 01:20:1880
Andrey Lushnikovf3500102018-07-16 19:55:2281 // content::PermissionControllerDelegate implementation.
danakj47c8fb52019-05-02 16:34:3682 int RequestPermission(content::PermissionType permission,
83 content::RenderFrameHost* render_frame_host,
84 const GURL& requesting_origin,
85 bool user_gesture,
86 base::OnceCallback<void(blink::mojom::PermissionStatus)>
87 callback) override;
mlamouri8b5ec902015-10-24 00:52:0388 int RequestPermissions(
89 const std::vector<content::PermissionType>& permissions,
90 content::RenderFrameHost* render_frame_host,
91 const GURL& requesting_origin,
benwellsfd2b1552016-07-05 04:26:5392 bool user_gesture,
danakj47c8fb52019-05-02 16:34:3693 base::OnceCallback<
94 void(const std::vector<blink::mojom::PermissionStatus>&)> callback)
leon.han06e55662016-03-26 17:19:4295 override;
mlamouri4e372022015-03-29 14:51:0696 void ResetPermission(content::PermissionType permission,
97 const GURL& requesting_origin,
98 const GURL& embedding_origin) override;
mathpcc29ae52016-05-04 15:22:1799 blink::mojom::PermissionStatus GetPermissionStatus(
mlamouri4e372022015-03-29 14:51:06100 content::PermissionType permission,
101 const GURL& requesting_origin,
102 const GURL& embedding_origin) override;
Raymes Khoury4ead6c32018-03-07 04:43:48103 blink::mojom::PermissionStatus GetPermissionStatusForFrame(
104 content::PermissionType permission,
105 content::RenderFrameHost* render_frame_host,
106 const GURL& requesting_origin) override;
Rohan Pavone013c4002019-08-21 20:13:52107 bool IsPermissionOverridableByDevTools(content::PermissionType permission,
Rohan Pavone8180cba62019-08-26 20:55:09108 const url::Origin& origin) override;
mlamouri23957a22015-04-01 10:37:56109 int SubscribePermissionStatusChange(
110 content::PermissionType permission,
Raymes Khoury3ef4f6e2018-08-09 09:34:48111 content::RenderFrameHost* render_frame_host,
mlamouri23957a22015-04-01 10:37:56112 const GURL& requesting_origin,
danakj47c8fb52019-05-02 16:34:36113 base::RepeatingCallback<void(blink::mojom::PermissionStatus)> callback)
mathpcc29ae52016-05-04 15:22:17114 override;
mlamouri23957a22015-04-01 10:37:56115 void UnsubscribePermissionStatusChange(int subscription_id) override;
mlamouri4e372022015-03-29 14:51:06116
timlohc6911802017-03-01 05:37:03117 // TODO(raymes): Rather than exposing this, use the denial reason from
118 // GetPermissionStatus in callers to determine whether a permission is
raymes893dbdd602016-12-19 22:49:29119 // denied due to the kill switch.
timloh9a180ad2017-02-20 07:15:23120 bool IsPermissionKillSwitchOn(ContentSettingsType);
raymes893dbdd602016-12-19 22:49:29121
Rohan Pavonefaf64572019-07-30 17:50:20122 // For the given |origin|, overrides permissions that belong to |overrides|.
123 // These permissions are in-sync with the PermissionController.
124 void SetPermissionOverridesForDevTools(
Rohan Pavone8180cba62019-08-26 20:55:09125 const url::Origin& origin,
Rohan Pavonefaf64572019-07-30 17:50:20126 const PermissionOverrides& overrides) override;
127 void ResetPermissionOverridesForDevTools() override;
Pavel Feldman73b22022018-11-02 02:55:30128
Alexey Baskakov386f1742019-09-03 04:08:47129 // KeyedService implementation
130 void Shutdown() override;
131
mlamouri4e372022015-03-29 14:51:06132 private:
raymes158a8c12017-07-06 02:52:59133 friend class PermissionManagerTest;
raymese3afee6b2016-04-18 02:00:50134 friend class GeolocationPermissionContextTests;
raymese3afee6b2016-04-18 02:00:50135
mlamouri8b5ec902015-10-24 00:52:03136 class PendingRequest;
Brett Wilsonf976d3f2017-08-18 17:23:39137 using PendingRequestsMap = base::IDMap<std::unique_ptr<PendingRequest>>;
lalitm27583e92015-10-02 11:34:17138
raymes158a8c12017-07-06 02:52:59139 class PermissionResponseCallback;
140
mlamouri23957a22015-04-01 10:37:56141 struct Subscription;
Brett Wilsonf976d3f2017-08-18 17:23:39142 using SubscriptionsMap = base::IDMap<std::unique_ptr<Subscription>>;
mlamouri23957a22015-04-01 10:37:56143
timloh9a180ad2017-02-20 07:15:23144 PermissionContextBase* GetPermissionContext(ContentSettingsType type);
raymese3afee6b2016-04-18 02:00:50145
mlamouri8b5ec902015-10-24 00:52:03146 // Called when a permission was decided for a given PendingRequest. The
147 // PendingRequest is identified by its |request_id| and the permission is
148 // identified by its |permission_id|. If the PendingRequest contains more than
149 // one permission, it will wait for the remaining permissions to be resolved.
150 // When all the permissions have been resolved, the PendingRequest's callback
151 // is run.
timlohc6911802017-03-01 05:37:03152 void OnPermissionsRequestResponseStatus(int request_id,
153 int permission_id,
154 ContentSetting status);
lalitm27583e92015-10-02 11:34:17155
mlamouri23957a22015-04-01 10:37:56156 // content_settings::Observer implementation.
157 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern,
158 const ContentSettingsPattern& secondary_pattern,
159 ContentSettingsType content_type,
Daniel Cheng5bf35542018-06-19 01:15:27160 const std::string& resource_identifier) override;
mlamouri23957a22015-04-01 10:37:56161
raymesf6104d492017-03-09 01:20:18162 PermissionResult GetPermissionStatusHelper(
163 ContentSettingsType permission,
164 content::RenderFrameHost* render_frame_host,
165 const GURL& requesting_origin,
166 const GURL& embedding_origin);
167
Pavel Feldman73b22022018-11-02 02:55:30168 ContentSetting GetPermissionOverrideForDevTools(
Rohan Pavone8180cba62019-08-26 20:55:09169 const url::Origin& origin,
Pavel Feldman73b22022018-11-02 02:55:30170 ContentSettingsType permission);
171
mlamouri4e372022015-03-29 14:51:06172 Profile* profile_;
lalitm27583e92015-10-02 11:34:17173 PendingRequestsMap pending_requests_;
mlamouri23957a22015-04-01 10:37:56174 SubscriptionsMap subscriptions_;
mlamouri4e372022015-03-29 14:51:06175
timloh9a180ad2017-02-20 07:15:23176 std::unordered_map<ContentSettingsType,
dcheng4af48582016-04-19 00:29:35177 std::unique_ptr<PermissionContextBase>,
timloh9a180ad2017-02-20 07:15:23178 ContentSettingsTypeHash>
raymese3afee6b2016-04-18 02:00:50179 permission_contexts_;
Rohan Pavonefaf64572019-07-30 17:50:20180 using ContentSettingsTypeOverrides =
181 base::flat_map<ContentSettingsType, ContentSetting>;
182 std::map<url::Origin, ContentSettingsTypeOverrides>
183 devtools_permission_overrides_;
raymese3afee6b2016-04-18 02:00:50184
Alexey Baskakov386f1742019-09-03 04:08:47185 bool is_shutting_down_ = false;
186
mlamouri4e372022015-03-29 14:51:06187 DISALLOW_COPY_AND_ASSIGN(PermissionManager);
188};
189
190#endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_