|
| 1 | +#ifndef FLUTTER_WEBRTC_COMMON_HXX |
| 2 | +#define FLUTTER_WEBRTC_COMMON_HXX |
| 3 | + |
| 4 | +#include <flutter/encodable_value.h> |
| 5 | +#include <flutter/event_channel.h> |
| 6 | +#include <flutter/event_stream_handler_functions.h> |
| 7 | +#include <flutter/method_channel.h> |
| 8 | +#include <flutter/plugin_registrar.h> |
| 9 | +#include <flutter/standard_message_codec.h> |
| 10 | +#include <flutter/standard_method_codec.h> |
| 11 | +#include <flutter/texture_registrar.h> |
| 12 | + |
| 13 | +#include <string> |
| 14 | +#include <memory> |
| 15 | +#include <list> |
| 16 | + |
| 17 | +typedef flutter::EncodableValue EncodableValue; |
| 18 | +typedef flutter::EncodableMap EncodableMap; |
| 19 | +typedef flutter::EncodableList EncodableList; |
| 20 | +typedef flutter::BinaryMessenger BinaryMessenger; |
| 21 | +typedef flutter::TextureRegistrar TextureRegistrar; |
| 22 | +typedef flutter::PluginRegistrar PluginRegistrar; |
| 23 | +typedef flutter::MethodChannel<EncodableValue> MethodChannel; |
| 24 | +typedef flutter::EventChannel<EncodableValue> EventChannel; |
| 25 | +typedef flutter::EventSink<EncodableValue> EventSink; |
| 26 | +typedef flutter::MethodCall<EncodableValue> MethodCall; |
| 27 | +typedef flutter::MethodResult<EncodableValue> MethodResult; |
| 28 | + |
| 29 | +// foo.StringValue() becomes std::get<std::string>(foo) |
| 30 | +// foo.IsString() becomes std::holds_alternative<std::string>(foo) |
| 31 | + |
| 32 | +template <typename T> |
| 33 | +inline bool TypeIs(const EncodableValue val) { |
| 34 | + return std::holds_alternative<T>(val); |
| 35 | +} |
| 36 | + |
| 37 | +template <typename T> |
| 38 | +inline const T GetValue(EncodableValue val) { |
| 39 | + return std::get<T>(val); |
| 40 | +} |
| 41 | + |
| 42 | +inline EncodableValue findEncodableValue(const EncodableMap& map, |
| 43 | + const std::string& key) { |
| 44 | + auto it = map.find(EncodableValue(key)); |
| 45 | + if (it != map.end()) |
| 46 | + return it->second; |
| 47 | + return EncodableValue(); |
| 48 | +} |
| 49 | + |
| 50 | +inline EncodableMap findMap(const EncodableMap& map, const std::string& key) { |
| 51 | + auto it = map.find(EncodableValue(key)); |
| 52 | + if (it != map.end() && TypeIs<EncodableMap>(it->second)) |
| 53 | + return GetValue<EncodableMap>(it->second); |
| 54 | + return EncodableMap(); |
| 55 | +} |
| 56 | + |
| 57 | +inline EncodableList findList(const EncodableMap& map, const std::string& key) { |
| 58 | + auto it = map.find(EncodableValue(key)); |
| 59 | + if (it != map.end() && TypeIs<EncodableList>(it->second)) |
| 60 | + return GetValue<EncodableList>(it->second); |
| 61 | + return EncodableList(); |
| 62 | +} |
| 63 | + |
| 64 | +inline std::string findString(const EncodableMap& map, const std::string& key) { |
| 65 | + auto it = map.find(EncodableValue(key)); |
| 66 | + if (it != map.end() && TypeIs<std::string>(it->second)) |
| 67 | + return GetValue<std::string>(it->second); |
| 68 | + return std::string(); |
| 69 | +} |
| 70 | + |
| 71 | +inline int findInt(const EncodableMap& map, const std::string& key) { |
| 72 | + auto it = map.find(EncodableValue(key)); |
| 73 | + if (it != map.end() && TypeIs<int>(it->second)) |
| 74 | + return GetValue<int>(it->second); |
| 75 | + return -1; |
| 76 | +} |
| 77 | + |
| 78 | +inline double findDouble(const EncodableMap& map, const std::string& key) { |
| 79 | + auto it = map.find(EncodableValue(key)); |
| 80 | + if (it != map.end() && TypeIs<double>(it->second)) |
| 81 | + return GetValue<double>(it->second); |
| 82 | + return 0.0; |
| 83 | +} |
| 84 | + |
| 85 | +inline int64_t findLongInt(const EncodableMap& map, const std::string& key) { |
| 86 | + for (auto it : map) { |
| 87 | + if (key == GetValue<std::string>(it.first)) { |
| 88 | + if (TypeIs<int64_t>(it.second)) { |
| 89 | + return GetValue<int64_t>(it.second); |
| 90 | + } else if (TypeIs<int32_t>(it.second)) { |
| 91 | + return GetValue<int32_t>(it.second); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return -1; |
| 97 | +} |
| 98 | + |
| 99 | +inline int toInt(flutter::EncodableValue inputVal, int defaultVal) { |
| 100 | + int intValue = defaultVal; |
| 101 | + if (TypeIs<int>(inputVal)) { |
| 102 | + intValue = GetValue<int>(inputVal); |
| 103 | + } else if (TypeIs<int32_t>(inputVal)) { |
| 104 | + intValue = GetValue<int32_t>(inputVal); |
| 105 | + } else if (TypeIs<std::string>(inputVal)) { |
| 106 | + intValue = atoi(GetValue<std::string>(inputVal).c_str()); |
| 107 | + } |
| 108 | + return intValue; |
| 109 | +} |
| 110 | + |
| 111 | +class MethodCallProxy { |
| 112 | + public: |
| 113 | + static std::unique_ptr<MethodCallProxy> Create(const MethodCall& call); |
| 114 | + virtual ~MethodCallProxy() = default; |
| 115 | + // The name of the method being called. |
| 116 | + virtual const std::string& method_name() const = 0; |
| 117 | + |
| 118 | + // The arguments to the method call, or NULL if there are none. |
| 119 | + virtual const EncodableValue* arguments() const = 0; |
| 120 | +}; |
| 121 | + |
| 122 | +class MethodResultProxy { |
| 123 | + public: |
| 124 | + static std::unique_ptr<MethodResultProxy> Create( |
| 125 | + std::unique_ptr<MethodResult> method_result); |
| 126 | + |
| 127 | + virtual ~MethodResultProxy() = default; |
| 128 | + |
| 129 | + // Reports success with no result. |
| 130 | + virtual void Success() = 0; |
| 131 | + |
| 132 | + // Reports success with a result. |
| 133 | + virtual void Success(const EncodableValue& result) = 0; |
| 134 | + |
| 135 | + // Reports an error. |
| 136 | + virtual void Error(const std::string& error_code, |
| 137 | + const std::string& error_message, |
| 138 | + const EncodableValue& error_details) = 0; |
| 139 | + |
| 140 | + // Reports an error with a default error code and no details. |
| 141 | + virtual void Error(const std::string& error_code, |
| 142 | + const std::string& error_message = "") = 0; |
| 143 | + |
| 144 | + virtual void NotImplemented() = 0; |
| 145 | +}; |
| 146 | + |
| 147 | +class EventChannelProxy { |
| 148 | + public: |
| 149 | + static std::unique_ptr<EventChannelProxy> Create( |
| 150 | + BinaryMessenger* messenger, |
| 151 | + const std::string& channelName); |
| 152 | + |
| 153 | + virtual ~EventChannelProxy() = default; |
| 154 | + |
| 155 | + virtual void Success(const EncodableValue& event, |
| 156 | + bool cache_event = true) = 0; |
| 157 | +}; |
| 158 | + |
| 159 | +#endif // FLUTTER_WEBRTC_COMMON_HXX |
0 commit comments