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