blob: 935c7303843890c416c113a255025c7592f362d7 [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"
Andy Paicu0a6d4b502023-08-29 15:13:0916#include "components/permissions/permission_request_data.h"
Andy Paicu4a88f422020-11-12 18:21:3917#include "components/permissions/permission_request_enums.h"
Illia Klimovfabd8b52021-10-21 07:15:4018#include "components/permissions/request_type.h"
Illia Klimove406ecc12022-11-22 15:53:2919#include "content/public/browser/global_routing_id.h"
[email protected]d23cdeee2014-03-10 06:39:5320#include "url/gurl.h"
[email protected]efad90f2014-01-17 00:45:5421
Clark DuVall484c2562020-01-23 22:05:0922namespace permissions {
Bret Sepulveda362cce42021-01-13 18:47:5423enum class RequestType;
tsergeant58defcfb2016-07-19 23:47:2824// Describes the interface a feature making permission requests should
25// implement. A class of this type is registered with the permission request
[email protected]efad90f2014-01-17 00:45:5426// manager to receive updates about the result of the permissions request
tsergeant58defcfb2016-07-19 23:47:2827// from the bubble or infobar. It should live until it is unregistered or until
[email protected]634e5982014-04-18 19:20:4828// RequestFinished is called.
[email protected]efad90f2014-01-17 00:45:5429// Note that no particular guarantees are made about what exact UI surface
30// is presented to the user. The delegate may be coalesced with other bubble
31// requests, or depending on the situation, not shown at all.
tsergeant58defcfb2016-07-19 23:47:2832class PermissionRequest {
[email protected]efad90f2014-01-17 00:45:5433 public:
Bret Sepulveda5327d8b52021-07-21 17:44:2334 // If `result` is CONTENT_SETTING_ALLOW, the permission was granted by the
35 // user. If it's CONTENT_SETTING_BLOCK, the permission was blocked by the
36 // user. If it's CONTENT_SETTING_DEFAULT, the permission was ignored or
37 // dismissed without an explicit decision. No other ContentSetting value will
38 // be passed into this callback.
39 // If `is_one_time` is true, the decision will last until all tabs of
40 // `requesting_origin_` are closed or navigated away from.
41 using PermissionDecidedCallback =
Illia Klimove406ecc12022-11-22 15:53:2942 base::RepeatingCallback<void(ContentSetting /*result*/,
43 bool /*is_one_time*/,
44 bool /*is_final_decision*/)>;
[email protected]efad90f2014-01-17 00:45:5445
Bret Sepulveda5327d8b52021-07-21 17:44:2346 // `permission_decided_callback` is called when the permission request is
47 // resolved by the user (see comment on PermissionDecidedCallback above).
48 // `delete_callback` is called when the permission request is no longer needed
49 // by the permission system. Therefore, it is safe to delete `this` inside
50 // `delete_callback`. It will always be called eventually by the permission
51 // system.
52 // `delete_callback` may be called before `permission_decided_callback`, for
53 // example if the tab is closed without user interaction. In this case, the
54 // javascript promise from the requesting origin will not be resolved.
55 PermissionRequest(const GURL& requesting_origin,
56 RequestType request_type,
57 bool has_gesture,
58 PermissionDecidedCallback permission_decided_callback,
59 base::OnceClosure delete_callback);
60
Andy Paicu0a6d4b502023-08-29 15:13:0961 PermissionRequest(PermissionRequestData request_data,
62 PermissionDecidedCallback permission_decided_callback,
Elad Alonb3331802024-01-24 22:29:3663 base::OnceClosure delete_callback,
64 bool uses_automatic_embargo);
Andy Paicu0a6d4b502023-08-29 15:13:0965
Bret Sepulveda5327d8b52021-07-21 17:44:2366 PermissionRequest(const PermissionRequest&) = delete;
67 PermissionRequest& operator=(const PermissionRequest&) = delete;
68
Florian Jackyeeb62062022-10-05 18:04:0769 enum ChipTextType {
70 LOUD_REQUEST,
71 QUIET_REQUEST,
72 ALLOW_CONFIRMATION,
Florian Jacky2b3f54f2023-05-03 08:31:5273 ALLOW_ONCE_CONFIRMATION,
Florian Jackyeeb62062022-10-05 18:04:0774 BLOCKED_CONFIRMATION,
75 ACCESSIBILITY_ALLOWED_CONFIRMATION,
Florian Jacky2b3f54f2023-05-03 08:31:5276 ACCESSIBILITY_ALLOWED_ONCE_CONFIRMATION,
Florian Jackyeeb62062022-10-05 18:04:0777 ACCESSIBILITY_BLOCKED_CONFIRMATION
78 };
79
Bret Sepulveda5327d8b52021-07-21 17:44:2380 virtual ~PermissionRequest();
81
Andy Paicu0a6d4b502023-08-29 15:13:0982 GURL requesting_origin() const { return data_.requesting_origin; }
83 RequestType request_type() const;
[email protected]d23cdeee2014-03-10 06:39:5384
Bret Sepulvedad7e4d442021-04-20 13:46:4185 // Whether |this| and |other_request| are duplicates and therefore don't both
86 // need to be shown in the UI.
87 virtual bool IsDuplicateOf(PermissionRequest* other_request) const;
88
Xiaohan Wange813582302022-01-14 14:50:4689#if BUILDFLAG(IS_ANDROID)
Florian Jacky00aea9832024-01-15 08:27:0590 // A message text with formatting information.
91 struct AnnotatedMessageText {
92 // |text| specifies the text string itself.
93 // |bolded_ranges| defines a (potentially empty) list of ranges represented
94 // as pairs of <textOffset, rangeSize>, which shall be used by the UI to
95 // format the specified ranges as bold text.
96 AnnotatedMessageText(std::u16string text,
97 std::vector<std::pair<size_t, size_t>> bolded_ranges);
98 ~AnnotatedMessageText();
99 AnnotatedMessageText(const AnnotatedMessageText& other) = delete;
100 AnnotatedMessageText& operator=(const AnnotatedMessageText& other) = delete;
101
102 std::u16string text;
103
104 // A list of ranges defined as pairs of <offset, size> which
105 // will be used by Clank to format the ranges in |text| as bold.
106 std::vector<std::pair<size_t, size_t>> bolded_ranges;
107 };
108
109 virtual AnnotatedMessageText GetDialogAnnotatedMessageText(
110 const GURL& embedding_origin) const;
111
Bret Sepulveda5327d8b52021-07-21 17:44:23112 // Returns prompt text appropriate for displaying in an Android dialog.
Florian Jacky00aea9832024-01-15 08:27:05113 static AnnotatedMessageText GetDialogAnnotatedMessageText(
114 std::u16string requesting_origin_formatted_for_display,
115 int message_id,
116 bool format_origin_bold);
timlohaa3ce262017-06-01 05:29:40117#endif
118
Thomas Nguyen9f1ff7302023-03-30 12:23:35119 // Returns a weak pointer to this instance.
120 base::WeakPtr<PermissionRequest> GetWeakPtr();
121
Xiaohan Wange813582302022-01-14 14:50:46122#if !BUILDFLAG(IS_ANDROID)
Florian Jacky4748cf32022-10-06 09:04:18123 // Returns whether displaying a confirmation chip for the request is
124 // supported.
125 bool IsConfirmationChipSupported();
126
Illia Klimovfabd8b52021-10-21 07:15:40127 // Returns prompt icon appropriate for displaying on the chip button in the
128 // location bar.
129 IconId GetIconForChip();
130
131 // Returns prompt icon appropriate for displaying on the quiet chip button in
132 // the location bar.
133 IconId GetBlockedIconForChip();
134
Bret Sepulveda5327d8b52021-07-21 17:44:23135 // Returns prompt text appropriate for displaying on the chip button in the
136 // location bar.
Arthur Sonzognic571efb2024-01-26 20:26:18137 std::optional<std::u16string> GetRequestChipText(ChipTextType type) const;
Olesia Marukhnof8a4bed82020-06-17 13:35:31138
Bret Sepulveda5327d8b52021-07-21 17:44:23139 // Returns prompt text appropriate for displaying under the dialog title
140 // "[domain] wants to:".
141 virtual std::u16string GetMessageTextFragment() const;
Bret Sepulvedad7e4d442021-04-20 13:46:41142#endif
[email protected]dd1ba692014-01-24 23:17:37143
Daniel d'Andradab51fb9e22023-12-18 20:10:04144 // Returns the text to be used in the "allow always" button of the
145 // permission prompt.
146 // If not provided, the generic text for this button will be used instead.
147 // The default implementation returns std::nullopt (ie, use generic text).
148 virtual std::optional<std::u16string> GetAllowAlwaysText() const;
149
Luke Klimek76bda632025-03-13 21:06:46150 // Returns the text to be used in the "block" button of the permission
151 // prompt.
152 //
153 // If not provided, the generic text for this button will be used instead.
154 // The default implementation returns std::nullopt (ie, use generic text).
155 virtual std::optional<std::u16string> GetBlockText() const;
156
Kamila04c0b182023-09-01 09:47:55157 // Whether the request was initiated by the user clicking on the permission
158 // element.
159 bool IsEmbeddedPermissionElementInitiated() const;
160
Andy Paicu1d4b6502024-03-08 10:20:11161 // Returns the position of the element that caused the prompt to open.
162 std::optional<gfx::Rect> GetAnchorElementPosition() const;
163
Filipa Senra1f968d452023-04-24 17:15:40164 // Returns true if the request has two origins and should use the two origin
165 // prompt. Returns false otherwise.
166 bool ShouldUseTwoOriginPrompt() const;
167
[email protected]efad90f2014-01-17 00:45:54168 // Called when the user has granted the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23169 // If |is_one_time| is true the permission will last until all tabs of
170 // |origin| are closed or navigated away from, and then the permission will
Ravjit Singh Uppalc73b5a62020-11-13 01:38:52171 // automatically expire after 1 day.
Bret Sepulveda5327d8b52021-07-21 17:44:23172 void PermissionGranted(bool is_one_time);
[email protected]efad90f2014-01-17 00:45:54173
174 // Called when the user has denied the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23175 void PermissionDenied();
[email protected]efad90f2014-01-17 00:45:54176
177 // Called when the user has cancelled the permission request. This
178 // corresponds to a denial, but is segregated in case the context needs to
179 // be able to distinguish between an active refusal or an implicit refusal.
Illia Klimove406ecc12022-11-22 15:53:29180 void Cancelled(bool is_final_decision = true);
[email protected]efad90f2014-01-17 00:45:54181
tsergeant58defcfb2016-07-19 23:47:28182 // The UI this request was associated with was answered by the user.
[email protected]e2ff17e2014-02-06 02:32:33183 // It is safe for the request to be deleted at this point -- it will receive
tsergeant58defcfb2016-07-19 23:47:28184 // no further message from the permission request system. This method will
[email protected]e2ff17e2014-02-06 02:32:33185 // eventually be called on every request which is not unregistered.
Anatoliy Potapchuk1c46f7e2020-01-23 13:31:03186 // It is ok to call this method without actually resolving the request via
187 // PermissionGranted(), PermissionDenied() or Canceled(). However, it will not
188 // resolve the javascript promise from the requesting origin.
Bret Sepulveda5327d8b52021-07-21 17:44:23189 void RequestFinished();
benwells46b02fa2016-04-20 02:37:02190
benwells471d1f12016-07-25 23:58:04191 // Used to record UMA for whether requests are associated with a user gesture.
192 // To keep things simple this metric is only recorded for the most popular
193 // request types.
Bret Sepulveda5327d8b52021-07-21 17:44:23194 PermissionRequestGestureType GetGestureType() const;
dominicknd4e446a2016-09-13 07:44:13195
ahmedmoussa6b4e55f2024-04-05 18:56:38196 virtual const std::vector<std::string>& GetRequestedAudioCaptureDeviceIds()
197 const;
198 virtual const std::vector<std::string>& GetRequestedVideoCaptureDeviceIds()
199 const;
Bryant Chandlereff3f1b2024-01-12 16:42:40200
lshangada00c12016-10-17 04:51:10201 // Used on Android to determine what Android OS permissions are needed for
202 // this permission request.
Bret Sepulveda5327d8b52021-07-21 17:44:23203 ContentSettingsType GetContentSettingsType() const;
lshangada00c12016-10-17 04:51:10204
Illia Klimove406ecc12022-11-22 15:53:29205 void set_requesting_frame_id(content::GlobalRenderFrameHostId id) {
Andy Paicu0a6d4b502023-08-29 15:13:09206 data_.id.set_global_render_frame_host_id(id);
Illia Klimove406ecc12022-11-22 15:53:29207 }
208
Andy Paicu0a6d4b502023-08-29 15:13:09209 const content::GlobalRenderFrameHostId& get_requesting_frame_id() {
210 return data_.id.global_render_frame_host_id();
Illia Klimove406ecc12022-11-22 15:53:29211 }
212
Andy Paicu11734362023-09-11 09:39:29213 // Permission name text fragment which can be used in permission prompts to
214 // identify the permission being requested.
215 virtual std::u16string GetPermissionNameTextFragment() const;
216
Elad Alonb3331802024-01-24 22:29:36217 bool uses_automatic_embargo() const { return uses_automatic_embargo_; }
218
Andy Paicu489b8d52023-10-16 20:49:55219 protected:
220 // Sets whether this request is permission element initiated, for testing
221 // subclasses only.
222 void SetEmbeddedPermissionElementInitiatedForTesting(
223 bool embedded_permission_element_initiated);
224
dominicknd4e446a2016-09-13 07:44:13225 private:
Andy Paicu0a6d4b502023-08-29 15:13:09226 PermissionRequestData data_;
Bret Sepulveda5327d8b52021-07-21 17:44:23227
228 // Called once a decision is made about the permission.
229 PermissionDecidedCallback permission_decided_callback_;
230
231 // Called when the request is no longer in use so it can be deleted by the
232 // caller.
233 base::OnceClosure delete_callback_;
Thomas Nguyen9f1ff7302023-03-30 12:23:35234
Elad Alonb3331802024-01-24 22:29:36235 const bool uses_automatic_embargo_ = true;
236
Thomas Nguyen9f1ff7302023-03-30 12:23:35237 base::WeakPtrFactory<PermissionRequest> weak_factory_{this};
[email protected]efad90f2014-01-17 00:45:54238};
239
Clark DuVall484c2562020-01-23 22:05:09240} // namespace permissions
241
242#endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_