websocket_deflate_predictor.h 2.3 KB

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