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_HTTP2_DECODER_FRAME_PARTS_COLLECTOR_H_ | |
6 #define NET_HTTP2_DECODER_FRAME_PARTS_COLLECTOR_H_ | |
7 | |
8 // FramePartsCollector is a base class for Http2FrameDecoderListener | |
9 // implementations that create one FrameParts instance for each decoded frame. | |
10 | |
11 #include <stddef.h> | |
12 | |
13 #include <memory> | |
14 #include <vector> | |
15 | |
16 #include "net/http2/decoder/frame_parts.h" | |
17 #include "net/http2/decoder/http2_frame_decoder_listener.h" | |
18 #include "net/http2/decoder/http2_frame_decoder_listener_test_util.h" | |
19 #include "net/http2/http2_structures.h" | |
20 | |
21 namespace net { | |
22 namespace test { | |
23 | |
24 class FramePartsCollector : public FailingHttp2FrameDecoderListener { | |
25 public: | |
26 FramePartsCollector(); | |
27 ~FramePartsCollector() override; | |
28 | |
29 // Toss out the collected data. | |
30 void Reset(); | |
31 | |
32 // Returns true if has started recording the info for a frame and has not yet | |
33 // finished doing so. | |
34 bool IsInProgress() const { return current_frame_ != nullptr; } | |
35 | |
36 // Returns the FrameParts instance into which we're currently recording | |
37 // callback info if IsInProgress, else nullptr. | |
38 const FrameParts* current_frame() const { return current_frame_.get(); } | |
39 | |
40 // Returns the completely collected FrameParts instances. | |
41 const std::vector<std::unique_ptr<FrameParts>>& collected_frames() const { | |
42 return collected_frames_; | |
43 } | |
44 | |
45 // Returns the number of completely collected FrameParts instances. | |
46 size_t size() const { return collected_frames_.size(); } | |
47 | |
48 // Returns the n'th frame, where 0 is the oldest of the collected frames, | |
49 // and n==size() is the frame currently being collected, if there is one. | |
50 // Returns nullptr if the requested index is not valid. | |
51 const FrameParts* frame(size_t n) const; | |
52 | |
53 protected: | |
54 // In support of OnFrameHeader, set the header that we expect to be used in | |
55 // the next call. | |
56 // TODO(jamessynge): Remove ExpectFrameHeader et al. once done with supporting | |
57 // SpdyFramer's exact states. | |
58 void ExpectFrameHeader(const Http2FrameHeader& header); | |
59 | |
60 // For use in implementing On*Start methods of Http2FrameDecoderListener, | |
61 // returns a FrameParts instance, which will be newly created if | |
62 // IsInProgress==false (which the caller should ensure), else will be the | |
63 // current_frame(); never returns nullptr. | |
64 // If called when IsInProgress==true, a test failure will be recorded. | |
65 Http2FrameDecoderListener* StartFrame(const Http2FrameHeader& header); | |
66 | |
67 // For use in implementing On* callbacks, such as OnPingAck, that are the only | |
68 // call expected for the frame being decoded; not for On*Start methods. | |
69 // Returns a FrameParts instance, which will be newly created if | |
70 // IsInProgress==false (which the caller should ensure), else will be the | |
71 // current_frame(); never returns nullptr. | |
72 // If called when IsInProgress==true, a test failure will be recorded. | |
73 Http2FrameDecoderListener* StartAndEndFrame(const Http2FrameHeader& header); | |
74 | |
75 // If IsInProgress==true, returns the FrameParts into which the current | |
76 // frame is being recorded; else records a test failure and returns | |
77 // failing_listener_, which will record a test failure when any of its | |
78 // On* methods is called. | |
79 Http2FrameDecoderListener* CurrentFrame(); | |
80 | |
81 // For use in implementing On*End methods, pushes the current frame onto | |
82 // the vector of completed frames, and returns a pointer to it for recording | |
83 // the info in the final call. If IsInProgress==false, records a test failure | |
84 // and returns failing_listener_, which will record a test failure when any | |
85 // of its On* methods is called. | |
86 Http2FrameDecoderListener* EndFrame(); | |
87 | |
88 // For use in implementing OnPaddingTooLong and OnFrameSizeError, is | |
89 // equivalent to EndFrame() if IsInProgress==true, else equivalent to | |
90 // StartAndEndFrame(). | |
91 Http2FrameDecoderListener* FrameError(const Http2FrameHeader& header); | |
92 | |
93 private: | |
94 // Returns the mutable FrameParts instance into which we're currently | |
95 // recording callback info if IsInProgress, else nullptr. | |
96 FrameParts* current_frame() { return current_frame_.get(); } | |
97 | |
98 // If expected header is set, verify that it matches the header param. | |
99 // TODO(jamessynge): Remove TestExpectedHeader et al. once done | |
100 // with supporting SpdyFramer's exact states. | |
101 void TestExpectedHeader(const Http2FrameHeader& header); | |
102 | |
103 std::unique_ptr<FrameParts> current_frame_; | |
104 std::vector<std::unique_ptr<FrameParts>> collected_frames_; | |
105 FailingHttp2FrameDecoderListener failing_listener_; | |
106 | |
107 // TODO(jamessynge): Remove expected_header_ et al. once done with supporting | |
108 // SpdyFramer's exact states. | |
109 Http2FrameHeader expected_header_; | |
110 bool expected_header_set_ = false; | |
111 }; | |
112 | |
113 } // namespace test | |
114 } // namespace net | |
115 | |
116 #endif // NET_HTTP2_DECODER_FRAME_PARTS_COLLECTOR_H_ | |
OLD | NEW |