Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / websockets / websocket_deflate_parameters.h
blobd0c73bfff5a14157356c2a9ae8c42ca9f108abad
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_
8 #include <stdint.h>
9 #include <string>
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"
16 namespace net {
18 // A WebSocketDeflateParameters represents permessage-deflate extension
19 // parameters. This class is used either for request and response.
20 class NET_EXPORT_PRIVATE WebSocketDeflateParameters {
21 public:
22 using ContextTakeOverMode = WebSocketDeflater::ContextTakeOverMode;
24 // Returns a WebSocketExtension instance containing the parameters stored in
25 // this object.
26 WebSocketExtension AsExtension() const;
28 // Returns true when succeeded.
29 // Returns false and stores the failure message to |failure_message|
30 // otherwise.
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
38 // response.
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 {
44 std::string message;
45 return IsValidAsRequest(&message);
47 bool IsValidAsResponse() const {
48 std::string message;
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; }
101 private:
102 struct WindowBits {
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) {}
107 int16_t bits;
108 // True when "window bits" parameter appears in the parameters.
109 bool is_specified;
110 // True when "window bits" parameter has the value.
111 bool has_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_;
126 } // namespace net
128 #endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PARAMETERS_H_