OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_DEFLATE_STREAM_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_DEFLATE_STREAM_H_ |
6 #define NET_WEBSOCKETS_WEBSOCKET_DEFLATE_STREAM_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_DEFLATE_STREAM_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
14 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
15 #include "net/websockets/websocket_deflater.h" | 15 #include "net/websockets/websocket_deflater.h" |
16 #include "net/websockets/websocket_frame.h" | 16 #include "net/websockets/websocket_frame.h" |
17 #include "net/websockets/websocket_inflater.h" | 17 #include "net/websockets/websocket_inflater.h" |
18 #include "net/websockets/websocket_stream.h" | 18 #include "net/websockets/websocket_stream.h" |
19 | 19 |
20 class GURL; | 20 class GURL; |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
| 24 class WebSocketDeflatePredictor; |
| 25 |
24 // WebSocketDeflateStream is a WebSocketStream subclass. | 26 // WebSocketDeflateStream is a WebSocketStream subclass. |
25 // WebSocketDeflateStream is for permessage-deflate WebSocket extension[1]. | 27 // WebSocketDeflateStream is for permessage-deflate WebSocket extension[1]. |
26 // | 28 // |
| 29 // WebSocketDeflateStream::ReadFrames and WriteFrames may change frame |
| 30 // boundary. In particular, if a control frame is placed in the middle of |
| 31 // data message frames, the control frame can overtake data frames. |
| 32 // Say there are frames df1, df2 and cf, df1 and df2 are frames of a |
| 33 // data message and cf is a control message frame. cf may arrive first and |
| 34 // data frames may follow cf. |
| 35 // Note that message boundary will be preserved, i.e. if the last frame of |
| 36 // a message m1 is read / written before the last frame of a message m2, |
| 37 // WebSocketDeflateStream will respect the order. |
| 38 // |
27 // [1]: http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-12 | 39 // [1]: http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-12 |
28 class NET_EXPORT_PRIVATE WebSocketDeflateStream : public WebSocketStream { | 40 class NET_EXPORT_PRIVATE WebSocketDeflateStream : public WebSocketStream { |
29 public: | 41 public: |
30 WebSocketDeflateStream(scoped_ptr<WebSocketStream> stream, | 42 WebSocketDeflateStream(scoped_ptr<WebSocketStream> stream, |
31 WebSocketDeflater::ContextTakeOverMode mode); | 43 WebSocketDeflater::ContextTakeOverMode mode, |
| 44 scoped_ptr<WebSocketDeflatePredictor> predictor); |
32 virtual ~WebSocketDeflateStream(); | 45 virtual ~WebSocketDeflateStream(); |
33 | 46 |
34 // WebSocketStream functions. | 47 // WebSocketStream functions. |
35 virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames, | 48 virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames, |
36 const CompletionCallback& callback) OVERRIDE; | 49 const CompletionCallback& callback) OVERRIDE; |
37 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames, | 50 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames, |
38 const CompletionCallback& callback) OVERRIDE; | 51 const CompletionCallback& callback) OVERRIDE; |
39 virtual void Close() OVERRIDE; | 52 virtual void Close() OVERRIDE; |
40 virtual std::string GetSubProtocol() const OVERRIDE; | 53 virtual std::string GetSubProtocol() const OVERRIDE; |
41 virtual std::string GetExtensions() const OVERRIDE; | 54 virtual std::string GetExtensions() const OVERRIDE; |
42 | 55 |
43 private: | 56 private: |
44 enum ReadingState { | 57 enum ReadingState { |
45 READING_COMPRESSED_MESSAGE, | 58 READING_COMPRESSED_MESSAGE, |
46 READING_UNCOMPRESSED_MESSAGE, | 59 READING_UNCOMPRESSED_MESSAGE, |
47 NOT_READING, | 60 NOT_READING, |
48 }; | 61 }; |
49 | 62 |
50 enum WritingState { | 63 enum WritingState { |
51 WRITING_COMPRESSED_MESSAGE, | 64 WRITING_COMPRESSED_MESSAGE, |
52 WRITING_UNCOMPRESSED_MESSAGE, | 65 WRITING_UNCOMPRESSED_MESSAGE, |
| 66 WRITING_POSSIBLY_COMPRESSED_MESSAGE, |
53 NOT_WRITING, | 67 NOT_WRITING, |
54 }; | 68 }; |
55 | 69 |
56 void OnReadComplete(ScopedVector<WebSocketFrame>* frames, | 70 void OnReadComplete(ScopedVector<WebSocketFrame>* frames, |
57 const CompletionCallback& callback, | 71 const CompletionCallback& callback, |
58 int result); | 72 int result); |
59 | 73 |
60 // This function deflates |frames| and stores the result to |frames| itself. | 74 // This function deflates |frames| and stores the result to |frames| itself. |
61 int Deflate(ScopedVector<WebSocketFrame>* frames); | 75 int Deflate(ScopedVector<WebSocketFrame>* frames); |
| 76 void OnMessageStart(const ScopedVector<WebSocketFrame>& frames, size_t index); |
| 77 int AppendCompressedFrame(const WebSocketFrameHeader& header, |
| 78 ScopedVector<WebSocketFrame>* frames_to_write); |
| 79 int AppendPossiblyCompressedMessage( |
| 80 ScopedVector<WebSocketFrame>* frames, |
| 81 ScopedVector<WebSocketFrame>* frames_to_write); |
62 | 82 |
63 // This function inflates |frames| and stores the result to |frames| itself. | 83 // This function inflates |frames| and stores the result to |frames| itself. |
64 int Inflate(ScopedVector<WebSocketFrame>* frames); | 84 int Inflate(ScopedVector<WebSocketFrame>* frames); |
65 | 85 |
66 int InflateAndReadIfNecessary(ScopedVector<WebSocketFrame>* frames, | 86 int InflateAndReadIfNecessary(ScopedVector<WebSocketFrame>* frames, |
67 const CompletionCallback& callback); | 87 const CompletionCallback& callback); |
68 | 88 |
69 const scoped_ptr<WebSocketStream> stream_; | 89 const scoped_ptr<WebSocketStream> stream_; |
70 WebSocketDeflater deflater_; | 90 WebSocketDeflater deflater_; |
71 WebSocketInflater inflater_; | 91 WebSocketInflater inflater_; |
72 ReadingState reading_state_; | 92 ReadingState reading_state_; |
73 WritingState writing_state_; | 93 WritingState writing_state_; |
74 WebSocketFrameHeader::OpCode current_reading_opcode_; | 94 WebSocketFrameHeader::OpCode current_reading_opcode_; |
75 WebSocketFrameHeader::OpCode current_writing_opcode_; | 95 WebSocketFrameHeader::OpCode current_writing_opcode_; |
| 96 scoped_ptr<WebSocketDeflatePredictor> predictor_; |
76 | 97 |
77 DISALLOW_COPY_AND_ASSIGN(WebSocketDeflateStream); | 98 DISALLOW_COPY_AND_ASSIGN(WebSocketDeflateStream); |
78 }; | 99 }; |
79 | 100 |
80 } // namespace net | 101 } // namespace net |
81 | 102 |
82 #endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATE_STREAM_H_ | 103 #endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATE_STREAM_H_ |
OLD | NEW |