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_METHOD_CODEC_H_ 6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_CODEC_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "method_call.h" 13 14 namespace flutter { 15 16 // Translates between a binary message and higher-level method call and 17 // response/error objects. 18 template <typename T> 19 class MethodCodec { 20 public: 21 MethodCodec() = default; 22 23 virtual ~MethodCodec() = default; 24 25 // Prevent copying. 26 MethodCodec(MethodCodec<T> const&) = delete; 27 MethodCodec& operator=(MethodCodec<T> const&) = delete; 28 29 // Returns the MethodCall encoded in |message|, or nullptr if it cannot be 30 // decoded. DecodeMethodCall(const uint8_t * message,const size_t message_size)31 std::unique_ptr<MethodCall<T>> DecodeMethodCall( 32 const uint8_t* message, 33 const 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. DecodeMethodCall(const std::vector<uint8_t> & message)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. EncodeMethodCall(const MethodCall<T> & method_call)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 protected: 71 // Implementation of the public interface, to be provided by subclasses. 72 virtual std::unique_ptr<MethodCall<T>> DecodeMethodCallInternal( 73 const uint8_t* message, 74 const size_t message_size) const = 0; 75 76 // Implementation of the public interface, to be provided by subclasses. 77 virtual std::unique_ptr<std::vector<uint8_t>> EncodeMethodCallInternal( 78 const MethodCall<T>& method_call) const = 0; 79 80 // Implementation of the public interface, to be provided by subclasses. 81 virtual std::unique_ptr<std::vector<uint8_t>> EncodeSuccessEnvelopeInternal( 82 const T* result) const = 0; 83 84 // Implementation of the public interface, to be provided by subclasses. 85 virtual std::unique_ptr<std::vector<uint8_t>> EncodeErrorEnvelopeInternal( 86 const std::string& error_code, 87 const std::string& error_message, 88 const T* error_details) const = 0; 89 }; 90 91 } // namespace flutter 92 93 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_CODEC_H_ 94