[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / quic / crypto / crypto_framer.h
blob694b20716c6d02398f9b38a54ea95e17c6a8a7d0
1 // Copyright (c) 2012 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_QUIC_CRYPTO_CRYPTO_FRAMER_H_
6 #define NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_
8 #include <utility>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/strings/string_piece.h"
14 #include "net/base/net_export.h"
15 #include "net/quic/crypto/crypto_handshake_message.h"
16 #include "net/quic/quic_protocol.h"
18 namespace net {
20 class CryptoFramer;
21 class QuicData;
22 class QuicDataReader;
23 class QuicDataWriter;
25 class NET_EXPORT_PRIVATE CryptoFramerVisitorInterface {
26 public:
27 virtual ~CryptoFramerVisitorInterface() {}
29 // Called if an error is detected.
30 virtual void OnError(CryptoFramer* framer) = 0;
32 // Called when a complete handshake message has been parsed.
33 virtual void OnHandshakeMessage(const CryptoHandshakeMessage& message) = 0;
36 // A class for framing the crypto messages that are exchanged in a QUIC
37 // session.
38 class NET_EXPORT_PRIVATE CryptoFramer {
39 public:
40 CryptoFramer();
42 virtual ~CryptoFramer();
44 // ParseMessage parses exactly one message from the given StringPiece. If
45 // there is an error, the message is truncated, or the message has trailing
46 // garbage then nullptr will be returned.
47 static CryptoHandshakeMessage* ParseMessage(base::StringPiece in);
49 // Set callbacks to be called from the framer. A visitor must be set, or
50 // else the framer will crash. It is acceptable for the visitor to do
51 // nothing. If this is called multiple times, only the last visitor
52 // will be used. |visitor| will be owned by the framer.
53 void set_visitor(CryptoFramerVisitorInterface* visitor) {
54 visitor_ = visitor;
57 QuicErrorCode error() const { return error_; }
59 // Processes input data, which must be delivered in order. Returns
60 // false if there was an error, and true otherwise.
61 bool ProcessInput(base::StringPiece input);
63 // Returns the number of bytes of buffered input data remaining to be
64 // parsed.
65 size_t InputBytesRemaining() const { return buffer_.length(); }
67 // Returns a new QuicData owned by the caller that contains a serialized
68 // |message|, or nullptr if there was an error.
69 static QuicData* ConstructHandshakeMessage(
70 const CryptoHandshakeMessage& message);
72 private:
73 // Clears per-message state. Does not clear the visitor.
74 void Clear();
76 // Process does does the work of |ProcessInput|, but returns an error code,
77 // doesn't set error_ and doesn't call |visitor_->OnError()|.
78 QuicErrorCode Process(base::StringPiece input);
80 static bool WritePadTag(QuicDataWriter* writer,
81 size_t pad_length,
82 uint32* end_offset);
84 // Represents the current state of the parsing state machine.
85 enum CryptoFramerState {
86 STATE_READING_TAG,
87 STATE_READING_NUM_ENTRIES,
88 STATE_READING_TAGS_AND_LENGTHS,
89 STATE_READING_VALUES
92 // Visitor to invoke when messages are parsed.
93 CryptoFramerVisitorInterface* visitor_;
94 // Last error.
95 QuicErrorCode error_;
96 // Remaining unparsed data.
97 std::string buffer_;
98 // Current state of the parsing.
99 CryptoFramerState state_;
100 // The message currently being parsed.
101 CryptoHandshakeMessage message_;
102 // Number of entires in the message currently being parsed.
103 uint16 num_entries_;
104 // tags_and_lengths_ contains the tags that are currently being parsed and
105 // their lengths.
106 std::vector<std::pair<QuicTag, size_t> > tags_and_lengths_;
107 // Cumulative length of all values in the message currently being parsed.
108 size_t values_len_;
111 } // namespace net
113 #endif // NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_