1// Copyright (c) 2013 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 NET_SPDY_SPDY_WRITE_QUEUE_H_
6#define NET_SPDY_SPDY_WRITE_QUEUE_H_
7
8#include <deque>
9
10#include "base/basictypes.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/memory/weak_ptr.h"
13#include "net/base/net_export.h"
14#include "net/base/request_priority.h"
15#include "net/spdy/spdy_protocol.h"
16
17namespace net {
18
19class SpdyBuffer;
20class SpdyBufferProducer;
21class SpdyStream;
22
23// A queue of SpdyBufferProducers to produce frames to write. Ordered
24// by priority, and then FIFO.
25class NET_EXPORT_PRIVATE SpdyWriteQueue {
26 public:
27  SpdyWriteQueue();
28  ~SpdyWriteQueue();
29
30  // Returns whether there is anything in the write queue,
31  // i.e. whether the next call to Dequeue will return true.
32  bool IsEmpty() const;
33
34  // Enqueues the given frame producer of the given type at the given
35  // priority associated with the given stream, which may be NULL if
36  // the frame producer is not associated with a stream. If |stream|
37  // is non-NULL, its priority must be equal to |priority|, and it
38  // must remain non-NULL until the write is dequeued or removed.
39  void Enqueue(RequestPriority priority,
40               SpdyFrameType frame_type,
41               scoped_ptr<SpdyBufferProducer> frame_producer,
42               const base::WeakPtr<SpdyStream>& stream);
43
44  // Dequeues the frame producer with the highest priority that was
45  // enqueued the earliest and its associated stream. Returns true and
46  // fills in |frame_type|, |frame_producer|, and |stream| if
47  // successful -- otherwise, just returns false.
48  bool Dequeue(SpdyFrameType* frame_type,
49               scoped_ptr<SpdyBufferProducer>* frame_producer,
50               base::WeakPtr<SpdyStream>* stream);
51
52  // Removes all pending writes for the given stream, which must be
53  // non-NULL.
54  void RemovePendingWritesForStream(const base::WeakPtr<SpdyStream>& stream);
55
56  // Removes all pending writes for streams after |last_good_stream_id|
57  // and streams with no stream id.
58  void RemovePendingWritesForStreamsAfter(SpdyStreamId last_good_stream_id);
59
60  // Removes all pending writes.
61  void Clear();
62
63 private:
64  // A struct holding a frame producer and its associated stream.
65  struct PendingWrite {
66    SpdyFrameType frame_type;
67    // This has to be a raw pointer since we store this in an STL
68    // container.
69    SpdyBufferProducer* frame_producer;
70    base::WeakPtr<SpdyStream> stream;
71    // Whether |stream| was non-NULL when enqueued.
72    bool has_stream;
73
74    PendingWrite();
75    PendingWrite(SpdyFrameType frame_type,
76                 SpdyBufferProducer* frame_producer,
77                 const base::WeakPtr<SpdyStream>& stream);
78    ~PendingWrite();
79  };
80
81  // The actual write queue, binned by priority.
82  std::deque<PendingWrite> queue_[NUM_PRIORITIES];
83
84  DISALLOW_COPY_AND_ASSIGN(SpdyWriteQueue);
85};
86
87}  // namespace net
88
89#endif  // NET_SPDY_SPDY_WRITE_QUEUE_H_
90