blob: 6523048ced49746a8c7b3ebd4da9d37fd2e52968 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2014 The Chromium Authors
[email protected]efad90f2014-01-17 00:45:542// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Clark DuVall484c2562020-01-23 22:05:095#ifndef COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_
6#define COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_
[email protected]efad90f2014-01-17 00:45:547
Arthur Sonzognic571efb2024-01-26 20:26:188#include <optional>
Jan Wilken Dörriead587c32021-03-11 14:09:279#include <string>
10
Avi Drissman12be0312023-01-11 09:16:0911#include "base/functional/callback.h"
Thomas Nguyen9f1ff7302023-03-30 12:23:3512#include "base/memory/weak_ptr.h"
Clark DuVall484c2562020-01-23 22:05:0913#include "build/build_config.h"
Bret Sepulveda5327d8b52021-07-21 17:44:2314#include "components/content_settings/core/common/content_settings.h"
lshangada00c12016-10-17 04:51:1015#include "components/content_settings/core/common/content_settings_types.h"
Piotr Bialeckib62b90cc2025-04-18 12:39:5816#include "components/permissions/permission_hats_trigger_helper.h"
Andy Paicu0a6d4b502023-08-29 15:13:0917#include "components/permissions/permission_request_data.h"
Andy Paicu4a88f422020-11-12 18:21:3918#include "components/permissions/permission_request_enums.h"
Illia Klimovfabd8b52021-10-21 07:15:4019#include "components/permissions/request_type.h"
Illia Klimove406ecc12022-11-22 15:53:2920#include "content/public/browser/global_routing_id.h"
[email protected]d23cdeee2014-03-10 06:39:5321#include "url/gurl.h"
[email protected]efad90f2014-01-17 00:45:5422
Clark DuVall484c2562020-01-23 22:05:0923namespace permissions {
Bret Sepulveda362cce42021-01-13 18:47:5424enum class RequestType;
tsergeant58defcfb2016-07-19 23:47:2825// Describes the interface a feature making permission requests should
26// implement. A class of this type is registered with the permission request
[email protected]efad90f2014-01-17 00:45:5427// manager to receive updates about the result of the permissions request
tsergeant58defcfb2016-07-19 23:47:2828// from the bubble or infobar. It should live until it is unregistered or until
[email protected]634e5982014-04-18 19:20:4829// RequestFinished is called.
[email protected]efad90f2014-01-17 00:45:5430// Note that no particular guarantees are made about what exact UI surface
31// is presented to the user. The delegate may be coalesced with other bubble
32// requests, or depending on the situation, not shown at all.
tsergeant58defcfb2016-07-19 23:47:2833class PermissionRequest {
[email protected]efad90f2014-01-17 00:45:5434 public:
Bret Sepulveda5327d8b52021-07-21 17:44:2335 // If `result` is CONTENT_SETTING_ALLOW, the permission was granted by the
36 // user. If it's CONTENT_SETTING_BLOCK, the permission was blocked by the
37 // user. If it's CONTENT_SETTING_DEFAULT, the permission was ignored or
38 // dismissed without an explicit decision. No other ContentSetting value will
39 // be passed into this callback.
40 // If `is_one_time` is true, the decision will last until all tabs of
41 // `requesting_origin_` are closed or navigated away from.
42 using PermissionDecidedCallback =
Illia Klimove406ecc12022-11-22 15:53:2943 base::RepeatingCallback<void(ContentSetting /*result*/,
44 bool /*is_one_time*/,
45 bool /*is_final_decision*/)>;
[email protected]efad90f2014-01-17 00:45:5446
Bret Sepulveda5327d8b52021-07-21 17:44:2347 // `permission_decided_callback` is called when the permission request is
48 // resolved by the user (see comment on PermissionDecidedCallback above).
49 // `delete_callback` is called when the permission request is no longer needed
50 // by the permission system. Therefore, it is safe to delete `this` inside
51 // `delete_callback`. It will always be called eventually by the permission
52 // system.
53 // `delete_callback` may be called before `permission_decided_callback`, for
54 // example if the tab is closed without user interaction. In this case, the
55 // javascript promise from the requesting origin will not be resolved.
56 PermissionRequest(const GURL& requesting_origin,
57 RequestType request_type,
58 bool has_gesture,
59 PermissionDecidedCallback permission_decided_callback,
60 base::OnceClosure delete_callback);
61
Andy Paicu0a6d4b502023-08-29 15:13:0962 PermissionRequest(PermissionRequestData request_data,
63 PermissionDecidedCallback permission_decided_callback,
Elad Alonb3331802024-01-24 22:29:3664 base::OnceClosure delete_callback,
65 bool uses_automatic_embargo);
Andy Paicu0a6d4b502023-08-29 15:13:0966
Bret Sepulveda5327d8b52021-07-21 17:44:2367 PermissionRequest(const PermissionRequest&) = delete;
68 PermissionRequest& operator=(const PermissionRequest&) = delete;
69
Florian Jackyeeb62062022-10-05 18:04:0770 enum ChipTextType {
71 LOUD_REQUEST,
72 QUIET_REQUEST,
73 ALLOW_CONFIRMATION,
Florian Jacky2b3f54f2023-05-03 08:31:5274 ALLOW_ONCE_CONFIRMATION,
Florian Jackyeeb62062022-10-05 18:04:0775 BLOCKED_CONFIRMATION,
76 ACCESSIBILITY_ALLOWED_CONFIRMATION,
Florian Jacky2b3f54f2023-05-03 08:31:5277 ACCESSIBILITY_ALLOWED_ONCE_CONFIRMATION,
Florian Jackyeeb62062022-10-05 18:04:0778 ACCESSIBILITY_BLOCKED_CONFIRMATION
79 };
80
Bret Sepulveda5327d8b52021-07-21 17:44:2381 virtual ~PermissionRequest();
82
Andy Paicu0a6d4b502023-08-29 15:13:0983 GURL requesting_origin() const { return data_.requesting_origin; }
84 RequestType request_type() const;
[email protected]d23cdeee2014-03-10 06:39:5385
Bret Sepulvedad7e4d442021-04-20 13:46:4186 // Whether |this| and |other_request| are duplicates and therefore don't both
87 // need to be shown in the UI.
88 virtual bool IsDuplicateOf(PermissionRequest* other_request) const;
89
Xiaohan Wange813582302022-01-14 14:50:4690#if BUILDFLAG(IS_ANDROID)
Florian Jacky00aea9832024-01-15 08:27:0591 // A message text with formatting information.
92 struct AnnotatedMessageText {
93 // |text| specifies the text string itself.
94 // |bolded_ranges| defines a (potentially empty) list of ranges represented
95 // as pairs of <textOffset, rangeSize>, which shall be used by the UI to
96 // format the specified ranges as bold text.
97 AnnotatedMessageText(std::u16string text,
98 std::vector<std::pair<size_t, size_t>> bolded_ranges);
99 ~AnnotatedMessageText();
100 AnnotatedMessageText(const AnnotatedMessageText& other) = delete;
101 AnnotatedMessageText& operator=(const AnnotatedMessageText& other) = delete;
102
103 std::u16string text;
104
105 // A list of ranges defined as pairs of <offset, size> which
106 // will be used by Clank to format the ranges in |text| as bold.
107 std::vector<std::pair<size_t, size_t>> bolded_ranges;
108 };
109
110 virtual AnnotatedMessageText GetDialogAnnotatedMessageText(
111 const GURL& embedding_origin) const;
112
Bret Sepulveda5327d8b52021-07-21 17:44:23113 // Returns prompt text appropriate for displaying in an Android dialog.
Florian Jacky00aea9832024-01-15 08:27:05114 static AnnotatedMessageText GetDialogAnnotatedMessageText(
115 std::u16string requesting_origin_formatted_for_display,
116 int message_id,
117 bool format_origin_bold);
timlohaa3ce262017-06-01 05:29:40118#endif
119
Thomas Nguyen9f1ff7302023-03-30 12:23:35120 // Returns a weak pointer to this instance.
121 base::WeakPtr<PermissionRequest> GetWeakPtr();
122
Xiaohan Wange813582302022-01-14 14:50:46123#if !BUILDFLAG(IS_ANDROID)
Florian Jacky4748cf32022-10-06 09:04:18124 // Returns whether displaying a confirmation chip for the request is
125 // supported.
126 bool IsConfirmationChipSupported();
127
Illia Klimovfabd8b52021-10-21 07:15:40128 // Returns prompt icon appropriate for displaying on the chip button in the
129 // location bar.
130 IconId GetIconForChip();
131
132 // Returns prompt icon appropriate for displaying on the quiet chip button in
133 // the location bar.
134 IconId GetBlockedIconForChip();
135
Bret Sepulveda5327d8b52021-07-21 17:44:23136 // Returns prompt text appropriate for displaying on the chip button in the
137 // location bar.
Arthur Sonzognic571efb2024-01-26 20:26:18138 std::optional<std::u16string> GetRequestChipText(ChipTextType type) const;
Olesia Marukhnof8a4bed82020-06-17 13:35:31139
Bret Sepulveda5327d8b52021-07-21 17:44:23140 // Returns prompt text appropriate for displaying under the dialog title
141 // "[domain] wants to:".
142 virtual std::u16string GetMessageTextFragment() const;
Bret Sepulvedad7e4d442021-04-20 13:46:41143#endif
[email protected]dd1ba692014-01-24 23:17:37144
Daniel d'Andradab51fb9e22023-12-18 20:10:04145 // Returns the text to be used in the "allow always" button of the
146 // permission prompt.
147 // If not provided, the generic text for this button will be used instead.
148 // The default implementation returns std::nullopt (ie, use generic text).
149 virtual std::optional<std::u16string> GetAllowAlwaysText() const;
150
Luke Klimek76bda632025-03-13 21:06:46151 // Returns the text to be used in the "block" button of the permission
152 // prompt.
153 //
154 // If not provided, the generic text for this button will be used instead.
155 // The default implementation returns std::nullopt (ie, use generic text).
156 virtual std::optional<std::u16string> GetBlockText() const;
157
Kamila04c0b182023-09-01 09:47:55158 // Whether the request was initiated by the user clicking on the permission
159 // element.
160 bool IsEmbeddedPermissionElementInitiated() const;
161
Andy Paicu1d4b6502024-03-08 10:20:11162 // Returns the position of the element that caused the prompt to open.
163 std::optional<gfx::Rect> GetAnchorElementPosition() const;
164
Filipa Senra1f968d452023-04-24 17:15:40165 // Returns true if the request has two origins and should use the two origin
166 // prompt. Returns false otherwise.
167 bool ShouldUseTwoOriginPrompt() const;
168
[email protected]efad90f2014-01-17 00:45:54169 // Called when the user has granted the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23170 // If |is_one_time| is true the permission will last until all tabs of
171 // |origin| are closed or navigated away from, and then the permission will
Ravjit Singh Uppalc73b5a62020-11-13 01:38:52172 // automatically expire after 1 day.
Bret Sepulveda5327d8b52021-07-21 17:44:23173 void PermissionGranted(bool is_one_time);
[email protected]efad90f2014-01-17 00:45:54174
175 // Called when the user has denied the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23176 void PermissionDenied();
[email protected]efad90f2014-01-17 00:45:54177
178 // Called when the user has cancelled the permission request. This
179 // corresponds to a denial, but is segregated in case the context needs to
180 // be able to distinguish between an active refusal or an implicit refusal.
Illia Klimove406ecc12022-11-22 15:53:29181 void Cancelled(bool is_final_decision = true);
[email protected]efad90f2014-01-17 00:45:54182
tsergeant58defcfb2016-07-19 23:47:28183 // The UI this request was associated with was answered by the user.
[email protected]e2ff17e2014-02-06 02:32:33184 // It is safe for the request to be deleted at this point -- it will receive
tsergeant58defcfb2016-07-19 23:47:28185 // no further message from the permission request system. This method will
[email protected]e2ff17e2014-02-06 02:32:33186 // eventually be called on every request which is not unregistered.
Anatoliy Potapchuk1c46f7e2020-01-23 13:31:03187 // It is ok to call this method without actually resolving the request via
188 // PermissionGranted(), PermissionDenied() or Canceled(). However, it will not
189 // resolve the javascript promise from the requesting origin.
Bret Sepulveda5327d8b52021-07-21 17:44:23190 void RequestFinished();
benwells46b02fa2016-04-20 02:37:02191
benwells471d1f12016-07-25 23:58:04192 // Used to record UMA for whether requests are associated with a user gesture.
193 // To keep things simple this metric is only recorded for the most popular
194 // request types.
Bret Sepulveda5327d8b52021-07-21 17:44:23195 PermissionRequestGestureType GetGestureType() const;
dominicknd4e446a2016-09-13 07:44:13196
ahmedmoussa6b4e55f2024-04-05 18:56:38197 virtual const std::vector<std::string>& GetRequestedAudioCaptureDeviceIds()
198 const;
199 virtual const std::vector<std::string>& GetRequestedVideoCaptureDeviceIds()
200 const;
Bryant Chandlereff3f1b2024-01-12 16:42:40201
lshangada00c12016-10-17 04:51:10202 // Used on Android to determine what Android OS permissions are needed for
203 // this permission request.
Bret Sepulveda5327d8b52021-07-21 17:44:23204 ContentSettingsType GetContentSettingsType() const;
lshangada00c12016-10-17 04:51:10205
Illia Klimove406ecc12022-11-22 15:53:29206 void set_requesting_frame_id(content::GlobalRenderFrameHostId id) {
Andy Paicu0a6d4b502023-08-29 15:13:09207 data_.id.set_global_render_frame_host_id(id);
Illia Klimove406ecc12022-11-22 15:53:29208 }
209
Andy Paicu0a6d4b502023-08-29 15:13:09210 const content::GlobalRenderFrameHostId& get_requesting_frame_id() {
211 return data_.id.global_render_frame_host_id();
Illia Klimove406ecc12022-11-22 15:53:29212 }
213
Andy Paicu11734362023-09-11 09:39:29214 // Permission name text fragment which can be used in permission prompts to
215 // identify the permission being requested.
216 virtual std::u16string GetPermissionNameTextFragment() const;
217
Elad Alonb3331802024-01-24 22:29:36218 bool uses_automatic_embargo() const { return uses_automatic_embargo_; }
219
Piotr Bialeckib62b90cc2025-04-18 12:39:58220 std::optional<PermissionHatsTriggerHelper::PreviewParametersForHats>
221 get_preview_parameters() const;
222
223 void set_preview_parameters(
224 PermissionHatsTriggerHelper::PreviewParametersForHats preview_parmeters);
225
Andy Paicu489b8d52023-10-16 20:49:55226 protected:
227 // Sets whether this request is permission element initiated, for testing
228 // subclasses only.
229 void SetEmbeddedPermissionElementInitiatedForTesting(
230 bool embedded_permission_element_initiated);
231
dominicknd4e446a2016-09-13 07:44:13232 private:
Andy Paicu0a6d4b502023-08-29 15:13:09233 PermissionRequestData data_;
Bret Sepulveda5327d8b52021-07-21 17:44:23234
235 // Called once a decision is made about the permission.
236 PermissionDecidedCallback permission_decided_callback_;
237
238 // Called when the request is no longer in use so it can be deleted by the
239 // caller.
240 base::OnceClosure delete_callback_;
Thomas Nguyen9f1ff7302023-03-30 12:23:35241
Elad Alonb3331802024-01-24 22:29:36242 const bool uses_automatic_embargo_ = true;
243
Piotr Bialeckib62b90cc2025-04-18 12:39:58244 std::optional<PermissionHatsTriggerHelper::PreviewParametersForHats>
245 preview_parameters_;
246
Thomas Nguyen9f1ff7302023-03-30 12:23:35247 base::WeakPtrFactory<PermissionRequest> weak_factory_{this};
[email protected]efad90f2014-01-17 00:45:54248};
249
Clark DuVall484c2562020-01-23 22:05:09250} // namespace permissions
251
252#endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_