OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ | 5 #ifndef NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ |
6 #define NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ | 6 #define NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
12 #include "net/spdy/spdy_protocol.h" | 12 #include "net/spdy/spdy_protocol.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 // A helper class encapsulating the state and logic to set dependencies of | 16 // A helper class encapsulating the state and logic to set dependencies of |
17 // HTTP2 streams based on their SpdyPriority and the ordering | 17 // HTTP2 streams based on their SpdyPriority and the ordering |
18 // of creation and deletion of the streams. | 18 // of creation and deletion of the streams. |
19 class NET_EXPORT_PRIVATE Http2PriorityDependencies { | 19 class NET_EXPORT_PRIVATE Http2PriorityDependencies { |
20 public: | 20 public: |
21 Http2PriorityDependencies(); | 21 Http2PriorityDependencies(); |
22 ~Http2PriorityDependencies(); | 22 ~Http2PriorityDependencies(); |
23 | 23 |
24 // Called when a stream SYN is sent to the server. Note that in the | 24 // Called when a stream is created. This is used for both client-initiated |
25 // case of server push, a stream may be created without this routine | 25 // and server-initiated (pushed) streams. |
26 // being called. In such cases, the client ignores the stream's priority | |
27 // (as the server is effectively overriding the client's notions of | |
28 // priority anyway). | |
29 // On return, |*dependent_stream_id| is set to the stream id that | 26 // On return, |*dependent_stream_id| is set to the stream id that |
30 // this stream should be made dependent on, and |*exclusive| set to | 27 // this stream should be made dependent on, and |*exclusive| set to |
31 // whether that dependency should be exclusive. | 28 // whether that dependency should be exclusive. |
32 void OnStreamSynSent(SpdyStreamId id, | 29 void OnStreamCreation(SpdyStreamId id, |
33 SpdyPriority priority, | 30 SpdyPriority priority, |
34 SpdyStreamId* dependent_stream_id, | 31 SpdyStreamId* dependent_stream_id, |
35 bool* exclusive); | 32 bool* exclusive); |
36 | 33 |
| 34 // Called when a stream is destroyed. |
37 void OnStreamDestruction(SpdyStreamId id); | 35 void OnStreamDestruction(SpdyStreamId id); |
38 | 36 |
| 37 struct DependencyUpdate { |
| 38 SpdyStreamId id; |
| 39 SpdyStreamId dependent_stream_id; |
| 40 bool exclusive; |
| 41 }; |
| 42 |
| 43 // Called when a stream's priority has changed. Returns a list of |
| 44 // dependency updates that should be sent to the server to describe |
| 45 // the requested priority change. The updates should be sent in the |
| 46 // given order. |
| 47 std::vector<DependencyUpdate> OnStreamUpdate(SpdyStreamId id, |
| 48 SpdyPriority new_priority); |
| 49 |
39 private: | 50 private: |
40 // The requirements for the internal data structure for this class are: | 51 // The requirements for the internal data structure for this class are: |
41 // a) Constant time insertion of entries at the end of the list, | 52 // a) Constant time insertion of entries at the end of the list, |
42 // b) Fast removal of any entry based on its id. | 53 // b) Fast removal of any entry based on its id. |
43 // c) Constant time lookup of the entry at the end of the list. | 54 // c) Constant time lookup of the entry at the end of the list. |
44 // std::list would satisfy (a) & (c), but some form of map is | 55 // std::list would satisfy (a) & (c), but some form of map is |
45 // needed for (b). The priority must be included in the map | 56 // needed for (b). The priority must be included in the map |
46 // entries so that deletion can determine which list in id_priority_lists_ | 57 // entries so that deletion can determine which list in id_priority_lists_ |
47 // to erase from. | 58 // to erase from. |
48 using IdList = std::list<std::pair<SpdyStreamId, SpdyPriority>>; | 59 using IdList = std::list<std::pair<SpdyStreamId, SpdyPriority>>; |
49 using EntryMap = std::map<SpdyStreamId, IdList::iterator>; | 60 using EntryMap = std::map<SpdyStreamId, IdList::iterator>; |
50 | 61 |
51 IdList id_priority_lists_[kV3LowestPriority + 1]; | 62 IdList id_priority_lists_[kV3LowestPriority + 1]; |
52 | 63 |
53 // Tracks the location of an id anywhere in the above vector of lists. | 64 // Tracks the location of an id anywhere in the above vector of lists. |
54 // Iterators to list elements remain valid until those particular elements | 65 // Iterators to list elements remain valid until those particular elements |
55 // are erased. | 66 // are erased. |
56 EntryMap entry_by_stream_id_; | 67 EntryMap entry_by_stream_id_; |
| 68 |
| 69 // Finds the lowest-priority stream that has a priority >= |priority|. |
| 70 // Returns false if there are no such streams. |
| 71 // Otherwise, returns true and sets |*bound|. |
| 72 bool PriorityLowerBound(SpdyPriority priority, IdList::iterator* bound); |
| 73 |
| 74 // Finds the stream just above |id| in the total order. |
| 75 // Returns false if there are no streams with a higher priority. |
| 76 // Otherwise, returns true and sets |*parent|. |
| 77 bool ParentOfStream(SpdyStreamId id, IdList::iterator* parent); |
| 78 |
| 79 // Finds the stream just below |id| in the total order. |
| 80 // Returns false if there are no streams with a lower priority. |
| 81 // Otherwise, returns true and sets |*child|. |
| 82 bool ChildOfStream(SpdyStreamId id, IdList::iterator* child); |
57 }; | 83 }; |
58 | 84 |
59 } // namespace net | 85 } // namespace net |
60 | 86 |
61 #endif // NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ | 87 #endif // NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ |
OLD | NEW |