blob: da85bfec912d9132e97292f35d07cb8a43ced0c9 [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
8#include <deque>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "net/base/net_export.h"
15
16extern "C" struct z_stream_s;
17
18namespace net {
19
20class IOBufferWithSize;
21
22class NET_EXPORT_PRIVATE WebSocketDeflater {
23 public:
24 enum ContextTakeOverMode {
25 DO_NOT_TAKE_OVER_CONTEXT,
26 TAKE_OVER_CONTEXT,
27 };
28
29 explicit WebSocketDeflater(ContextTakeOverMode mode);
30 ~WebSocketDeflater();
31
32 // Returns true if there is no error and false otherwise.
33 // This function must be called exactly once before calling any of
34 // following methods.
35 // |window_bits| must be between 8 and 15 (both inclusive).
36 bool Initialize(int window_bits);
37
38 // Adds bytes to |stream_|.
39 // Returns true if there is no error and false otherwise.
40 bool AddBytes(const char* data, size_t size);
41
42 // Flushes the current processing data.
43 // Returns true if there is no error and false otherwise.
44 bool Finish();
45
46 // Pushes "\x00\x00\xff\xff" to the end of the buffer.
47 void PushSyncMark();
48
49 // Returns the current deflated output.
50 // If the current output is larger than |size| bytes,
51 // returns the first |size| bytes of the current output.
52 // The returned bytes will be dropped from the current output and never be
53 // returned thereafter.
54 scoped_refptr<IOBufferWithSize> GetOutput(size_t size);
55
56 // Returns the size of the current deflated output.
57 size_t CurrentOutputSize() const { return buffer_.size(); }
58
59 private:
60 void ResetContext();
61 int Deflate(int flush);
62
63 scoped_ptr<z_stream_s> stream_;
64 ContextTakeOverMode mode_;
65 std::deque<char> buffer_;
66 std::vector<char> fixed_buffer_;
67 // true if bytes were added after last Finish().
68 bool are_bytes_added_;
69
70 DISALLOW_COPY_AND_ASSIGN(WebSocketDeflater);
71};
72
73} // namespace net
74
75#endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_