xref: /aosp_15_r20/external/cronet/net/websockets/websocket_deflate_predictor.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 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_WEBSOCKETS_WEBSOCKET_DEFLATE_PREDICTOR_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PREDICTOR_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "net/base/net_export.h"
14 
15 namespace net {
16 
17 struct WebSocketFrame;
18 
19 // WebSocketDeflatePredictor is an interface class used for judging whether
20 // a WebSocketDeflateStream should compress a message or not.
21 class NET_EXPORT_PRIVATE WebSocketDeflatePredictor {
22  public:
23   enum Result {
24     // Deflate and send the message.
25     DEFLATE,
26     // Do not deflate and send the original message.
27     DO_NOT_DEFLATE,
28     // Try compressing the message and send the smaller one of the original
29     // and the compressed message.
30     // Returning this result implies that the deflater is running on
31     // DoNotTakeOverContext mode and the entire message is visible.
32     TRY_DEFLATE,
33   };
34 
35   virtual ~WebSocketDeflatePredictor() = default;
36 
37   // Predicts and returns whether the deflater should deflate the message
38   // which begins with |frames[frame_index]| or not.
39   // |frames[(frame_index + 1):]| consists of future frames if any.
40   // |frames[frame_index]| must be the first frame of a data message,
41   // but future frames may contain control message frames.
42   // |frames[frame_index]| cannot be recorded yet and all preceding
43   // data frames have to be already recorded when this method is called.
44   virtual Result Predict(
45       const std::vector<std::unique_ptr<WebSocketFrame>>& frames,
46       size_t frame_index) = 0;
47 
48   // Records frame data for future prediction.
49   // Only data frames should be recorded. Do not pass control frames' data.
50   // All input data frames for the stream must be recorded in order.
51   virtual void RecordInputDataFrame(const WebSocketFrame* frame) = 0;
52 
53   // Records frame data for future prediction.
54   // Only data frames should be recorded. Do not pass control frames' data.
55   // All data frames written by the stream must be recorded in order
56   // regardless of whether they are compressed or not.
57   virtual void RecordWrittenDataFrame(const WebSocketFrame* frame) = 0;
58 };
59 
60 }  // namespace net
61 
62 #endif  // NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PREDICTOR_H_
63