From 9f04e2a7b24e6830304605e0f9346aff06afd75c Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 12 Oct 2016 12:52:11 -0700 Subject: [PATCH] Finish routing platform messages on iOS (flutter/engine#3119) --- .../Source/application_messages_impl.h | 7 +++- .../Source/application_messages_impl.mm | 37 +++++++++++++++++++ .../platform/darwin/ios/platform_view_ios.h | 7 +++- .../platform/darwin/ios/platform_view_ios.mm | 20 +++++++--- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.h index d473ce3ca5..81bbe9bdda 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.h @@ -7,11 +7,12 @@ #include -#include "lib/ftl/memory/weak_ptr.h" -#include "mojo/public/cpp/bindings/binding_set.h" +#include "flutter/lib/ui/window/platform_message.h" #include "flutter/services/platform/app_messages.mojom.h" #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterAsyncMessageListener.h" #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterMessageListener.h" +#include "lib/ftl/memory/weak_ptr.h" +#include "mojo/public/cpp/bindings/binding_set.h" namespace shell { @@ -24,6 +25,8 @@ class ApplicationMessagesImpl : public flutter::platform::ApplicationMessages { void AddBinding( mojo::InterfaceRequest request); + void HandlePlatformMessage(ftl::RefPtr message); + void SetMessageListener(const std::string& message_name, NSObject* listener); diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.mm index b6d4de6832..d14b1fe8ac 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.mm @@ -4,9 +4,21 @@ #include "flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.h" +#include + #include "base/strings/sys_string_conversions.h" namespace shell { +namespace { + +std::vector SysNSStringToVector(NSString* string) { + if (!string.length) + return std::vector(); + const char* utf8 = string.UTF8String; + return std::vector(utf8, utf8 + strlen(utf8)); +} + +} // namespace ApplicationMessagesImpl::ApplicationMessagesImpl() : weak_factory_(this) {} @@ -21,6 +33,31 @@ void ApplicationMessagesImpl::AddBinding( binding_.AddBinding(this, request.Pass()); } +void ApplicationMessagesImpl::HandlePlatformMessage( + ftl::RefPtr message) { + NSString* string = [NSString stringWithUTF8String:message->data().data()]; + + { + auto it = listeners_.find(message->name()); + if (it != listeners_.end()) { + NSString* response = [it->second didReceiveString:string]; + message->InvokeCallback(SysNSStringToVector(response)); + return; + } + } + + { + auto it = async_listeners_.find(message->name()); + if (it != async_listeners_.end()) { + [it->second + didReceiveString:string + callback:^(NSString* response) { + message->InvokeCallback(SysNSStringToVector(response)); + }]; + } + } +} + void ApplicationMessagesImpl::SetMessageListener( const std::string& message_name, NSObject* listener) { diff --git a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.h b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.h index 862ccb73a7..476c6325a3 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.h +++ b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.h @@ -49,12 +49,15 @@ class PlatformViewIOS : public PlatformView, public GPUSurfaceGLDelegate { intptr_t GLContextFBO() const override; + void HandlePlatformMessage( + ftl::RefPtr message) override; + + void UpdateSemantics(std::vector update) override; + void RunFromSource(const std::string& main, const std::string& packages, const std::string& assets_directory) override; - void UpdateSemantics(std::vector update) override; - private: std::unique_ptr context_; sky::SkyEnginePtr engine_; diff --git a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm index 8b0cae2371..e1f6de8f3c 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm @@ -8,6 +8,9 @@ #import #import #import + +#include + #include "base/mac/scoped_nsautorelease_pool.h" #include "base/trace_event/trace_event.h" #include "flutter/shell/gpu/gpu_rasterizer.h" @@ -369,6 +372,17 @@ bool PlatformViewIOS::GLContextPresent() { return context_ != nullptr ? context_->PresentRenderBuffer() : false; } +void PlatformViewIOS::UpdateSemantics( + std::vector update) { + if (accessibility_bridge_) + accessibility_bridge_->UpdateSemantics(std::move(update)); +} + +void PlatformViewIOS::HandlePlatformMessage( + ftl::RefPtr message) { + app_message_receiver_.HandlePlatformMessage(std::move(message)); +} + void PlatformViewIOS::RunFromSource(const std::string& main, const std::string& packages, const std::string& assets_directory) { @@ -383,10 +397,4 @@ void PlatformViewIOS::RunFromSource(const std::string& main, delete latch; } -void PlatformViewIOS::UpdateSemantics( - std::vector update) { - if (accessibility_bridge_) - accessibility_bridge_->UpdateSemantics(std::move(update)); -} - } // namespace shell