OLD | NEW |
| (Empty) |
1 // Copyright 2015 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_HTTP_BIDIRECTIONAL_STREAM_JOB_H_ | |
6 #define NET_HTTP_BIDIRECTIONAL_STREAM_JOB_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "net/base/net_export.h" | |
13 #include "net/socket/next_proto.h" | |
14 | |
15 namespace base { | |
16 class Timer; | |
17 } // namespace base | |
18 | |
19 namespace net { | |
20 | |
21 class BoundNetLog; | |
22 class IOBuffer; | |
23 class SpdyHeaderBlock; | |
24 struct BidirectionalStreamRequestInfo; | |
25 | |
26 // Exposes an interface to do HTTP/2 bidirectional streaming. | |
27 // Note that only one ReadData or SendData should be in flight until the | |
28 // operation completes synchronously or asynchronously. | |
29 // BidirectionalStreamJob once created by HttpStreamFactoryImpl should be owned | |
30 // by BidirectionalStream. | |
31 class NET_EXPORT_PRIVATE BidirectionalStreamJob { | |
32 public: | |
33 // Delegate to handle BidirectionalStreamJob events. | |
34 class NET_EXPORT_PRIVATE Delegate { | |
35 public: | |
36 Delegate(); | |
37 | |
38 // Called when the request headers have been sent. | |
39 // The delegate may call BidirectionalStreamJob::ReadData to start reading, | |
40 // call BidirectionalStreamJob::SendData to send data, | |
41 // or call BidirectionalStreamJob::Cancel to cancel the stream. | |
42 // The delegate should not call BidirectionalStreamJob::Cancel | |
43 // during this callback. | |
44 virtual void OnHeadersSent() = 0; | |
45 | |
46 // Called when response headers are received. | |
47 // This is called at most once for the lifetime of a stream. | |
48 // The delegate may call BidirectionalStreamJob::ReadData to start | |
49 // reading, call BidirectionalStreamJob::SendData to send data, | |
50 // or call BidirectionalStreamJob::Cancel to cancel the stream. | |
51 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0; | |
52 | |
53 // Called when read is completed asynchronously. |bytes_read| specifies how | |
54 // much data is available. | |
55 // The delegate may call BidirectionalStreamJob::ReadData to continue | |
56 // reading, call BidirectionalStreamJob::SendData to send data, | |
57 // or call BidirectionalStreamJob::Cancel to cancel the stream. | |
58 virtual void OnDataRead(int bytes_read) = 0; | |
59 | |
60 // Called when the entire buffer passed through SendData is sent. | |
61 // The delegate may call BidirectionalStreamJob::ReadData to continue | |
62 // reading, or call BidirectionalStreamJob::SendData to send data. | |
63 // The delegate should not call BidirectionalStreamJob::Cancel | |
64 // during this callback. | |
65 virtual void OnDataSent() = 0; | |
66 | |
67 // Called when trailers are received. This is called as soon as trailers | |
68 // are received, which can happen before a read completes. | |
69 // The delegate is able to continue reading if there is no pending read and | |
70 // EOF has not been received, or to send data if there is no pending send. | |
71 virtual void OnTrailersReceived(const SpdyHeaderBlock& trailers) = 0; | |
72 | |
73 // Called when an error occurred. | |
74 // No other delegate functions will be called after this. | |
75 virtual void OnFailed(int status) = 0; | |
76 | |
77 protected: | |
78 virtual ~Delegate(); | |
79 | |
80 private: | |
81 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
82 }; | |
83 | |
84 BidirectionalStreamJob(); | |
85 | |
86 // |this| should not be destroyed during Delegate::OnHeadersSent or | |
87 // Delegate::OnDataSent. | |
88 virtual ~BidirectionalStreamJob(); | |
89 | |
90 // Starts the BidirectionalStreamJob and sends request headers. | |
91 virtual void Start(const BidirectionalStreamRequestInfo* request_info, | |
92 const BoundNetLog& net_log, | |
93 BidirectionalStreamJob::Delegate* delegate, | |
94 scoped_ptr<base::Timer> timer) = 0; | |
95 | |
96 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, | |
97 // ERR_IO_PENDING if the read is to be completed asynchronously, or an error | |
98 // code if any error occurred. If returns 0, there is no more data to read. | |
99 // This should not be called before Delegate::OnHeadersReceived is invoked, | |
100 // and should not be called again unless it returns with number greater than | |
101 // 0 or until Delegate::OnDataRead is invoked. | |
102 virtual int ReadData(IOBuffer* buf, int buf_len) = 0; | |
103 | |
104 // Sends data. This should not be called be called before | |
105 // Delegate::OnHeadersSent is invoked, and should not be called again until | |
106 // Delegate::OnDataSent is invoked. If |end_stream| is true, the DATA frame | |
107 // will have an END_STREAM flag. | |
108 virtual void SendData(IOBuffer* data, int length, bool end_stream) = 0; | |
109 | |
110 // Cancels the stream. No Delegate method will be called. Any pending | |
111 // operations may or may not succeed. | |
112 virtual void Cancel() = 0; | |
113 | |
114 // Returns the protocol used by this stream. If stream has not been | |
115 // established, return kProtoUnknown. | |
116 virtual NextProto GetProtocol() const = 0; | |
117 | |
118 // Total number of bytes received over the network of SPDY data, headers, and | |
119 // push_promise frames associated with this stream, including the size of | |
120 // frame headers, after SSL decryption and not including proxy overhead. | |
121 virtual int64_t GetTotalReceivedBytes() const = 0; | |
122 | |
123 // Total number of bytes sent over the network of SPDY frames associated with | |
124 // this stream, including the size of frame headers, before SSL encryption and | |
125 // not including proxy overhead. Note that some SPDY frames such as pings are | |
126 // not associated with any stream, and are not included in this value. | |
127 virtual int64_t GetTotalSentBytes() const = 0; | |
128 | |
129 private: | |
130 DISALLOW_COPY_AND_ASSIGN(BidirectionalStreamJob); | |
131 }; | |
132 | |
133 } // namespace net | |
134 | |
135 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_JOB_H_ | |
OLD | NEW |