blob: d688ff5dacf72ab83e1e94d52eba34cbc1a90245 [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
Bret Sepulveda5327d8b52021-07-21 17:44:2310#include "base/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"
Anton Bikineev1156b5f2021-05-15 22:35:3616#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]d23cdeee2014-03-10 06:39:5317#include "url/gurl.h"
[email protected]efad90f2014-01-17 00:45:5418
Clark DuVall484c2562020-01-23 22:05:0919namespace permissions {
Bret Sepulveda362cce42021-01-13 18:47:5420enum class RequestType;
Clark DuVall484c2562020-01-23 22:05:0921
tsergeant58defcfb2016-07-19 23:47:2822// Describes the interface a feature making permission requests should
23// implement. A class of this type is registered with the permission request
[email protected]efad90f2014-01-17 00:45:5424// manager to receive updates about the result of the permissions request
tsergeant58defcfb2016-07-19 23:47:2825// from the bubble or infobar. It should live until it is unregistered or until
[email protected]634e5982014-04-18 19:20:4826// RequestFinished is called.
[email protected]efad90f2014-01-17 00:45:5427// Note that no particular guarantees are made about what exact UI surface
28// is presented to the user. The delegate may be coalesced with other bubble
29// requests, or depending on the situation, not shown at all.
tsergeant58defcfb2016-07-19 23:47:2830class PermissionRequest {
[email protected]efad90f2014-01-17 00:45:5431 public:
Bret Sepulveda5327d8b52021-07-21 17:44:2332 // If `result` is CONTENT_SETTING_ALLOW, the permission was granted by the
33 // user. If it's CONTENT_SETTING_BLOCK, the permission was blocked by the
34 // user. If it's CONTENT_SETTING_DEFAULT, the permission was ignored or
35 // dismissed without an explicit decision. No other ContentSetting value will
36 // be passed into this callback.
37 // If `is_one_time` is true, the decision will last until all tabs of
38 // `requesting_origin_` are closed or navigated away from.
39 using PermissionDecidedCallback =
40 base::OnceCallback<void(ContentSetting /*result*/, bool /*is_one_time*/)>;
[email protected]efad90f2014-01-17 00:45:5441
Bret Sepulveda5327d8b52021-07-21 17:44:2342 // `permission_decided_callback` is called when the permission request is
43 // resolved by the user (see comment on PermissionDecidedCallback above).
44 // `delete_callback` is called when the permission request is no longer needed
45 // by the permission system. Therefore, it is safe to delete `this` inside
46 // `delete_callback`. It will always be called eventually by the permission
47 // system.
48 // `delete_callback` may be called before `permission_decided_callback`, for
49 // example if the tab is closed without user interaction. In this case, the
50 // javascript promise from the requesting origin will not be resolved.
51 PermissionRequest(const GURL& requesting_origin,
52 RequestType request_type,
53 bool has_gesture,
54 PermissionDecidedCallback permission_decided_callback,
55 base::OnceClosure delete_callback);
56
57 PermissionRequest(const PermissionRequest&) = delete;
58 PermissionRequest& operator=(const PermissionRequest&) = delete;
59
Florian Jackyeeb62062022-10-05 18:04:0760 enum ChipTextType {
61 LOUD_REQUEST,
62 QUIET_REQUEST,
63 ALLOW_CONFIRMATION,
64 BLOCKED_CONFIRMATION,
65 ACCESSIBILITY_ALLOWED_CONFIRMATION,
66 ACCESSIBILITY_BLOCKED_CONFIRMATION
67 };
68
Bret Sepulveda5327d8b52021-07-21 17:44:2369 virtual ~PermissionRequest();
70
71 GURL requesting_origin() const { return requesting_origin_; }
72 RequestType request_type() const { return request_type_; }
[email protected]d23cdeee2014-03-10 06:39:5373
Bret Sepulvedad7e4d442021-04-20 13:46:4174 // Whether |this| and |other_request| are duplicates and therefore don't both
75 // need to be shown in the UI.
76 virtual bool IsDuplicateOf(PermissionRequest* other_request) const;
77
Xiaohan Wange813582302022-01-14 14:50:4678#if BUILDFLAG(IS_ANDROID)
Bret Sepulveda5327d8b52021-07-21 17:44:2379 // Returns prompt text appropriate for displaying in an Android dialog.
80 virtual std::u16string GetDialogMessageText() const;
timlohaa3ce262017-06-01 05:29:4081#endif
82
Xiaohan Wange813582302022-01-14 14:50:4683#if !BUILDFLAG(IS_ANDROID)
Florian Jacky4748cf32022-10-06 09:04:1884 // Returns whether displaying a confirmation chip for the request is
85 // supported.
86 bool IsConfirmationChipSupported();
87
Illia Klimovfabd8b52021-10-21 07:15:4088 // Returns prompt icon appropriate for displaying on the chip button in the
89 // location bar.
90 IconId GetIconForChip();
91
92 // Returns prompt icon appropriate for displaying on the quiet chip button in
93 // the location bar.
94 IconId GetBlockedIconForChip();
95
Bret Sepulveda5327d8b52021-07-21 17:44:2396 // Returns prompt text appropriate for displaying on the chip button in the
97 // location bar.
Florian Jackyeeb62062022-10-05 18:04:0798 absl::optional<std::u16string> GetRequestChipText(ChipTextType type) const;
Olesia Marukhnof8a4bed82020-06-17 13:35:3199
Bret Sepulveda5327d8b52021-07-21 17:44:23100 // Returns prompt text appropriate for displaying under the dialog title
101 // "[domain] wants to:".
102 virtual std::u16string GetMessageTextFragment() const;
Bret Sepulvedad7e4d442021-04-20 13:46:41103#endif
[email protected]dd1ba692014-01-24 23:17:37104
[email protected]efad90f2014-01-17 00:45:54105 // Called when the user has granted the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23106 // If |is_one_time| is true the permission will last until all tabs of
107 // |origin| are closed or navigated away from, and then the permission will
Ravjit Singh Uppalc73b5a62020-11-13 01:38:52108 // automatically expire after 1 day.
Bret Sepulveda5327d8b52021-07-21 17:44:23109 void PermissionGranted(bool is_one_time);
[email protected]efad90f2014-01-17 00:45:54110
111 // Called when the user has denied the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:23112 void PermissionDenied();
[email protected]efad90f2014-01-17 00:45:54113
114 // Called when the user has cancelled the permission request. This
115 // corresponds to a denial, but is segregated in case the context needs to
116 // be able to distinguish between an active refusal or an implicit refusal.
Bret Sepulveda5327d8b52021-07-21 17:44:23117 void Cancelled();
[email protected]efad90f2014-01-17 00:45:54118
tsergeant58defcfb2016-07-19 23:47:28119 // The UI this request was associated with was answered by the user.
[email protected]e2ff17e2014-02-06 02:32:33120 // It is safe for the request to be deleted at this point -- it will receive
tsergeant58defcfb2016-07-19 23:47:28121 // no further message from the permission request system. This method will
[email protected]e2ff17e2014-02-06 02:32:33122 // eventually be called on every request which is not unregistered.
Anatoliy Potapchuk1c46f7e2020-01-23 13:31:03123 // It is ok to call this method without actually resolving the request via
124 // PermissionGranted(), PermissionDenied() or Canceled(). However, it will not
125 // resolve the javascript promise from the requesting origin.
Bret Sepulveda5327d8b52021-07-21 17:44:23126 void RequestFinished();
benwells46b02fa2016-04-20 02:37:02127
benwells471d1f12016-07-25 23:58:04128 // Used to record UMA for whether requests are associated with a user gesture.
129 // To keep things simple this metric is only recorded for the most popular
130 // request types.
Bret Sepulveda5327d8b52021-07-21 17:44:23131 PermissionRequestGestureType GetGestureType() const;
dominicknd4e446a2016-09-13 07:44:13132
lshangada00c12016-10-17 04:51:10133 // Used on Android to determine what Android OS permissions are needed for
134 // this permission request.
Bret Sepulveda5327d8b52021-07-21 17:44:23135 ContentSettingsType GetContentSettingsType() const;
lshangada00c12016-10-17 04:51:10136
dominicknd4e446a2016-09-13 07:44:13137 private:
Bret Sepulveda5327d8b52021-07-21 17:44:23138 // The origin on whose behalf this permission request is being made.
139 GURL requesting_origin_;
140
141 // The type of this request.
142 RequestType request_type_;
143
144 // Whether the request was associated with a user gesture.
145 bool has_gesture_;
146
147 // Called once a decision is made about the permission.
148 PermissionDecidedCallback permission_decided_callback_;
149
150 // Called when the request is no longer in use so it can be deleted by the
151 // caller.
152 base::OnceClosure delete_callback_;
[email protected]efad90f2014-01-17 00:45:54153};
154
Clark DuVall484c2562020-01-23 22:05:09155} // namespace permissions
156
157#endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_