- add sources.
[platform/framework/web/crosswalk.git] / src / net / spdy / write_blocked_list.h
1 // Copyright 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_WRITE_BLOCKED_LIST_H_
6 #define NET_SPDY_WRITE_BLOCKED_LIST_H_
7
8 #include <algorithm>
9 #include <deque>
10
11 #include "base/logging.h"
12
13 namespace net {
14
15 const int kHighestPriority = 0;
16 const int kLowestPriority = 7;
17
18 template <typename IdType>
19 class WriteBlockedList {
20  public:
21   // 0(1) size lookup.  0(1) insert at front or back.
22   typedef std::deque<IdType> BlockedList;
23   typedef typename BlockedList::iterator iterator;
24
25   // Returns the priority of the highest priority list with sessions on it, or
26   // -1 if none of the lists have pending sessions.
27   int GetHighestPriorityWriteBlockedList() const {
28     for (int i = 0; i <= kLowestPriority; ++i) {
29       if (write_blocked_lists_[i].size() > 0)
30         return i;
31     }
32     return -1;
33   }
34
35   int PopFront(int priority) {
36     DCHECK(!write_blocked_lists_[priority].empty());
37     IdType stream_id = write_blocked_lists_[priority].front();
38     write_blocked_lists_[priority].pop_front();
39     return stream_id;
40   }
41
42   bool HasWriteBlockedStreamsGreaterThanPriority(int priority) const {
43     for (int i = kHighestPriority; i < priority; ++i) {
44       if (!write_blocked_lists_[i].empty()) {
45         return true;
46       }
47     }
48     return false;
49   }
50
51   bool HasWriteBlockedStreams() const {
52     return HasWriteBlockedStreamsGreaterThanPriority(kLowestPriority + 1);
53   }
54
55   void PushBack(IdType stream_id, int priority) {
56     write_blocked_lists_[priority].push_back(stream_id);
57   }
58
59   void RemoveStreamFromWriteBlockedList(IdType stream_id, int priority) {
60     iterator it = std::find(write_blocked_lists_[priority].begin(),
61                             write_blocked_lists_[priority].end(),
62                             stream_id);
63     while (it != write_blocked_lists_[priority].end()) {
64       write_blocked_lists_[priority].erase(it);
65       it = std::find(write_blocked_lists_[priority].begin(),
66                      write_blocked_lists_[priority].end(),
67                      stream_id);
68     }
69   }
70
71   int NumBlockedStreams() {
72     int num_blocked_streams = 0;
73     for (int i = kHighestPriority; i <= kLowestPriority; ++i) {
74       num_blocked_streams += write_blocked_lists_[i].size();
75     }
76     return num_blocked_streams;
77   }
78
79  private:
80   // Priority ranges from 0 to 7
81   BlockedList write_blocked_lists_[8];
82 };
83
84 }  // namespace net
85
86 #endif  // NET_SPDY_WRITE_BLOCKED_LIST_H_