|
| 1 | +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. |
| 2 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style license that can be |
| 4 | +// found in the LICENSE file. |
| 5 | + |
| 6 | +#ifndef EMBEDDER_STRING_CODEC_H_ |
| 7 | +#define EMBEDDER_STRING_CODEC_H_ |
| 8 | + |
| 9 | +#include <memory> |
| 10 | +#include <string> |
| 11 | +#include <variant> |
| 12 | + |
| 13 | +#include "flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h" |
| 14 | +#include "flutter/shell/platform/common/client_wrapper/include/flutter/message_codec.h" |
| 15 | + |
| 16 | +namespace flutter { |
| 17 | + |
| 18 | +// A string message encoding/decoding mechanism for communications to/from the |
| 19 | +// Flutter engine via message channels. |
| 20 | +class StringCodec : public MessageCodec<EncodableValue> { |
| 21 | + public: |
| 22 | + ~StringCodec() = default; |
| 23 | + |
| 24 | + // Returns an instance of the codec. |
| 25 | + static const StringCodec& GetInstance() { |
| 26 | + static StringCodec sInstance; |
| 27 | + return sInstance; |
| 28 | + } |
| 29 | + |
| 30 | + // Prevent copying. |
| 31 | + StringCodec(StringCodec const&) = delete; |
| 32 | + StringCodec& operator=(StringCodec const&) = delete; |
| 33 | + |
| 34 | + protected: |
| 35 | + // |flutter::MessageCodec| |
| 36 | + std::unique_ptr<EncodableValue> DecodeMessageInternal( |
| 37 | + const uint8_t* binary_message, |
| 38 | + const size_t message_size) const override { |
| 39 | + return std::make_unique<EncodableValue>(std::string( |
| 40 | + reinterpret_cast<const char*>(binary_message), message_size)); |
| 41 | + } |
| 42 | + |
| 43 | + // |flutter::MessageCodec| |
| 44 | + std::unique_ptr<std::vector<uint8_t>> EncodeMessageInternal( |
| 45 | + const EncodableValue& message) const override { |
| 46 | + auto string_value = std::get<std::string>(message); |
| 47 | + return std::make_unique<std::vector<uint8_t>>(string_value.begin(), |
| 48 | + string_value.end()); |
| 49 | + } |
| 50 | + |
| 51 | + private: |
| 52 | + // Instances should be obtained via GetInstance. |
| 53 | + explicit StringCodec() = default; |
| 54 | +}; |
| 55 | + |
| 56 | +} // namespace flutter |
| 57 | + |
| 58 | +#endif // EMBEDDER_STRING_CODEC_H_ |
0 commit comments