From 7b222c831056ef9e0ce3baa1f6dc319c92955408 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 2 Apr 2020 14:57:46 -0700 Subject: [PATCH] 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 --- engine/src/flutter/BUILD.gn | 1 + .../ci/licenses_golden/licenses_flutter | 11 +- .../shell/platform/common/cpp/BUILD.gn | 34 +++++- .../common/cpp/client_wrapper/BUILD.gn | 3 - .../cpp/client_wrapper/core_wrapper_files.gni | 23 ++-- .../include/flutter/json_type.h | 26 ----- .../json_message_codec.cc | 34 ++---- .../include/flutter => }/json_message_codec.h | 17 +-- .../cpp/json_message_codec_unittests.cc | 46 ++++++++ .../{client_wrapper => }/json_method_codec.cc | 43 ++----- .../include/flutter => }/json_method_codec.h | 23 ++-- .../common/cpp/json_method_codec_unittests.cc | 107 ++++++++++++++++++ .../src/flutter/shell/platform/glfw/BUILD.gn | 2 - .../shell/platform/glfw/key_event_handler.cc | 2 +- .../shell/platform/glfw/platform_handler.cc | 2 +- .../shell/platform/glfw/text_input_plugin.cc | 2 +- .../flutter/shell/platform/windows/BUILD.gn | 2 - .../platform/windows/key_event_handler.cc | 2 +- .../platform/windows/platform_handler.cc | 2 +- .../platform/windows/text_input_plugin.cc | 2 +- engine/src/flutter/testing/run_tests.py | 2 + 21 files changed, 239 insertions(+), 147 deletions(-) delete mode 100644 engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_type.h rename engine/src/flutter/shell/platform/common/cpp/{client_wrapper => }/json_message_codec.cc (53%) rename engine/src/flutter/shell/platform/common/cpp/{client_wrapper/include/flutter => }/json_message_codec.h (64%) create mode 100644 engine/src/flutter/shell/platform/common/cpp/json_message_codec_unittests.cc rename engine/src/flutter/shell/platform/common/cpp/{client_wrapper => }/json_method_codec.cc (75%) rename engine/src/flutter/shell/platform/common/cpp/{client_wrapper/include/flutter => }/json_method_codec.h (60%) create mode 100644 engine/src/flutter/shell/platform/common/cpp/json_method_codec_unittests.cc diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index d35c608ba1..fea2a2b5be 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -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", diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 903a33b252..724af2afdc 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -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 diff --git a/engine/src/flutter/shell/platform/common/cpp/BUILD.gn b/engine/src/flutter/shell/platform/common/cpp/BUILD.gn index 00db206ed2..4d211c8fe8 100644 --- a/engine/src/flutter/shell/platform/common/cpp/BUILD.gn +++ b/engine/src/flutter/shell/platform/common/cpp/BUILD.gn @@ -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 = [ diff --git a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/BUILD.gn b/engine/src/flutter/shell/platform/common/cpp/client_wrapper/BUILD.gn index a8d30109d2..f46cd81461 100644 --- a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/BUILD.gn +++ b/engine/src/flutter/shell/platform/common/cpp/client_wrapper/BUILD.gn @@ -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" ] diff --git a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni b/engine/src/flutter/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni index ce91be7866..0662cca6be 100644 --- a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni +++ b/engine/src/flutter/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni @@ -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") diff --git a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_type.h b/engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_type.h deleted file mode 100644 index 24baf1328d..0000000000 --- a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_type.h +++ /dev/null @@ -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 - -// 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 - -using JsonValueType = Json::Value; -#endif - -#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_JSON_TYPE_H_ diff --git a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/json_message_codec.cc b/engine/src/flutter/shell/platform/common/cpp/json_message_codec.cc similarity index 53% rename from engine/src/flutter/shell/platform/common/cpp/client_wrapper/json_message_codec.cc rename to engine/src/flutter/shell/platform/common/cpp/json_message_codec.cc index 5610c4762e..d2a43bd806 100644 --- a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/json_message_codec.cc +++ b/engine/src/flutter/shell/platform/common/cpp/json_message_codec.cc @@ -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 #include -#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> 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 writer(buffer); @@ -31,37 +28,20 @@ std::unique_ptr> JsonMessageCodec::EncodeMessageInternal( const char* buffer_start = buffer.GetString(); return std::make_unique>( buffer_start, buffer_start + buffer.GetSize()); -#else - Json::StreamWriterBuilder writer_builder; - std::string serialization = Json::writeString(writer_builder, message); - return std::make_unique>(serialization.begin(), - serialization.end()); -#endif } -std::unique_ptr JsonMessageCodec::DecodeMessageInternal( +std::unique_ptr JsonMessageCodec::DecodeMessageInternal( const uint8_t* binary_message, const size_t message_size) const { auto raw_message = reinterpret_cast(binary_message); - auto json_message = std::make_unique(); - std::string parse_errors; - bool parsing_successful = false; -#ifdef USE_RAPID_JSON + auto json_message = std::make_unique(); 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 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; diff --git a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_message_codec.h b/engine/src/flutter/shell/platform/common/cpp/json_message_codec.h similarity index 64% rename from engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_message_codec.h rename to engine/src/flutter/shell/platform/common/cpp/json_message_codec.h index ba1aa2ffc3..81f2502c0c 100644 --- a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_message_codec.h +++ b/engine/src/flutter/shell/platform/common/cpp/json_message_codec.h @@ -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 + +#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 { +class JsonMessageCodec : public MessageCodec { public: // Returns the shared instance of the codec. static const JsonMessageCodec& GetInstance(); @@ -28,15 +29,15 @@ class JsonMessageCodec : public MessageCodec { JsonMessageCodec() = default; // |flutter::MessageCodec| - std::unique_ptr DecodeMessageInternal( + std::unique_ptr DecodeMessageInternal( const uint8_t* binary_message, const size_t message_size) const override; // |flutter::MessageCodec| std::unique_ptr> 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_ diff --git a/engine/src/flutter/shell/platform/common/cpp/json_message_codec_unittests.cc b/engine/src/flutter/shell/platform/common/cpp/json_message_codec_unittests.cc new file mode 100644 index 0000000000..3354f30a72 --- /dev/null +++ b/engine/src/flutter/shell/platform/common/cpp/json_message_codec_unittests.cc @@ -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 +#include +#include + +#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::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 diff --git a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/json_method_codec.cc b/engine/src/flutter/shell/platform/common/cpp/json_method_codec.cc similarity index 75% rename from engine/src/flutter/shell/platform/common/cpp/client_wrapper/json_method_codec.cc rename to engine/src/flutter/shell/platform/common/cpp/json_method_codec.cc index 6c9528ff2e..26f6a24020 100644 --- a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/json_method_codec.cc +++ b/engine/src/flutter/shell/platform/common/cpp/json_method_codec.cc @@ -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> +std::unique_ptr> JsonMethodCodec::DecodeMethodCallInternal(const uint8_t* message, const size_t message_size) const { - std::unique_ptr json_message = + std::unique_ptr 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>( method_name, std::move(arguments)); -#else - Json::Value method = (*json_message)[kMessageMethodKey]; - if (method.isNull()) { - return nullptr; - } - return std::make_unique>( - method.asString(), - std::make_unique((*json_message)[kMessageArgumentsKey])); -#endif } std::unique_ptr> JsonMethodCodec::EncodeMethodCallInternal( - const MethodCall& method_call) const { -#if USE_RAPID_JSON + const MethodCall& 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> 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> 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> 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); } diff --git a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h b/engine/src/flutter/shell/platform/common/cpp/json_method_codec.h similarity index 60% rename from engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h rename to engine/src/flutter/shell/platform/common/cpp/json_method_codec.h index 92af82000a..597fb6e6d1 100644 --- a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h +++ b/engine/src/flutter/shell/platform/common/cpp/json_method_codec.h @@ -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 + +#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 { +class JsonMethodCodec : public MethodCodec { public: // Returns the shared instance of the codec. static const JsonMethodCodec& GetInstance(); @@ -28,25 +29,25 @@ class JsonMethodCodec : public MethodCodec { JsonMethodCodec() = default; // |flutter::MethodCodec| - std::unique_ptr> DecodeMethodCallInternal( + std::unique_ptr> DecodeMethodCallInternal( const uint8_t* message, const size_t message_size) const override; // |flutter::MethodCodec| std::unique_ptr> EncodeMethodCallInternal( - const MethodCall& method_call) const override; + const MethodCall& method_call) const override; // |flutter::MethodCodec| std::unique_ptr> EncodeSuccessEnvelopeInternal( - const JsonValueType* result) const override; + const rapidjson::Document* result) const override; // |flutter::MethodCodec| std::unique_ptr> 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_ diff --git a/engine/src/flutter/shell/platform/common/cpp/json_method_codec_unittests.cc b/engine/src/flutter/shell/platform/common/cpp/json_method_codec_unittests.cc new file mode 100644 index 0000000000..36bffcff1d --- /dev/null +++ b/engine/src/flutter/shell/platform/common/cpp/json_method_codec_unittests.cc @@ -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& a, + const MethodCall& 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 call("hello", nullptr); + auto encoded = codec.EncodeMethodCall(call); + ASSERT_TRUE(encoded); + std::unique_ptr> 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::kArrayType); + auto& allocator = arguments->GetAllocator(); + arguments->PushBack(42, allocator); + arguments->PushBack("world", allocator); + MethodCall call("hello", std::move(arguments)); + auto encoded = codec.EncodeMethodCall(call); + ASSERT_TRUE(encoded); + std::unique_ptr> 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 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 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 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 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 diff --git a/engine/src/flutter/shell/platform/glfw/BUILD.gn b/engine/src/flutter/shell/platform/glfw/BUILD.gn index 546718bc14..c78eef6183 100644 --- a/engine/src/flutter/shell/platform/glfw/BUILD.gn +++ b/engine/src/flutter/shell/platform/glfw/BUILD.gn @@ -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" ] diff --git a/engine/src/flutter/shell/platform/glfw/key_event_handler.cc b/engine/src/flutter/shell/platform/glfw/key_event_handler.cc index 2ad013c064..bdd121091f 100644 --- a/engine/src/flutter/shell/platform/glfw/key_event_handler.cc +++ b/engine/src/flutter/shell/platform/glfw/key_event_handler.cc @@ -6,7 +6,7 @@ #include -#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"; diff --git a/engine/src/flutter/shell/platform/glfw/platform_handler.cc b/engine/src/flutter/shell/platform/glfw/platform_handler.cc index 39c8b4708c..6d4046364a 100644 --- a/engine/src/flutter/shell/platform/glfw/platform_handler.cc +++ b/engine/src/flutter/shell/platform/glfw/platform_handler.cc @@ -6,7 +6,7 @@ #include -#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"; diff --git a/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc b/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc index 94fa0a267c..d1f163b2f1 100644 --- a/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc @@ -7,7 +7,7 @@ #include #include -#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"; diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index fee227ea7c..88869ace76 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -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", diff --git a/engine/src/flutter/shell/platform/windows/key_event_handler.cc b/engine/src/flutter/shell/platform/windows/key_event_handler.cc index 63af2446b8..f611210174 100644 --- a/engine/src/flutter/shell/platform/windows/key_event_handler.cc +++ b/engine/src/flutter/shell/platform/windows/key_event_handler.cc @@ -8,7 +8,7 @@ #include -#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"; diff --git a/engine/src/flutter/shell/platform/windows/platform_handler.cc b/engine/src/flutter/shell/platform/windows/platform_handler.cc index c4bf592744..f1540a9819 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler.cc +++ b/engine/src/flutter/shell/platform/windows/platform_handler.cc @@ -8,7 +8,7 @@ #include -#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"; diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc index 6b71ff23e5..0fd4dee9d2 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc @@ -9,7 +9,7 @@ #include #include -#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"; diff --git a/engine/src/flutter/testing/run_tests.py b/engine/src/flutter/testing/run_tests.py index 68abca30b3..3fe88f065f 100755 --- a/engine/src/flutter/testing/run_tests.py +++ b/engine/src/flutter/testing/run_tests.py @@ -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