Flutter Engine
The 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_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
13namespace flutter {
14
15class EncodableValue;
16
17template <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)
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().
37template <typename T = EncodableValue>
39 public:
40 StreamHandler() = default;
41 virtual ~StreamHandler() = default;
42
43 // Prevent copying.
44 StreamHandler(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_
std::unique_ptr< StreamHandlerError< T > > OnCancel(const T *arguments)
virtual std::unique_ptr< StreamHandlerError< T > > OnCancelInternal(const T *arguments)=0
std::unique_ptr< StreamHandlerError< T > > OnListen(const T *arguments, std::unique_ptr< EventSink< T > > &&events)
virtual ~StreamHandler()=default
virtual std::unique_ptr< StreamHandlerError< T > > OnListenInternal(const T *arguments, std::unique_ptr< EventSink< T > > &&events)=0
StreamHandler & operator=(StreamHandler const &)=delete
StreamHandler(StreamHandler const &)=delete
Definition: ref_ptr.h:256
#define T
Definition: precompiler.cc:65
StreamHandlerError(const std::string &error_code, const std::string &error_message, std::unique_ptr< T > &&error_details)
const std::unique_ptr< T > error_details