Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 | |
| 5 #ifndef CONTENT_COMMON_INPUT_WEB_INPUT_EVENT_QUEUE_H_ | |
| 6 #define CONTENT_COMMON_INPUT_WEB_INPUT_EVENT_QUEUE_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 // WebInputEventQueue is a coalescing queue with the addition of a state | |
| 15 // variable that represents whether an item is pending to be processed. | |
| 16 // The desired usage sending with this queue is: | |
| 17 // if (queue.state() == ITEM_PENDING) { | |
| 18 // queue.Queue(T); | |
| 19 // } else { | |
| 20 // send T | |
| 21 // queue.SetState(ITEM_PENDING); | |
|
tdresser
2016/02/08 17:51:32
SetState -> set_state, to align with implementatio
dtapuska
2016/02/09 19:40:11
Done.
| |
| 22 // } | |
| 23 // | |
| 24 // Processing the event response: | |
| 25 // if (!queue.empty()) { | |
| 26 // T = queue.Deque(); | |
| 27 // send T now | |
| 28 // } else { | |
| 29 // queue.SetState(ITEM_NOT_PENDING); | |
| 30 // } | |
| 31 // | |
| 32 template <typename T> | |
| 33 class WebInputEventQueue { | |
|
tdresser
2016/02/08 17:51:32
Do you think it would be worth having a super simp
dtapuska
2016/02/09 19:40:11
I thought it was kind of redundant.
| |
| 34 public: | |
| 35 enum class State { ITEM_PENDING, ITEM_NOT_PENDING }; | |
| 36 | |
| 37 WebInputEventQueue() : state_(State::ITEM_NOT_PENDING) {} | |
| 38 | |
| 39 // Adds an event to the queue. The event may be coalesced with previously | |
| 40 // queued events. | |
| 41 void Queue(const T& event) { | |
| 42 if (!queue_.empty()) { | |
| 43 scoped_ptr<T>& last_event = queue_.back(); | |
| 44 if (last_event->CanCoalesceWith(event)) { | |
| 45 last_event->CoalesceWith(event); | |
| 46 return; | |
| 47 } | |
| 48 } | |
| 49 queue_.emplace_back(scoped_ptr<T>(new T(event))); | |
| 50 } | |
| 51 | |
| 52 scoped_ptr<T> Deque() { | |
| 53 scoped_ptr<T> result; | |
| 54 if (!queue_.empty()) { | |
| 55 result.reset(queue_.front().release()); | |
| 56 queue_.pop_front(); | |
| 57 } | |
| 58 return result; | |
| 59 } | |
| 60 | |
| 61 bool empty() const { return queue_.empty(); } | |
| 62 | |
| 63 size_t size() const { return queue_.size(); } | |
| 64 | |
| 65 void set_state(State state) { state_ = state; } | |
| 66 | |
| 67 State state() const WARN_UNUSED_RESULT { return state_; } | |
| 68 | |
| 69 private: | |
| 70 typedef std::deque<scoped_ptr<T>> EventQueue; | |
| 71 EventQueue queue_; | |
| 72 State state_; | |
| 73 | |
| 74 DISALLOW_COPY_AND_ASSIGN(WebInputEventQueue); | |
| 75 }; | |
| 76 | |
| 77 } // namespace content | |
| 78 | |
| 79 #endif // CONTENT_COMMON_INPUT_WEB_INPUT_EVENT_QUEUE_H_ | |
| OLD | NEW |