Fix emulator build error
[platform/framework/web/chromium-efl.git] / components / permissions / permission_request_queue.h
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.
4
5 #ifndef COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_QUEUE_H_
6 #define COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_QUEUE_H_
7
8 #include <cstddef>
9 #include <vector>
10
11 #include "base/containers/circular_deque.h"
12 #include "components/permissions/permission_request.h"
13
14 namespace permissions {
15
16 // Provides a container for holding pending PermissionRequest objects and
17 // provides access methods respecting the currently applicable feature flag
18 // configuration.
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.
24
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
30 // requests.
31 // Normal Priority Requests are all other requests.
32 class PermissionRequestQueue {
33  public:
34   using const_iterator =
35       std::vector<base::circular_deque<PermissionRequest*>>::const_iterator;
36
37   // Not copyable or movable
38   PermissionRequestQueue(const PermissionRequestQueue&) = delete;
39   PermissionRequestQueue& operator=(const PermissionRequestQueue&) = delete;
40   PermissionRequestQueue();
41   ~PermissionRequestQueue();
42
43   bool IsEmpty() const;
44   size_t Count(PermissionRequest* request) const;
45   size_t size() const { return size_; }
46
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);
50
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);
56
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);
62
63   PermissionRequest* Pop();
64   PermissionRequest* Peek() const;
65
66   // Searches queued_requests_ and returns the first matching request, or
67   // nullptr if there is no match.
68   PermissionRequest* FindDuplicate(PermissionRequest* request) const;
69
70   const_iterator begin() const;
71   const_iterator end() const;
72
73  private:
74   enum class Priority {
75     kLow,
76     kMedium,
77     kHigh,
78
79     // Used to set the correct size of the |queued_requests_| vector.
80     kNum,
81   };
82
83   static Priority DetermineRequestPriority(
84       permissions::PermissionRequest* request);
85
86   void PushFrontInternal(permissions::PermissionRequest* request,
87                          Priority priority);
88   void PushBackInternal(permissions::PermissionRequest* request,
89                         Priority priority);
90
91   // Each priority has a separate deque. There is an assumption made that the
92   // priorities have strictly ascending, contignous values from lowest to
93   // highest.
94   std::vector<base::circular_deque<PermissionRequest*>> queued_requests_;
95
96   size_t size_{0};
97 };
98
99 }  // namespace permissions
100
101 #endif  // COMPONENTS_PERMISSIONS_PERMISSION_REQUEST_QUEUE_H_