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 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
override

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)
override

Definition at line 44 of file platform_message_handler_ios.mm.

44  {
45  // This can be called from any isolate's thread.
46  @autoreleasepool {
47  fml::RefPtr<flutter::PlatformMessageResponse> completer = message->response();
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 }

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 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 
)
override

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 }

◆ 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 =
130  fml::ScopedBlock<FlutterBinaryMessageHandler>{
131  handler, fml::scoped_policy::OwnershipPolicy::kRetain},
132  };
133  }
134 }

The documentation for this class was generated from the following files:
flutter::ConvertMappingToNSData
NSData * ConvertMappingToNSData(fml::MallocMapping buffer)
Definition: buffer_conversions.mm:35
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
platform_message_counter
static FLUTTER_ASSERT_ARC uint64_t platform_message_counter
Definition: platform_message_handler_ios.mm:14
FLTSerialTaskQueue
Definition: platform_message_handler_ios.mm:16