Flutter iOS Embedder
flutter::PlatformViewIOS Class Referencefinal

#include <platform_view_ios.h>

Inheritance diagram for flutter::PlatformViewIOS:

Public Member Functions

 PlatformViewIOS (PlatformView::Delegate &delegate, const std::shared_ptr< IOSContext > &context, __weak FlutterPlatformViewsController *platform_views_controller, const flutter::TaskRunners &task_runners)
 
 PlatformViewIOS (PlatformView::Delegate &delegate, __weak FlutterPlatformViewsController *platform_views_controller, const flutter::TaskRunners &task_runners, const std::shared_ptr< const fml::SyncSwitch > &is_gpu_disabled_sync_switch)
 
 ~PlatformViewIOS () override
 
FlutterViewController * GetOwnerViewController () const __attribute__((cf_audited_transfer))
 
void SetOwnerViewController (__weak FlutterViewController *owner_controller)
 
void attachView (FlutterView *previousView=nil)
 
void RegisterExternalTexture (int64_t id, NSObject< FlutterTexture > *texture)
 
void SetSemanticsTreeEnabled (bool enabled) override
 
void HandlePlatformMessage (std::unique_ptr< flutter::PlatformMessage > message) override
 
std::unique_ptr< Surface > CreateRenderingSurface () override
 
std::shared_ptr< ExternalViewEmbedder > CreateExternalViewEmbedder () override
 
std::shared_ptr< impeller::Context > GetImpellerContext () const override
 
void UpdateSemantics (int64_t view_id, flutter::SemanticsNodeUpdates update, flutter::CustomAccessibilityActionUpdates actions) override
 
void SetApplicationLocale (std::string locale) override
 
std::unique_ptr< VsyncWaiter > CreateVSyncWaiter () override
 
void OnPreEngineRestart () const override
 
std::unique_ptr< std::vector< std::string > > ComputePlatformResolvedLocales (const std::vector< std::string > &supported_locale_data) override
 
const std::shared_ptr< IOSContext > & GetIosContext ()
 
std::shared_ptr< PlatformMessageHandlerIos > GetPlatformMessageHandlerIos () const
 
std::shared_ptr< PlatformMessageHandler > GetPlatformMessageHandler () const override
 
AccessibilityBridge * GetAccessibilityBridge ()
 

Detailed Description

A bridge connecting the platform agnostic shell and the iOS embedding.

The shell provides and requests for UI related data and this PlatformView subclass fulfills it with iOS specific capabilities. As an example, the iOS embedding (the FlutterEngine and the FlutterViewController) sends pointer data to the shell and receives the shell's request for a Impeller AiksContext and supplies it.

Despite the name "view", this class is unrelated to UIViews on iOS and doesn't have the same lifecycle. It's a long lived bridge owned by the FlutterEngine and can be attached and detached sequentially to multiple FlutterViewControllers and FlutterViews.

Definition at line 39 of file platform_view_ios.h.

Constructor & Destructor Documentation

◆ PlatformViewIOS() [1/2]

flutter::PlatformViewIOS::PlatformViewIOS ( PlatformView::Delegate &  delegate,
const std::shared_ptr< IOSContext > &  context,
__weak FlutterPlatformViewsController *  platform_views_controller,
const flutter::TaskRunners &  task_runners 
)

Definition at line 24 of file platform_view_ios.mm.

28  : PlatformView(delegate, task_runners),
29  ios_context_(context),
30  platform_views_controller_(platform_views_controller),
31  platform_message_handler_(
32  new PlatformMessageHandlerIos(task_runners.GetPlatformTaskRunner())) {}

◆ PlatformViewIOS() [2/2]

flutter::PlatformViewIOS::PlatformViewIOS ( PlatformView::Delegate &  delegate,
__weak FlutterPlatformViewsController *  platform_views_controller,
const flutter::TaskRunners &  task_runners,
const std::shared_ptr< const fml::SyncSwitch > &  is_gpu_disabled_sync_switch 
)
explicit

Definition at line 34 of file platform_view_ios.mm.

40  delegate,
41  IOSContext::Create(is_gpu_disabled_sync_switch, delegate.OnPlatformViewGetSettings()),
42  platform_views_controller,
43  task_runners) {}
static std::unique_ptr< IOSContext > Create(const std::shared_ptr< const fml::SyncSwitch > &is_gpu_disabled_sync_switch, const Settings &settings)
Create an iOS context object capable of creating the on-screen and off-screen GPU context for use by ...
Definition: ios_context.mm:21
PlatformViewIOS(PlatformView::Delegate &delegate, const std::shared_ptr< IOSContext > &context, __weak FlutterPlatformViewsController *platform_views_controller, const flutter::TaskRunners &task_runners)

◆ ~PlatformViewIOS()

flutter::PlatformViewIOS::~PlatformViewIOS ( )
overridedefault

Member Function Documentation

◆ attachView()

void flutter::PlatformViewIOS::attachView ( FlutterView *  previousView = nil)

Called one time per FlutterViewController when the FlutterViewController's UIView is first loaded.

Can be used to perform late initialization after FlutterViewController's init.

The previousView is the Flutter view that was attached before this one, if any.

Definition at line 98 of file platform_view_ios.mm.

98  {
99  FML_DCHECK(owner_controller_);
100  FML_DCHECK(owner_controller_.isViewLoaded) << "FlutterViewController's view should be loaded "
101  "before attaching to PlatformViewIOS.";
102  FlutterView* flutter_view = static_cast<FlutterView*>(owner_controller_.view);
103  CALayer* ca_layer = flutter_view.layer;
104  ios_surface_ = IOSSurface::Create(ios_context_, ca_layer);
105  FML_DCHECK(ios_surface_ != nullptr);
106 
107  EnsureAccessibilityBridge();
108  if (accessibility_bridge_) {
109  accessibility_bridge_->SetViewController(owner_controller_, previousView);
110  if (accessibility_bridge_->HasSemantics()) {
111  PostSemanticsUpdateNotification();
112  }
113  }
114 }
static std::unique_ptr< IOSSurface > Create(const std::shared_ptr< IOSContext > &context, CALayer *layer)
Definition: ios_surface.mm:18

References flutter::IOSSurface::Create().

Referenced by SetOwnerViewController().

◆ ComputePlatformResolvedLocales()

std::unique_ptr< std::vector< std::string > > flutter::PlatformViewIOS::ComputePlatformResolvedLocales ( const std::vector< std::string > &  supported_locale_data)
override

Definition at line 197 of file platform_view_ios.mm.

198  {
199  size_t localeDataLength = 3;
200  NSMutableArray<NSString*>* supported_locale_identifiers =
201  [NSMutableArray arrayWithCapacity:supported_locale_data.size() / localeDataLength];
202  for (size_t i = 0; i < supported_locale_data.size(); i += localeDataLength) {
203  NSDictionary<NSString*, NSString*>* dict = @{
204  NSLocaleLanguageCode : [NSString stringWithUTF8String:supported_locale_data[i].c_str()]
205  ?: @"",
206  NSLocaleCountryCode : [NSString stringWithUTF8String:supported_locale_data[i + 1].c_str()]
207  ?: @"",
208  NSLocaleScriptCode : [NSString stringWithUTF8String:supported_locale_data[i + 2].c_str()]
209  ?: @""
210  };
211  [supported_locale_identifiers addObject:[NSLocale localeIdentifierFromComponents:dict]];
212  }
213  NSArray<NSString*>* result =
214  [NSBundle preferredLocalizationsFromArray:supported_locale_identifiers];
215 
216  // Output format should be either empty or 3 strings for language, country, and script.
217  std::unique_ptr<std::vector<std::string>> out = std::make_unique<std::vector<std::string>>();
218 
219  if (result != nullptr && [result count] > 0) {
220  NSLocale* locale = [NSLocale localeWithLocaleIdentifier:[result firstObject]];
221  NSString* languageCode = [locale languageCode];
222  out->emplace_back(languageCode == nullptr ? "" : languageCode.UTF8String);
223  NSString* countryCode = [locale countryCode];
224  out->emplace_back(countryCode == nullptr ? "" : countryCode.UTF8String);
225  NSString* scriptCode = [locale scriptCode];
226  out->emplace_back(scriptCode == nullptr ? "" : scriptCode.UTF8String);
227  }
228  return out;
229 }

◆ CreateExternalViewEmbedder()

std::shared_ptr< ExternalViewEmbedder > flutter::PlatformViewIOS::CreateExternalViewEmbedder ( )
override

Definition at line 134 of file platform_view_ios.mm.

134  {
135  return std::make_shared<IOSExternalViewEmbedder>(platform_views_controller_, ios_context_);
136 }

◆ CreateRenderingSurface()

std::unique_ptr< Surface > flutter::PlatformViewIOS::CreateRenderingSurface ( )
override

Definition at line 122 of file platform_view_ios.mm.

122  {
123  FML_DCHECK(task_runners_.GetRasterTaskRunner()->RunsTasksOnCurrentThread());
124  std::lock_guard<std::mutex> guard(ios_surface_mutex_);
125  if (!ios_surface_) {
126  FML_DLOG(INFO) << "Could not CreateRenderingSurface, this PlatformViewIOS "
127  "has no ViewController.";
128  return nullptr;
129  }
130  return ios_surface_->CreateGPUSurface();
131 }

◆ CreateVSyncWaiter()

std::unique_ptr< VsyncWaiter > flutter::PlatformViewIOS::CreateVSyncWaiter ( )
override

Definition at line 179 of file platform_view_ios.mm.

179  {
180  return std::make_unique<VsyncWaiterIOS>(task_runners_, FlutterDisplayLinkManager.shared);
181 }

◆ GetAccessibilityBridge()

AccessibilityBridge* flutter::PlatformViewIOS::GetAccessibilityBridge ( )
inline

Gets the accessibility bridge created in this platform view.

Definition at line 131 of file platform_view_ios.h.

131 { return accessibility_bridge_.get(); }

◆ GetImpellerContext()

std::shared_ptr< impeller::Context > flutter::PlatformViewIOS::GetImpellerContext ( ) const
override

Definition at line 139 of file platform_view_ios.mm.

139  {
140  return ios_context_->GetImpellerContext();
141 }

◆ GetIosContext()

const std::shared_ptr<IOSContext>& flutter::PlatformViewIOS::GetIosContext ( )
inline

Accessor for the IOSContext associated with the platform view.

Definition at line 118 of file platform_view_ios.h.

118 { return ios_context_; }

◆ GetOwnerViewController()

FlutterViewController * flutter::PlatformViewIOS::GetOwnerViewController ( ) const

Returns the FlutterViewController currently attached to the FlutterEngine owning this PlatformViewIOS.

Definition at line 52 of file platform_view_ios.mm.

52  {
53  return owner_controller_;
54 }

◆ GetPlatformMessageHandler()

std::shared_ptr<PlatformMessageHandler> flutter::PlatformViewIOS::GetPlatformMessageHandler ( ) const
inlineoverride

Definition at line 124 of file platform_view_ios.h.

124  {
125  return platform_message_handler_;
126  }

◆ GetPlatformMessageHandlerIos()

std::shared_ptr<PlatformMessageHandlerIos> flutter::PlatformViewIOS::GetPlatformMessageHandlerIos ( ) const
inline

Definition at line 120 of file platform_view_ios.h.

120  {
121  return platform_message_handler_;
122  }

◆ HandlePlatformMessage()

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

Definition at line 48 of file platform_view_ios.mm.

48  {
49  platform_message_handler_->HandlePlatformMessage(std::move(message));
50 }

◆ OnPreEngineRestart()

void flutter::PlatformViewIOS::OnPreEngineRestart ( ) const
override

Definition at line 184 of file platform_view_ios.mm.

184  {
185  if (accessibility_bridge_) {
186  accessibility_bridge_.get()->clearState();
187  }
188  if (!owner_controller_) {
189  return;
190  }
191  [owner_controller_.platformViewsController reset];
192  [owner_controller_.restorationPlugin reset];
193  [owner_controller_.textInputPlugin reset];
194 }

◆ RegisterExternalTexture()

void flutter::PlatformViewIOS::RegisterExternalTexture ( int64_t  id,
NSObject< FlutterTexture > *  texture 
)

Called through when an external texture such as video or camera is given to the FlutterEngine or FlutterViewController.

Definition at line 116 of file platform_view_ios.mm.

117  {
118  RegisterTexture(ios_context_->CreateExternalTexture(texture_id, texture));
119 }
int64_t texture_id

References texture_id.

◆ SetApplicationLocale()

void flutter::PlatformViewIOS::SetApplicationLocale ( std::string  locale)
override

Definition at line 163 of file platform_view_ios.mm.

163  {
164  application_locale_ = std::move(locale);
165  ApplyLocaleToOwnerController();
166 }

◆ SetOwnerViewController()

void flutter::PlatformViewIOS::SetOwnerViewController ( __weak FlutterViewController *  owner_controller)

Updates the FlutterViewController currently attached to the FlutterEngine owning this PlatformViewIOS. This should be updated when the FlutterEngine is given a new FlutterViewController.

Definition at line 56 of file platform_view_ios.mm.

56  {
57  FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread());
58  std::lock_guard<std::mutex> guard(ios_surface_mutex_);
59  FlutterView* previousView = static_cast<FlutterView*>(owner_controller_.viewIfLoaded);
60  if (ios_surface_ || !owner_controller) {
61  NotifyDestroyed();
62  ios_surface_.reset();
63  }
64  owner_controller_ = owner_controller;
65  ApplyLocaleToOwnerController();
66 
67  // Clear the owner and rebind the accessibility bridge to nil when the view controller is deleted.
68  dealloc_view_controller_observer_.reset([[NSNotificationCenter defaultCenter]
69  addObserverForName:FlutterViewControllerWillDealloc
70  object:owner_controller_
71  queue:[NSOperationQueue mainQueue]
72  usingBlock:^(NSNotification* note) {
73  // Implicit copy of 'this' is fine.
74  if (accessibility_bridge_) {
75  FlutterViewController* deallocatingController =
76  static_cast<FlutterViewController*>(note.object);
77  FlutterView* deallocatingView =
78  static_cast<FlutterView*>(deallocatingController.viewIfLoaded);
79  accessibility_bridge_->SetViewController(nil, deallocatingView);
80  }
81  owner_controller_ = nil;
82  }]);
83 
84  if (owner_controller_ && owner_controller_.isViewLoaded) {
85  this->attachView(previousView);
86  } else {
87  EnsureAccessibilityBridge();
88  if (accessibility_bridge_) {
89  accessibility_bridge_->SetViewController(owner_controller_, previousView);
90  }
91  }
92  // Do not call `NotifyCreated()` here - let FlutterViewController take care
93  // of that when its Viewport is sized. If `NotifyCreated()` is called here,
94  // it can occasionally get invoked before the viewport is sized resulting in
95  // a framebuffer that will not be able to completely attach.
96 }
NSNotificationName const FlutterViewControllerWillDealloc
void attachView(FlutterView *previousView=nil)

References attachView(), and FlutterViewControllerWillDealloc.

◆ SetSemanticsTreeEnabled()

void flutter::PlatformViewIOS::SetSemanticsTreeEnabled ( bool  enabled)
override

Definition at line 169 of file platform_view_ios.mm.

169  {
170  semantics_tree_enabled_ = enabled;
171  if (enabled) {
172  EnsureAccessibilityBridge();
173  } else {
174  accessibility_bridge_.reset();
175  }
176 }

◆ UpdateSemantics()

void flutter::PlatformViewIOS::UpdateSemantics ( int64_t  view_id,
flutter::SemanticsNodeUpdates  update,
flutter::CustomAccessibilityActionUpdates  actions 
)
override

Definition at line 144 of file platform_view_ios.mm.

146  {
147  EnsureAccessibilityBridge();
148  if (!accessibility_bridge_) {
149  return;
150  }
151  accessibility_bridge_.get()->UpdateSemantics(std::move(update), actions);
152  if (owner_controller_ && owner_controller_.isViewLoaded) {
153  // The bridge can cache semantics before the owner FlutterView is loaded. For example, a
154  // pre-warmed engine can receive semantics before FlutterViewController.loadView, when
155  // viewIfLoaded is still nil. Only notify observers after the semantics are applied to a loaded
156  // UIKit view, so accessibilityElements are ready to inspect. If cached semantics are applied
157  // later by attachView, attachView posts this same notification.
158  PostSemanticsUpdateNotification();
159  }
160 }

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