Flutter Engine
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_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_ENGINE_METHOD_RESULT_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_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  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_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_ENGINE_METHOD_RESULT_H_
Definition: ref_ptr.h:252
ReplyManager & operator=(ReplyManager const &)=delete
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
EngineMethodResult(BinaryReply reply_handler, const MethodCodec< T > *codec)
void SendResponseData(const std::vector< uint8_t > *data)
void ErrorInternal(const std::string &error_code, const std::string &error_message, const T *error_details) override
ReplyManager(BinaryReply reply_handler_)
void SuccessInternal(const T *result) override