blob: 60fedbd18c769a9f254691ca846d926a8c837ea0 [file] [log] [blame]
[email protected]efad90f2014-01-17 00:45:541// Copyright 2014 The Chromium Authors. All rights reserved.
2// 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"
dominicknd4e446a2016-09-13 07:44:1311#include "base/macros.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 Paicu4a88f422020-11-12 18:21:3915#include "components/permissions/permission_request_enums.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
60 virtual ~PermissionRequest();
61
62 GURL requesting_origin() const { return requesting_origin_; }
63 RequestType request_type() const { return request_type_; }
[email protected]d23cdeee2014-03-10 06:39:5364
Bret Sepulvedad7e4d442021-04-20 13:46:4165 // Whether |this| and |other_request| are duplicates and therefore don't both
66 // need to be shown in the UI.
67 virtual bool IsDuplicateOf(PermissionRequest* other_request) const;
68
timlohaa3ce262017-06-01 05:29:4069#if defined(OS_ANDROID)
Bret Sepulveda5327d8b52021-07-21 17:44:2370 // Returns prompt text appropriate for displaying in an Android dialog.
71 virtual std::u16string GetDialogMessageText() const;
timlohaa3ce262017-06-01 05:29:4072#endif
73
Olesia Marukhnof8a4bed82020-06-17 13:35:3174#if !defined(OS_ANDROID)
Bret Sepulveda5327d8b52021-07-21 17:44:2375 // Returns prompt text appropriate for displaying on the chip button in the
76 // location bar.
77 absl::optional<std::u16string> GetChipText() const;
Olesia Marukhnof8a4bed82020-06-17 13:35:3178
Bret Sepulveda5327d8b52021-07-21 17:44:2379 // Returns prompt text appropriate for displaying under the dialog title
80 // "[domain] wants to:".
81 virtual std::u16string GetMessageTextFragment() const;
Bret Sepulvedad7e4d442021-04-20 13:46:4182#endif
[email protected]dd1ba692014-01-24 23:17:3783
[email protected]efad90f2014-01-17 00:45:5484 // Called when the user has granted the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:2385 // If |is_one_time| is true the permission will last until all tabs of
86 // |origin| are closed or navigated away from, and then the permission will
Ravjit Singh Uppalc73b5a62020-11-13 01:38:5287 // automatically expire after 1 day.
Bret Sepulveda5327d8b52021-07-21 17:44:2388 void PermissionGranted(bool is_one_time);
[email protected]efad90f2014-01-17 00:45:5489
90 // Called when the user has denied the requested permission.
Bret Sepulveda5327d8b52021-07-21 17:44:2391 void PermissionDenied();
[email protected]efad90f2014-01-17 00:45:5492
93 // Called when the user has cancelled the permission request. This
94 // corresponds to a denial, but is segregated in case the context needs to
95 // be able to distinguish between an active refusal or an implicit refusal.
Bret Sepulveda5327d8b52021-07-21 17:44:2396 void Cancelled();
[email protected]efad90f2014-01-17 00:45:5497
tsergeant58defcfb2016-07-19 23:47:2898 // The UI this request was associated with was answered by the user.
[email protected]e2ff17e2014-02-06 02:32:3399 // It is safe for the request to be deleted at this point -- it will receive
tsergeant58defcfb2016-07-19 23:47:28100 // no further message from the permission request system. This method will
[email protected]e2ff17e2014-02-06 02:32:33101 // eventually be called on every request which is not unregistered.
Anatoliy Potapchuk1c46f7e2020-01-23 13:31:03102 // It is ok to call this method without actually resolving the request via
103 // PermissionGranted(), PermissionDenied() or Canceled(). However, it will not
104 // resolve the javascript promise from the requesting origin.
Bret Sepulveda5327d8b52021-07-21 17:44:23105 void RequestFinished();
benwells46b02fa2016-04-20 02:37:02106
benwells471d1f12016-07-25 23:58:04107 // Used to record UMA for whether requests are associated with a user gesture.
108 // To keep things simple this metric is only recorded for the most popular
109 // request types.
Bret Sepulveda5327d8b52021-07-21 17:44:23110 PermissionRequestGestureType GetGestureType() const;
dominicknd4e446a2016-09-13 07:44:13111
lshangada00c12016-10-17 04:51:10112 // Used on Android to determine what Android OS permissions are needed for
113 // this permission request.
Bret Sepulveda5327d8b52021-07-21 17:44:23114 ContentSettingsType GetContentSettingsType() const;
lshangada00c12016-10-17 04:51:10115
dominicknd4e446a2016-09-13 07:44:13116 private:
Bret Sepulveda5327d8b52021-07-21 17:44:23117 // The origin on whose behalf this permission request is being made.
118 GURL requesting_origin_;
119
120 // The type of this request.
121 RequestType request_type_;
122
123 // Whether the request was associated with a user gesture.
124 bool has_gesture_;
125
126 // Called once a decision is made about the permission.
127 PermissionDecidedCallback permission_decided_callback_;
128
129 // Called when the request is no longer in use so it can be deleted by the
130 // caller.
131 base::OnceClosure delete_callback_;
[email protected]efad90f2014-01-17 00:45:54132};
133
Clark DuVall484c2562020-01-23 22:05:09134} // namespace permissions
135
136#endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_H_