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.
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PARAMETERS_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PARAMETERS_H_
11 #include "base/logging.h"
12 #include "net/base/net_export.h"
13 #include "net/websockets/websocket_deflater.h"
14 #include "net/websockets/websocket_extension.h"
18 // A WebSocketDeflateParameters represents permessage-deflate extension
19 // parameters. This class is used either for request and response.
20 class NET_EXPORT_PRIVATE WebSocketDeflateParameters
{
22 using ContextTakeOverMode
= WebSocketDeflater::ContextTakeOverMode
;
24 // Returns a WebSocketExtension instance containing the parameters stored in
26 WebSocketExtension
AsExtension() const;
28 // Returns true when succeeded.
29 // Returns false and stores the failure message to |failure_message|
31 // Note that even if this function succeeds it is not guaranteed that the
32 // object is valid. To check it, call IsValidAsRequest or IsValidAsResponse.
33 bool Initialize(const WebSocketExtension
& input
,
34 std::string
* failure_message
);
36 // Returns true when |*this| and |response| are compatible.
37 // |*this| must be valid as a request and |response| must be valid as a
39 bool IsCompatibleWith(const WebSocketDeflateParameters
& response
) const;
41 bool IsValidAsRequest(std::string
* failure_message
) const;
42 bool IsValidAsResponse(std::string
* failure_message
) const;
43 bool IsValidAsRequest() const {
45 return IsValidAsRequest(&message
);
47 bool IsValidAsResponse() const {
49 return IsValidAsResponse(&message
);
52 ContextTakeOverMode
server_context_take_over_mode() const {
53 return server_context_take_over_mode_
;
55 ContextTakeOverMode
client_context_take_over_mode() const {
56 return client_context_take_over_mode_
;
58 bool is_server_max_window_bits_specified() const {
59 return server_max_window_bits_
.is_specified
;
61 int server_max_window_bits() const {
62 DCHECK(is_server_max_window_bits_specified());
63 return server_max_window_bits_
.bits
;
65 bool is_client_max_window_bits_specified() const {
66 return client_max_window_bits_
.is_specified
;
68 bool has_client_max_window_bits_value() const {
69 DCHECK(is_client_max_window_bits_specified());
70 return client_max_window_bits_
.has_value
;
72 int client_max_window_bits() const {
73 DCHECK(has_client_max_window_bits_value());
74 return client_max_window_bits_
.bits
;
76 void SetServerNoContextTakeOver() {
77 server_context_take_over_mode_
=
78 WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT
;
80 void SetClientNoContextTakeOver() {
81 client_context_take_over_mode_
=
82 WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT
;
84 // |bits| must be valid as a max_window_bits value.
85 void SetServerMaxWindowBits(int bits
) {
86 DCHECK(IsValidWindowBits(bits
));
87 server_max_window_bits_
= WindowBits(bits
, true, true);
89 void SetClientMaxWindowBits() {
90 client_max_window_bits_
= WindowBits(0, true, false);
92 // |bits| must be valid as a max_window_bits value.
93 void SetClientMaxWindowBits(int bits
) {
94 DCHECK(IsValidWindowBits(bits
));
95 client_max_window_bits_
= WindowBits(bits
, true, true);
98 // Return true if |bits| is valid as a max_window_bits value.
99 static bool IsValidWindowBits(int bits
) { return 8 <= bits
&& bits
<= 15; }
103 WindowBits() : WindowBits(0, false, false) {}
104 WindowBits(int16_t bits
, bool is_specified
, bool has_value
)
105 : bits(bits
), is_specified(is_specified
), has_value(has_value
) {}
108 // True when "window bits" parameter appears in the parameters.
110 // True when "window bits" parameter has the value.
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_
=
117 WebSocketDeflater::TAKE_OVER_CONTEXT
;
118 // |client_context_take_over_mode| is set to DO_NOT_TAKE_OVER_CONTEXT if and
119 // only if |client_no_context_takeover| is set in the parameters.
120 ContextTakeOverMode client_context_take_over_mode_
=
121 WebSocketDeflater::TAKE_OVER_CONTEXT
;
122 WindowBits server_max_window_bits_
;
123 WindowBits client_max_window_bits_
;
128 #endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PARAMETERS_H_