1 // Copyright 2016 The Chromium Authors 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 #include <utility> 11 #include <vector> 12 13 #include "net/base/net_export.h" 14 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h" 15 16 namespace net { 17 18 // A helper class encapsulating the state and logic to set the priority fields 19 // for HTTP/2 streams based on their spdy::SpdyPriority and the ordering of 20 // creation and deletion of the streams. This implentation includes a gross hack 21 // in which the HTTP/2 weight is set to a transformation of the 22 // spdy::SpdyPriority value in order to support servers which do not honor 23 // HTTP/2 stream dependencies and instead treat the weight value like a SPDY/3 24 // priority. 25 // TODO(rch): Eliminate this gross hack when servers no longer act like this. 26 class NET_EXPORT_PRIVATE Http2PriorityDependencies { 27 public: 28 Http2PriorityDependencies(); 29 ~Http2PriorityDependencies(); 30 31 // Called when a stream is created. This is used for both client-initiated 32 // and server-initiated (pushed) streams. 33 // On return, |*parent_stream_id| is set to the stream id that should become 34 // the parent of this stream, |*exclusive| is set to whether that dependency 35 // should be exclusive, and |*weight| is set to the relative weight for the 36 // created stream given this priority. 37 void OnStreamCreation(spdy::SpdyStreamId id, 38 spdy::SpdyPriority priority, 39 spdy::SpdyStreamId* parent_stream_id, 40 int* weight, 41 bool* exclusive); 42 43 // Called when a stream is destroyed. 44 void OnStreamDestruction(spdy::SpdyStreamId id); 45 46 struct DependencyUpdate { 47 spdy::SpdyStreamId id; 48 spdy::SpdyStreamId parent_stream_id; 49 int weight; 50 bool exclusive; 51 }; 52 53 // Called when a stream's priority has changed. Returns a list of 54 // dependency updates that should be sent to the server to describe 55 // the requested priority change. The updates should be sent in the 56 // given order. 57 std::vector<DependencyUpdate> OnStreamUpdate(spdy::SpdyStreamId id, 58 spdy::SpdyPriority new_priority); 59 60 private: 61 // The requirements for the internal data structure for this class are: 62 // a) Constant time insertion of entries at the end of the list, 63 // b) Fast removal of any entry based on its id. 64 // c) Constant time lookup of the entry at the end of the list. 65 // std::list would satisfy (a) & (c), but some form of map is 66 // needed for (b). The priority must be included in the map 67 // entries so that deletion can determine which list in id_priority_lists_ 68 // to erase from. 69 using IdList = std::list<std::pair<spdy::SpdyStreamId, spdy::SpdyPriority>>; 70 using EntryMap = std::map<spdy::SpdyStreamId, IdList::iterator>; 71 72 IdList id_priority_lists_[spdy::kV3LowestPriority + 1]; 73 74 // Tracks the location of an id anywhere in the above vector of lists. 75 // Iterators to list elements remain valid until those particular elements 76 // are erased. 77 EntryMap entry_by_stream_id_; 78 79 // Finds the lowest-priority stream that has a priority >= |priority|. 80 // Returns false if there are no such streams. 81 // Otherwise, returns true and sets |*bound|. 82 bool PriorityLowerBound(spdy::SpdyPriority priority, IdList::iterator* bound); 83 84 // Finds the stream just above |id| in the total order. 85 // Returns false if there are no streams with a higher priority. 86 // Otherwise, returns true and sets |*parent|. 87 bool ParentOfStream(spdy::SpdyStreamId id, IdList::iterator* parent); 88 89 // Finds the stream just below |id| in the total order. 90 // Returns false if there are no streams with a lower priority. 91 // Otherwise, returns true and sets |*child|. 92 bool ChildOfStream(spdy::SpdyStreamId id, IdList::iterator* child); 93 }; 94 95 } // namespace net 96 97 #endif // NET_SPDY_HTTP2_PRIORITY_DEPENDENCIES_H_ 98