diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 2d4afaff7f585..c17012f99d507 100755 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -170,6 +170,7 @@ FILE: ../../../flutter/fml/macros.h FILE: ../../../flutter/fml/make_copyable.h FILE: ../../../flutter/fml/mapping.cc FILE: ../../../flutter/fml/mapping.h +FILE: ../../../flutter/fml/mapping_unittests.cc FILE: ../../../flutter/fml/memory/ref_counted.h FILE: ../../../flutter/fml/memory/ref_counted_internal.h FILE: ../../../flutter/fml/memory/ref_counted_unittest.cc diff --git a/fml/BUILD.gn b/fml/BUILD.gn index 3a720fae2f9fb..55c88e2569d6f 100644 --- a/fml/BUILD.gn +++ b/fml/BUILD.gn @@ -252,6 +252,7 @@ if (enable_unittests) { "file_unittest.cc", "hash_combine_unittests.cc", "logging_unittests.cc", + "mapping_unittests.cc", "memory/ref_counted_unittest.cc", "memory/task_runner_checker_unittest.cc", "memory/weak_ptr_unittest.cc", diff --git a/fml/mapping.cc b/fml/mapping.cc index 64d09e93464fc..254ccafc441ea 100644 --- a/fml/mapping.cc +++ b/fml/mapping.cc @@ -82,7 +82,6 @@ const uint8_t* DataMapping::GetMapping() const { } // NonOwnedMapping - NonOwnedMapping::NonOwnedMapping(const uint8_t* data, size_t size, const ReleaseProc& release_proc) @@ -102,6 +101,46 @@ const uint8_t* NonOwnedMapping::GetMapping() const { return data_; } +// MallocMapping +MallocMapping::MallocMapping() : data_(nullptr), size_(0) {} + +MallocMapping::MallocMapping(uint8_t* data, size_t size) + : data_(data), size_(size) {} + +MallocMapping::MallocMapping(fml::MallocMapping&& mapping) + : data_(mapping.data_), size_(mapping.size_) { + mapping.data_ = nullptr; + mapping.size_ = 0; +} + +MallocMapping::~MallocMapping() { + free(data_); + data_ = nullptr; +} + +MallocMapping MallocMapping::Copy(const void* begin, size_t length) { + auto result = + MallocMapping(reinterpret_cast(malloc(length)), length); + FML_CHECK(result.GetMapping() != nullptr); + memcpy(const_cast(result.GetMapping()), begin, length); + return result; +} + +size_t MallocMapping::GetSize() const { + return size_; +} + +const uint8_t* MallocMapping::GetMapping() const { + return data_; +} + +uint8_t* MallocMapping::Release() { + uint8_t* result = data_; + data_ = nullptr; + size_ = 0; + return result; +} + // Symbol Mapping SymbolMapping::SymbolMapping(fml::RefPtr native_library, diff --git a/fml/mapping.h b/fml/mapping.h index f6b54ed7bbb8a..cd1be8e47d73d 100644 --- a/fml/mapping.h +++ b/fml/mapping.h @@ -125,6 +125,54 @@ class NonOwnedMapping final : public Mapping { FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping); }; +/// A Mapping like NonOwnedMapping, but uses Free as its release proc. +class MallocMapping final : public Mapping { + public: + MallocMapping(); + + /// Creates a MallocMapping for a region of memory (without copying it). + /// The function will `abort()` if the malloc fails. + /// @param data The starting address of the mapping. + /// @param size The size of the mapping in bytes. + MallocMapping(uint8_t* data, size_t size); + + MallocMapping(fml::MallocMapping&& mapping); + + ~MallocMapping() override; + + /// Copies the data from `begin` to `end`. + /// It's templated since void* arithemetic isn't allowed and we want support + /// for `uint8_t` and `char`. + template + static MallocMapping Copy(const T* begin, const T* end) { + FML_DCHECK(end > begin); + size_t length = end - begin; + return Copy(begin, length); + } + + /// Copies a region of memory into a MallocMapping. + /// The function will `abort()` if the malloc fails. + /// @param begin The starting address of where we will copy. + /// @param length The length of the region to copy in bytes. + static MallocMapping Copy(const void* begin, size_t length); + + // |Mapping| + size_t GetSize() const override; + + // |Mapping| + const uint8_t* GetMapping() const override; + + /// Removes ownership of the data buffer. + /// After this is called; the mapping will point to nullptr. + [[nodiscard]] uint8_t* Release(); + + private: + uint8_t* data_; + size_t size_; + + FML_DISALLOW_COPY_AND_ASSIGN(MallocMapping); +}; + class SymbolMapping final : public Mapping { public: SymbolMapping(fml::RefPtr native_library, diff --git a/fml/mapping_unittests.cc b/fml/mapping_unittests.cc new file mode 100644 index 0000000000000..0a7309ccf3c4c --- /dev/null +++ b/fml/mapping_unittests.cc @@ -0,0 +1,55 @@ +// 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/fml/mapping.h" +#include "flutter/testing/testing.h" + +namespace fml { + +TEST(MallocMapping, EmptyContructor) { + MallocMapping mapping; + ASSERT_EQ(nullptr, mapping.GetMapping()); + ASSERT_EQ(0u, mapping.GetSize()); +} + +TEST(MallocMapping, NotEmptyContructor) { + size_t length = 10; + MallocMapping mapping(reinterpret_cast(malloc(length)), length); + ASSERT_NE(nullptr, mapping.GetMapping()); + ASSERT_EQ(length, mapping.GetSize()); +} + +TEST(MallocMapping, MoveConstructor) { + size_t length = 10; + MallocMapping mapping(reinterpret_cast(malloc(length)), length); + MallocMapping moved = std::move(mapping); + + ASSERT_EQ(nullptr, mapping.GetMapping()); + ASSERT_EQ(0u, mapping.GetSize()); + ASSERT_NE(nullptr, moved.GetMapping()); + ASSERT_EQ(length, moved.GetSize()); +} + +TEST(MallocMapping, Copy) { + size_t length = 10; + MallocMapping mapping(reinterpret_cast(malloc(length)), length); + memset(const_cast(mapping.GetMapping()), 0xac, mapping.GetSize()); + MallocMapping copied = + MallocMapping::Copy(mapping.GetMapping(), mapping.GetSize()); + + ASSERT_NE(mapping.GetMapping(), copied.GetMapping()); + ASSERT_EQ(mapping.GetSize(), copied.GetSize()); + ASSERT_EQ( + 0, memcmp(mapping.GetMapping(), copied.GetMapping(), mapping.GetSize())); +} + +TEST(MallocMapping, Release) { + size_t length = 10; + MallocMapping mapping(reinterpret_cast(malloc(length)), length); + free(const_cast(mapping.Release())); + ASSERT_EQ(nullptr, mapping.GetMapping()); + ASSERT_EQ(0u, mapping.GetSize()); +} + +} // namespace fml diff --git a/lib/ui/window/platform_configuration.cc b/lib/ui/window/platform_configuration.cc index 981cbbf8b74de..18133a2e98827 100644 --- a/lib/ui/window/platform_configuration.cc +++ b/lib/ui/window/platform_configuration.cc @@ -130,7 +130,7 @@ Dart_Handle SendPlatformMessage(Dart_Handle window, const uint8_t* buffer = static_cast(data.data()); dart_state->platform_configuration()->client()->HandlePlatformMessage( std::make_unique( - name, std::vector(buffer, buffer + data.length_in_bytes()), + name, fml::MallocMapping::Copy(buffer, data.length_in_bytes()), response)); } @@ -190,8 +190,8 @@ void _RespondToKeyData(Dart_NativeArguments args) { tonic::DartCallStatic(&RespondToKeyData, args); } -Dart_Handle ToByteData(const std::vector& buffer) { - return tonic::DartByteData::Create(buffer.data(), buffer.size()); +Dart_Handle ToByteData(const fml::Mapping& buffer) { + return tonic::DartByteData::Create(buffer.GetMapping(), buffer.GetSize()); } } // namespace @@ -338,7 +338,7 @@ void PlatformConfiguration::DispatchPlatformMessage( void PlatformConfiguration::DispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args) { + fml::MallocMapping args) { std::shared_ptr dart_state = dispatch_semantics_action_.dart_state().lock(); if (!dart_state) { @@ -346,7 +346,8 @@ void PlatformConfiguration::DispatchSemanticsAction(int32_t id, } tonic::DartState::Scope scope(dart_state); - Dart_Handle args_handle = (args.empty()) ? Dart_Null() : ToByteData(args); + Dart_Handle args_handle = + (args.GetSize() <= 0) ? Dart_Null() : ToByteData(args); if (Dart_IsError(args_handle)) { return; diff --git a/lib/ui/window/platform_configuration.h b/lib/ui/window/platform_configuration.h index 11aef73a2fff1..a32b2def91f49 100644 --- a/lib/ui/window/platform_configuration.h +++ b/lib/ui/window/platform_configuration.h @@ -325,7 +325,7 @@ class PlatformConfiguration final { /// void DispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args); + fml::MallocMapping args); //---------------------------------------------------------------------------- /// @brief Registers a callback to be invoked when the framework has diff --git a/lib/ui/window/platform_message.cc b/lib/ui/window/platform_message.cc index 8423da599ed29..14cd7bb21c9d1 100644 --- a/lib/ui/window/platform_message.cc +++ b/lib/ui/window/platform_message.cc @@ -9,7 +9,7 @@ namespace flutter { PlatformMessage::PlatformMessage(std::string channel, - std::vector data, + fml::MallocMapping data, fml::RefPtr response) : channel_(std::move(channel)), data_(std::move(data)), diff --git a/lib/ui/window/platform_message.h b/lib/ui/window/platform_message.h index c0e807bf32aa9..7fe09cdcac1c7 100644 --- a/lib/ui/window/platform_message.h +++ b/lib/ui/window/platform_message.h @@ -17,23 +17,25 @@ namespace flutter { class PlatformMessage { public: PlatformMessage(std::string channel, - std::vector data, + fml::MallocMapping data, fml::RefPtr response); PlatformMessage(std::string channel, fml::RefPtr response); ~PlatformMessage(); const std::string& channel() const { return channel_; } - const std::vector& data() const { return data_; } + const fml::MallocMapping& data() const { return data_; } bool hasData() { return hasData_; } const fml::RefPtr& response() const { return response_; } + fml::MallocMapping releaseData() { return std::move(data_); } + private: std::string channel_; - std::vector data_; + fml::MallocMapping data_; bool hasData_; fml::RefPtr response_; }; diff --git a/runtime/runtime_controller.cc b/runtime/runtime_controller.cc index f532cc772e956..7da201b7a056f 100644 --- a/runtime/runtime_controller.cc +++ b/runtime/runtime_controller.cc @@ -278,7 +278,7 @@ bool RuntimeController::DispatchKeyDataPacket(const KeyDataPacket& packet, bool RuntimeController::DispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args) { + fml::MallocMapping args) { TRACE_EVENT1("flutter", "RuntimeController::DispatchSemanticsAction", "mode", "basic"); if (auto* platform_configuration = GetPlatformConfigurationIfAvailable()) { diff --git a/runtime/runtime_controller.h b/runtime/runtime_controller.h index 9f90b022ae530..38976557496e9 100644 --- a/runtime/runtime_controller.h +++ b/runtime/runtime_controller.h @@ -453,7 +453,7 @@ class RuntimeController : public PlatformConfigurationClient { /// bool DispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args); + fml::MallocMapping args); //---------------------------------------------------------------------------- /// @brief Gets the main port identifier of the root isolate. diff --git a/shell/common/engine.cc b/shell/common/engine.cc index 0203f986a8a2c..db72230590b2a 100644 --- a/shell/common/engine.cc +++ b/shell/common/engine.cc @@ -4,6 +4,7 @@ #include "flutter/shell/common/engine.h" +#include #include #include #include @@ -35,6 +36,12 @@ static constexpr char kLocalizationChannel[] = "flutter/localization"; static constexpr char kSettingsChannel[] = "flutter/settings"; static constexpr char kIsolateChannel[] = "flutter/isolate"; +namespace { +fml::MallocMapping MakeMapping(const std::string& str) { + return fml::MallocMapping::Copy(str.c_str(), str.length()); +} +} // namespace + Engine::Engine( Delegate& delegate, const PointerDataDispatcherMaker& dispatcher_maker, @@ -205,10 +212,7 @@ Engine::RunStatus Engine::Run(RunConfiguration configuration) { if (service_id.has_value()) { std::unique_ptr service_id_message = std::make_unique( - kIsolateChannel, - std::vector(service_id.value().begin(), - service_id.value().end()), - nullptr); + kIsolateChannel, MakeMapping(service_id.value()), nullptr); HandlePlatformMessage(std::move(service_id_message)); } @@ -326,7 +330,8 @@ void Engine::DispatchPlatformMessage(std::unique_ptr message) { bool Engine::HandleLifecyclePlatformMessage(PlatformMessage* message) { const auto& data = message->data(); - std::string state(reinterpret_cast(data.data()), data.size()); + std::string state(reinterpret_cast(data.GetMapping()), + data.GetSize()); if (state == "AppLifecycleState.paused" || state == "AppLifecycleState.detached") { activity_running_ = false; @@ -353,7 +358,8 @@ bool Engine::HandleNavigationPlatformMessage( const auto& data = message->data(); rapidjson::Document document; - document.Parse(reinterpret_cast(data.data()), data.size()); + document.Parse(reinterpret_cast(data.GetMapping()), + data.GetSize()); if (document.HasParseError() || !document.IsObject()) { return false; } @@ -371,7 +377,8 @@ bool Engine::HandleLocalizationPlatformMessage(PlatformMessage* message) { const auto& data = message->data(); rapidjson::Document document; - document.Parse(reinterpret_cast(data.data()), data.size()); + document.Parse(reinterpret_cast(data.GetMapping()), + data.GetSize()); if (document.HasParseError() || !document.IsObject()) { return false; } @@ -411,7 +418,8 @@ bool Engine::HandleLocalizationPlatformMessage(PlatformMessage* message) { void Engine::HandleSettingsPlatformMessage(PlatformMessage* message) { const auto& data = message->data(); - std::string jsonData(reinterpret_cast(data.data()), data.size()); + std::string jsonData(reinterpret_cast(data.GetMapping()), + data.GetSize()); if (runtime_controller_->SetUserSettingsData(std::move(jsonData)) && have_surface_) { ScheduleFrame(); @@ -436,7 +444,7 @@ void Engine::DispatchKeyDataPacket(std::unique_ptr packet, void Engine::DispatchSemanticsAction(int id, SemanticsAction action, - std::vector args) { + fml::MallocMapping args) { runtime_controller_->DispatchSemanticsAction(id, action, std::move(args)); } @@ -538,8 +546,8 @@ void Engine::HandleAssetPlatformMessage( return; } const auto& data = message->data(); - std::string asset_name(reinterpret_cast(data.data()), - data.size()); + std::string asset_name(reinterpret_cast(data.GetMapping()), + data.GetSize()); if (asset_manager_) { std::unique_ptr asset_mapping = diff --git a/shell/common/engine.h b/shell/common/engine.h index a4d17893dbadb..1b42fe244d180 100644 --- a/shell/common/engine.h +++ b/shell/common/engine.h @@ -763,7 +763,7 @@ class Engine final : public RuntimeDelegate, /// void DispatchSemanticsAction(int id, SemanticsAction action, - std::vector args); + fml::MallocMapping args); //---------------------------------------------------------------------------- /// @brief Notifies the engine that the embedder has expressed an opinion diff --git a/shell/common/engine_unittests.cc b/shell/common/engine_unittests.cc index 44a3cce1ea724..18163e7d1c404 100644 --- a/shell/common/engine_unittests.cc +++ b/shell/common/engine_unittests.cc @@ -96,7 +96,7 @@ std::unique_ptr MakePlatformMessage( const uint8_t* data = reinterpret_cast(buffer.GetString()); std::unique_ptr message = std::make_unique( - channel, std::vector(data, data + buffer.GetSize()), response); + channel, fml::MallocMapping::Copy(data, buffer.GetSize()), response); return message; } diff --git a/shell/common/platform_view.cc b/shell/common/platform_view.cc index 6db8ba0c29be5..7c7b4dd1d421e 100644 --- a/shell/common/platform_view.cc +++ b/shell/common/platform_view.cc @@ -50,7 +50,7 @@ void PlatformView::DispatchKeyDataPacket(std::unique_ptr packet, void PlatformView::DispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args) { + fml::MallocMapping args) { delegate_.OnPlatformViewDispatchSemanticsAction(id, action, std::move(args)); } diff --git a/shell/common/platform_view.h b/shell/common/platform_view.h index 52d42cd19baef..186237b02ad33 100644 --- a/shell/common/platform_view.h +++ b/shell/common/platform_view.h @@ -157,7 +157,7 @@ class PlatformView { virtual void OnPlatformViewDispatchSemanticsAction( int32_t id, SemanticsAction action, - std::vector args) = 0; + fml::MallocMapping args) = 0; //-------------------------------------------------------------------------- /// @brief Notifies the delegate that the embedder has expressed an @@ -408,7 +408,7 @@ class PlatformView { /// void DispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args); + fml::MallocMapping args); //---------------------------------------------------------------------------- /// @brief Used by embedder to notify the running isolate hosted by the diff --git a/shell/common/shell.cc b/shell/common/shell.cc index 7387ec75d51db..b9d1765e2a431 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -998,16 +998,17 @@ void Shell::OnPlatformViewDispatchKeyDataPacket( // |PlatformView::Delegate| void Shell::OnPlatformViewDispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args) { + fml::MallocMapping args) { FML_DCHECK(is_setup_); FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread()); task_runners_.GetUITaskRunner()->PostTask( - [engine = engine_->GetWeakPtr(), id, action, args = std::move(args)] { + fml::MakeCopyable([engine = engine_->GetWeakPtr(), id, action, + args = std::move(args)]() mutable { if (engine) { engine->DispatchSemanticsAction(id, action, std::move(args)); } - }); + })); } // |PlatformView::Delegate| @@ -1225,7 +1226,8 @@ void Shell::HandleEngineSkiaMessage(std::unique_ptr message) { const auto& data = message->data(); rapidjson::Document document; - document.Parse(reinterpret_cast(data.data()), data.size()); + document.Parse(reinterpret_cast(data.GetMapping()), + data.GetSize()); if (document.HasParseError() || !document.IsObject()) return; auto root = document.GetObject(); @@ -1806,8 +1808,8 @@ bool Shell::ReloadSystemFonts() { std::string message = buffer.GetString(); std::unique_ptr fontsChangeMessage = std::make_unique( - kSystemChannel, std::vector(message.begin(), message.end()), - nullptr); + kSystemChannel, + fml::MallocMapping::Copy(message.c_str(), message.length()), nullptr); OnPlatformViewDispatchPlatformMessage(std::move(fontsChangeMessage)); return true; diff --git a/shell/common/shell.h b/shell/common/shell.h index 4bd957e218255..af0e85b7cef20 100644 --- a/shell/common/shell.h +++ b/shell/common/shell.h @@ -493,10 +493,9 @@ class Shell final : public PlatformView::Delegate, std::function callback) override; // |PlatformView::Delegate| - void OnPlatformViewDispatchSemanticsAction( - int32_t id, - SemanticsAction action, - std::vector args) override; + void OnPlatformViewDispatchSemanticsAction(int32_t id, + SemanticsAction action, + fml::MallocMapping args) override; // |PlatformView::Delegate| void OnPlatformViewSetSemanticsEnabled(bool enabled) override; diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 123a29a9f2c43..f2931ec109da2 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -69,7 +69,7 @@ class MockPlatformViewDelegate : public PlatformView::Delegate { MOCK_METHOD3(OnPlatformViewDispatchSemanticsAction, void(int32_t id, SemanticsAction action, - std::vector args)); + fml::MallocMapping args)); MOCK_METHOD1(OnPlatformViewSetSemanticsEnabled, void(bool enabled)); @@ -1489,7 +1489,8 @@ TEST_F(ShellTest, SetResourceCacheSize) { "method": "Skia.setResourceCacheMaxBytes", "args": 10000 })json"; - std::vector data(request_json.begin(), request_json.end()); + auto data = + fml::MallocMapping::Copy(request_json.c_str(), request_json.length()); auto platform_message = std::make_unique( "flutter/skia", std::move(data), nullptr); SendEnginePlatformMessage(shell.get(), std::move(platform_message)); @@ -1811,10 +1812,7 @@ TEST_F(ShellTest, CanConvertToAndFromMappings) { buffer, buffer_size, MemsetPatternOp::kMemsetPatternOpSetBuffer)); std::unique_ptr mapping = - std::make_unique( - buffer, buffer_size, [](const uint8_t* buffer, size_t size) { - ::free(const_cast(buffer)); - }); + std::make_unique(buffer, buffer_size); ASSERT_EQ(mapping->GetSize(), buffer_size); diff --git a/shell/platform/android/platform_view_android.cc b/shell/platform/android/platform_view_android.cc index 1420fbebe4b9b..ff514d93cb5d8 100644 --- a/shell/platform/android/platform_view_android.cc +++ b/shell/platform/android/platform_view_android.cc @@ -180,8 +180,8 @@ void PlatformViewAndroid::DispatchPlatformMessage(JNIEnv* env, jint response_id) { uint8_t* message_data = static_cast(env->GetDirectBufferAddress(java_message_data)); - std::vector message = - std::vector(message_data, message_data + java_message_position); + fml::MallocMapping message = + fml::MallocMapping::Copy(message_data, java_message_position); fml::RefPtr response; if (response_id) { @@ -266,15 +266,14 @@ void PlatformViewAndroid::DispatchSemanticsAction(JNIEnv* env, jobject args, jint args_position) { if (env->IsSameObject(args, NULL)) { - std::vector args_vector; PlatformView::DispatchSemanticsAction( - id, static_cast(action), args_vector); + id, static_cast(action), + fml::MallocMapping()); return; } uint8_t* args_data = static_cast(env->GetDirectBufferAddress(args)); - std::vector args_vector = - std::vector(args_data, args_data + args_position); + auto args_vector = fml::MallocMapping::Copy(args_data, args_position); PlatformView::DispatchSemanticsAction( id, static_cast(action), diff --git a/shell/platform/android/platform_view_android_jni_impl.cc b/shell/platform/android/platform_view_android_jni_impl.cc index 2e6e81c615376..16e58e46ce280 100644 --- a/shell/platform/android/platform_view_android_jni_impl.cc +++ b/shell/platform/android/platform_view_android_jni_impl.cc @@ -1108,10 +1108,10 @@ void PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessage( if (message->hasData()) { fml::jni::ScopedJavaLocalRef message_array( - env, env->NewByteArray(message->data().size())); + env, env->NewByteArray(message->data().GetSize())); env->SetByteArrayRegion( - message_array.obj(), 0, message->data().size(), - reinterpret_cast(message->data().data())); + message_array.obj(), 0, message->data().GetSize(), + reinterpret_cast(message->data().GetMapping())); env->CallVoidMethod(java_object.obj(), g_handle_platform_message_method, java_channel.obj(), message_array.obj(), responseId); } else { diff --git a/shell/platform/common/BUILD.gn b/shell/platform/common/BUILD.gn index 323f8e5477853..8b3cf4c25a597 100644 --- a/shell/platform/common/BUILD.gn +++ b/shell/platform/common/BUILD.gn @@ -83,6 +83,7 @@ source_set("common_cpp_accessibility") { [ "//flutter/third_party/accessibility:accessibility_config" ] public_deps = [ + "//flutter/fml:fml", "//flutter/shell/platform/embedder:embedder_as_internal_library", "//flutter/third_party/accessibility", ] diff --git a/shell/platform/common/accessibility_bridge.cc b/shell/platform/common/accessibility_bridge.cc index 3923815952d85..e68a18ae371ff 100644 --- a/shell/platform/common/accessibility_bridge.cc +++ b/shell/platform/common/accessibility_bridge.cc @@ -539,8 +539,8 @@ gfx::RectF AccessibilityBridge::RelativeToGlobalBounds(const ui::AXNode* node, void AccessibilityBridge::DispatchAccessibilityAction( AccessibilityNodeId target, FlutterSemanticsAction action, - std::vector data) { - delegate_->DispatchAccessibilityAction(target, action, data); + fml::MallocMapping data) { + delegate_->DispatchAccessibilityAction(target, action, std::move(data)); } } // namespace flutter diff --git a/shell/platform/common/accessibility_bridge.h b/shell/platform/common/accessibility_bridge.h index faebd452988c8..ab8969ab47cb7 100644 --- a/shell/platform/common/accessibility_bridge.h +++ b/shell/platform/common/accessibility_bridge.h @@ -7,6 +7,7 @@ #include +#include "flutter/fml/mapping.h" #include "flutter/shell/platform/embedder/embedder.h" #include "flutter/third_party/accessibility/ax/ax_event_generator.h" @@ -86,10 +87,9 @@ class AccessibilityBridge /// @param[in] action The generated flutter semantics action. /// @param[in] data Additional data associated with the /// action. - virtual void DispatchAccessibilityAction( - AccessibilityNodeId target, - FlutterSemanticsAction action, - const std::vector& data) = 0; + virtual void DispatchAccessibilityAction(AccessibilityNodeId target, + FlutterSemanticsAction action, + fml::MallocMapping data) = 0; //--------------------------------------------------------------------------- /// @brief Creates a platform specific FlutterPlatformNodeDelegate. @@ -277,7 +277,7 @@ class AccessibilityBridge // |FlutterPlatformNodeDelegate::OwnerBridge| void DispatchAccessibilityAction(AccessibilityNodeId target, FlutterSemanticsAction action, - std::vector data) override; + fml::MallocMapping data) override; // |FlutterPlatformNodeDelegate::OwnerBridge| gfx::RectF RelativeToGlobalBounds(const ui::AXNode* node, diff --git a/shell/platform/common/flutter_platform_node_delegate.h b/shell/platform/common/flutter_platform_node_delegate.h index ca65ff708f492..3a562d1c345a0 100644 --- a/shell/platform/common/flutter_platform_node_delegate.h +++ b/shell/platform/common/flutter_platform_node_delegate.h @@ -5,8 +5,8 @@ #ifndef FLUTTER_SHELL_PLATFORM_COMMON_FLUTTER_PLATFORM_NODE_DELEGATE_H_ #define FLUTTER_SHELL_PLATFORM_COMMON_FLUTTER_PLATFORM_NODE_DELEGATE_H_ +#include "flutter/fml/mapping.h" #include "flutter/shell/platform/embedder/embedder.h" - #include "flutter/third_party/accessibility/ax/ax_event_generator.h" #include "flutter/third_party/accessibility/ax/platform/ax_platform_node_delegate_base.h" @@ -57,7 +57,7 @@ class FlutterPlatformNodeDelegate : public ui::AXPlatformNodeDelegateBase { /// action. virtual void DispatchAccessibilityAction(AccessibilityNodeId target, FlutterSemanticsAction action, - std::vector data) = 0; + fml::MallocMapping data) = 0; //--------------------------------------------------------------------------- /// @brief Get the native accessibility node with the given id. diff --git a/shell/platform/common/test_accessibility_bridge.cc b/shell/platform/common/test_accessibility_bridge.cc index 79677a9660256..7127989a8d4f7 100644 --- a/shell/platform/common/test_accessibility_bridge.cc +++ b/shell/platform/common/test_accessibility_bridge.cc @@ -19,7 +19,7 @@ void TestAccessibilityBridgeDelegate::OnAccessibilityEvent( void TestAccessibilityBridgeDelegate::DispatchAccessibilityAction( AccessibilityNodeId target, FlutterSemanticsAction action, - const std::vector& data) { + fml::MallocMapping data) { performed_actions.push_back(action); } diff --git a/shell/platform/common/test_accessibility_bridge.h b/shell/platform/common/test_accessibility_bridge.h index 892a00d1221d0..427b88c276cbb 100644 --- a/shell/platform/common/test_accessibility_bridge.h +++ b/shell/platform/common/test_accessibility_bridge.h @@ -18,7 +18,7 @@ class TestAccessibilityBridgeDelegate ui::AXEventGenerator::TargetedEvent targeted_event) override; void DispatchAccessibilityAction(AccessibilityNodeId target, FlutterSemanticsAction action, - const std::vector& data) override; + fml::MallocMapping data) override; std::unique_ptr CreateFlutterPlatformNodeDelegate(); diff --git a/shell/platform/darwin/common/buffer_conversions.h b/shell/platform/darwin/common/buffer_conversions.h index 60559b2dfe230..7dd90f558d6cb 100644 --- a/shell/platform/darwin/common/buffer_conversions.h +++ b/shell/platform/darwin/common/buffer_conversions.h @@ -13,13 +13,13 @@ namespace flutter { -std::vector GetVectorFromNSData(NSData* data); +fml::MallocMapping CopyNSDataToMapping(NSData* data); -NSData* GetNSDataFromVector(const std::vector& buffer); +NSData* CopyMappingToNSData(fml::MallocMapping buffer); -std::unique_ptr GetMappingFromNSData(NSData* data); +std::unique_ptr CopyNSDataToMappingPtr(NSData* data); -NSData* GetNSDataFromMapping(std::unique_ptr mapping); +NSData* CopyMappingPtrToNSData(std::unique_ptr mapping); } // namespace flutter diff --git a/shell/platform/darwin/common/buffer_conversions.mm b/shell/platform/darwin/common/buffer_conversions.mm index 69e81de46592b..1856ec0761151 100644 --- a/shell/platform/darwin/common/buffer_conversions.mm +++ b/shell/platform/darwin/common/buffer_conversions.mm @@ -6,20 +6,21 @@ namespace flutter { -std::vector GetVectorFromNSData(NSData* data) { - const uint8_t* bytes = reinterpret_cast(data.bytes); - return std::vector(bytes, bytes + data.length); +fml::MallocMapping CopyNSDataToMapping(NSData* data) { + const uint8_t* bytes = static_cast(data.bytes); + return fml::MallocMapping::Copy(bytes, data.length); } -NSData* GetNSDataFromVector(const std::vector& buffer) { - return [NSData dataWithBytes:buffer.data() length:buffer.size()]; +NSData* CopyMappingToNSData(fml::MallocMapping buffer) { + return [NSData dataWithBytes:const_cast(buffer.GetMapping()) length:buffer.GetSize()]; } -std::unique_ptr GetMappingFromNSData(NSData* data) { - return std::make_unique(GetVectorFromNSData(data)); +std::unique_ptr CopyNSDataToMappingPtr(NSData* data) { + auto mapping = CopyNSDataToMapping(data); + return std::make_unique(std::move(mapping)); } -NSData* GetNSDataFromMapping(std::unique_ptr mapping) { +NSData* CopyMappingPtrToNSData(std::unique_ptr mapping) { return [NSData dataWithBytes:mapping->GetMapping() length:mapping->GetSize()]; } diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index 780bac01ea0f4..8155fd2de1817 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -807,7 +807,7 @@ - (void)sendOnChannel:(NSString*)channel std::unique_ptr platformMessage = (message == nil) ? std::make_unique(channel.UTF8String, response) : std::make_unique( - channel.UTF8String, flutter::GetVectorFromNSData(message), response); + channel.UTF8String, flutter::CopyNSDataToMapping(message), response); _shell->GetPlatformView()->DispatchPlatformMessage(std::move(platformMessage)); } diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm index 5710f832332d1..0d339fbc722f6 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm @@ -29,7 +29,7 @@ void OnPlatformViewDispatchKeyDataPacket(std::unique_ptr packet, std::function callback) override {} void OnPlatformViewDispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args) override {} + fml::MallocMapping args) override {} void OnPlatformViewSetSemanticsEnabled(bool enabled) override {} void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {} void OnPlatformViewRegisterTexture(std::shared_ptr texture) override {} diff --git a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm index a22d182792e68..6cd7c349c8dfe 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm @@ -99,7 +99,7 @@ void OnPlatformViewDispatchKeyDataPacket(std::unique_ptr packet, std::function callback) override {} void OnPlatformViewDispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args) override {} + fml::MallocMapping args) override {} void OnPlatformViewSetSemanticsEnabled(bool enabled) override {} void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {} void OnPlatformViewRegisterTexture(std::shared_ptr texture) override {} diff --git a/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm b/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm index e78fda0b3288e..1c80753d9272a 100644 --- a/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm +++ b/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm @@ -308,8 +308,9 @@ - (BOOL)onCustomAccessibilityAction:(FlutterCustomAccessibilityAction*)action { args.push_back(action_id >> 8); args.push_back(action_id >> 16); args.push_back(action_id >> 24); - [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kCustomAction, - std::move(args)); + [self bridge]->DispatchSemanticsAction( + [self uid], flutter::SemanticsAction::kCustomAction, + fml::MallocMapping::Copy(args.data(), args.size() * sizeof(uint8_t))); return YES; } diff --git a/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm b/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm index b9179a27f6ad5..8380ce735aaeb 100644 --- a/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm +++ b/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm @@ -33,7 +33,7 @@ void DispatchSemanticsAction(int32_t id, SemanticsAction action) override { } void DispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args) override { + fml::MallocMapping args) override { SemanticsActionObservation observation(id, action); observations.push_back(observation); } diff --git a/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h b/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h index bcafa90c478c7..c1456ab446e1b 100644 --- a/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h +++ b/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h @@ -60,7 +60,7 @@ class AccessibilityBridge final : public AccessibilityBridgeIos { void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action) override; void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action, - std::vector args) override; + fml::MallocMapping args) override; void AccessibilityObjectDidBecomeFocused(int32_t id) override; void AccessibilityObjectDidLoseFocus(int32_t id) override; diff --git a/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm b/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm index 1c9f6e4f2529d..77b1a95ff4bbe 100644 --- a/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm +++ b/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm @@ -233,7 +233,7 @@ void PostAccessibilityNotification(UIAccessibilityNotifications notification, void AccessibilityBridge::DispatchSemanticsAction(int32_t uid, flutter::SemanticsAction action, - std::vector args) { + fml::MallocMapping args) { platform_view_->DispatchSemanticsAction(uid, action, std::move(args)); } diff --git a/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h b/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h index 3be237473e9cc..a97f179ece53e 100644 --- a/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h +++ b/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h @@ -8,6 +8,7 @@ #include #include +#import "flutter/fml/mapping.h" #include "flutter/lib/ui/semantics/semantics_node.h" @class UIView; @@ -24,7 +25,7 @@ class AccessibilityBridgeIos { virtual void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action) = 0; virtual void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action, - std::vector args) = 0; + fml::MallocMapping args) = 0; /** * A callback that is called when a SemanticObject receives focus. * diff --git a/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm b/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm index f9bf1c2a2f86b..4ddda97355523 100644 --- a/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm +++ b/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm @@ -82,7 +82,7 @@ void OnPlatformViewDispatchKeyDataPacket(std::unique_ptr packet, std::function callback) override {} void OnPlatformViewDispatchSemanticsAction(int32_t id, SemanticsAction action, - std::vector args) override {} + fml::MallocMapping args) override {} void OnPlatformViewSetSemanticsEnabled(bool enabled) override {} void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {} void OnPlatformViewRegisterTexture(std::shared_ptr texture) override {} diff --git a/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.mm b/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.mm index e7e09855c4625..53b04e484d6fb 100644 --- a/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.mm +++ b/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.mm @@ -17,7 +17,7 @@ void PlatformMessageResponseDarwin::Complete(std::unique_ptr data) { fml::RefPtr self(this); platform_task_runner_->PostTask(fml::MakeCopyable([self, data = std::move(data)]() mutable { - self->callback_.get()(GetNSDataFromMapping(std::move(data))); + self->callback_.get()(CopyMappingPtrToNSData(std::move(data))); })); } diff --git a/shell/platform/darwin/ios/framework/Source/platform_message_router.mm b/shell/platform/darwin/ios/framework/Source/platform_message_router.mm index 70ae8d75cb586..db8b53afa4125 100644 --- a/shell/platform/darwin/ios/framework/Source/platform_message_router.mm +++ b/shell/platform/darwin/ios/framework/Source/platform_message_router.mm @@ -22,12 +22,12 @@ FlutterBinaryMessageHandler handler = it->second; NSData* data = nil; if (message->hasData()) { - data = GetNSDataFromVector(message->data()); + data = CopyMappingToNSData(message->releaseData()); } handler(data, ^(NSData* reply) { if (completer) { if (reply) { - completer->Complete(GetMappingFromNSData(reply)); + completer->Complete(CopyNSDataToMappingPtr(reply)); } else { completer->CompleteEmpty(); } diff --git a/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.h b/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.h index e192732c50807..ea51f93c2cf31 100644 --- a/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.h +++ b/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.h @@ -30,7 +30,7 @@ class AccessibilityBridgeMacDelegate : public AccessibilityBridge::Accessibility // |AccessibilityBridge::AccessibilityBridgeDelegate| void DispatchAccessibilityAction(AccessibilityNodeId target, FlutterSemanticsAction action, - const std::vector& data) override; + fml::MallocMapping data) override; // |AccessibilityBridge::AccessibilityBridgeDelegate| std::unique_ptr CreateFlutterPlatformNodeDelegate() override; diff --git a/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.mm b/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.mm index 5db727eb28ccb..35746b04e64ed 100644 --- a/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.mm +++ b/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.mm @@ -339,12 +339,12 @@ void AccessibilityBridgeMacDelegate::DispatchAccessibilityAction(ui::AXNode::AXID target, FlutterSemanticsAction action, - const std::vector& data) { + fml::MallocMapping data) { NSCAssert(flutter_engine_, @"Flutter engine should not be deallocated"); NSCAssert(flutter_engine_.viewController.viewLoaded && flutter_engine_.viewController.view.window, @"The accessibility bridge should not receive accessibility actions if the flutter view" @"is not loaded or attached to a NSWindow."); - [flutter_engine_ dispatchSemanticsAction:action toTarget:target withData:data]; + [flutter_engine_ dispatchSemanticsAction:action toTarget:target withData:std::move(data)]; } std::unique_ptr diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index a95a0343fd154..58a212a1bb42c 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -465,8 +465,8 @@ - (void)setSemanticsEnabled:(BOOL)enabled { - (void)dispatchSemanticsAction:(FlutterSemanticsAction)action toTarget:(uint16_t)target - withData:(const std::vector&)data { - _embedderAPI.DispatchSemanticsAction(_engine, target, action, data.data(), data.size()); + withData:(fml::MallocMapping)data { + _embedderAPI.DispatchSemanticsAction(_engine, target, action, data.GetMapping(), data.GetSize()); } #pragma mark - Private methods diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h b/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h index 5d2176ea59098..a379e93d867a7 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h @@ -77,6 +77,6 @@ */ - (void)dispatchSemanticsAction:(FlutterSemanticsAction)action toTarget:(uint16_t)target - withData:(const std::vector&)data; + withData:(fml::MallocMapping)data; @end diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index b9d432e3f4a8d..23fe59db350b7 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -1104,8 +1104,8 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, const FlutterPlatformMessage incoming_message = { sizeof(FlutterPlatformMessage), // struct_size message->channel().c_str(), // channel - message->data().data(), // message - message->data().size(), // message_size + message->data().GetMapping(), // message + message->data().GetSize(), // message_size handle, // response_handle }; handle->message = std::move(message); @@ -1637,8 +1637,7 @@ FlutterEngineResult FlutterEngineSendPlatformMessage( } else { message = std::make_unique( flutter_message->channel, - std::vector(message_data, message_data + message_size), - response); + fml::MallocMapping::Copy(message_data, message_size), response); } return reinterpret_cast(engine) @@ -1829,7 +1828,7 @@ FlutterEngineResult FlutterEngineDispatchSemanticsAction( if (!reinterpret_cast(engine) ->DispatchSemanticsAction( id, engine_action, - std::vector({data, data + data_length}))) { + fml::MallocMapping::Copy(data, data_length))) { return LOG_EMBEDDER_ERROR(kInternalInconsistency, "Could not dispatch semantics action."); } @@ -1953,9 +1952,10 @@ static bool DispatchJSONPlatformMessage(FLUTTER_API_SYMBOL(FlutterEngine) } auto platform_message = std::make_unique( - channel_name.c_str(), // channel - std::vector{message, message + buffer.GetSize()}, // message - nullptr // response + channel_name.c_str(), // channel + fml::MallocMapping::Copy(message, + buffer.GetSize()), // message + nullptr // response ); return reinterpret_cast(engine) diff --git a/shell/platform/embedder/embedder_engine.cc b/shell/platform/embedder/embedder_engine.cc index f179130e27ad5..7d1f74bdb3897 100644 --- a/shell/platform/embedder/embedder_engine.cc +++ b/shell/platform/embedder/embedder_engine.cc @@ -210,7 +210,7 @@ bool EmbedderEngine::SetAccessibilityFeatures(int32_t flags) { bool EmbedderEngine::DispatchSemanticsAction(int id, flutter::SemanticsAction action, - std::vector args) { + fml::MallocMapping args) { if (!IsValid()) { return false; } diff --git a/shell/platform/embedder/embedder_engine.h b/shell/platform/embedder/embedder_engine.h index a2b0e398fd599..30b0dd87a4155 100644 --- a/shell/platform/embedder/embedder_engine.h +++ b/shell/platform/embedder/embedder_engine.h @@ -82,7 +82,7 @@ class EmbedderEngine { bool DispatchSemanticsAction(int id, flutter::SemanticsAction action, - std::vector args); + fml::MallocMapping args); bool OnVsyncEvent(intptr_t baton, fml::TimePoint frame_start_time, diff --git a/shell/platform/fuchsia/flutter/fuchsia_intl.cc b/shell/platform/fuchsia/flutter/fuchsia_intl.cc index e1f4fa6238ce0..b3235385ce94d 100644 --- a/shell/platform/fuchsia/flutter/fuchsia_intl.cc +++ b/shell/platform/fuchsia/flutter/fuchsia_intl.cc @@ -27,7 +27,7 @@ namespace flutter_runner { using fuchsia::intl::Profile; -std::vector MakeLocalizationPlatformMessageData( +fml::MallocMapping MakeLocalizationPlatformMessageData( const Profile& intl_profile) { rapidjson::Document document; auto& allocator = document.GetAllocator(); @@ -66,7 +66,7 @@ std::vector MakeLocalizationPlatformMessageData( rapidjson::Writer writer(buffer); document.Accept(writer); auto data = reinterpret_cast(buffer.GetString()); - return std::vector(data, data + buffer.GetSize()); + return fml::MallocMapping::Copy(data, buffer.GetSize()); } } // namespace flutter_runner diff --git a/shell/platform/fuchsia/flutter/fuchsia_intl.h b/shell/platform/fuchsia/flutter/fuchsia_intl.h index 349d6c516cd49..c769af9fd531d 100644 --- a/shell/platform/fuchsia/flutter/fuchsia_intl.h +++ b/shell/platform/fuchsia/flutter/fuchsia_intl.h @@ -6,6 +6,7 @@ #define FLUTTER_SHELL_PLATFORM_FUCHSIA_FUCHSIA_INTL_H_ #include +#include "flutter/fml/mapping.h" namespace flutter_runner { @@ -15,7 +16,7 @@ namespace flutter_runner { // This method does not return a `std::unique_ptr` for // testing convenience; that would require an unreasonably large set of // dependencies for the unit tests. -std::vector MakeLocalizationPlatformMessageData( +fml::MallocMapping MakeLocalizationPlatformMessageData( const fuchsia::intl::Profile& intl_profile); } // namespace flutter_runner diff --git a/shell/platform/fuchsia/flutter/fuchsia_intl_unittest.cc b/shell/platform/fuchsia/flutter/fuchsia_intl_unittest.cc index 08874f5390ca1..dddca90303dad 100644 --- a/shell/platform/fuchsia/flutter/fuchsia_intl_unittest.cc +++ b/shell/platform/fuchsia/flutter/fuchsia_intl_unittest.cc @@ -34,7 +34,8 @@ TEST_F(FuchsiaIntlTest, MakeLocalizationPlatformMessageData_SimpleLocale) { const std::string expected = R"({"method":"setLocale","args":["en","US","",""]})"; const auto actual = MakeLocalizationPlatformMessageData(profile); - ASSERT_EQ(expected, std::string(actual.begin(), actual.end())); + ASSERT_EQ(expected, std::string(actual.GetMapping(), + actual.GetMapping() + actual.GetSize())); } TEST_F(FuchsiaIntlTest, MakeLocalizationPlatformMessageData_OneLocale) { @@ -48,7 +49,8 @@ TEST_F(FuchsiaIntlTest, MakeLocalizationPlatformMessageData_OneLocale) { const std::string expected = R"({"method":"setLocale","args":["en","US","",""]})"; const auto actual = MakeLocalizationPlatformMessageData(profile); - ASSERT_EQ(expected, std::string(actual.begin(), actual.end())); + ASSERT_EQ(expected, std::string(actual.GetMapping(), + actual.GetMapping() + actual.GetSize())); } TEST_F(FuchsiaIntlTest, MakeLocalizationPlatformMessageData_MultipleLocales) { @@ -65,7 +67,8 @@ TEST_F(FuchsiaIntlTest, MakeLocalizationPlatformMessageData_MultipleLocales) { R"({"method":"setLocale","args":["en","US","","","sl","IT","Latn","nedis",)" R"("zh","","Hans","","sr","CS","Cyrl",""]})"; const auto actual = MakeLocalizationPlatformMessageData(profile); - ASSERT_EQ(expected, std::string(actual.begin(), actual.end())); + ASSERT_EQ(expected, std::string(actual.GetMapping(), + actual.GetMapping() + actual.GetSize())); } } // namespace diff --git a/shell/platform/fuchsia/flutter/platform_view.cc b/shell/platform/fuchsia/flutter/platform_view.cc index 0f1831f310c57..29bd90e018dc2 100644 --- a/shell/platform/fuchsia/flutter/platform_view.cc +++ b/shell/platform/fuchsia/flutter/platform_view.cc @@ -177,9 +177,9 @@ void PlatformView::DidUpdateState( const uint8_t* data = reinterpret_cast(buffer.GetString()); DispatchPlatformMessage(std::make_unique( - kTextInputChannel, // channel - std::vector(data, data + buffer.GetSize()), // message - nullptr) // response + kTextInputChannel, // channel + fml::MallocMapping::Copy(data, buffer.GetSize()), // message + nullptr) // response ); last_text_state_ = std::make_unique(state); @@ -207,9 +207,9 @@ void PlatformView::OnAction(fuchsia::ui::input::InputMethodAction action) { const uint8_t* data = reinterpret_cast(buffer.GetString()); DispatchPlatformMessage(std::make_unique( - kTextInputChannel, // channel - std::vector(data, data + buffer.GetSize()), // message - nullptr) // response + kTextInputChannel, // channel + fml::MallocMapping::Copy(data, buffer.GetSize()), // message + nullptr) // response ); } @@ -446,7 +446,7 @@ bool PlatformView::OnChildViewConnected(scenic::ResourceId view_holder_id) { std::unique_ptr message = std::make_unique( "flutter/platform_views", - std::vector(call.begin(), call.end()), nullptr); + fml::MallocMapping::Copy(call.c_str(), call.size()), nullptr); DispatchPlatformMessage(std::move(message)); return true; @@ -470,7 +470,7 @@ bool PlatformView::OnChildViewDisconnected(scenic::ResourceId view_holder_id) { std::unique_ptr message = std::make_unique( "flutter/platform_views", - std::vector(call.begin(), call.end()), nullptr); + fml::MallocMapping::Copy(call.c_str(), call.size()), nullptr); DispatchPlatformMessage(std::move(message)); return true; @@ -497,7 +497,7 @@ bool PlatformView::OnChildViewStateChanged(scenic::ResourceId view_holder_id, std::unique_ptr message = std::make_unique( "flutter/platform_views", - std::vector(call.begin(), call.end()), nullptr); + fml::MallocMapping::Copy(call.c_str(), call.size()), nullptr); DispatchPlatformMessage(std::move(message)); return true; @@ -646,9 +646,9 @@ void PlatformView::OnKeyEvent( const uint8_t* data = reinterpret_cast(buffer.GetString()); DispatchPlatformMessage(std::make_unique( - kKeyEventChannel, // channel - std::vector(data, data + buffer.GetSize()), // data - nullptr) // response + kKeyEventChannel, // channel + fml::MallocMapping::Copy(data, buffer.GetSize()), // data + nullptr) // response ); callback(fuchsia::ui::input3::KeyEventStatus::HANDLED); } @@ -766,7 +766,8 @@ void PlatformView::HandleAccessibilityChannelPlatformMessage( const flutter::StandardMessageCodec& standard_message_codec = flutter::StandardMessageCodec::GetInstance(nullptr); std::unique_ptr decoded = - standard_message_codec.DecodeMessage(message->data()); + standard_message_codec.DecodeMessage(message->data().GetMapping(), + message->data().GetSize()); flutter::EncodableMap map = std::get(*decoded); std::string type = @@ -789,7 +790,8 @@ void PlatformView::HandleFlutterPlatformChannelPlatformMessage( FML_DCHECK(message->channel() == kFlutterPlatformChannel); const auto& data = message->data(); rapidjson::Document document; - document.Parse(reinterpret_cast(data.data()), data.size()); + document.Parse(reinterpret_cast(data.GetMapping()), + data.GetSize()); if (document.HasParseError() || !document.IsObject()) { return; } @@ -810,7 +812,8 @@ void PlatformView::HandleFlutterTextInputChannelPlatformMessage( FML_DCHECK(message->channel() == kTextInputChannel); const auto& data = message->data(); rapidjson::Document document; - document.Parse(reinterpret_cast(data.data()), data.size()); + document.Parse(reinterpret_cast(data.GetMapping()), + data.GetSize()); if (document.HasParseError() || !document.IsObject()) { return; } @@ -900,7 +903,8 @@ void PlatformView::HandleFlutterPlatformViewsChannelPlatformMessage( FML_DCHECK(message->channel() == kFlutterPlatformViewsChannel); const auto& data = message->data(); rapidjson::Document document; - document.Parse(reinterpret_cast(data.data()), data.size()); + document.Parse(reinterpret_cast(data.GetMapping()), + data.GetSize()); if (document.HasParseError() || !document.IsObject()) { FML_LOG(ERROR) << "Could not parse document"; return; diff --git a/shell/platform/fuchsia/flutter/platform_view_unittest.cc b/shell/platform/fuchsia/flutter/platform_view_unittest.cc index fdb5ec0b46039..ffc470ff6df52 100644 --- a/shell/platform/fuchsia/flutter/platform_view_unittest.cc +++ b/shell/platform/fuchsia/flutter/platform_view_unittest.cc @@ -31,6 +31,11 @@ namespace flutter_runner::testing { namespace { +std::string ToString(const fml::Mapping& mapping) { + return std::string(mapping.GetMapping(), + mapping.GetMapping() + mapping.GetSize()); +} + class MockExternalViewEmbedder : public flutter::ExternalViewEmbedder { public: SkCanvas* GetRootCanvas() override { return nullptr; } @@ -96,7 +101,7 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate { // |flutter::PlatformView::Delegate| void OnPlatformViewDispatchSemanticsAction(int32_t id, flutter::SemanticsAction action, - std::vector args) {} + fml::MallocMapping args) {} // |flutter::PlatformView::Delegate| void OnPlatformViewSetSemanticsEnabled(bool enabled) { semantics_enabled_ = enabled; @@ -570,8 +575,7 @@ TEST_F(PlatformViewTests, EnableWireframeTest) { std::unique_ptr message = std::make_unique( - "flutter/platform_views", - std::vector(txt, txt + sizeof(txt)), + "flutter/platform_views", fml::MallocMapping::Copy(txt, sizeof(txt)), fml::RefPtr()); base_view->HandlePlatformMessage(std::move(message)); @@ -630,8 +634,7 @@ TEST_F(PlatformViewTests, CreateViewTest) { std::unique_ptr message = std::make_unique( - "flutter/platform_views", - std::vector(txt, txt + sizeof(txt)), + "flutter/platform_views", fml::MallocMapping::Copy(txt, sizeof(txt)), fml::RefPtr()); base_view->HandlePlatformMessage(std::move(message)); @@ -681,8 +684,7 @@ TEST_F(PlatformViewTests, UpdateViewTest) { std::unique_ptr message = std::make_unique( - "flutter/platform_views", - std::vector(txt, txt + sizeof(txt)), + "flutter/platform_views", fml::MallocMapping::Copy(txt, sizeof(txt)), fml::RefPtr()); base_view->HandlePlatformMessage(std::move(message)); @@ -738,8 +740,7 @@ TEST_F(PlatformViewTests, DestroyViewTest) { std::unique_ptr message = std::make_unique( - "flutter/platform_views", - std::vector(txt, txt + sizeof(txt)), + "flutter/platform_views", fml::MallocMapping::Copy(txt, sizeof(txt)), fml::RefPtr()); base_view->HandlePlatformMessage(std::move(message)); @@ -800,8 +801,8 @@ TEST_F(PlatformViewTests, ViewEventsTest) { static_cast(&platform_view) ->HandlePlatformMessage(std::make_unique( "flutter/platform_views", - std::vector(create_view_call.begin(), - create_view_call.end()), + fml::MallocMapping::Copy(create_view_call.c_str(), + create_view_call.size()), fml::RefPtr())); RunLoopUntilIdle(); @@ -827,8 +828,7 @@ TEST_F(PlatformViewTests, ViewEventsTest) { << " }" << "}"; EXPECT_EQ(view_connected_expected_out.str(), - std::string(view_connected_msg->data().begin(), - view_connected_msg->data().end())); + ToString(view_connected_msg->data())); // ViewDisconnected event. delegate.Reset(); @@ -852,8 +852,7 @@ TEST_F(PlatformViewTests, ViewEventsTest) { << " }" << "}"; EXPECT_EQ(view_disconnected_expected_out.str(), - std::string(view_disconnected_msg->data().begin(), - view_disconnected_msg->data().end())); + ToString(view_disconnected_msg->data())); // ViewStateChanged event. delegate.Reset(); @@ -883,8 +882,7 @@ TEST_F(PlatformViewTests, ViewEventsTest) { << " }" << "}"; EXPECT_EQ(view_state_changed_expected_out.str(), - std::string(view_state_changed_msg->data().begin(), - view_state_changed_msg->data().end())); + ToString(view_state_changed_msg->data())); } // This test makes sure that the PlatformView forwards messages on the @@ -939,14 +937,13 @@ TEST_F(PlatformViewTests, RequestFocusTest) { std::unique_ptr message = std::make_unique( "flutter/platform_views", - std::vector(buff, buff + sizeof(buff)), response); + fml::MallocMapping::Copy(buff, sizeof(buff)), response); base_view->HandlePlatformMessage(std::move(message)); RunLoopUntilIdle(); EXPECT_TRUE(mock_focuser.request_focus_called()); - auto result = std::string((const char*)data_arg.data->GetMapping(), - data_arg.data->GetSize()); + auto result = ToString(*data_arg.data); EXPECT_EQ(std::string("[0]"), result); } @@ -1002,14 +999,13 @@ TEST_F(PlatformViewTests, RequestFocusFailTest) { std::unique_ptr message = std::make_unique( "flutter/platform_views", - std::vector(buff, buff + sizeof(buff)), response); + fml::MallocMapping::Copy(buff, sizeof(buff)), response); base_view->HandlePlatformMessage(std::move(message)); RunLoopUntilIdle(); EXPECT_TRUE(mock_focuser.request_focus_called()); - auto result = std::string((const char*)data_arg.data->GetMapping(), - data_arg.data->GetSize()); + auto result = ToString(*data_arg.data); std::ostringstream out; out << "[" << static_cast>( @@ -1094,8 +1090,8 @@ TEST_F(PlatformViewTests, OnKeyEvent) { key_event_status = status; }); RunLoopUntilIdle(); - const std::vector data = delegate.message()->data(); - const std::string message = std::string(data.begin(), data.end()); + const fml::MallocMapping data = delegate.message()->releaseData(); + const std::string message = ToString(data); EXPECT_EQ(event.expected_platform_message, message); EXPECT_EQ(key_event_status, event.expected_key_event_status); diff --git a/shell/testing/tester_main.cc b/shell/testing/tester_main.cc index bd751ca8ed46c..b96166c5faee6 100644 --- a/shell/testing/tester_main.cc +++ b/shell/testing/tester_main.cc @@ -254,12 +254,12 @@ int RunTester(const flutter::Settings& settings, const char* locale_json = "{\"method\":\"setLocale\",\"args\":[\"en\",\"US\",\"\",\"\",\"zh\"," "\"CN\",\"\",\"\"]}"; - std::vector locale_bytes(locale_json, - locale_json + std::strlen(locale_json)); + auto locale_bytes = fml::MallocMapping::Copy( + locale_json, locale_json + std::strlen(locale_json)); fml::RefPtr response; shell->GetPlatformView()->DispatchPlatformMessage( - std::make_unique("flutter/localization", - locale_bytes, response)); + std::make_unique( + "flutter/localization", std::move(locale_bytes), response)); std::initializer_list protection = { fml::FileMapping::Protection::kRead};