OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ | |
6 #define NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ | |
7 | |
8 #include <list> | |
9 #include <map> | |
10 | |
11 #include "net/base/net_export.h" | |
12 #include "net/spdy/spdy_protocol.h" | |
13 | |
14 namespace net { | |
15 | |
16 // A helper class encapsulating the state and logic to set dependencies of | |
17 // HTTP2 streams based on their SpdyPriority and the ordering | |
18 // of creation and deletion of the streams. | |
19 class NET_EXPORT_PRIVATE Http2PriorityDependencies { | |
20 public: | |
21 Http2PriorityDependencies(); | |
22 ~Http2PriorityDependencies(); | |
23 | |
24 // Called when a stream is created. This is used for both client-initiated | |
25 // and server-initiated (pushed) streams. | |
26 // On return, |*dependent_stream_id| is set to the stream id that | |
27 // this stream should be made dependent on, and |*exclusive| set to | |
28 // whether that dependency should be exclusive. | |
29 void OnStreamCreation(SpdyStreamId id, | |
30 SpdyPriority priority, | |
31 SpdyStreamId* dependent_stream_id, | |
32 bool* exclusive); | |
33 | |
34 // Called when a stream is destroyed. | |
35 void OnStreamDestruction(SpdyStreamId id); | |
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 | |
50 // Returns the estimate of dynamically allocated memory in bytes. | |
51 size_t EstimateMemoryUsage() const; | |
52 | |
53 private: | |
54 // The requirements for the internal data structure for this class are: | |
55 // a) Constant time insertion of entries at the end of the list, | |
56 // b) Fast removal of any entry based on its id. | |
57 // c) Constant time lookup of the entry at the end of the list. | |
58 // std::list would satisfy (a) & (c), but some form of map is | |
59 // needed for (b). The priority must be included in the map | |
60 // entries so that deletion can determine which list in id_priority_lists_ | |
61 // to erase from. | |
62 using IdList = std::list<std::pair<SpdyStreamId, SpdyPriority>>; | |
63 using EntryMap = std::map<SpdyStreamId, IdList::iterator>; | |
64 | |
65 IdList id_priority_lists_[kV3LowestPriority + 1]; | |
66 | |
67 // Tracks the location of an id anywhere in the above vector of lists. | |
68 // Iterators to list elements remain valid until those particular elements | |
69 // are erased. | |
70 EntryMap entry_by_stream_id_; | |
71 | |
72 // Finds the lowest-priority stream that has a priority >= |priority|. | |
73 // Returns false if there are no such streams. | |
74 // Otherwise, returns true and sets |*bound|. | |
75 bool PriorityLowerBound(SpdyPriority priority, IdList::iterator* bound); | |
76 | |
77 // Finds the stream just above |id| in the total order. | |
78 // Returns false if there are no streams with a higher priority. | |
79 // Otherwise, returns true and sets |*parent|. | |
80 bool ParentOfStream(SpdyStreamId id, IdList::iterator* parent); | |
81 | |
82 // Finds the stream just below |id| in the total order. | |
83 // Returns false if there are no streams with a lower priority. | |
84 // Otherwise, returns true and sets |*child|. | |
85 bool ChildOfStream(SpdyStreamId id, IdList::iterator* child); | |
86 }; | |
87 | |
88 } // namespace net | |
89 | |
90 #endif // NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ | |
OLD | NEW |