1 // Copyright 2022 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_QUEUE_H_
6 #define COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_QUEUE_H_
11 #include "base/containers/circular_deque.h"
12 #include "components/permissions/permission_request.h"
14 namespace permissions {
16 // Provides a container for holding pending PermissionRequest objects and
17 // provides access methods respecting the currently applicable feature flag
19 // The queue of permission requests is always held in the order of:
20 // High Priority Requests > Normal Priority Requests > Low Priority Requests.
21 // Using the |PushFront| and |PushBack| functions will push the new request in
22 // the front or back of the section of the queue that corresponds to that
23 // request's priority.
25 // High Priority Requests are requests that come from an Page-Embedded
26 // Permission Control.
27 // Low Priority Requests are requests for non-urgent permission types
28 // (notifications, geolocation) if the current platform supports the permission
29 // chip. If the permission chip is not supported, there are no low priority
31 // Normal Priority Requests are all other requests.
32 class PermissionRequestQueue {
34 using const_iterator =
35 std::vector<base::circular_deque<PermissionRequest*>>::const_iterator;
37 // Not copyable or movable
38 PermissionRequestQueue(const PermissionRequestQueue&) = delete;
39 PermissionRequestQueue& operator=(const PermissionRequestQueue&) = delete;
40 PermissionRequestQueue();
41 ~PermissionRequestQueue();
44 size_t Count(PermissionRequest* request) const;
45 size_t size() const { return size_; }
47 // Push a new request into queue. This function will decide based on request
48 // priority and platform whether to call |PushBack| or |PushFront|.
49 void Push(permissions::PermissionRequest* request);
51 // Push a new request into the front of the section of the queue that
52 // corresponds to its priority. E.g.: calling this function on a normal
53 // priority |request| will put it in front of any other normal priority
54 // requests, but still behind any high priority requests.
55 void PushFront(permissions::PermissionRequest* request);
57 // Push a new request into the back of the section of the queue that
58 // corresponds to its priority. E.g.: calling this function on a normal
59 // priority |request| will put it behind any other normal priority requests,
60 // but still in front of any low priority requests.
61 void PushBack(permissions::PermissionRequest* request);
63 PermissionRequest* Pop();
64 PermissionRequest* Peek() const;
66 // Searches queued_requests_ and returns the first matching request, or
67 // nullptr if there is no match.
68 PermissionRequest* FindDuplicate(PermissionRequest* request) const;
70 const_iterator begin() const;
71 const_iterator end() const;
79 // Used to set the correct size of the |queued_requests_| vector.
83 static Priority DetermineRequestPriority(
84 permissions::PermissionRequest* request);
86 void PushFrontInternal(permissions::PermissionRequest* request,
88 void PushBackInternal(permissions::PermissionRequest* request,
91 // Each priority has a separate deque. There is an assumption made that the
92 // priorities have strictly ascending, contignous values from lowest to
94 std::vector<base::circular_deque<PermissionRequest*>> queued_requests_;
99 } // namespace permissions
101 #endif // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_QUEUE_H_