summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/input/input_queue.h
blob: 2c7acb707d2cd4b788ffe1db5e1cf89a6bcc6f06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2013 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_BROWSER_RENDERER_HOST_INPUT_INPUT_QUEUE_H_
#define CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_QUEUE_H_

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "content/common/content_export.h"
#include "content/common/input/input_event_disposition.h"

namespace content {

class BrowserInputEvent;
class EventPacketAck;
class InputQueueClient;

// |InputQueue| handles browser input event batching and response.
// Event packets are assembled into sequential event packets. A flush entails
// delivery and dispatch of a single event packet, and continues until the
// packet is ack'ed and all its events have been dispatched to the renderer.
class CONTENT_EXPORT InputQueue {
 public:
  // The |client| should outlive the InputQueue.
  InputQueue(InputQueueClient* client);
  virtual ~InputQueue();

  // If a flush is in progress, |event| will be dispatched in the next flush.
  // If no flush is in progress, a flush will be requested if necessary.
  // |event| is assumed to be both non-NULL and valid.
  void QueueEvent(scoped_ptr<BrowserInputEvent> event);

  // Initiates the flush of the pending event packet to |client_|, if necessary.
  // This should only be called in response to |client_->SetNeedsFlush()|.
  void BeginFlush();

  // Called by the owner upon EventPacket responses from the sender target. This
  // will dispatch event acks for events with a corresponding |ack_handler|.
  enum AckResult {
    ACK_OK,          // |acked_packet| was processed successfully.
    ACK_UNEXPECTED,  // |acked_packet| was unexpected; no flush was in-progress.
    ACK_INVALID,     // |acked_packet| contains invalid data.
    ACK_SHUTDOWN     // |acked_packet| processing triggered queue shutdown.
  };
  AckResult OnEventPacketAck(int64 packet_id,
                             const InputEventDispositions& dispositions);

  // Total number of evenst in the queue, both being flushed and pending flush.
  size_t QueuedEventCount() const;

 protected:
  friend class InputQueueTest;

  // Delivers |in_flush_packet_| to the client.
  void DeliverInFlushPacket();

  // Requests a flush via |client_| if the necessary request has not been made.
  void RequestFlushIfNecessary();

  // True when |in_flush_packet_| is non-empty.
  bool FlushInProgress() const;

 private:
  InputQueueClient* client_;

  // Used to assign unique, non-zero ID's to each delivered EventPacket.
  int64 next_packet_id_;

  // Avoid spamming the client with redundant flush requests.
  bool flush_requested_;

  class BrowserEventPacket;
  scoped_ptr<BrowserEventPacket> in_flush_packet_;
  scoped_ptr<BrowserEventPacket> pending_flush_packet_;

  DISALLOW_COPY_AND_ASSIGN(InputQueue);
};

}  // namespace content

#endif  // CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_QUEUE_H_