Remove JSON codec from C++ client wrapper (flutter/engine#17312)
The JSON codec is awkward to use in the wrapper (since the client has to build and link one of the JSON libraries to do so). Since it would be very cumbersome to wrap in a C API, and there's essentially no reason to use it instead of the standard codec, this removes it from the wrapper entirely. Since some system channels (internal to the engine) still use it, it's moved into common/cpp instead of being eliminated entirely. Internally we always use RapidJSON though, so the jsoncpp implementation is removed. Also adds some unit test coverage, since there wasn't any. Fixes #30669
This commit is contained in:
@@ -76,6 +76,7 @@ group("flutter") {
|
||||
"//flutter/runtime:runtime_unittests",
|
||||
"//flutter/shell/common:shell_unittests",
|
||||
"//flutter/shell/platform/common/cpp:common_cpp_core_unittests",
|
||||
"//flutter/shell/platform/common/cpp:common_cpp_unittests",
|
||||
"//flutter/shell/platform/common/cpp/client_wrapper:client_wrapper_unittests",
|
||||
"//flutter/shell/platform/embedder:embedder_unittests",
|
||||
"//flutter/shell/platform/glfw/client_wrapper:client_wrapper_glfw_unittests",
|
||||
|
||||
@@ -778,9 +778,6 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/encodable_value.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/engine_method_result.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_message_codec.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_type.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/message_codec.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_call.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h
|
||||
@@ -790,8 +787,6 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registry.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/json_message_codec.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/json_method_codec.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_call_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_channel_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar.cc
|
||||
@@ -802,6 +797,12 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_message
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_method_codec_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/incoming_message_dispatcher.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/incoming_message_dispatcher.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/json_message_codec.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/json_message_codec.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/json_message_codec_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/json_method_codec.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/json_method_codec.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/json_method_codec_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/path_utils.cc
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/path_utils.h
|
||||
FILE: ../../../flutter/shell/platform/common/cpp/path_utils_unittests.cc
|
||||
|
||||
@@ -33,6 +33,8 @@ source_set("common_cpp_library_headers") {
|
||||
source_set("common_cpp") {
|
||||
public = [
|
||||
"incoming_message_dispatcher.h",
|
||||
"json_message_codec.h",
|
||||
"json_method_codec.h",
|
||||
"text_input_model.h",
|
||||
]
|
||||
|
||||
@@ -40,6 +42,8 @@ source_set("common_cpp") {
|
||||
# to the _public_headers above into this target.
|
||||
sources = [
|
||||
"incoming_message_dispatcher.cc",
|
||||
"json_message_codec.cc",
|
||||
"json_method_codec.cc",
|
||||
"text_input_model.cc",
|
||||
]
|
||||
|
||||
@@ -53,16 +57,12 @@ source_set("common_cpp") {
|
||||
|
||||
public_deps = [
|
||||
":common_cpp_core",
|
||||
"//third_party/rapidjson",
|
||||
]
|
||||
|
||||
# TODO: Remove once text input model refactor lands, at which point this code
|
||||
# won't have a JSON dependency.
|
||||
defines = [ "USE_RAPID_JSON" ]
|
||||
deps += [ "//third_party/rapidjson" ]
|
||||
|
||||
if (is_win) {
|
||||
# For wstring_conversion. See issue #50053.
|
||||
defines += [ "_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING" ]
|
||||
defines = [ "_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING" ]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +101,28 @@ executable("common_cpp_core_unittests") {
|
||||
public_configs = [ "//flutter:config" ]
|
||||
}
|
||||
|
||||
test_fixtures("common_cpp_fixtures") {
|
||||
fixtures = []
|
||||
}
|
||||
|
||||
executable("common_cpp_unittests") {
|
||||
testonly = true
|
||||
|
||||
sources = [
|
||||
"json_message_codec_unittests.cc",
|
||||
"json_method_codec_unittests.cc",
|
||||
]
|
||||
|
||||
deps = [
|
||||
":common_cpp",
|
||||
":common_cpp_fixtures",
|
||||
"//flutter/shell/platform/common/cpp/client_wrapper:client_wrapper_library_stubs",
|
||||
"//flutter/testing",
|
||||
]
|
||||
|
||||
public_configs = [ "//flutter:config" ]
|
||||
}
|
||||
|
||||
copy("publish_headers") {
|
||||
sources = _public_headers
|
||||
outputs = [
|
||||
|
||||
@@ -12,11 +12,8 @@ source_set("client_wrapper") {
|
||||
|
||||
deps = [
|
||||
"//flutter/shell/platform/common/cpp:common_cpp_library_headers",
|
||||
"//third_party/rapidjson",
|
||||
]
|
||||
|
||||
defines = [ "USE_RAPID_JSON" ]
|
||||
|
||||
configs +=
|
||||
[ "//flutter/shell/platform/common/cpp:desktop_library_implementation" ]
|
||||
|
||||
|
||||
@@ -8,9 +8,6 @@ core_cpp_client_wrapper_includes =
|
||||
"include/flutter/binary_messenger.h",
|
||||
"include/flutter/encodable_value.h",
|
||||
"include/flutter/engine_method_result.h",
|
||||
"include/flutter/json_message_codec.h",
|
||||
"include/flutter/json_method_codec.h",
|
||||
"include/flutter/json_type.h",
|
||||
"include/flutter/message_codec.h",
|
||||
"include/flutter/method_call.h",
|
||||
"include/flutter/method_channel.h",
|
||||
@@ -26,15 +23,11 @@ core_cpp_client_wrapper_includes =
|
||||
# TODO: Once the wrapper API is more stable, consolidate to as few files as is
|
||||
# reasonable (without forcing different kinds of clients to take unnecessary
|
||||
# code) to simplify use.
|
||||
core_cpp_client_wrapper_sources =
|
||||
get_path_info(
|
||||
[
|
||||
"byte_stream_wrappers.h",
|
||||
"engine_method_result.cc",
|
||||
"json_message_codec.cc", # TODO combine into a single json_codec.cc.
|
||||
"json_method_codec.cc", # TODO combine into a single json_codec.cc.
|
||||
"plugin_registrar.cc",
|
||||
"standard_codec_serializer.h",
|
||||
"standard_codec.cc",
|
||||
],
|
||||
"abspath")
|
||||
core_cpp_client_wrapper_sources = get_path_info([
|
||||
"byte_stream_wrappers.h",
|
||||
"engine_method_result.cc",
|
||||
"plugin_registrar.cc",
|
||||
"standard_codec_serializer.h",
|
||||
"standard_codec.cc",
|
||||
],
|
||||
"abspath")
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_TYPE_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_TYPE_H_
|
||||
|
||||
// By default, the Json codecs use jsoncpp, but a version using RapidJSON is
|
||||
// implemented as well. To use the latter, set USE_RAPID_JSON.
|
||||
//
|
||||
// When writing code using the JSON codec classes, do not use JsonValueType;
|
||||
// instead use the underlying type for the library you have selected directly.
|
||||
|
||||
#ifdef USE_RAPID_JSON
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
// The APIs often pass owning references, which in RapidJSON must include the
|
||||
// allocator, so the value type for the APIs is Document rather than Value.
|
||||
using JsonValueType = rapidjson::Document;
|
||||
#else
|
||||
#include <json/json.h>
|
||||
|
||||
using JsonValueType = Json::Value;
|
||||
#endif
|
||||
|
||||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_TYPE_H_
|
||||
@@ -2,16 +2,14 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "include/flutter/json_message_codec.h"
|
||||
#include "flutter/shell/platform/common/cpp/json_message_codec.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#ifdef USE_RAPID_JSON
|
||||
#include "rapidjson/error/en.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "rapidjson/writer.h"
|
||||
#endif
|
||||
|
||||
namespace flutter {
|
||||
|
||||
@@ -22,8 +20,7 @@ const JsonMessageCodec& JsonMessageCodec::GetInstance() {
|
||||
}
|
||||
|
||||
std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessageInternal(
|
||||
const JsonValueType& message) const {
|
||||
#ifdef USE_RAPID_JSON
|
||||
const rapidjson::Document& message) const {
|
||||
// TODO: Look into alternate writers that would avoid the buffer copy.
|
||||
rapidjson::StringBuffer buffer;
|
||||
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
|
||||
@@ -31,37 +28,20 @@ std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessageInternal(
|
||||
const char* buffer_start = buffer.GetString();
|
||||
return std::make_unique<std::vector<uint8_t>>(
|
||||
buffer_start, buffer_start + buffer.GetSize());
|
||||
#else
|
||||
Json::StreamWriterBuilder writer_builder;
|
||||
std::string serialization = Json::writeString(writer_builder, message);
|
||||
return std::make_unique<std::vector<uint8_t>>(serialization.begin(),
|
||||
serialization.end());
|
||||
#endif
|
||||
}
|
||||
|
||||
std::unique_ptr<JsonValueType> JsonMessageCodec::DecodeMessageInternal(
|
||||
std::unique_ptr<rapidjson::Document> JsonMessageCodec::DecodeMessageInternal(
|
||||
const uint8_t* binary_message,
|
||||
const size_t message_size) const {
|
||||
auto raw_message = reinterpret_cast<const char*>(binary_message);
|
||||
auto json_message = std::make_unique<JsonValueType>();
|
||||
std::string parse_errors;
|
||||
bool parsing_successful = false;
|
||||
#ifdef USE_RAPID_JSON
|
||||
auto json_message = std::make_unique<rapidjson::Document>();
|
||||
rapidjson::ParseResult result =
|
||||
json_message->Parse(raw_message, message_size);
|
||||
parsing_successful = result == rapidjson::ParseErrorCode::kParseErrorNone;
|
||||
if (!parsing_successful) {
|
||||
parse_errors = rapidjson::GetParseError_En(result.Code());
|
||||
}
|
||||
#else
|
||||
Json::CharReaderBuilder reader_builder;
|
||||
std::unique_ptr<Json::CharReader> parser(reader_builder.newCharReader());
|
||||
parsing_successful = parser->parse(raw_message, raw_message + message_size,
|
||||
json_message.get(), &parse_errors);
|
||||
#endif
|
||||
bool parsing_successful =
|
||||
result == rapidjson::ParseErrorCode::kParseErrorNone;
|
||||
if (!parsing_successful) {
|
||||
std::cerr << "Unable to parse JSON message:" << std::endl
|
||||
<< parse_errors << std::endl;
|
||||
<< rapidjson::GetParseError_En(result.Code()) << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
return json_message;
|
||||
@@ -2,17 +2,18 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_MESSAGE_CODEC_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_MESSAGE_CODEC_H_
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_JSON_MESSAGE_CODEC_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_JSON_MESSAGE_CODEC_H_
|
||||
|
||||
#include "json_type.h"
|
||||
#include "message_codec.h"
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/message_codec.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
// A message encoding/decoding mechanism for communications to/from the
|
||||
// Flutter engine via JSON channels.
|
||||
class JsonMessageCodec : public MessageCodec<JsonValueType> {
|
||||
class JsonMessageCodec : public MessageCodec<rapidjson::Document> {
|
||||
public:
|
||||
// Returns the shared instance of the codec.
|
||||
static const JsonMessageCodec& GetInstance();
|
||||
@@ -28,15 +29,15 @@ class JsonMessageCodec : public MessageCodec<JsonValueType> {
|
||||
JsonMessageCodec() = default;
|
||||
|
||||
// |flutter::MessageCodec|
|
||||
std::unique_ptr<JsonValueType> DecodeMessageInternal(
|
||||
std::unique_ptr<rapidjson::Document> DecodeMessageInternal(
|
||||
const uint8_t* binary_message,
|
||||
const size_t message_size) const override;
|
||||
|
||||
// |flutter::MessageCodec|
|
||||
std::unique_ptr<std::vector<uint8_t>> EncodeMessageInternal(
|
||||
const JsonValueType& message) const override;
|
||||
const rapidjson::Document& message) const override;
|
||||
};
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_MESSAGE_CODEC_H_
|
||||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_JSON_MESSAGE_CODEC_H_
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/json_message_codec.h"
|
||||
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
namespace {
|
||||
|
||||
// Validates round-trip encoding and decoding of |value|.
|
||||
static void CheckEncodeDecode(const rapidjson::Document& value) {
|
||||
const JsonMessageCodec& codec = JsonMessageCodec::GetInstance();
|
||||
auto encoded = codec.EncodeMessage(value);
|
||||
ASSERT_TRUE(encoded);
|
||||
auto decoded = codec.DecodeMessage(*encoded);
|
||||
EXPECT_EQ(value, *decoded);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// Tests that a JSON document with various data types round-trips correctly.
|
||||
TEST(JsonMessageCodec, EncodeDecode) {
|
||||
rapidjson::Document array(rapidjson::kArrayType);
|
||||
auto& allocator = array.GetAllocator();
|
||||
|
||||
array.PushBack("string", allocator);
|
||||
|
||||
rapidjson::Value map(rapidjson::kObjectType);
|
||||
map.AddMember("a", -7, allocator);
|
||||
map.AddMember("b", std::numeric_limits<int>::max(), allocator);
|
||||
map.AddMember("c", 3.14159, allocator);
|
||||
map.AddMember("d", true, allocator);
|
||||
map.AddMember("e", rapidjson::Value(), allocator);
|
||||
array.PushBack(map, allocator);
|
||||
|
||||
CheckEncodeDecode(array);
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
@@ -2,9 +2,9 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "include/flutter/json_method_codec.h"
|
||||
#include "flutter/shell/platform/common/cpp/json_method_codec.h"
|
||||
|
||||
#include "include/flutter/json_message_codec.h"
|
||||
#include "flutter/shell/platform/common/cpp/json_message_codec.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
@@ -20,16 +20,15 @@ const JsonMethodCodec& JsonMethodCodec::GetInstance() {
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
std::unique_ptr<MethodCall<JsonValueType>>
|
||||
std::unique_ptr<MethodCall<rapidjson::Document>>
|
||||
JsonMethodCodec::DecodeMethodCallInternal(const uint8_t* message,
|
||||
const size_t message_size) const {
|
||||
std::unique_ptr<JsonValueType> json_message =
|
||||
std::unique_ptr<rapidjson::Document> json_message =
|
||||
JsonMessageCodec::GetInstance().DecodeMessage(message, message_size);
|
||||
if (!json_message) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#if USE_RAPID_JSON
|
||||
auto method_name_iter = json_message->FindMember(kMessageMethodKey);
|
||||
if (method_name_iter == json_message->MemberEnd()) {
|
||||
return nullptr;
|
||||
@@ -57,20 +56,10 @@ JsonMethodCodec::DecodeMethodCallInternal(const uint8_t* message,
|
||||
}
|
||||
return std::make_unique<MethodCall<rapidjson::Document>>(
|
||||
method_name, std::move(arguments));
|
||||
#else
|
||||
Json::Value method = (*json_message)[kMessageMethodKey];
|
||||
if (method.isNull()) {
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_unique<MethodCall<Json::Value>>(
|
||||
method.asString(),
|
||||
std::make_unique<Json::Value>((*json_message)[kMessageArgumentsKey]));
|
||||
#endif
|
||||
}
|
||||
|
||||
std::unique_ptr<std::vector<uint8_t>> JsonMethodCodec::EncodeMethodCallInternal(
|
||||
const MethodCall<JsonValueType>& method_call) const {
|
||||
#if USE_RAPID_JSON
|
||||
const MethodCall<rapidjson::Document>& method_call) const {
|
||||
// TODO: Consider revisiting the codec APIs to avoid the need to copy
|
||||
// everything when doing encoding (e.g., by having a version that takes
|
||||
// owership of the object to encode, so that it can be moved instead).
|
||||
@@ -83,20 +72,13 @@ std::unique_ptr<std::vector<uint8_t>> JsonMethodCodec::EncodeMethodCallInternal(
|
||||
}
|
||||
message.AddMember(kMessageMethodKey, name, allocator);
|
||||
message.AddMember(kMessageArgumentsKey, arguments, allocator);
|
||||
#else
|
||||
Json::Value message(Json::objectValue);
|
||||
message[kMessageMethodKey] = method_call.method_name();
|
||||
const Json::Value* arguments = method_call.arguments();
|
||||
message[kMessageArgumentsKey] = arguments ? *arguments : Json::Value();
|
||||
#endif
|
||||
|
||||
return JsonMessageCodec::GetInstance().EncodeMessage(message);
|
||||
}
|
||||
|
||||
std::unique_ptr<std::vector<uint8_t>>
|
||||
JsonMethodCodec::EncodeSuccessEnvelopeInternal(
|
||||
const JsonValueType* result) const {
|
||||
#if USE_RAPID_JSON
|
||||
const rapidjson::Document* result) const {
|
||||
rapidjson::Document envelope;
|
||||
envelope.SetArray();
|
||||
rapidjson::Value result_value;
|
||||
@@ -104,10 +86,6 @@ JsonMethodCodec::EncodeSuccessEnvelopeInternal(
|
||||
result_value.CopyFrom(*result, envelope.GetAllocator());
|
||||
}
|
||||
envelope.PushBack(result_value, envelope.GetAllocator());
|
||||
#else
|
||||
Json::Value envelope(Json::arrayValue);
|
||||
envelope.append(result == nullptr ? Json::Value() : *result);
|
||||
#endif
|
||||
|
||||
return JsonMessageCodec::GetInstance().EncodeMessage(envelope);
|
||||
}
|
||||
@@ -116,8 +94,7 @@ std::unique_ptr<std::vector<uint8_t>>
|
||||
JsonMethodCodec::EncodeErrorEnvelopeInternal(
|
||||
const std::string& error_code,
|
||||
const std::string& error_message,
|
||||
const JsonValueType* error_details) const {
|
||||
#if USE_RAPID_JSON
|
||||
const rapidjson::Document* error_details) const {
|
||||
rapidjson::Document envelope(rapidjson::kArrayType);
|
||||
auto& allocator = envelope.GetAllocator();
|
||||
envelope.PushBack(rapidjson::Value(error_code, allocator), allocator);
|
||||
@@ -127,12 +104,6 @@ JsonMethodCodec::EncodeErrorEnvelopeInternal(
|
||||
details_value.CopyFrom(*error_details, allocator);
|
||||
}
|
||||
envelope.PushBack(details_value, allocator);
|
||||
#else
|
||||
Json::Value envelope(Json::arrayValue);
|
||||
envelope.append(error_code);
|
||||
envelope.append(error_message.empty() ? Json::Value() : error_message);
|
||||
envelope.append(error_details == nullptr ? Json::Value() : *error_details);
|
||||
#endif
|
||||
|
||||
return JsonMessageCodec::GetInstance().EncodeMessage(envelope);
|
||||
}
|
||||
@@ -2,17 +2,18 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_METHOD_CODEC_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_METHOD_CODEC_H_
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_JSON_METHOD_CODEC_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_JSON_METHOD_CODEC_H_
|
||||
|
||||
#include "json_type.h"
|
||||
#include "method_call.h"
|
||||
#include "method_codec.h"
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_call.h"
|
||||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_codec.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
// An implementation of MethodCodec that uses JSON strings as the serialization.
|
||||
class JsonMethodCodec : public MethodCodec<JsonValueType> {
|
||||
class JsonMethodCodec : public MethodCodec<rapidjson::Document> {
|
||||
public:
|
||||
// Returns the shared instance of the codec.
|
||||
static const JsonMethodCodec& GetInstance();
|
||||
@@ -28,25 +29,25 @@ class JsonMethodCodec : public MethodCodec<JsonValueType> {
|
||||
JsonMethodCodec() = default;
|
||||
|
||||
// |flutter::MethodCodec|
|
||||
std::unique_ptr<MethodCall<JsonValueType>> DecodeMethodCallInternal(
|
||||
std::unique_ptr<MethodCall<rapidjson::Document>> DecodeMethodCallInternal(
|
||||
const uint8_t* message,
|
||||
const size_t message_size) const override;
|
||||
|
||||
// |flutter::MethodCodec|
|
||||
std::unique_ptr<std::vector<uint8_t>> EncodeMethodCallInternal(
|
||||
const MethodCall<JsonValueType>& method_call) const override;
|
||||
const MethodCall<rapidjson::Document>& method_call) const override;
|
||||
|
||||
// |flutter::MethodCodec|
|
||||
std::unique_ptr<std::vector<uint8_t>> EncodeSuccessEnvelopeInternal(
|
||||
const JsonValueType* result) const override;
|
||||
const rapidjson::Document* result) const override;
|
||||
|
||||
// |flutter::MethodCodec|
|
||||
std::unique_ptr<std::vector<uint8_t>> EncodeErrorEnvelopeInternal(
|
||||
const std::string& error_code,
|
||||
const std::string& error_message,
|
||||
const JsonValueType* error_details) const override;
|
||||
const rapidjson::Document* error_details) const override;
|
||||
};
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_METHOD_CODEC_H_
|
||||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_JSON_METHOD_CODEC_H_
|
||||
@@ -0,0 +1,107 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/json_method_codec.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
namespace {
|
||||
|
||||
// Returns true if the given method calls have the same method name, and their
|
||||
// arguments have equivalent values.
|
||||
bool MethodCallsAreEqual(const MethodCall<rapidjson::Document>& a,
|
||||
const MethodCall<rapidjson::Document>& b) {
|
||||
if (a.method_name() != b.method_name()) {
|
||||
return false;
|
||||
}
|
||||
// Treat nullptr and Null as equivalent.
|
||||
if ((!a.arguments() || a.arguments()->IsNull()) &&
|
||||
(!b.arguments() || b.arguments()->IsNull())) {
|
||||
return true;
|
||||
}
|
||||
return *a.arguments() == *b.arguments();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(JsonMethodCodec, HandlesMethodCallsWithNullArguments) {
|
||||
const JsonMethodCodec& codec = JsonMethodCodec::GetInstance();
|
||||
MethodCall<rapidjson::Document> call("hello", nullptr);
|
||||
auto encoded = codec.EncodeMethodCall(call);
|
||||
ASSERT_TRUE(encoded);
|
||||
std::unique_ptr<MethodCall<rapidjson::Document>> decoded =
|
||||
codec.DecodeMethodCall(*encoded);
|
||||
ASSERT_TRUE(decoded);
|
||||
EXPECT_TRUE(MethodCallsAreEqual(call, *decoded));
|
||||
}
|
||||
|
||||
TEST(JsonMethodCodec, HandlesMethodCallsWithArgument) {
|
||||
const JsonMethodCodec& codec = JsonMethodCodec::GetInstance();
|
||||
|
||||
auto arguments = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
|
||||
auto& allocator = arguments->GetAllocator();
|
||||
arguments->PushBack(42, allocator);
|
||||
arguments->PushBack("world", allocator);
|
||||
MethodCall<rapidjson::Document> call("hello", std::move(arguments));
|
||||
auto encoded = codec.EncodeMethodCall(call);
|
||||
ASSERT_TRUE(encoded);
|
||||
std::unique_ptr<MethodCall<rapidjson::Document>> decoded =
|
||||
codec.DecodeMethodCall(*encoded);
|
||||
ASSERT_TRUE(decoded);
|
||||
EXPECT_TRUE(MethodCallsAreEqual(call, *decoded));
|
||||
}
|
||||
|
||||
TEST(JsonMethodCodec, HandlesSuccessEnvelopesWithNullResult) {
|
||||
const JsonMethodCodec& codec = JsonMethodCodec::GetInstance();
|
||||
auto encoded = codec.EncodeSuccessEnvelope();
|
||||
ASSERT_TRUE(encoded);
|
||||
std::vector<uint8_t> bytes = {'[', 'n', 'u', 'l', 'l', ']'};
|
||||
EXPECT_EQ(*encoded, bytes);
|
||||
// TODO: Add round-trip check once decoding replies is implemented.
|
||||
}
|
||||
|
||||
TEST(JsonMethodCodec, HandlesSuccessEnvelopesWithResult) {
|
||||
const JsonMethodCodec& codec = JsonMethodCodec::GetInstance();
|
||||
rapidjson::Document result;
|
||||
result.SetInt(42);
|
||||
auto encoded = codec.EncodeSuccessEnvelope(&result);
|
||||
ASSERT_TRUE(encoded);
|
||||
std::vector<uint8_t> bytes = {'[', '4', '2', ']'};
|
||||
EXPECT_EQ(*encoded, bytes);
|
||||
// TODO: Add round-trip check once decoding replies is implemented.
|
||||
}
|
||||
|
||||
TEST(JsonMethodCodec, HandlesErrorEnvelopesWithNulls) {
|
||||
const JsonMethodCodec& codec = JsonMethodCodec::GetInstance();
|
||||
auto encoded = codec.EncodeErrorEnvelope("errorCode");
|
||||
ASSERT_TRUE(encoded);
|
||||
std::vector<uint8_t> bytes = {
|
||||
'[', '"', 'e', 'r', 'r', 'o', 'r', 'C', 'o', 'd', 'e',
|
||||
'"', ',', '"', '"', ',', 'n', 'u', 'l', 'l', ']',
|
||||
};
|
||||
EXPECT_EQ(*encoded, bytes);
|
||||
// TODO: Add round-trip check once decoding replies is implemented.
|
||||
}
|
||||
|
||||
TEST(JsonMethodCodec, HandlesErrorEnvelopesWithDetails) {
|
||||
const JsonMethodCodec& codec = JsonMethodCodec::GetInstance();
|
||||
rapidjson::Document details(rapidjson::kArrayType);
|
||||
auto& allocator = details.GetAllocator();
|
||||
details.PushBack("a", allocator);
|
||||
details.PushBack(42, allocator);
|
||||
auto encoded =
|
||||
codec.EncodeErrorEnvelope("errorCode", "something failed", &details);
|
||||
ASSERT_NE(encoded.get(), nullptr);
|
||||
std::vector<uint8_t> bytes = {
|
||||
'[', '"', 'e', 'r', 'r', 'o', 'r', 'C', 'o', 'd', 'e', '"', ',', '"',
|
||||
's', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g', ' ', 'f', 'a', 'i', 'l',
|
||||
'e', 'd', '"', ',', '[', '"', 'a', '"', ',', '4', '2', ']', ']',
|
||||
};
|
||||
EXPECT_EQ(*encoded, bytes);
|
||||
// TODO: Add round-trip check once decoding replies is implemented.
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
@@ -42,8 +42,6 @@ source_set("flutter_glfw") {
|
||||
"text_input_plugin.h",
|
||||
]
|
||||
|
||||
defines = [ "USE_RAPID_JSON" ]
|
||||
|
||||
configs +=
|
||||
[ "//flutter/shell/platform/common/cpp:desktop_library_implementation" ]
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_message_codec.h"
|
||||
#include "flutter/shell/platform/common/cpp/json_message_codec.h"
|
||||
|
||||
static constexpr char kChannelName[] = "flutter/keyevent";
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h"
|
||||
#include "flutter/shell/platform/common/cpp/json_method_codec.h"
|
||||
|
||||
static constexpr char kChannelName[] = "flutter/platform";
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h"
|
||||
#include "flutter/shell/platform/common/cpp/json_method_codec.h"
|
||||
|
||||
static constexpr char kSetEditingStateMethod[] = "TextInput.setEditingState";
|
||||
static constexpr char kClearClientMethod[] = "TextInput.clearClient";
|
||||
|
||||
@@ -60,8 +60,6 @@ source_set("flutter_windows_source") {
|
||||
"window_state.h",
|
||||
]
|
||||
|
||||
defines = [ "USE_RAPID_JSON" ]
|
||||
|
||||
configs += [
|
||||
"//flutter/shell/platform/common/cpp:desktop_library_implementation",
|
||||
"//third_party/angle:gl_prototypes",
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_message_codec.h"
|
||||
#include "flutter/shell/platform/common/cpp/json_message_codec.h"
|
||||
|
||||
static constexpr char kChannelName[] = "flutter/keyevent";
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h"
|
||||
#include "flutter/shell/platform/common/cpp/json_method_codec.h"
|
||||
|
||||
static constexpr char kChannelName[] = "flutter/platform";
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h"
|
||||
#include "flutter/shell/platform/common/cpp/json_method_codec.h"
|
||||
|
||||
static constexpr char kSetEditingStateMethod[] = "TextInput.setEditingState";
|
||||
static constexpr char kClearClientMethod[] = "TextInput.clearClient";
|
||||
|
||||
@@ -110,6 +110,8 @@ def RunCCTests(build_dir, filter):
|
||||
|
||||
RunEngineExecutable(build_dir, 'common_cpp_core_unittests', filter, shuffle_flags)
|
||||
|
||||
RunEngineExecutable(build_dir, 'common_cpp_unittests', filter, shuffle_flags)
|
||||
|
||||
RunEngineExecutable(build_dir, 'client_wrapper_unittests', filter, shuffle_flags)
|
||||
|
||||
# https://github.com/flutter/flutter/issues/36294
|
||||
|
||||
Reference in New Issue
Block a user