blob: 3aa6f24653039f291f68a7786ac9b9aef6c995f3 [file] [log] [blame]
[email protected]f8e65162013-09-10 16:13:041// 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
5#ifndef NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_
6#define NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_
7
tfarinaea94afc232015-10-20 04:23:368#include <stddef.h>
Avi Drissman13fc8932015-12-20 04:40:469
danakj9c5cab52016-04-16 00:54:3310#include <memory>
[email protected]f8e65162013-09-10 16:13:0411#include <vector>
12
Brett Wilsonc6a0c822017-09-12 00:04:2913#include "base/containers/circular_deque.h"
tfarinaea94afc232015-10-20 04:23:3614#include "base/macros.h"
Bence Béky65623972018-03-05 15:31:5615#include "base/memory/scoped_refptr.h"
[email protected]f8e65162013-09-10 16:13:0416#include "net/base/net_export.h"
17
18extern "C" struct z_stream_s;
19
20namespace net {
21
22class IOBufferWithSize;
23
24class NET_EXPORT_PRIVATE WebSocketDeflater {
25 public:
[email protected]9c50b042014-04-28 06:40:1526 // Do not reorder or remove entries of this enum. The values of them are used
27 // in UMA.
[email protected]f8e65162013-09-10 16:13:0428 enum ContextTakeOverMode {
29 DO_NOT_TAKE_OVER_CONTEXT,
30 TAKE_OVER_CONTEXT,
[email protected]9c50b042014-04-28 06:40:1531 NUM_CONTEXT_TAKEOVER_MODE_TYPES,
[email protected]f8e65162013-09-10 16:13:0432 };
33
34 explicit WebSocketDeflater(ContextTakeOverMode mode);
35 ~WebSocketDeflater();
36
37 // Returns true if there is no error and false otherwise.
38 // This function must be called exactly once before calling any of
39 // following methods.
40 // |window_bits| must be between 8 and 15 (both inclusive).
41 bool Initialize(int window_bits);
42
43 // Adds bytes to |stream_|.
44 // Returns true if there is no error and false otherwise.
45 bool AddBytes(const char* data, size_t size);
46
47 // Flushes the current processing data.
48 // Returns true if there is no error and false otherwise.
49 bool Finish();
50
51 // Pushes "\x00\x00\xff\xff" to the end of the buffer.
52 void PushSyncMark();
53
54 // Returns the current deflated output.
55 // If the current output is larger than |size| bytes,
56 // returns the first |size| bytes of the current output.
57 // The returned bytes will be dropped from the current output and never be
58 // returned thereafter.
59 scoped_refptr<IOBufferWithSize> GetOutput(size_t size);
60
61 // Returns the size of the current deflated output.
62 size_t CurrentOutputSize() const { return buffer_.size(); }
63
64 private:
65 void ResetContext();
66 int Deflate(int flush);
67
danakj9c5cab52016-04-16 00:54:3368 std::unique_ptr<z_stream_s> stream_;
[email protected]f8e65162013-09-10 16:13:0469 ContextTakeOverMode mode_;
Brett Wilsonc6a0c822017-09-12 00:04:2970 base::circular_deque<char> buffer_;
[email protected]f8e65162013-09-10 16:13:0471 std::vector<char> fixed_buffer_;
72 // true if bytes were added after last Finish().
73 bool are_bytes_added_;
74
75 DISALLOW_COPY_AND_ASSIGN(WebSocketDeflater);
76};
77
78} // namespace net
79
80#endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_