1 // Copyright 2020 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 CHROME_BROWSER_NEARBY_SHARING_INSTANTMESSAGING_STREAM_PARSER_H_
6 #define CHROME_BROWSER_NEARBY_SHARING_INSTANTMESSAGING_STREAM_PARSER_H_
7 
8 #include <string>
9 
10 #include "base/callback.h"
11 #include "base/optional.h"
12 #include "base/strings/string_piece.h"
13 
14 namespace chrome_browser_nearby_sharing_instantmessaging {
15 class StreamBody;
16 }  // namespace chrome_browser_nearby_sharing_instantmessaging
17 
18 // Parses incoming stream of data into valid proto objects and delegates them to
19 // the registered callback.
20 class StreamParser {
21  public:
22   explicit StreamParser(
23       base::RepeatingCallback<void(const std::string& message)> listener,
24       base::OnceClosure fastpath_ready);
25   ~StreamParser();
26 
27   void Append(base::StringPiece data);
28 
29  private:
30   base::Optional<chrome_browser_nearby_sharing_instantmessaging::StreamBody>
31   GetNextMessage();
32   void DelegateMessage(
33       const chrome_browser_nearby_sharing_instantmessaging::StreamBody&
34           stream_body);
35 
36   base::RepeatingCallback<void(const std::string& message)> listener_;
37   base::OnceClosure fastpath_ready_callback_;
38   std::string data_;
39 };
40 
41 #endif  // CHROME_BROWSER_NEARBY_SHARING_INSTANTMESSAGING_STREAM_PARSER_H_
42