Flutter iOS Embedder
flutter::PlatformMessageHandlerIos Class Reference

#include <platform_message_handler_ios.h>

Inheritance diagram for flutter::PlatformMessageHandlerIos:

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)
 

Static Public Member Functions

static NSObject< FlutterTaskQueue > * MakeBackgroundTaskQueue ()
 

Detailed Description

Definition at line 19 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 43 of file platform_message_handler_ios.mm.

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

Member Function Documentation

◆ DoesHandlePlatformMessageOnPlatformThread()

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

Definition at line 99 of file platform_message_handler_ios.mm.

99  {
100  return false;
101 }

◆ HandlePlatformMessage()

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

Definition at line 47 of file platform_message_handler_ios.mm.

47  {
48  // This can be called from any isolate's thread.
49  @autoreleasepool {
50  fml::RefPtr<flutter::PlatformMessageResponse> completer = message->response();
51  HandlerInfo handler_info;
52  {
53  // TODO(gaaclarke): This mutex is a bottleneck for multiple isolates sending
54  // messages at the same time. This could be potentially changed to a
55  // read-write lock.
56  std::lock_guard lock(message_handlers_mutex_);
57  auto it = message_handlers_.find(message->channel());
58  if (it != message_handlers_.end()) {
59  handler_info = it->second;
60  }
61  }
62  if (handler_info.handler) {
63  FlutterBinaryMessageHandler handler = handler_info.handler;
64  NSData* data = nil;
65  if (message->hasData()) {
66  data = ConvertMappingToNSData(message->releaseData());
67  }
68 
69  uint64_t platform_message_id = platform_message_counter++;
70  TRACE_EVENT_ASYNC_BEGIN1("flutter", "PlatformChannel ScheduleHandler", platform_message_id,
71  "channel", message->channel().c_str());
72  dispatch_block_t run_handler = ^{
73  handler(data, ^(NSData* reply) {
74  TRACE_EVENT_ASYNC_END0("flutter", "PlatformChannel ScheduleHandler", platform_message_id);
75  // Called from any thread.
76  if (completer) {
77  if (reply) {
78  completer->Complete(ConvertNSDataToMappingPtr(reply));
79  } else {
80  completer->CompleteEmpty();
81  }
82  }
83  });
84  };
85 
86  if (handler_info.task_queue.get()) {
87  [handler_info.task_queue.get() dispatch:run_handler];
88  } else {
89  dispatch_async(dispatch_get_main_queue(), run_handler);
90  }
91  } else {
92  if (completer) {
93  completer->CompleteEmpty();
94  }
95  }
96  }
97 }

References flutter::ConvertMappingToNSData(), flutter::ConvertNSDataToMappingPtr(), flutter::PlatformMessageHandlerIos::HandlerInfo::handler, platform_message_counter, and flutter::PlatformMessageHandlerIos::HandlerInfo::task_queue.

◆ InvokePlatformMessageEmptyResponseCallback()

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

Definition at line 111 of file platform_message_handler_ios.mm.

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

◆ InvokePlatformMessageResponseCallback()

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

Definition at line 103 of file platform_message_handler_ios.mm.

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

◆ MakeBackgroundTaskQueue()

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

Definition at line 39 of file platform_message_handler_ios.mm.

39  {
40  return [[[FLTSerialTaskQueue alloc] init] autorelease];
41 }

◆ 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 117 of file platform_message_handler_ios.mm.

119  {
120  FML_CHECK(platform_task_runner_->RunsTasksOnCurrentThread());
121  // Use `respondsToSelector` instead of `conformsToProtocol` to accomodate
122  // injecting your own `FlutterTaskQueue`. This is not a supported usage but
123  // not one worth breaking.
124  FML_CHECK(!task_queue || [task_queue respondsToSelector:@selector(dispatch:)]);
125  /// TODO(gaaclarke): This should be migrated to a lockfree datastructure.
126  std::lock_guard lock(message_handlers_mutex_);
127  message_handlers_.erase(channel);
128  if (handler) {
129  message_handlers_[channel] = {
130  .task_queue = fml::scoped_nsprotocol(
131  [static_cast<NSObject<FlutterTaskQueueDispatch>*>(task_queue) retain]),
132  .handler =
133  fml::ScopedBlock<FlutterBinaryMessageHandler>{
134  handler, fml::scoped_policy::OwnershipPolicy::kRetain},
135  };
136  }
137 }

The documentation for this class was generated from the following files:
flutter::ConvertMappingToNSData
NSData * ConvertMappingToNSData(fml::MallocMapping buffer)
Definition: buffer_conversions.mm:35
platform_message_counter
static uint64_t platform_message_counter
Definition: platform_message_handler_ios.mm:12
FlutterBinaryMessageHandler
void(^ FlutterBinaryMessageHandler)(NSData *_Nullable message, FlutterBinaryReply reply)
Definition: FlutterBinaryMessenger.h:30
flutter::ConvertNSDataToMappingPtr
std::unique_ptr< fml::Mapping > ConvertNSDataToMappingPtr(NSData *data)
Definition: buffer_conversions.mm:40
FLTSerialTaskQueue
Definition: platform_message_handler_ios.mm:14