Flutter iOS Embedder
platform_view_ios.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_PLATFORM_VIEW_IOS_H_
6 #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_PLATFORM_VIEW_IOS_H_
7 
8 #include <memory>
9 
10 #include "flutter/fml/closure.h"
11 #include "flutter/fml/macros.h"
12 #include "flutter/fml/platform/darwin/scoped_nsobject.h"
13 #include "flutter/fml/platform/darwin/weak_nsobject.h"
14 #include "flutter/shell/common/platform_view.h"
24 
26 
27 namespace flutter {
28 
29 /**
30  * A bridge connecting the platform agnostic shell and the iOS embedding.
31  *
32  * The shell provides and requests for UI related data and this PlatformView subclass fulfills
33  * it with iOS specific capabilities. As an example, the iOS embedding (the `FlutterEngine` and the
34  * `FlutterViewController`) sends pointer data to the shell and receives the shell's request for a
35  * Skia GrDirectContext and supplies it.
36  *
37  * Despite the name "view", this class is unrelated to UIViews on iOS and doesn't have the same
38  * lifecycle. It's a long lived bridge owned by the `FlutterEngine` and can be attached and
39  * detached sequentially to multiple `FlutterViewController`s and `FlutterView`s.
40  */
41 class PlatformViewIOS final : public PlatformView {
42  public:
43  PlatformViewIOS(PlatformView::Delegate& delegate,
44  const std::shared_ptr<IOSContext>& context,
45  const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller,
46  const flutter::TaskRunners& task_runners);
47 
48  explicit PlatformViewIOS(
49  PlatformView::Delegate& delegate,
50  IOSRenderingAPI rendering_api,
51  const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller,
52  const flutter::TaskRunners& task_runners,
53  const std::shared_ptr<fml::ConcurrentTaskRunner>& worker_task_runner,
54  const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch);
55 
56  ~PlatformViewIOS() override;
57 
58  /**
59  * Returns the `FlutterViewController` currently attached to the `FlutterEngine` owning
60  * this PlatformViewIOS.
61  */
62  fml::WeakNSObject<FlutterViewController> GetOwnerViewController() const;
63 
64  /**
65  * Updates the `FlutterViewController` currently attached to the `FlutterEngine` owning
66  * this PlatformViewIOS. This should be updated when the `FlutterEngine`
67  * is given a new `FlutterViewController`.
68  */
69  void SetOwnerViewController(const fml::WeakNSObject<FlutterViewController>& owner_controller);
70 
71  /**
72  * Called one time per `FlutterViewController` when the `FlutterViewController`'s
73  * UIView is first loaded.
74  *
75  * Can be used to perform late initialization after `FlutterViewController`'s
76  * init.
77  */
78  void attachView();
79 
80  /**
81  * Called through when an external texture such as video or camera is
82  * given to the `FlutterEngine` or `FlutterViewController`.
83  */
84  void RegisterExternalTexture(int64_t id, NSObject<FlutterTexture>* texture);
85 
86  // |PlatformView|
87  PointerDataDispatcherMaker GetDispatcherMaker() override;
88 
89  // |PlatformView|
90  void SetSemanticsEnabled(bool enabled) override;
91 
92  /** Accessor for the `IOSContext` associated with the platform view. */
93  const std::shared_ptr<IOSContext>& GetIosContext() { return ios_context_; }
94 
95  std::shared_ptr<PlatformMessageHandlerIos> GetPlatformMessageHandlerIos() const {
96  return platform_message_handler_;
97  }
98 
99  std::shared_ptr<PlatformMessageHandler> GetPlatformMessageHandler() const override {
100  return platform_message_handler_;
101  }
102 
103  private:
104  /// Smart pointer for use with objective-c observers.
105  /// This guarantees we remove the observer.
106  class ScopedObserver {
107  public:
108  ScopedObserver();
109  ~ScopedObserver();
110  void reset(id<NSObject> observer);
111  ScopedObserver(const ScopedObserver&) = delete;
112  ScopedObserver& operator=(const ScopedObserver&) = delete;
113 
114  private:
115  id<NSObject> observer_ = nil;
116  };
117 
118  /// Wrapper that guarantees we communicate clearing Accessibility
119  /// information to Dart.
120  class AccessibilityBridgeManager {
121  public:
122  explicit AccessibilityBridgeManager(const std::function<void(bool)>& set_semantics_enabled);
123  AccessibilityBridgeManager(const std::function<void(bool)>& set_semantics_enabled,
124  AccessibilityBridge* bridge);
125  explicit operator bool() const noexcept { return static_cast<bool>(accessibility_bridge_); }
126  AccessibilityBridge* get() const noexcept { return accessibility_bridge_.get(); }
127  void Set(std::unique_ptr<AccessibilityBridge> bridge);
128  void Clear();
129 
130  private:
131  FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridgeManager);
132  std::unique_ptr<AccessibilityBridge> accessibility_bridge_;
133  std::function<void(bool)> set_semantics_enabled_;
134  };
135 
136  fml::WeakNSObject<FlutterViewController> owner_controller_;
137  // Since the `ios_surface_` is created on the platform thread but
138  // used on the raster thread we need to protect it with a mutex.
139  std::mutex ios_surface_mutex_;
140  std::unique_ptr<IOSSurface> ios_surface_;
141  std::shared_ptr<IOSContext> ios_context_;
142  const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller_;
143  AccessibilityBridgeManager accessibility_bridge_;
144  fml::scoped_nsprotocol<FlutterTextInputPlugin*> text_input_plugin_;
145  ScopedObserver dealloc_view_controller_observer_;
146  std::vector<std::string> platform_resolved_locale_;
147  std::shared_ptr<PlatformMessageHandlerIos> platform_message_handler_;
148 
149  // |PlatformView|
150  void HandlePlatformMessage(std::unique_ptr<flutter::PlatformMessage> message) override;
151 
152  // |PlatformView|
153  std::unique_ptr<Surface> CreateRenderingSurface() override;
154 
155  // |PlatformView|
156  std::shared_ptr<ExternalViewEmbedder> CreateExternalViewEmbedder() override;
157 
158  // |PlatformView|
159  sk_sp<GrDirectContext> CreateResourceContext() const override;
160 
161  // |PlatformView|
162  std::shared_ptr<impeller::Context> GetImpellerContext() const override;
163 
164  // |PlatformView|
165  void SetAccessibilityFeatures(int32_t flags) override;
166 
167  // |PlatformView|
168  void UpdateSemantics(flutter::SemanticsNodeUpdates update,
169  flutter::CustomAccessibilityActionUpdates actions) override;
170 
171  // |PlatformView|
172  std::unique_ptr<VsyncWaiter> CreateVSyncWaiter() override;
173 
174  // |PlatformView|
175  void OnPreEngineRestart() const override;
176 
177  // |PlatformView|
178  std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocales(
179  const std::vector<std::string>& supported_locale_data) override;
180 
181  FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewIOS);
182 };
183 
184 } // namespace flutter
185 
186 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_PLATFORM_VIEW_IOS_H_
FlutterViewController
Definition: FlutterViewController.h:56
ios_external_view_embedder.h
FlutterTexture.h
flutter::PlatformViewIOS
Definition: platform_view_ios.h:41
flutter::PlatformViewIOS::GetIosContext
const std::shared_ptr< IOSContext > & GetIosContext()
Definition: platform_view_ios.h:93
flutter::PlatformViewIOS::SetOwnerViewController
void SetOwnerViewController(const fml::WeakNSObject< FlutterViewController > &owner_controller)
Definition: platform_view_ios.mm:81
ios_surface.h
flutter::PlatformViewIOS::~PlatformViewIOS
~PlatformViewIOS() override
flutter::PlatformViewIOS::GetPlatformMessageHandler
std::shared_ptr< PlatformMessageHandler > GetPlatformMessageHandler() const override
Definition: platform_view_ios.h:99
flutter
Definition: accessibility_bridge.h:28
accessibility_bridge.h
flutter::PlatformViewIOS::attachView
void attachView()
Definition: platform_view_ios.mm:113
flutter::IOSRenderingAPI
IOSRenderingAPI
Definition: rendering_api_selection.h:14
flutter::PlatformViewIOS::GetOwnerViewController
fml::WeakNSObject< FlutterViewController > GetOwnerViewController() const
Definition: platform_view_ios.mm:77
rendering_api_selection.h
flutter::PlatformViewIOS::PlatformViewIOS
PlatformViewIOS(PlatformView::Delegate &delegate, const std::shared_ptr< IOSContext > &context, const std::shared_ptr< FlutterPlatformViewsController > &platform_views_controller, const flutter::TaskRunners &task_runners)
Definition: platform_view_ios.mm:42
platform_message_handler_ios.h
flutter::PlatformViewIOS::GetPlatformMessageHandlerIos
std::shared_ptr< PlatformMessageHandlerIos > GetPlatformMessageHandlerIos() const
Definition: platform_view_ios.h:95
flutter::PlatformViewIOS::SetSemanticsEnabled
void SetSemanticsEnabled(bool enabled) override
Definition: platform_view_ios.mm:169
ios_context.h
flutter::PlatformViewIOS::RegisterExternalTexture
void RegisterExternalTexture(int64_t id, NSObject< FlutterTexture > *texture)
Definition: platform_view_ios.mm:135
flutter::PlatformViewIOS::GetDispatcherMaker
PointerDataDispatcherMaker GetDispatcherMaker() override
Definition: platform_view_ios.mm:129
FlutterView.h
FlutterViewController.h