OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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_DEFLATE_PARAMETERS_H_ | |
6 #define NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PARAMETERS_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/logging.h" | |
11 #include "net/base/net_export.h" | |
12 #include "net/websockets/websocket_deflater.h" | |
13 #include "net/websockets/websocket_extension.h" | |
14 | |
15 namespace net { | |
16 | |
17 // A WebSocketDeflateParameters represents permessage-deflate extension | |
18 // parameters. This class is used either for request and response. | |
19 class NET_EXPORT_PRIVATE WebSocketDeflateParameters { | |
20 using ContextTakeOverMode = WebSocketDeflater::ContextTakeOverMode; | |
21 static const ContextTakeOverMode TAKE_OVER_CONTEXT = | |
22 WebSocketDeflater::TAKE_OVER_CONTEXT; | |
23 static const ContextTakeOverMode DO_NOT_TAKE_OVER_CONTEXT = | |
24 WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT; | |
davidben
2015/09/10 15:59:39
This violates the One Definition Rule and is undef
yhirano
2015/09/11 01:49:12
Thanks, I didn't notice the thread. Fixed.
| |
25 struct WindowBits { | |
26 WindowBits() : WindowBits(0, false, false) {} | |
27 WindowBits(int16 bits, bool is_specified, bool has_value) | |
28 : bits(bits), is_specified(is_specified), has_value(has_value) {} | |
29 | |
30 int16 bits; | |
31 // True when "window bits" parameter appears in the parameters. | |
32 bool is_specified; | |
33 // True when "window bits" parameter has the value. | |
34 bool has_value; | |
35 }; | |
davidben
2015/09/10 15:59:39
Style: private values should be at the bottom.
yhirano
2015/09/11 01:49:12
Done.
| |
36 | |
37 public: | |
38 // Returns a WebSocketExtension instance containing the parameters stored in | |
39 // this object. | |
40 WebSocketExtension AsExtension() const; | |
41 | |
42 // Returns true when succeeded. | |
43 // Returns false and stores the failure message to |failure_message| | |
44 // otherwise. | |
45 // Note that even if this function succeeds it is not guaranteed that the | |
46 // object is valid. To check it, call IsValidAsRequest or IsValidAsResponse. | |
47 bool Initialize(const WebSocketExtension& input, | |
48 std::string* failure_message); | |
49 | |
50 // Returns true when |*this| and |response| are compatible. | |
51 // |*this| must be valid as a request and |response| must be valid as a | |
52 // response. | |
53 bool IsCompatibleWith(const WebSocketDeflateParameters& response) const; | |
54 | |
55 bool IsValidAsRequest(std::string* failure_message) const; | |
56 bool IsValidAsResponse(std::string* failure_message) const; | |
57 bool IsValidAsRequest() const { | |
58 std::string message; | |
59 return IsValidAsRequest(&message); | |
60 } | |
61 bool IsValidAsResponse() const { | |
62 std::string message; | |
63 return IsValidAsResponse(&message); | |
64 } | |
65 | |
66 ContextTakeOverMode server_context_take_over_mode() const { | |
67 return server_context_take_over_mode_; | |
68 } | |
69 ContextTakeOverMode client_context_take_over_mode() const { | |
70 return client_context_take_over_mode_; | |
71 } | |
72 bool is_server_max_window_bits_specified() const { | |
73 return server_max_window_bits_.is_specified; | |
74 } | |
75 int server_max_window_bits() const { | |
76 DCHECK(is_server_max_window_bits_specified()); | |
77 return server_max_window_bits_.bits; | |
78 } | |
79 bool is_client_max_window_bits_specified() const { | |
80 return client_max_window_bits_.is_specified; | |
81 } | |
82 bool has_client_max_window_bits_value() const { | |
83 DCHECK(is_client_max_window_bits_specified()); | |
84 return client_max_window_bits_.has_value; | |
85 } | |
86 int client_max_window_bits() const { | |
87 DCHECK(has_client_max_window_bits_value()); | |
88 return client_max_window_bits_.bits; | |
89 } | |
90 void SetServerNoContextTakeOver() { | |
91 server_context_take_over_mode_ = DO_NOT_TAKE_OVER_CONTEXT; | |
92 } | |
93 void SetClientNoContextTakeOver() { | |
94 client_context_take_over_mode_ = DO_NOT_TAKE_OVER_CONTEXT; | |
95 } | |
96 // |bits| must be valid as a max_window_bits value. | |
97 void SetServerMaxWindowBits(int bits) { | |
98 DCHECK(IsValidWindowBits(bits)); | |
99 server_max_window_bits_ = WindowBits(bits, true, true); | |
100 } | |
101 void SetClientMaxWindowBits() { | |
102 client_max_window_bits_ = WindowBits(0, true, false); | |
103 } | |
104 // |bits| must be valid as a max_window_bits value. | |
105 void SetClientMaxWindowBits(int bits) { | |
106 DCHECK(IsValidWindowBits(bits)); | |
107 client_max_window_bits_ = WindowBits(bits, true, true); | |
108 } | |
109 | |
110 // Return true if |bits| is valid as a max_window_bits value. | |
111 static bool IsValidWindowBits(int bits) { return 8 <= bits && bits <= 15; } | |
112 | |
113 private: | |
114 // |server_context_take_over_mode| is set to DO_NOT_TAKE_OVER_CONTEXT if and | |
115 // only if |server_no_context_takeover| is set in the parameters. | |
116 ContextTakeOverMode server_context_take_over_mode_ = TAKE_OVER_CONTEXT; | |
117 // |client_context_take_over_mode| is set to DO_NOT_TAKE_OVER_CONTEXT if and | |
118 // only if |client_no_context_takeover| is set in the parameters. | |
119 ContextTakeOverMode client_context_take_over_mode_ = TAKE_OVER_CONTEXT; | |
120 WindowBits server_max_window_bits_; | |
121 WindowBits client_max_window_bits_; | |
122 }; | |
123 | |
124 } // namespace net | |
125 | |
126 #endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PARAMETERS_H_ | |
OLD | NEW |