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 | |
| 5 | #ifndef CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_ |
| 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" |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 11 | #include "base/containers/id_map.h" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 12 | #include "base/macros.h" |
raymes | 7c1c35a2 | 2016-06-23 00:20:53 | [diff] [blame] | 13 | #include "chrome/browser/permissions/permission_util.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" |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 17 | #include "content/public/browser/permission_controller_delegate.h" |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 18 | #include "content/public/browser/permission_type.h" |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 19 | #include "url/origin.h" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 20 | |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 21 | class PermissionContextBase; |
timloh | c691180 | 2017-03-01 05:37:03 | [diff] [blame] | 22 | struct PermissionResult; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 23 | class Profile; |
| 24 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 25 | class PermissionManager : public KeyedService, |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 26 | public content::PermissionControllerDelegate, |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 27 | public content_settings::Observer { |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 28 | public: |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 29 | static PermissionManager* Get(Profile* profile); |
| 30 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 31 | explicit PermissionManager(Profile* profile); |
| 32 | ~PermissionManager() override; |
| 33 | |
Marc Treib | 9e4bd92 | 2017-09-25 08:32:13 | [diff] [blame] | 34 | // 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 Khoury | b474c64 | 2018-02-28 06:16:28 | [diff] [blame] | 36 | // 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 Engedy | f39e22b | 2019-07-30 11:16:24 | [diff] [blame] | 45 | GURL GetCanonicalOrigin(ContentSettingsType permission, |
| 46 | const GURL& requesting_origin, |
Raymes Khoury | b474c64 | 2018-02-28 06:16:28 | [diff] [blame] | 47 | const GURL& embedding_origin) const; |
Marc Treib | 9e4bd92 | 2017-09-25 08:32:13 | [diff] [blame] | 48 | |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 49 | // Callers from within chrome/ should use the methods which take the |
| 50 | // ContentSettingsType enum. The methods which take PermissionType values |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 51 | // are for the content::PermissionControllerDelegate overrides and shouldn't |
| 52 | // be used from chrome/. |
timloh | 592d732 | 2017-02-23 07:23:54 | [diff] [blame] | 53 | |
timloh | c691180 | 2017-03-01 05:37:03 | [diff] [blame] | 54 | int RequestPermission(ContentSettingsType permission, |
| 55 | content::RenderFrameHost* render_frame_host, |
| 56 | const GURL& requesting_origin, |
| 57 | bool user_gesture, |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 58 | base::OnceCallback<void(ContentSetting)> callback); |
timloh | 592d732 | 2017-02-23 07:23:54 | [diff] [blame] | 59 | int RequestPermissions( |
| 60 | const std::vector<ContentSettingsType>& permissions, |
| 61 | content::RenderFrameHost* render_frame_host, |
| 62 | const GURL& requesting_origin, |
| 63 | bool user_gesture, |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 64 | base::OnceCallback<void(const std::vector<ContentSetting>&)> callback); |
timloh | 592d732 | 2017-02-23 07:23:54 | [diff] [blame] | 65 | |
timloh | c691180 | 2017-03-01 05:37:03 | [diff] [blame] | 66 | PermissionResult GetPermissionStatus(ContentSettingsType permission, |
| 67 | const GURL& requesting_origin, |
| 68 | const GURL& embedding_origin); |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 69 | |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 70 | // 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. |
raymes | 79f22a61 | 2017-03-13 05:28:10 | [diff] [blame] | 73 | // 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. |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 76 | PermissionResult GetPermissionStatusForFrame( |
| 77 | ContentSettingsType permission, |
raymes | 79f22a61 | 2017-03-13 05:28:10 | [diff] [blame] | 78 | content::RenderFrameHost* render_frame_host, |
| 79 | const GURL& requesting_origin); |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 80 | |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 81 | // content::PermissionControllerDelegate implementation. |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 82 | 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; |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 88 | int RequestPermissions( |
| 89 | const std::vector<content::PermissionType>& permissions, |
| 90 | content::RenderFrameHost* render_frame_host, |
| 91 | const GURL& requesting_origin, |
benwells | fd2b155 | 2016-07-05 04:26:53 | [diff] [blame] | 92 | bool user_gesture, |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 93 | base::OnceCallback< |
| 94 | void(const std::vector<blink::mojom::PermissionStatus>&)> callback) |
leon.han | 06e5566 | 2016-03-26 17:19:42 | [diff] [blame] | 95 | override; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 96 | void ResetPermission(content::PermissionType permission, |
| 97 | const GURL& requesting_origin, |
| 98 | const GURL& embedding_origin) override; |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 99 | blink::mojom::PermissionStatus GetPermissionStatus( |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 100 | content::PermissionType permission, |
| 101 | const GURL& requesting_origin, |
| 102 | const GURL& embedding_origin) override; |
Raymes Khoury | 4ead6c3 | 2018-03-07 04:43:48 | [diff] [blame] | 103 | blink::mojom::PermissionStatus GetPermissionStatusForFrame( |
| 104 | content::PermissionType permission, |
| 105 | content::RenderFrameHost* render_frame_host, |
| 106 | const GURL& requesting_origin) override; |
Rohan Pavone | 013c400 | 2019-08-21 20:13:52 | [diff] [blame] | 107 | bool IsPermissionOverridableByDevTools(content::PermissionType permission, |
Rohan Pavone | 8180cba6 | 2019-08-26 20:55:09 | [diff] [blame] | 108 | const url::Origin& origin) override; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 109 | int SubscribePermissionStatusChange( |
| 110 | content::PermissionType permission, |
Raymes Khoury | 3ef4f6e | 2018-08-09 09:34:48 | [diff] [blame] | 111 | content::RenderFrameHost* render_frame_host, |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 112 | const GURL& requesting_origin, |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 113 | base::RepeatingCallback<void(blink::mojom::PermissionStatus)> callback) |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 114 | override; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 115 | void UnsubscribePermissionStatusChange(int subscription_id) override; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 116 | |
timloh | c691180 | 2017-03-01 05:37:03 | [diff] [blame] | 117 | // TODO(raymes): Rather than exposing this, use the denial reason from |
| 118 | // GetPermissionStatus in callers to determine whether a permission is |
raymes | 893dbdd60 | 2016-12-19 22:49:29 | [diff] [blame] | 119 | // denied due to the kill switch. |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 120 | bool IsPermissionKillSwitchOn(ContentSettingsType); |
raymes | 893dbdd60 | 2016-12-19 22:49:29 | [diff] [blame] | 121 | |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 122 | // For the given |origin|, overrides permissions that belong to |overrides|. |
| 123 | // These permissions are in-sync with the PermissionController. |
| 124 | void SetPermissionOverridesForDevTools( |
Rohan Pavone | 8180cba6 | 2019-08-26 20:55:09 | [diff] [blame] | 125 | const url::Origin& origin, |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 126 | const PermissionOverrides& overrides) override; |
| 127 | void ResetPermissionOverridesForDevTools() override; |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 128 | |
Alexey Baskakov | 386f174 | 2019-09-03 04:08:47 | [diff] [blame^] | 129 | // KeyedService implementation |
| 130 | void Shutdown() override; |
| 131 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 132 | private: |
raymes | 158a8c1 | 2017-07-06 02:52:59 | [diff] [blame] | 133 | friend class PermissionManagerTest; |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 134 | friend class GeolocationPermissionContextTests; |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 135 | |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 136 | class PendingRequest; |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 137 | using PendingRequestsMap = base::IDMap<std::unique_ptr<PendingRequest>>; |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 138 | |
raymes | 158a8c1 | 2017-07-06 02:52:59 | [diff] [blame] | 139 | class PermissionResponseCallback; |
| 140 | |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 141 | struct Subscription; |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 142 | using SubscriptionsMap = base::IDMap<std::unique_ptr<Subscription>>; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 143 | |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 144 | PermissionContextBase* GetPermissionContext(ContentSettingsType type); |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 145 | |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 146 | // 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. |
timloh | c691180 | 2017-03-01 05:37:03 | [diff] [blame] | 152 | void OnPermissionsRequestResponseStatus(int request_id, |
| 153 | int permission_id, |
| 154 | ContentSetting status); |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 155 | |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 156 | // content_settings::Observer implementation. |
| 157 | void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern, |
| 158 | const ContentSettingsPattern& secondary_pattern, |
| 159 | ContentSettingsType content_type, |
Daniel Cheng | 5bf3554 | 2018-06-19 01:15:27 | [diff] [blame] | 160 | const std::string& resource_identifier) override; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 161 | |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 162 | PermissionResult GetPermissionStatusHelper( |
| 163 | ContentSettingsType permission, |
| 164 | content::RenderFrameHost* render_frame_host, |
| 165 | const GURL& requesting_origin, |
| 166 | const GURL& embedding_origin); |
| 167 | |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 168 | ContentSetting GetPermissionOverrideForDevTools( |
Rohan Pavone | 8180cba6 | 2019-08-26 20:55:09 | [diff] [blame] | 169 | const url::Origin& origin, |
Pavel Feldman | 73b2202 | 2018-11-02 02:55:30 | [diff] [blame] | 170 | ContentSettingsType permission); |
| 171 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 172 | Profile* profile_; |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 173 | PendingRequestsMap pending_requests_; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 174 | SubscriptionsMap subscriptions_; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 175 | |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 176 | std::unordered_map<ContentSettingsType, |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 177 | std::unique_ptr<PermissionContextBase>, |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 178 | ContentSettingsTypeHash> |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 179 | permission_contexts_; |
Rohan Pavone | faf6457 | 2019-07-30 17:50:20 | [diff] [blame] | 180 | using ContentSettingsTypeOverrides = |
| 181 | base::flat_map<ContentSettingsType, ContentSetting>; |
| 182 | std::map<url::Origin, ContentSettingsTypeOverrides> |
| 183 | devtools_permission_overrides_; |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 184 | |
Alexey Baskakov | 386f174 | 2019-09-03 04:08:47 | [diff] [blame^] | 185 | bool is_shutting_down_ = false; |
| 186 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 187 | DISALLOW_COPY_AND_ASSIGN(PermissionManager); |
| 188 | }; |
| 189 | |
| 190 | #endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_ |