| Flutter Windows Embedder |

engine_method_result.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_ENGINE_METHOD_RESULT_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_ENGINE_METHOD_RESULT_H_
7
8 #include <memory>
9 #include <string>
10 #include <vector>
11
12 #include "binary_messenger.h"
13 #include "method_codec.h"
14 #include "method_result.h"
15
16 namespace flutter {
17
18 namespace internal {
19 // Manages the one-time sending of response data. This is an internal helper
20 // class for EngineMethodResult, separated out since the implementation doesn't
21 // vary based on the template type.
22 class ReplyManager {
23 public:
24 explicit ReplyManager(BinaryReply reply_handler_);
25 ~ReplyManager();
26
27 // Prevent copying.
28 ReplyManager(ReplyManager const&) = delete;
29 ReplyManager& operator=(ReplyManager const&) = delete;
30
31 // Sends the given response data (which must either be nullptr, which
32 // indicates an unhandled method, or a response serialized with |codec_|) to
33 // the engine.
34 void SendResponseData(const std::vector<uint8_t>* data);
35
36 private:
37 BinaryReply reply_handler_;
38 };
39 } // namespace internal
40
41 // Implemention of MethodResult that sends a response to the Flutter engine
42 // exactly once, encoded using a given codec.
43 template <typename T>
44 class EngineMethodResult : public MethodResult<T> {
45 public:
46 // Creates a result object that will send results to |reply_handler|, encoded
47 // using |codec|. The |codec| pointer must remain valid for as long as this
48 // object exists.
49 EngineMethodResult(BinaryReply reply_handler, const MethodCodec<T>* codec)
50 : reply_manager_(
51 std::make_unique<internal::ReplyManager>(std::move(reply_handler))),
52 codec_(codec) {}
53
54 ~EngineMethodResult() = default;
55
56 protected:
57 // |flutter::MethodResult|
58 void SuccessInternal(const T* result) override {
59 std::unique_ptr<std::vector<uint8_t>> data =
60 codec_->EncodeSuccessEnvelope(result);
61 reply_manager_->SendResponseData(data.get());
62 }
63
64 // |flutter::MethodResult|
65 void ErrorInternal(const std::string& error_code,
66 const std::string& error_message,
67 const T* error_details) override {
68 std::unique_ptr<std::vector<uint8_t>> data =
69 codec_->EncodeErrorEnvelope(error_code, error_message, error_details);
70 reply_manager_->SendResponseData(data.get());
71 }
72
73 // |flutter::MethodResult|
74 void NotImplementedInternal() override {
75 reply_manager_->SendResponseData(nullptr);
76 }
77
78 private:
79 std::unique_ptr<internal::ReplyManager> reply_manager_;
80
81 const MethodCodec<T>* codec_;
82 };
83
84 } // namespace flutter
85
86 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_ENGINE_METHOD_RESULT_H_
binary_messenger.h
flutter::EngineMethodResult
Definition: engine_method_result.h:44
flutter::EngineMethodResult::SuccessInternal
void SuccessInternal(const T *result) override
Definition: engine_method_result.h:58
flutter::EngineMethodResult::~EngineMethodResult
~EngineMethodResult()=default
flutter::EngineMethodResult::ErrorInternal
void ErrorInternal(const std::string &error_code, const std::string &error_message, const T *error_details) override
Definition: engine_method_result.h:65
flutter::EngineMethodResult::NotImplementedInternal
void NotImplementedInternal() override
Definition: engine_method_result.h:74
flutter::EngineMethodResult::EngineMethodResult
EngineMethodResult(BinaryReply reply_handler, const MethodCodec< T > *codec)
Definition: engine_method_result.h:49
flutter::MethodCodec
Definition: method_codec.h:20
flutter::MethodResult
Definition: method_result.h:17
flutter::internal::ReplyManager
Definition: engine_method_result.h:22
flutter::internal::ReplyManager::~ReplyManager
~ReplyManager()
Definition: core_implementations.cc:141
flutter::internal::ReplyManager::ReplyManager
ReplyManager(BinaryReply reply_handler_)
Definition: core_implementations.cc:136
flutter::internal::ReplyManager::operator=
ReplyManager & operator=(ReplyManager const &)=delete
flutter::internal::ReplyManager::ReplyManager
ReplyManager(ReplyManager const &)=delete
flutter::internal::ReplyManager::SendResponseData
void SendResponseData(const std::vector< uint8_t > *data)
Definition: core_implementations.cc:151
method_codec.h
method_result.h
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::BinaryReply
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
Definition: binary_messenger.h:17