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" |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 11 | #include "base/id_map.h" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 12 | #include "base/macros.h" |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
raymes | 7c1c35a2 | 2016-06-23 00:20:53 | [diff] [blame] | 14 | #include "chrome/browser/permissions/permission_util.h" |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 15 | #include "components/content_settings/core/browser/content_settings_observer.h" |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 16 | #include "components/content_settings/core/common/content_settings.h" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 17 | #include "components/keyed_service/core/keyed_service.h" |
| 18 | #include "content/public/browser/permission_manager.h" |
| 19 | |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 20 | class PermissionContextBase; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 21 | class Profile; |
| 22 | |
| 23 | namespace content { |
| 24 | enum class PermissionType; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 25 | }; // namespace content |
| 26 | |
| 27 | class PermissionManager : public KeyedService, |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 28 | public content::PermissionManager, |
| 29 | public content_settings::Observer { |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 30 | public: |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 31 | static PermissionManager* Get(Profile* profile); |
| 32 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 33 | explicit PermissionManager(Profile* profile); |
| 34 | ~PermissionManager() override; |
| 35 | |
| 36 | // content::PermissionManager implementation. |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 37 | int RequestPermission( |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 38 | content::PermissionType permission, |
mlamouri | 5fc460f | 2015-06-03 17:30:28 | [diff] [blame] | 39 | content::RenderFrameHost* render_frame_host, |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 40 | const GURL& requesting_origin, |
benwells | fd2b155 | 2016-07-05 04:26:53 | [diff] [blame] | 41 | bool user_gesture, |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 42 | const base::Callback<void(blink::mojom::PermissionStatus)>& callback) |
| 43 | override; |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 44 | int RequestPermissions( |
| 45 | const std::vector<content::PermissionType>& permissions, |
| 46 | content::RenderFrameHost* render_frame_host, |
| 47 | const GURL& requesting_origin, |
benwells | fd2b155 | 2016-07-05 04:26:53 | [diff] [blame] | 48 | bool user_gesture, |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 49 | const base::Callback< |
| 50 | void(const std::vector<blink::mojom::PermissionStatus>&)>& callback) |
leon.han | 06e5566 | 2016-03-26 17:19:42 | [diff] [blame] | 51 | override; |
lalitm | 5bd7eb04 | 2015-10-02 16:00:29 | [diff] [blame] | 52 | void CancelPermissionRequest(int request_id) override; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 53 | void ResetPermission(content::PermissionType permission, |
| 54 | const GURL& requesting_origin, |
| 55 | const GURL& embedding_origin) override; |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 56 | blink::mojom::PermissionStatus GetPermissionStatus( |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 57 | content::PermissionType permission, |
| 58 | const GURL& requesting_origin, |
| 59 | const GURL& embedding_origin) override; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 60 | void RegisterPermissionUsage(content::PermissionType permission, |
| 61 | const GURL& requesting_origin, |
| 62 | const GURL& embedding_origin) override; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 63 | int SubscribePermissionStatusChange( |
| 64 | content::PermissionType permission, |
| 65 | const GURL& requesting_origin, |
| 66 | const GURL& embedding_origin, |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 67 | const base::Callback<void(blink::mojom::PermissionStatus)>& callback) |
| 68 | override; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 69 | void UnsubscribePermissionStatusChange(int subscription_id) override; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 70 | |
raymes | 893dbdd60 | 2016-12-19 22:49:29 | [diff] [blame^] | 71 | // TODO(raymes): Rather than exposing this, expose a denial reason from |
| 72 | // GetPermissionStatus so that callers can determine whether a permission is |
| 73 | // denied due to the kill switch. |
| 74 | bool IsPermissionKillSwitchOn(content::PermissionType permission); |
| 75 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 76 | private: |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 77 | friend class GeolocationPermissionContextTests; |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 78 | |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 79 | class PendingRequest; |
rlanday | de24c6d | 2016-12-01 03:05:40 | [diff] [blame] | 80 | using PendingRequestsMap = IDMap<std::unique_ptr<PendingRequest>>; |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 81 | |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 82 | struct Subscription; |
rlanday | de24c6d | 2016-12-01 03:05:40 | [diff] [blame] | 83 | using SubscriptionsMap = IDMap<std::unique_ptr<Subscription>>; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 84 | |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 85 | PermissionContextBase* GetPermissionContext(content::PermissionType type); |
| 86 | |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 87 | // Called when a permission was decided for a given PendingRequest. The |
| 88 | // PendingRequest is identified by its |request_id| and the permission is |
| 89 | // identified by its |permission_id|. If the PendingRequest contains more than |
| 90 | // one permission, it will wait for the remaining permissions to be resolved. |
| 91 | // When all the permissions have been resolved, the PendingRequest's callback |
| 92 | // is run. |
| 93 | void OnPermissionsRequestResponseStatus( |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 94 | int request_id, |
mlamouri | 8b5ec90 | 2015-10-24 00:52:03 | [diff] [blame] | 95 | int permission_id, |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 96 | blink::mojom::PermissionStatus status); |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 97 | |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 98 | // content_settings::Observer implementation. |
| 99 | void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern, |
| 100 | const ContentSettingsPattern& secondary_pattern, |
| 101 | ContentSettingsType content_type, |
| 102 | std::string resource_identifier) override; |
| 103 | |
raymes | 893dbdd60 | 2016-12-19 22:49:29 | [diff] [blame^] | 104 | ContentSetting GetPermissionStatusInternal(content::PermissionType permission, |
| 105 | const GURL& requesting_origin, |
| 106 | const GURL& embedding_origin); |
| 107 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 108 | Profile* profile_; |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 109 | PendingRequestsMap pending_requests_; |
mlamouri | 23957a2 | 2015-04-01 10:37:56 | [diff] [blame] | 110 | SubscriptionsMap subscriptions_; |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 111 | |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 112 | std::unordered_map<content::PermissionType, |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 113 | std::unique_ptr<PermissionContextBase>, |
raymes | e3afee6b | 2016-04-18 02:00:50 | [diff] [blame] | 114 | PermissionTypeHash> |
| 115 | permission_contexts_; |
| 116 | |
lalitm | 27583e9 | 2015-10-02 11:34:17 | [diff] [blame] | 117 | base::WeakPtrFactory<PermissionManager> weak_ptr_factory_; |
| 118 | |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(PermissionManager); |
| 120 | }; |
| 121 | |
| 122 | #endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_ |