Flutter Engine
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 SHELL_PLATFORM_IOS_PLATFORM_VIEW_IOS_H_
6 #define SHELL_PLATFORM_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/memory/weak_ptr.h"
13 #include "flutter/fml/platform/darwin/scoped_nsobject.h"
14 #include "flutter/shell/common/platform_view.h"
15 #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
16 #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
17 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
18 #import "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h"
19 #import "flutter/shell/platform/darwin/ios/framework/Source/platform_message_router.h"
20 #import "flutter/shell/platform/darwin/ios/ios_context.h"
21 #import "flutter/shell/platform/darwin/ios/ios_external_view_embedder.h"
22 #import "flutter/shell/platform/darwin/ios/ios_surface.h"
23 #import "flutter/shell/platform/darwin/ios/rendering_api_selection.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:
44  const std::shared_ptr<IOSContext>& context,
45  const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller,
46  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  flutter::TaskRunners task_runners);
53 
54  ~PlatformViewIOS() override;
55 
56  /**
57  * The `PlatformMessageRouter` is the iOS bridge connecting the shell's
58  * platform agnostic `PlatformMessage` to iOS's channel message handler.
59  */
61 
62  /**
63  * Returns the `FlutterViewController` currently attached to the `FlutterEngine` owning
64  * this PlatformViewIOS.
65  */
67 
68  /**
69  * Updates the `FlutterViewController` currently attached to the `FlutterEngine` owning
70  * this PlatformViewIOS. This should be updated when the `FlutterEngine`
71  * is given a new `FlutterViewController`.
72  */
74 
75  /**
76  * Called one time per `FlutterViewController` when the `FlutterViewController`'s
77  * UIView is first loaded.
78  *
79  * Can be used to perform late initialization after `FlutterViewController`'s
80  * init.
81  */
82  void attachView();
83 
84  /**
85  * Called through when an external texture such as video or camera is
86  * given to the `FlutterEngine` or `FlutterViewController`.
87  */
88  void RegisterExternalTexture(int64_t id, NSObject<FlutterTexture>* texture);
89 
90  // |PlatformView|
92 
93  void DispatchKeyDataPacket(std::unique_ptr<KeyDataPacket> packet,
94  std::function<void(bool /* handled */)> callback);
95 
96  // |PlatformView|
97  void SetSemanticsEnabled(bool enabled) override;
98 
99  /** Accessor for the `IOSContext` associated with the platform view. */
100  const std::shared_ptr<IOSContext>& GetIosContext() { return ios_context_; }
101 
102  private:
103  /// Smart pointer for use with objective-c observers.
104  /// This guarantees we remove the observer.
105  class ScopedObserver {
106  public:
107  ScopedObserver();
108  ~ScopedObserver();
109  void reset(id<NSObject> observer);
110  ScopedObserver(const ScopedObserver&) = delete;
111  ScopedObserver& operator=(const ScopedObserver&) = delete;
112 
113  private:
114  id<NSObject> observer_;
115  };
116 
117  /// Smart pointer that guarantees we communicate clearing Accessibility
118  /// information to Dart.
119  class AccessibilityBridgePtr {
120  public:
121  explicit AccessibilityBridgePtr(const std::function<void(bool)>& set_semantics_enabled);
122  AccessibilityBridgePtr(const std::function<void(bool)>& set_semantics_enabled,
123  AccessibilityBridge* bridge);
124  ~AccessibilityBridgePtr();
125  explicit operator bool() const noexcept { return static_cast<bool>(accessibility_bridge_); }
126  AccessibilityBridge* operator->() const noexcept { return accessibility_bridge_.get(); }
127  void reset(AccessibilityBridge* bridge = nullptr);
128 
129  private:
130  FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridgePtr);
131  std::unique_ptr<AccessibilityBridge> accessibility_bridge_;
132  std::function<void(bool)> set_semantics_enabled_;
133  };
134 
135  fml::WeakPtr<FlutterViewController> owner_controller_;
136  // Since the `ios_surface_` is created on the platform thread but
137  // used on the raster thread we need to protect it with a mutex.
138  std::mutex ios_surface_mutex_;
139  std::unique_ptr<IOSSurface> ios_surface_;
140  std::shared_ptr<IOSContext> ios_context_;
141  const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller_;
142  PlatformMessageRouter platform_message_router_;
143  AccessibilityBridgePtr accessibility_bridge_;
145  fml::closure firstFrameCallback_;
146  ScopedObserver dealloc_view_controller_observer_;
147  std::vector<std::string> platform_resolved_locale_;
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  void SetAccessibilityFeatures(int32_t flags) override;
163 
164  // |PlatformView|
165  void UpdateSemantics(flutter::SemanticsNodeUpdates update,
167 
168  // |PlatformView|
169  std::unique_ptr<VsyncWaiter> CreateVSyncWaiter() override;
170 
171  // |PlatformView|
172  void OnPreEngineRestart() const override;
173 
174  // |PlatformView|
175  std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocales(
176  const std::vector<std::string>& supported_locale_data) override;
177 
179 };
180 
181 } // namespace flutter
182 
183 #endif // SHELL_PLATFORM_IOS_PLATFORM_VIEW_IOS_H_
G_BEGIN_DECLS FlTexture * texture
void RegisterExternalTexture(int64_t id, NSObject< FlutterTexture > *texture)
void DispatchKeyDataPacket(std::unique_ptr< KeyDataPacket > packet, std::function< void(bool)> callback)
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
std::function< std::unique_ptr< PointerDataDispatcher >(PointerDataDispatcher::Delegate &)> PointerDataDispatcherMaker
Signature for constructing PointerDataDispatcher.
Platform views are created by the shell on the platform task runner. Unless explicitly specified...
Definition: platform_view.h:47
FlKeyEvent FlKeyResponderAsyncCallback callback
PointerDataDispatcherMaker GetDispatcherMaker() override
Returns a platform-specific PointerDataDispatcherMaker so the Engine can construct the PointerDataPac...
PlatformMessageRouter & GetPlatformMessageRouter()
std::function< void()> closure
Definition: closure.h:14
void SetSemanticsEnabled(bool enabled) override
Used by embedder to notify the running isolate hosted by the engine on the UI thread that the accessi...
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
fml::WeakPtr< FlutterViewController > GetOwnerViewController() const
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
Definition: platform_view.h:54
const std::shared_ptr< IOSContext > & GetIosContext()
void SetOwnerViewController(fml::WeakPtr< FlutterViewController > owner_controller)
DEF_SWITCHES_START snapshot asset Path to the directory containing the four files specified by VmSnapshotInstructions and IsolateSnapshotInstructions vm snapshot The VM instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present isolate snapshot The isolate instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present icu symbol Prefix for the symbols representing ICU data linked into the Flutter library dart flags
Definition: switches.h:66
PlatformViewIOS(PlatformView::Delegate &delegate, const std::shared_ptr< IOSContext > &context, const std::shared_ptr< FlutterPlatformViewsController > &platform_views_controller, flutter::TaskRunners task_runners)