Flutter Engine
event_stream_handler.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_
7 
8 #include "event_sink.h"
9 
10 namespace flutter {
11 
12 class EncodableValue;
13 
14 template <typename T = EncodableValue>
16  const std::string& error_code;
17  const std::string& error_message;
18  const T* error_details;
19 
20  StreamHandlerError(const std::string& error_code,
21  const std::string& error_message,
22  const T* error_details)
23  : error_code(error_code),
24  error_message(error_message),
25  error_details(error_details) {}
26 };
27 
28 // Handler of stream setup and tear-down requests.
29 // Implementations must be prepared to accept sequences of alternating calls to
30 // OnListen() and OnCancel(). Implementations should ideally consume no
31 // resources when the last such call is not OnListen(). In typical situations,
32 // this means that the implementation should register itself with
33 // platform-specific event sources OnListen() and deregister again OnCancel().
34 template <typename T = EncodableValue>
36  public:
37  StreamHandler() = default;
38  virtual ~StreamHandler() = default;
39 
40  // Prevent copying.
41  StreamHandler(StreamHandler const&) = delete;
42  StreamHandler& operator=(StreamHandler const&) = delete;
43 
44  // Handles a request to set up an event stream. Returns nullptr on success,
45  // or an error on failure.
46  // |arguments| is stream configuration arguments and
47  // |events| is an EventSink for emitting events to the Flutter receiver.
48  std::unique_ptr<StreamHandlerError<T>> OnListen(
49  const T* arguments,
50  std::unique_ptr<EventSink<T>>&& events) {
51  return OnListenInternal(arguments, std::move(events));
52  }
53 
54  // Handles a request to tear down the most recently created event stream.
55  // Returns nullptr on success, or an error on failure.
56  // |arguments| is stream configuration arguments.
57  std::unique_ptr<StreamHandlerError<T>> OnCancel(const T* arguments) {
58  return OnCancelInternal(arguments);
59  }
60 
61  protected:
62  // Implementation of the public interface, to be provided by subclasses.
63  virtual std::unique_ptr<StreamHandlerError<T>> OnListenInternal(
64  const T* arguments,
65  std::unique_ptr<EventSink<T>>&& events) = 0;
66 
67  // Implementation of the public interface, to be provided by subclasses.
68  virtual std::unique_ptr<StreamHandlerError<T>> OnCancelInternal(
69  const T* arguments) = 0;
70 };
71 
72 } // namespace flutter
73 
74 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_
std::unique_ptr< StreamHandlerError< T > > OnListen(const T *arguments, std::unique_ptr< EventSink< T >> &&events)
StreamHandlerError(const std::string &error_code, const std::string &error_message, const T *error_details)
const std::string & error_message
std::unique_ptr< StreamHandlerError< T > > OnCancel(const T *arguments)