Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / websockets / websocket_deflater.h
blob1b631e2663ee749e8c6c2e8e5520c86ec54e8586
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.
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_
8 #include <deque>
9 #include <vector>
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"
16 extern "C" struct z_stream_s;
18 namespace net {
20 class IOBufferWithSize;
22 class NET_EXPORT_PRIVATE WebSocketDeflater {
23 public:
24 // Do not reorder or remove entries of this enum. The values of them are used
25 // in UMA.
26 enum ContextTakeOverMode {
27 DO_NOT_TAKE_OVER_CONTEXT,
28 TAKE_OVER_CONTEXT,
29 NUM_CONTEXT_TAKEOVER_MODE_TYPES,
32 explicit WebSocketDeflater(ContextTakeOverMode mode);
33 ~WebSocketDeflater();
35 // Returns true if there is no error and false otherwise.
36 // This function must be called exactly once before calling any of
37 // following methods.
38 // |window_bits| must be between 8 and 15 (both inclusive).
39 bool Initialize(int window_bits);
41 // Adds bytes to |stream_|.
42 // Returns true if there is no error and false otherwise.
43 bool AddBytes(const char* data, size_t size);
45 // Flushes the current processing data.
46 // Returns true if there is no error and false otherwise.
47 bool Finish();
49 // Pushes "\x00\x00\xff\xff" to the end of the buffer.
50 void PushSyncMark();
52 // Returns the current deflated output.
53 // If the current output is larger than |size| bytes,
54 // returns the first |size| bytes of the current output.
55 // The returned bytes will be dropped from the current output and never be
56 // returned thereafter.
57 scoped_refptr<IOBufferWithSize> GetOutput(size_t size);
59 // Returns the size of the current deflated output.
60 size_t CurrentOutputSize() const { return buffer_.size(); }
62 private:
63 void ResetContext();
64 int Deflate(int flush);
66 scoped_ptr<z_stream_s> stream_;
67 ContextTakeOverMode mode_;
68 std::deque<char> buffer_;
69 std::vector<char> fixed_buffer_;
70 // true if bytes were added after last Finish().
71 bool are_bytes_added_;
73 DISALLOW_COPY_AND_ASSIGN(WebSocketDeflater);
76 } // namespace net
78 #endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_