Flutter Windows Embedder
method_codec.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_METHOD_CODEC_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_CODEC_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "method_call.h"
13 #include "method_result.h"
14 
15 namespace flutter {
16 
17 // Translates between a binary message and higher-level method call and
18 // response/error objects.
19 template <typename T>
20 class MethodCodec {
21  public:
22  MethodCodec() = default;
23 
24  virtual ~MethodCodec() = default;
25 
26  // Prevent copying.
27  MethodCodec(MethodCodec<T> const&) = delete;
28  MethodCodec& operator=(MethodCodec<T> const&) = delete;
29 
30  // Returns the MethodCall encoded in |message|, or nullptr if it cannot be
31  // decoded.
32  std::unique_ptr<MethodCall<T>> DecodeMethodCall(const uint8_t* message,
33  size_t message_size) const {
34  return std::move(DecodeMethodCallInternal(message, message_size));
35  }
36 
37  // Returns the MethodCall encoded in |message|, or nullptr if it cannot be
38  // decoded.
39  std::unique_ptr<MethodCall<T>> DecodeMethodCall(
40  const std::vector<uint8_t>& message) const {
41  size_t size = message.size();
42  const uint8_t* data = size > 0 ? &message[0] : nullptr;
43  return std::move(DecodeMethodCallInternal(data, size));
44  }
45 
46  // Returns a binary encoding of the given |method_call|, or nullptr if the
47  // method call cannot be serialized by this codec.
48  std::unique_ptr<std::vector<uint8_t>> EncodeMethodCall(
49  const MethodCall<T>& method_call) const {
50  return std::move(EncodeMethodCallInternal(method_call));
51  }
52 
53  // Returns a binary encoding of |result|. |result| must be a type supported
54  // by the codec.
55  std::unique_ptr<std::vector<uint8_t>> EncodeSuccessEnvelope(
56  const T* result = nullptr) const {
57  return std::move(EncodeSuccessEnvelopeInternal(result));
58  }
59 
60  // Returns a binary encoding of |error|. The |error_details| must be a type
61  // supported by the codec.
62  std::unique_ptr<std::vector<uint8_t>> EncodeErrorEnvelope(
63  const std::string& error_code,
64  const std::string& error_message = "",
65  const T* error_details = nullptr) const {
66  return std::move(
67  EncodeErrorEnvelopeInternal(error_code, error_message, error_details));
68  }
69 
70  // Decodes the response envelope encoded in |response|, calling the
71  // appropriate method on |result|.
72  //
73  // Returns false if |response| cannot be decoded. In that case the caller is
74  // responsible for calling a |result| method.
75  bool DecodeAndProcessResponseEnvelope(const uint8_t* response,
76  size_t response_size,
77  MethodResult<T>* result) const {
78  return DecodeAndProcessResponseEnvelopeInternal(response, response_size,
79  result);
80  }
81 
82  protected:
83  // Implementation of the public interface, to be provided by subclasses.
84  virtual std::unique_ptr<MethodCall<T>> DecodeMethodCallInternal(
85  const uint8_t* message,
86  size_t message_size) const = 0;
87 
88  // Implementation of the public interface, to be provided by subclasses.
89  virtual std::unique_ptr<std::vector<uint8_t>> EncodeMethodCallInternal(
90  const MethodCall<T>& method_call) const = 0;
91 
92  // Implementation of the public interface, to be provided by subclasses.
93  virtual std::unique_ptr<std::vector<uint8_t>> EncodeSuccessEnvelopeInternal(
94  const T* result) const = 0;
95 
96  // Implementation of the public interface, to be provided by subclasses.
97  virtual std::unique_ptr<std::vector<uint8_t>> EncodeErrorEnvelopeInternal(
98  const std::string& error_code,
99  const std::string& error_message,
100  const T* error_details) const = 0;
101 
102  // Implementation of the public interface, to be provided by subclasses.
104  const uint8_t* response,
105  size_t response_size,
106  MethodResult<T>* result) const = 0;
107 };
108 
109 } // namespace flutter
110 
111 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_CODEC_H_
flutter::MethodCodec::EncodeMethodCallInternal
virtual std::unique_ptr< std::vector< uint8_t > > EncodeMethodCallInternal(const MethodCall< T > &method_call) const =0
flutter::MethodCodec::EncodeErrorEnvelope
std::unique_ptr< std::vector< uint8_t > > EncodeErrorEnvelope(const std::string &error_code, const std::string &error_message="", const T *error_details=nullptr) const
Definition: method_codec.h:62
flutter::MethodCodec::operator=
MethodCodec & operator=(MethodCodec< T > const &)=delete
flutter::MethodCodec::EncodeErrorEnvelopeInternal
virtual std::unique_ptr< std::vector< uint8_t > > EncodeErrorEnvelopeInternal(const std::string &error_code, const std::string &error_message, const T *error_details) const =0
flutter::MethodCodec::MethodCodec
MethodCodec()=default
method_result.h
flutter::MethodCodec::DecodeMethodCallInternal
virtual std::unique_ptr< MethodCall< T > > DecodeMethodCallInternal(const uint8_t *message, size_t message_size) const =0
flutter::MethodCall
Definition: method_call.h:18
flutter::MethodCodec::EncodeSuccessEnvelope
std::unique_ptr< std::vector< uint8_t > > EncodeSuccessEnvelope(const T *result=nullptr) const
Definition: method_codec.h:55
flutter::MethodCodec::EncodeMethodCall
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCall(const MethodCall< T > &method_call) const
Definition: method_codec.h:48
flutter::MethodCodec::~MethodCodec
virtual ~MethodCodec()=default
flutter::MethodCodec::DecodeAndProcessResponseEnvelopeInternal
virtual bool DecodeAndProcessResponseEnvelopeInternal(const uint8_t *response, size_t response_size, MethodResult< T > *result) const =0
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::MethodCodec::DecodeMethodCall
std::unique_ptr< MethodCall< T > > DecodeMethodCall(const std::vector< uint8_t > &message) const
Definition: method_codec.h:39
flutter::MethodResult
Definition: method_result.h:17
flutter::MethodCodec
Definition: method_codec.h:20
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter::MethodCodec::DecodeMethodCall
std::unique_ptr< MethodCall< T > > DecodeMethodCall(const uint8_t *message, size_t message_size) const
Definition: method_codec.h:32
method_call.h
flutter::MethodCodec::DecodeAndProcessResponseEnvelope
bool DecodeAndProcessResponseEnvelope(const uint8_t *response, size_t response_size, MethodResult< T > *result) const
Definition: method_codec.h:75
flutter::MethodCodec::EncodeSuccessEnvelopeInternal
virtual std::unique_ptr< std::vector< uint8_t > > EncodeSuccessEnvelopeInternal(const T *result) const =0