blob: d0c57a54ec368594e8bdf28824473427f1522c48 [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"
Clark DuVall484c2562020-01-23 22:05:0911#include "build/build_config.h"
Bret Sepulveda5327d8b52021-07-21 17:44:2312#include "components/content_settings/core/common/content_settings.h"
lshangada00c12016-10-17 04:51:1013#include "components/content_settings/core/common/content_settings_types.h"
Andy Paicu4a88f422020-11-12 18:21:3914#include "components/permissions/permission_request_enums.h"
Illia Klimovfabd8b52021-10-21 07:15:4015#include "components/permissions/request_type.h"
Illia Klimove406ecc12022-11-22 15:53:2916#include "content/public/browser/global_routing_id.h"
Anton Bikineev1156b5f2021-05-15 22:35:3617#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]d23cdeee2014-03-10 06:39:5318#include "url/gurl.h"
[email protected]efad90f2014-01-17 00:45:5419
Clark DuVall484c2562020-01-23 22:05:0920namespace permissions {
Bret Sepulveda362cce42021-01-13 18:47:5421enum class RequestType;
Clark DuVall484c2562020-01-23 22:05:0922
tsergeant58defcfb2016-07-19 23:47:2823// Describes the interface a feature making permission requests should
24// implement. A class of this type is registered with the permission request
[email protected]efad90f2014-01-17 00:45:5425// manager to receive updates about the result of the permissions request
tsergeant58defcfb2016-07-19 23:47:2826// from the bubble or infobar. It should live until it is unregistered or until
[email protected]634e5982014-04-18 19:20:4827// RequestFinished is called.
[email protected]efad90f2014-01-17 00:45:5428// Note that no particular guarantees are made about what exact UI surface
29// is presented to the user. The delegate may be coalesced with other bubble
30// requests, or depending on the situation, not shown at all.
tsergeant58defcfb2016-07-19 23:47:2831class PermissionRequest {
[email protected]efad90f2014-01-17 00:45:5432 public:
Bret Sepulveda5327d8b52021-07-21 17:44:2333 // If `result` is CONTENT_SETTING_ALLOW, the permission was granted by the
34 // user. If it's CONTENT_SETTING_BLOCK, the permission was blocked by the
35 // user. If it's CONTENT_SETTING_DEFAULT, the permission was ignored or
36 // dismissed without an explicit decision. No other ContentSetting value will
37 // be passed into this callback.
38 // If `is_one_time` is true, the decision will last until all tabs of
39 // `requesting_origin_` are closed or navigated away from.
40 using PermissionDecidedCallback =
Illia Klimove406ecc12022-11-22 15:53:2941 base::RepeatingCallback<void(ContentSetting /*result*/,
42 bool /*is_one_time*/,
43 bool /*is_final_decision*/)>;
[email protected]efad90f2014-01-17 00:45:5444
Bret Sepulveda5327d8b52021-07-21 17:44:2345 // `permission_decided_callback` is called when the permission request is
46 // resolved by the user (see comment on PermissionDecidedCallback above).
47 // `delete_callback` is called when the permission request is no longer needed
48 // by the permission system. Therefore, it is safe to delete `this` inside
49 // `delete_callback`. It will always be called eventually by the permission
50 // system.
51 // `delete_callback` may be called before `permission_decided_callback`, for
52 // example if the tab is closed without user interaction. In this case, the
53 // javascript promise from the requesting origin will not be resolved.
54 PermissionRequest(const GURL& requesting_origin,
55 RequestType request_type,
56 bool has_gesture,
57 PermissionDecidedCallback permission_decided_callback,
58 base::OnceClosure delete_callback);
59
60 PermissionRequest(const PermissionRequest&) = delete;
61 PermissionRequest& operator=(const PermissionRequest&) = delete;
62
Florian Jackyeeb62062022-10-05 18:04:0763 enum ChipTextType {
64 LOUD_REQUEST,
65 QUIET_REQUEST,
66 ALLOW_CONFIRMATION,
67 BLOCKED_CONFIRMATION,
68 ACCESSIBILITY_ALLOWED_CONFIRMATION,
69 ACCESSIBILITY_BLOCKED_CONFIRMATION
70 };
71
Bret Sepulveda5327d8b52021-07-21 17:44:2372 virtual ~PermissionRequest();
73
74 GURL requesting_origin() const { return requesting_origin_; }
75 RequestType request_type() const { return request_type_; }
[email protected]d23cdeee2014-03-10 06:39:5376
Bret Sepulvedad7e4d442021-04-20 13:46:4177 // Whether |this| and |other_request| are duplicates and therefore don't both
78 // need to be shown in the UI.
79 virtual bool IsDuplicateOf(PermissionRequest* other_request) const;
80
Xiaohan Wange813582302022-01-14 14:50:4681#if BUILDFLAG(IS_ANDROID)
Bret Sepulveda5327d8b52021-07-21 17:44:2382 // Returns prompt text appropriate for displaying in an Android dialog.
83 virtual std::u16string GetDialogMessageText() const;
timlohaa3ce262017-06-01 05:29:4084#endif
85
Xiaohan Wange813582302022-01-14 14:50:4686#if !BUILDFLAG(IS_ANDROID)
Florian Jacky4748cf32022-10-06 09:04:1887 // Returns whether displaying a confirmation chip for the request is
88 // supported.
89 bool IsConfirmationChipSupported();
90
Illia Klimovfabd8b52021-10-21 07:15:4091 // Returns prompt icon appropriate for displaying on the chip button in the
92 // location bar.
93 IconId GetIconForChip();
94
95 // Returns prompt icon appropriate for displaying on the quiet chip button in
96 // the location bar.
97 IconId GetBlockedIconForChip();
98
Bret Sepulveda5327d8b52021-07-21 17:44:2399 // Returns prompt text appropriate for displaying on the chip button in the
100 // location bar.
Florian Jackyeeb62062022-10-05 18:04:07101 absl::optional<std::u16string> GetRequestChipText(ChipTextType type) const;
Olesia Marukhnof8a4bed82020-06-17 13:35:31102
Bret Sepulveda5327d8b52021-07-21 17:44:23103 // Returns prompt text appropriate for displaying under the dialog title
104 // "[domain] wants to:".
105 virtual std::u16string GetMessageTextFragment() const;
Bret Sepulvedad7e4d442021-04-20 13:46:41106#endif
[email protected]dd1ba692014-01-24 23:17:37107
[email protected]efad90f2014-01-17 00:45:54108 // Called when the user has granted the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23109 // If |is_one_time| is true the permission will last until all tabs of
110 // |origin| are closed or navigated away from, and then the permission will
Ravjit Singh Uppalc73b5a62020-11-13 01:38:52111 // automatically expire after 1 day.
Bret Sepulveda5327d8b52021-07-21 17:44:23112 void PermissionGranted(bool is_one_time);
[email protected]efad90f2014-01-17 00:45:54113
114 // Called when the user has denied the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23115 void PermissionDenied();
[email protected]efad90f2014-01-17 00:45:54116
117 // Called when the user has cancelled the permission request. This
118 // corresponds to a denial, but is segregated in case the context needs to
119 // be able to distinguish between an active refusal or an implicit refusal.
Illia Klimove406ecc12022-11-22 15:53:29120 void Cancelled(bool is_final_decision = true);
[email protected]efad90f2014-01-17 00:45:54121
tsergeant58defcfb2016-07-19 23:47:28122 // The UI this request was associated with was answered by the user.
[email protected]e2ff17e2014-02-06 02:32:33123 // It is safe for the request to be deleted at this point -- it will receive
tsergeant58defcfb2016-07-19 23:47:28124 // no further message from the permission request system. This method will
[email protected]e2ff17e2014-02-06 02:32:33125 // eventually be called on every request which is not unregistered.
Anatoliy Potapchuk1c46f7e2020-01-23 13:31:03126 // It is ok to call this method without actually resolving the request via
127 // PermissionGranted(), PermissionDenied() or Canceled(). However, it will not
128 // resolve the javascript promise from the requesting origin.
Bret Sepulveda5327d8b52021-07-21 17:44:23129 void RequestFinished();
benwells46b02fa2016-04-20 02:37:02130
benwells471d1f12016-07-25 23:58:04131 // Used to record UMA for whether requests are associated with a user gesture.
132 // To keep things simple this metric is only recorded for the most popular
133 // request types.
Bret Sepulveda5327d8b52021-07-21 17:44:23134 PermissionRequestGestureType GetGestureType() const;
dominicknd4e446a2016-09-13 07:44:13135
lshangada00c12016-10-17 04:51:10136 // Used on Android to determine what Android OS permissions are needed for
137 // this permission request.
Bret Sepulveda5327d8b52021-07-21 17:44:23138 ContentSettingsType GetContentSettingsType() const;
lshangada00c12016-10-17 04:51:10139
Illia Klimove406ecc12022-11-22 15:53:29140 void set_requesting_frame_id(content::GlobalRenderFrameHostId id) {
141 request_frame_id_ = id;
142 }
143
144 content::GlobalRenderFrameHostId& get_requesting_frame_id() {
145 return request_frame_id_;
146 }
147
dominicknd4e446a2016-09-13 07:44:13148 private:
Illia Klimove406ecc12022-11-22 15:53:29149 content::GlobalRenderFrameHostId request_frame_id_;
150
Bret Sepulveda5327d8b52021-07-21 17:44:23151 // The origin on whose behalf this permission request is being made.
152 GURL requesting_origin_;
153
154 // The type of this request.
155 RequestType request_type_;
156
157 // Whether the request was associated with a user gesture.
158 bool has_gesture_;
159
160 // Called once a decision is made about the permission.
161 PermissionDecidedCallback permission_decided_callback_;
162
163 // Called when the request is no longer in use so it can be deleted by the
164 // caller.
165 base::OnceClosure delete_callback_;
[email protected]efad90f2014-01-17 00:45:54166};
167
Clark DuVall484c2562020-01-23 22:05:09168} // namespace permissions
169
170#endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_