Flutter Engine
The Flutter Engine
Classes | Public Member Functions | Static Public Member Functions | List of all members
flutter::PlatformMessageHandlerIos Class Reference

#include <platform_message_handler_ios.h>

Inheritance diagram for flutter::PlatformMessageHandlerIos:
flutter::PlatformMessageHandler

Classes

struct  HandlerInfo
 

Public Member Functions

 PlatformMessageHandlerIos (fml::RefPtr< fml::TaskRunner > platform_task_runner)
 
void HandlePlatformMessage (std::unique_ptr< PlatformMessage > message) override
 
bool DoesHandlePlatformMessageOnPlatformThread () const override
 
void InvokePlatformMessageResponseCallback (int response_id, std::unique_ptr< fml::Mapping > mapping) override
 
void InvokePlatformMessageEmptyResponseCallback (int response_id) override
 
void SetMessageHandler (const std::string &channel, FlutterBinaryMessageHandler handler, NSObject< FlutterTaskQueue > *task_queue)
 
- Public Member Functions inherited from flutter::PlatformMessageHandler
virtual ~PlatformMessageHandler ()=default
 
virtual void HandlePlatformMessage (std::unique_ptr< PlatformMessage > message)=0
 
virtual bool DoesHandlePlatformMessageOnPlatformThread () const =0
 
virtual void InvokePlatformMessageResponseCallback (int response_id, std::unique_ptr< fml::Mapping > mapping)=0
 
virtual void InvokePlatformMessageEmptyResponseCallback (int response_id)=0
 

Static Public Member Functions

static NSObject< FlutterTaskQueue > * MakeBackgroundTaskQueue ()
 

Detailed Description

Definition at line 16 of file platform_message_handler_ios.h.

Constructor & Destructor Documentation

◆ PlatformMessageHandlerIos()

flutter::PlatformMessageHandlerIos::PlatformMessageHandlerIos ( fml::RefPtr< fml::TaskRunner platform_task_runner)
explicit

Definition at line 40 of file platform_message_handler_ios.mm.

42 : platform_task_runner_(std::move(platform_task_runner)) {}

Member Function Documentation

◆ DoesHandlePlatformMessageOnPlatformThread()

bool flutter::PlatformMessageHandlerIos::DoesHandlePlatformMessageOnPlatformThread ( ) const
overridevirtual

Returns true if the platform message will ALWAYS be dispatched to the platform thread.

Platforms that support Background Platform Channels will return false.

Implements flutter::PlatformMessageHandler.

Definition at line 96 of file platform_message_handler_ios.mm.

96 {
97 return false;
98}

◆ HandlePlatformMessage()

void flutter::PlatformMessageHandlerIos::HandlePlatformMessage ( std::unique_ptr< PlatformMessage message)
overridevirtual

Ultimately sends the PlatformMessage to the host platform. This method is invoked on the ui thread.

Implements flutter::PlatformMessageHandler.

Definition at line 44 of file platform_message_handler_ios.mm.

44 {
45 // This can be called from any isolate's thread.
46 @autoreleasepool {
48 HandlerInfo handler_info;
49 {
50 // TODO(gaaclarke): This mutex is a bottleneck for multiple isolates sending
51 // messages at the same time. This could be potentially changed to a
52 // read-write lock.
53 std::lock_guard lock(message_handlers_mutex_);
54 auto it = message_handlers_.find(message->channel());
55 if (it != message_handlers_.end()) {
56 handler_info = it->second;
57 }
58 }
59 if (handler_info.handler) {
60 FlutterBinaryMessageHandler handler = handler_info.handler;
61 NSData* data = nil;
62 if (message->hasData()) {
63 data = ConvertMappingToNSData(message->releaseData());
64 }
65
66 uint64_t platform_message_id = platform_message_counter++;
67 TRACE_EVENT_ASYNC_BEGIN1("flutter", "PlatformChannel ScheduleHandler", platform_message_id,
68 "channel", message->channel().c_str());
69 dispatch_block_t run_handler = ^{
70 handler(data, ^(NSData* reply) {
71 TRACE_EVENT_ASYNC_END0("flutter", "PlatformChannel ScheduleHandler", platform_message_id);
72 // Called from any thread.
73 if (completer) {
74 if (reply) {
75 completer->Complete(ConvertNSDataToMappingPtr(reply));
76 } else {
77 completer->CompleteEmpty();
78 }
79 }
80 });
81 };
82
83 if (handler_info.task_queue.get()) {
84 [handler_info.task_queue.get() dispatch:run_handler];
85 } else {
86 dispatch_async(dispatch_get_main_queue(), run_handler);
87 }
88 } else {
89 if (completer) {
90 completer->CompleteEmpty();
91 }
92 }
93 }
94}
void(^ FlutterBinaryMessageHandler)(NSData *_Nullable message, FlutterBinaryReply reply)
virtual void Complete(std::unique_ptr< fml::Mapping > data)=0
Win32Message message
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
std::unique_ptr< fml::Mapping > ConvertNSDataToMappingPtr(NSData *data)
NSData * ConvertMappingToNSData(fml::MallocMapping buffer)
static FLUTTER_ASSERT_ARC uint64_t platform_message_counter
#define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, arg1_val)
Definition: trace_event.h:164
#define TRACE_EVENT_ASYNC_END0(category_group, name, id)
Definition: trace_event.h:161

◆ InvokePlatformMessageEmptyResponseCallback()

void flutter::PlatformMessageHandlerIos::InvokePlatformMessageEmptyResponseCallback ( int  response_id)
overridevirtual

Performs the return procedure for an associated call to HandlePlatformMessage where there is no return value. This method should be thread-safe and able to be invoked on any thread.

Implements flutter::PlatformMessageHandler.

Definition at line 108 of file platform_message_handler_ios.mm.

108 {
109 // Called from any thread.
110 // TODO(gaaclarke): This vestigal from the Android implementation, find a way
111 // to migrate this to PlatformMessageHandlerAndroid.
112}

◆ InvokePlatformMessageResponseCallback()

void flutter::PlatformMessageHandlerIos::InvokePlatformMessageResponseCallback ( int  response_id,
std::unique_ptr< fml::Mapping mapping 
)
overridevirtual

Performs the return procedure for an associated call to HandlePlatformMessage. This method should be thread-safe and able to be invoked on any thread.

Implements flutter::PlatformMessageHandler.

Definition at line 100 of file platform_message_handler_ios.mm.

102 {
103 // Called from any thread.
104 // TODO(gaaclarke): This vestigal from the Android implementation, find a way
105 // to migrate this to PlatformMessageHandlerAndroid.
106}

◆ MakeBackgroundTaskQueue()

NSObject< FlutterTaskQueue > * flutter::PlatformMessageHandlerIos::MakeBackgroundTaskQueue ( )
static

Definition at line 36 of file platform_message_handler_ios.mm.

36 {
37 return [[FLTSerialTaskQueue alloc] init];
38}
static bool init()

◆ SetMessageHandler()

void flutter::PlatformMessageHandlerIos::SetMessageHandler ( const std::string &  channel,
FlutterBinaryMessageHandler  handler,
NSObject< FlutterTaskQueue > *  task_queue 
)

TODO(gaaclarke): This should be migrated to a lockfree datastructure.

Definition at line 114 of file platform_message_handler_ios.mm.

116 {
117 FML_CHECK(platform_task_runner_->RunsTasksOnCurrentThread());
118 // Use `respondsToSelector` instead of `conformsToProtocol` to accomodate
119 // injecting your own `FlutterTaskQueue`. This is not a supported usage but
120 // not one worth breaking.
121 FML_CHECK(!task_queue || [task_queue respondsToSelector:@selector(dispatch:)]);
122 /// TODO(gaaclarke): This should be migrated to a lockfree datastructure.
123 std::lock_guard lock(message_handlers_mutex_);
124 message_handlers_.erase(channel);
125 if (handler) {
126 message_handlers_[channel] = {
127 .task_queue =
128 fml::scoped_nsprotocol(static_cast<NSObject<FlutterTaskQueueDispatch>*>(task_queue)),
129 .handler =
132 };
133 }
134}
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43
#define FML_CHECK(condition)
Definition: logging.h:85

The documentation for this class was generated from the following files: