[email protected] | f8e6516 | 2013-09-10 16:13:04 | [diff] [blame] | 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 | |
| 5 | #ifndef NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_ |
| 6 | #define NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_ |
| 7 | |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 8 | #include <stddef.h> |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 9 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 10 | #include <memory> |
[email protected] | f8e6516 | 2013-09-10 16:13:04 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 13 | #include "base/containers/circular_deque.h" |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 14 | #include "base/macros.h" |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame^] | 15 | #include "base/memory/scoped_refptr.h" |
[email protected] | f8e6516 | 2013-09-10 16:13:04 | [diff] [blame] | 16 | #include "net/base/net_export.h" |
| 17 | |
| 18 | extern "C" struct z_stream_s; |
| 19 | |
| 20 | namespace net { |
| 21 | |
| 22 | class IOBufferWithSize; |
| 23 | |
| 24 | class NET_EXPORT_PRIVATE WebSocketDeflater { |
| 25 | public: |
[email protected] | 9c50b04 | 2014-04-28 06:40:15 | [diff] [blame] | 26 | // Do not reorder or remove entries of this enum. The values of them are used |
| 27 | // in UMA. |
[email protected] | f8e6516 | 2013-09-10 16:13:04 | [diff] [blame] | 28 | enum ContextTakeOverMode { |
| 29 | DO_NOT_TAKE_OVER_CONTEXT, |
| 30 | TAKE_OVER_CONTEXT, |
[email protected] | 9c50b04 | 2014-04-28 06:40:15 | [diff] [blame] | 31 | NUM_CONTEXT_TAKEOVER_MODE_TYPES, |
[email protected] | f8e6516 | 2013-09-10 16:13:04 | [diff] [blame] | 32 | }; |
| 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 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 68 | std::unique_ptr<z_stream_s> stream_; |
[email protected] | f8e6516 | 2013-09-10 16:13:04 | [diff] [blame] | 69 | ContextTakeOverMode mode_; |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 70 | base::circular_deque<char> buffer_; |
[email protected] | f8e6516 | 2013-09-10 16:13:04 | [diff] [blame] | 71 | 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_ |