Chromium Code Reviews| Index: content/common/input/web_input_event_queue.h |
| diff --git a/content/common/input/web_input_event_queue.h b/content/common/input/web_input_event_queue.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..947494dbe3d071b48be29d1f1ca0acc7a508aec7 |
| --- /dev/null |
| +++ b/content/common/input/web_input_event_queue.h |
| @@ -0,0 +1,79 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_COMMON_INPUT_WEB_INPUT_EVENT_QUEUE_H_ |
| +#define CONTENT_COMMON_INPUT_WEB_INPUT_EVENT_QUEUE_H_ |
| + |
| +#include <deque> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| + |
| +namespace content { |
| + |
| +// WebInputEventQueue is a coalescing queue with the addition of a state |
| +// variable that represents whether an item is pending to be processed. |
| +// The desired usage sending with this queue is: |
| +// if (queue.state() == ITEM_PENDING) { |
| +// queue.Queue(T); |
| +// } else { |
| +// send T |
| +// 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.
|
| +// } |
| +// |
| +// Processing the event response: |
| +// if (!queue.empty()) { |
| +// T = queue.Deque(); |
| +// send T now |
| +// } else { |
| +// queue.SetState(ITEM_NOT_PENDING); |
| +// } |
| +// |
| +template <typename T> |
| +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.
|
| + public: |
| + enum class State { ITEM_PENDING, ITEM_NOT_PENDING }; |
| + |
| + WebInputEventQueue() : state_(State::ITEM_NOT_PENDING) {} |
| + |
| + // Adds an event to the queue. The event may be coalesced with previously |
| + // queued events. |
| + void Queue(const T& event) { |
| + if (!queue_.empty()) { |
| + scoped_ptr<T>& last_event = queue_.back(); |
| + if (last_event->CanCoalesceWith(event)) { |
| + last_event->CoalesceWith(event); |
| + return; |
| + } |
| + } |
| + queue_.emplace_back(scoped_ptr<T>(new T(event))); |
| + } |
| + |
| + scoped_ptr<T> Deque() { |
| + scoped_ptr<T> result; |
| + if (!queue_.empty()) { |
| + result.reset(queue_.front().release()); |
| + queue_.pop_front(); |
| + } |
| + return result; |
| + } |
| + |
| + bool empty() const { return queue_.empty(); } |
| + |
| + size_t size() const { return queue_.size(); } |
| + |
| + void set_state(State state) { state_ = state; } |
| + |
| + State state() const WARN_UNUSED_RESULT { return state_; } |
| + |
| + private: |
| + typedef std::deque<scoped_ptr<T>> EventQueue; |
| + EventQueue queue_; |
| + State state_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebInputEventQueue); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_COMMON_INPUT_WEB_INPUT_EVENT_QUEUE_H_ |