blob: eae05c47db271d0ef23e401de8ab13426a683664 [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
Jan Wilken Dörriead587c32021-03-11 14:09:278#include <string>
9
Avi Drissman12be0312023-01-11 09:16:0910#include "base/functional/callback.h"
Thomas Nguyen9f1ff7302023-03-30 12:23:3511#include "base/memory/weak_ptr.h"
Clark DuVall484c2562020-01-23 22:05:0912#include "build/build_config.h"
Bret Sepulveda5327d8b52021-07-21 17:44:2313#include "components/content_settings/core/common/content_settings.h"
lshangada00c12016-10-17 04:51:1014#include "components/content_settings/core/common/content_settings_types.h"
Andy Paicu0a6d4b502023-08-29 15:13:0915#include "components/permissions/permission_request_data.h"
Andy Paicu4a88f422020-11-12 18:21:3916#include "components/permissions/permission_request_enums.h"
Illia Klimovfabd8b52021-10-21 07:15:4017#include "components/permissions/request_type.h"
Illia Klimove406ecc12022-11-22 15:53:2918#include "content/public/browser/global_routing_id.h"
Anton Bikineev1156b5f2021-05-15 22:35:3619#include "third_party/abseil-cpp/absl/types/optional.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.
Florian Jackyeeb62062022-10-05 18:04:07137 absl::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
Kamila04c0b182023-09-01 09:47:55150 // Whether the request was initiated by the user clicking on the permission
151 // element.
152 bool IsEmbeddedPermissionElementInitiated() const;
153
Filipa Senra1f968d452023-04-24 17:15:40154 // Returns true if the request has two origins and should use the two origin
155 // prompt. Returns false otherwise.
156 bool ShouldUseTwoOriginPrompt() const;
157
[email protected]efad90f2014-01-17 00:45:54158 // Called when the user has granted the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23159 // If |is_one_time| is true the permission will last until all tabs of
160 // |origin| are closed or navigated away from, and then the permission will
Ravjit Singh Uppalc73b5a62020-11-13 01:38:52161 // automatically expire after 1 day.
Bret Sepulveda5327d8b52021-07-21 17:44:23162 void PermissionGranted(bool is_one_time);
[email protected]efad90f2014-01-17 00:45:54163
164 // Called when the user has denied the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23165 void PermissionDenied();
[email protected]efad90f2014-01-17 00:45:54166
167 // Called when the user has cancelled the permission request. This
168 // corresponds to a denial, but is segregated in case the context needs to
169 // be able to distinguish between an active refusal or an implicit refusal.
Illia Klimove406ecc12022-11-22 15:53:29170 void Cancelled(bool is_final_decision = true);
[email protected]efad90f2014-01-17 00:45:54171
tsergeant58defcfb2016-07-19 23:47:28172 // The UI this request was associated with was answered by the user.
[email protected]e2ff17e2014-02-06 02:32:33173 // It is safe for the request to be deleted at this point -- it will receive
tsergeant58defcfb2016-07-19 23:47:28174 // no further message from the permission request system. This method will
[email protected]e2ff17e2014-02-06 02:32:33175 // eventually be called on every request which is not unregistered.
Anatoliy Potapchuk1c46f7e2020-01-23 13:31:03176 // It is ok to call this method without actually resolving the request via
177 // PermissionGranted(), PermissionDenied() or Canceled(). However, it will not
178 // resolve the javascript promise from the requesting origin.
Bret Sepulveda5327d8b52021-07-21 17:44:23179 void RequestFinished();
benwells46b02fa2016-04-20 02:37:02180
benwells471d1f12016-07-25 23:58:04181 // Used to record UMA for whether requests are associated with a user gesture.
182 // To keep things simple this metric is only recorded for the most popular
183 // request types.
Bret Sepulveda5327d8b52021-07-21 17:44:23184 PermissionRequestGestureType GetGestureType() const;
dominicknd4e446a2016-09-13 07:44:13185
Bryant Chandlereff3f1b2024-01-12 16:42:40186 const std::vector<std::string>& GetRequestedAudioCaptureDeviceIds() const;
187 const std::vector<std::string>& GetRequestedVideoCaptureDeviceIds() const;
188
lshangada00c12016-10-17 04:51:10189 // Used on Android to determine what Android OS permissions are needed for
190 // this permission request.
Bret Sepulveda5327d8b52021-07-21 17:44:23191 ContentSettingsType GetContentSettingsType() const;
lshangada00c12016-10-17 04:51:10192
Illia Klimove406ecc12022-11-22 15:53:29193 void set_requesting_frame_id(content::GlobalRenderFrameHostId id) {
Andy Paicu0a6d4b502023-08-29 15:13:09194 data_.id.set_global_render_frame_host_id(id);
Illia Klimove406ecc12022-11-22 15:53:29195 }
196
Andy Paicu0a6d4b502023-08-29 15:13:09197 const content::GlobalRenderFrameHostId& get_requesting_frame_id() {
198 return data_.id.global_render_frame_host_id();
Illia Klimove406ecc12022-11-22 15:53:29199 }
200
Andy Paicu11734362023-09-11 09:39:29201 // Permission name text fragment which can be used in permission prompts to
202 // identify the permission being requested.
203 virtual std::u16string GetPermissionNameTextFragment() const;
204
Elad Alonb3331802024-01-24 22:29:36205 bool uses_automatic_embargo() const { return uses_automatic_embargo_; }
206
Andy Paicu489b8d52023-10-16 20:49:55207 protected:
208 // Sets whether this request is permission element initiated, for testing
209 // subclasses only.
210 void SetEmbeddedPermissionElementInitiatedForTesting(
211 bool embedded_permission_element_initiated);
212
dominicknd4e446a2016-09-13 07:44:13213 private:
Andy Paicu0a6d4b502023-08-29 15:13:09214 PermissionRequestData data_;
Bret Sepulveda5327d8b52021-07-21 17:44:23215
216 // Called once a decision is made about the permission.
217 PermissionDecidedCallback permission_decided_callback_;
218
219 // Called when the request is no longer in use so it can be deleted by the
220 // caller.
221 base::OnceClosure delete_callback_;
Thomas Nguyen9f1ff7302023-03-30 12:23:35222
Elad Alonb3331802024-01-24 22:29:36223 const bool uses_automatic_embargo_ = true;
224
Thomas Nguyen9f1ff7302023-03-30 12:23:35225 base::WeakPtrFactory<PermissionRequest> weak_factory_{this};
[email protected]efad90f2014-01-17 00:45:54226};
227
Clark DuVall484c2562020-01-23 22:05:09228} // namespace permissions
229
230#endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_