1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_
6 #define COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_
10 #include "base/functional/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "build/build_config.h"
13 #include "components/content_settings/core/common/content_settings.h"
14 #include "components/content_settings/core/common/content_settings_types.h"
15 #include "components/permissions/permission_request_data.h"
16 #include "components/permissions/permission_request_enums.h"
17 #include "components/permissions/request_type.h"
18 #include "content/public/browser/global_routing_id.h"
19 #include "third_party/abseil-cpp/absl/types/optional.h"
22 namespace permissions {
23 enum class RequestType;
25 // Describes the interface a feature making permission requests should
26 // implement. A class of this type is registered with the permission request
27 // manager to receive updates about the result of the permissions request
28 // from the bubble or infobar. It should live until it is unregistered or until
29 // RequestFinished is called.
30 // 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.
33 class PermissionRequest {
35 // 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 =
43 base::RepeatingCallback<void(ContentSetting /*result*/,
45 bool /*is_final_decision*/)>;
47 // `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
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,
59 PermissionDecidedCallback permission_decided_callback,
60 base::OnceClosure delete_callback);
62 PermissionRequest(PermissionRequestData request_data,
63 PermissionDecidedCallback permission_decided_callback,
64 base::OnceClosure delete_callback);
66 PermissionRequest(const PermissionRequest&) = delete;
67 PermissionRequest& operator=(const PermissionRequest&) = delete;
73 ALLOW_ONCE_CONFIRMATION,
75 ACCESSIBILITY_ALLOWED_CONFIRMATION,
76 ACCESSIBILITY_ALLOWED_ONCE_CONFIRMATION,
77 ACCESSIBILITY_BLOCKED_CONFIRMATION
80 virtual ~PermissionRequest();
82 GURL requesting_origin() const { return data_.requesting_origin; }
83 RequestType request_type() const;
85 // 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;
89 #if BUILDFLAG(IS_ANDROID)
90 // Returns prompt text appropriate for displaying in an Android dialog.
91 virtual std::u16string GetDialogMessageText() const;
94 // Returns a weak pointer to this instance.
95 base::WeakPtr<PermissionRequest> GetWeakPtr();
97 #if !BUILDFLAG(IS_ANDROID)
98 // Returns whether displaying a confirmation chip for the request is
100 bool IsConfirmationChipSupported();
102 // Returns prompt icon appropriate for displaying on the chip button in the
104 IconId GetIconForChip();
106 // Returns prompt icon appropriate for displaying on the quiet chip button in
108 IconId GetBlockedIconForChip();
110 // Returns prompt text appropriate for displaying on the chip button in the
112 absl::optional<std::u16string> GetRequestChipText(ChipTextType type) const;
114 // Returns prompt text appropriate for displaying under the dialog title
115 // "[domain] wants to:".
116 virtual std::u16string GetMessageTextFragment() const;
119 // Whether the request was initiated by the user clicking on the permission
121 bool IsEmbeddedPermissionElementInitiated() const;
123 // Returns true if the request has two origins and should use the two origin
124 // prompt. Returns false otherwise.
125 bool ShouldUseTwoOriginPrompt() const;
127 // Called when the user has granted the requested permission.
128 // If |is_one_time| is true the permission will last until all tabs of
129 // |origin| are closed or navigated away from, and then the permission will
130 // automatically expire after 1 day.
131 void PermissionGranted(bool is_one_time);
133 // Called when the user has denied the requested permission.
134 void PermissionDenied();
136 // Called when the user has cancelled the permission request. This
137 // corresponds to a denial, but is segregated in case the context needs to
138 // be able to distinguish between an active refusal or an implicit refusal.
139 void Cancelled(bool is_final_decision = true);
141 // The UI this request was associated with was answered by the user.
142 // It is safe for the request to be deleted at this point -- it will receive
143 // no further message from the permission request system. This method will
144 // eventually be called on every request which is not unregistered.
145 // It is ok to call this method without actually resolving the request via
146 // PermissionGranted(), PermissionDenied() or Canceled(). However, it will not
147 // resolve the javascript promise from the requesting origin.
148 void RequestFinished();
150 // Used to record UMA for whether requests are associated with a user gesture.
151 // To keep things simple this metric is only recorded for the most popular
153 PermissionRequestGestureType GetGestureType() const;
155 // Used on Android to determine what Android OS permissions are needed for
156 // this permission request.
157 ContentSettingsType GetContentSettingsType() const;
159 void set_requesting_frame_id(content::GlobalRenderFrameHostId id) {
160 data_.id.set_global_render_frame_host_id(id);
163 const content::GlobalRenderFrameHostId& get_requesting_frame_id() {
164 return data_.id.global_render_frame_host_id();
167 // Permission name text fragment which can be used in permission prompts to
168 // identify the permission being requested.
169 virtual std::u16string GetPermissionNameTextFragment() const;
172 // Sets whether this request is permission element initiated, for testing
174 void SetEmbeddedPermissionElementInitiatedForTesting(
175 bool embedded_permission_element_initiated);
178 PermissionRequestData data_;
180 // Called once a decision is made about the permission.
181 PermissionDecidedCallback permission_decided_callback_;
183 // Called when the request is no longer in use so it can be deleted by the
185 base::OnceClosure delete_callback_;
187 base::WeakPtrFactory<PermissionRequest> weak_factory_{this};
190 } // namespace permissions
192 #endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_