[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 1 | // Copyright 2014 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 DuVall | 484c256 | 2020-01-23 22:05:09 | [diff] [blame] | 5 | #ifndef COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_ |
| 6 | #define COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_ |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 7 | |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 10 | #include "base/callback.h" |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 11 | #include "base/macros.h" |
Clark DuVall | 484c256 | 2020-01-23 22:05:09 | [diff] [blame] | 12 | #include "build/build_config.h" |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 13 | #include "components/content_settings/core/common/content_settings.h" |
lshang | ada00c1 | 2016-10-17 04:51:10 | [diff] [blame] | 14 | #include "components/content_settings/core/common/content_settings_types.h" |
Andy Paicu | 4a88f42 | 2020-11-12 18:21:39 | [diff] [blame] | 15 | #include "components/permissions/permission_request_enums.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 16 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | d23cdeee | 2014-03-10 06:39:53 | [diff] [blame] | 17 | #include "url/gurl.h" |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 18 | |
Clark DuVall | 484c256 | 2020-01-23 22:05:09 | [diff] [blame] | 19 | namespace permissions { |
Bret Sepulveda | 362cce4 | 2021-01-13 18:47:54 | [diff] [blame] | 20 | enum class RequestType; |
Clark DuVall | 484c256 | 2020-01-23 22:05:09 | [diff] [blame] | 21 | |
tsergeant | 58defcfb | 2016-07-19 23:47:28 | [diff] [blame] | 22 | // Describes the interface a feature making permission requests should |
| 23 | // implement. A class of this type is registered with the permission request |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 24 | // manager to receive updates about the result of the permissions request |
tsergeant | 58defcfb | 2016-07-19 23:47:28 | [diff] [blame] | 25 | // from the bubble or infobar. It should live until it is unregistered or until |
[email protected] | 634e598 | 2014-04-18 19:20:48 | [diff] [blame] | 26 | // RequestFinished is called. |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 27 | // Note that no particular guarantees are made about what exact UI surface |
| 28 | // is presented to the user. The delegate may be coalesced with other bubble |
| 29 | // requests, or depending on the situation, not shown at all. |
tsergeant | 58defcfb | 2016-07-19 23:47:28 | [diff] [blame] | 30 | class PermissionRequest { |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 31 | public: |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 32 | // If `result` is CONTENT_SETTING_ALLOW, the permission was granted by the |
| 33 | // user. If it's CONTENT_SETTING_BLOCK, the permission was blocked by the |
| 34 | // user. If it's CONTENT_SETTING_DEFAULT, the permission was ignored or |
| 35 | // dismissed without an explicit decision. No other ContentSetting value will |
| 36 | // be passed into this callback. |
| 37 | // If `is_one_time` is true, the decision will last until all tabs of |
| 38 | // `requesting_origin_` are closed or navigated away from. |
| 39 | using PermissionDecidedCallback = |
| 40 | base::OnceCallback<void(ContentSetting /*result*/, bool /*is_one_time*/)>; |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 41 | |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 42 | // `permission_decided_callback` is called when the permission request is |
| 43 | // resolved by the user (see comment on PermissionDecidedCallback above). |
| 44 | // `delete_callback` is called when the permission request is no longer needed |
| 45 | // by the permission system. Therefore, it is safe to delete `this` inside |
| 46 | // `delete_callback`. It will always be called eventually by the permission |
| 47 | // system. |
| 48 | // `delete_callback` may be called before `permission_decided_callback`, for |
| 49 | // example if the tab is closed without user interaction. In this case, the |
| 50 | // javascript promise from the requesting origin will not be resolved. |
| 51 | PermissionRequest(const GURL& requesting_origin, |
| 52 | RequestType request_type, |
| 53 | bool has_gesture, |
| 54 | PermissionDecidedCallback permission_decided_callback, |
| 55 | base::OnceClosure delete_callback); |
| 56 | |
| 57 | PermissionRequest(const PermissionRequest&) = delete; |
| 58 | PermissionRequest& operator=(const PermissionRequest&) = delete; |
| 59 | |
| 60 | virtual ~PermissionRequest(); |
| 61 | |
| 62 | GURL requesting_origin() const { return requesting_origin_; } |
| 63 | RequestType request_type() const { return request_type_; } |
[email protected] | d23cdeee | 2014-03-10 06:39:53 | [diff] [blame] | 64 | |
Bret Sepulveda | d7e4d44 | 2021-04-20 13:46:41 | [diff] [blame] | 65 | // Whether |this| and |other_request| are duplicates and therefore don't both |
| 66 | // need to be shown in the UI. |
| 67 | virtual bool IsDuplicateOf(PermissionRequest* other_request) const; |
| 68 | |
timloh | aa3ce26 | 2017-06-01 05:29:40 | [diff] [blame] | 69 | #if defined(OS_ANDROID) |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 70 | // Returns prompt text appropriate for displaying in an Android dialog. |
| 71 | virtual std::u16string GetDialogMessageText() const; |
timloh | aa3ce26 | 2017-06-01 05:29:40 | [diff] [blame] | 72 | #endif |
| 73 | |
Olesia Marukhno | f8a4bed8 | 2020-06-17 13:35:31 | [diff] [blame] | 74 | #if !defined(OS_ANDROID) |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 75 | // Returns prompt text appropriate for displaying on the chip button in the |
| 76 | // location bar. |
| 77 | absl::optional<std::u16string> GetChipText() const; |
Olesia Marukhno | f8a4bed8 | 2020-06-17 13:35:31 | [diff] [blame] | 78 | |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 79 | // Returns prompt text appropriate for displaying under the dialog title |
| 80 | // "[domain] wants to:". |
| 81 | virtual std::u16string GetMessageTextFragment() const; |
Bret Sepulveda | d7e4d44 | 2021-04-20 13:46:41 | [diff] [blame] | 82 | #endif |
[email protected] | dd1ba69 | 2014-01-24 23:17:37 | [diff] [blame] | 83 | |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 84 | // Called when the user has granted the requested permission. |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 85 | // If |is_one_time| is true the permission will last until all tabs of |
| 86 | // |origin| are closed or navigated away from, and then the permission will |
Ravjit Singh Uppal | c73b5a6 | 2020-11-13 01:38:52 | [diff] [blame] | 87 | // automatically expire after 1 day. |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 88 | void PermissionGranted(bool is_one_time); |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 89 | |
| 90 | // Called when the user has denied the requested permission. |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 91 | void PermissionDenied(); |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 92 | |
| 93 | // Called when the user has cancelled the permission request. This |
| 94 | // corresponds to a denial, but is segregated in case the context needs to |
| 95 | // be able to distinguish between an active refusal or an implicit refusal. |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 96 | void Cancelled(); |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 97 | |
tsergeant | 58defcfb | 2016-07-19 23:47:28 | [diff] [blame] | 98 | // The UI this request was associated with was answered by the user. |
[email protected] | e2ff17e | 2014-02-06 02:32:33 | [diff] [blame] | 99 | // It is safe for the request to be deleted at this point -- it will receive |
tsergeant | 58defcfb | 2016-07-19 23:47:28 | [diff] [blame] | 100 | // no further message from the permission request system. This method will |
[email protected] | e2ff17e | 2014-02-06 02:32:33 | [diff] [blame] | 101 | // eventually be called on every request which is not unregistered. |
Anatoliy Potapchuk | 1c46f7e | 2020-01-23 13:31:03 | [diff] [blame] | 102 | // It is ok to call this method without actually resolving the request via |
| 103 | // PermissionGranted(), PermissionDenied() or Canceled(). However, it will not |
| 104 | // resolve the javascript promise from the requesting origin. |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 105 | void RequestFinished(); |
benwells | 46b02fa | 2016-04-20 02:37:02 | [diff] [blame] | 106 | |
benwells | 471d1f1 | 2016-07-25 23:58:04 | [diff] [blame] | 107 | // Used to record UMA for whether requests are associated with a user gesture. |
| 108 | // To keep things simple this metric is only recorded for the most popular |
| 109 | // request types. |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 110 | PermissionRequestGestureType GetGestureType() const; |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 111 | |
lshang | ada00c1 | 2016-10-17 04:51:10 | [diff] [blame] | 112 | // Used on Android to determine what Android OS permissions are needed for |
| 113 | // this permission request. |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 114 | ContentSettingsType GetContentSettingsType() const; |
lshang | ada00c1 | 2016-10-17 04:51:10 | [diff] [blame] | 115 | |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 116 | private: |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame^] | 117 | // The origin on whose behalf this permission request is being made. |
| 118 | GURL requesting_origin_; |
| 119 | |
| 120 | // The type of this request. |
| 121 | RequestType request_type_; |
| 122 | |
| 123 | // Whether the request was associated with a user gesture. |
| 124 | bool has_gesture_; |
| 125 | |
| 126 | // Called once a decision is made about the permission. |
| 127 | PermissionDecidedCallback permission_decided_callback_; |
| 128 | |
| 129 | // Called when the request is no longer in use so it can be deleted by the |
| 130 | // caller. |
| 131 | base::OnceClosure delete_callback_; |
[email protected] | efad90f | 2014-01-17 00:45:54 | [diff] [blame] | 132 | }; |
| 133 | |
Clark DuVall | 484c256 | 2020-01-23 22:05:09 | [diff] [blame] | 134 | } // namespace permissions |
| 135 | |
| 136 | #endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_ |