Flutter Engine
platform_view.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_FUCHSIA_PLATFORM_VIEW_H_
6 #define FLUTTER_SHELL_PLATFORM_FUCHSIA_PLATFORM_VIEW_H_
7 
8 #include <fuchsia/ui/input/cpp/fidl.h>
9 #include <fuchsia/ui/scenic/cpp/fidl.h>
10 #include <lib/fidl/cpp/binding.h>
11 #include <lib/fit/function.h>
12 #include <lib/ui/scenic/cpp/id.h>
13 
14 #include <map>
15 #include <set>
16 
17 #include "flow/embedded_views.h"
18 #include "flutter/fml/macros.h"
19 #include "flutter/fml/time/time_delta.h"
20 #include "flutter/shell/common/platform_view.h"
21 #include "flutter/shell/platform/fuchsia/flutter/fuchsia_external_view_embedder.h"
22 
23 #include "accessibility_bridge.h"
24 
25 namespace flutter_runner {
26 
27 using OnEnableWireframe = fit::function<void(bool)>;
28 using OnCreateView = fit::function<void(int64_t, bool, bool)>;
29 using OnUpdateView = fit::function<void(int64_t, bool, bool)>;
30 using OnDestroyView = fit::function<void(int64_t)>;
31 using OnCreateSurface = fit::function<std::unique_ptr<flutter::Surface>()>;
32 
33 // PlatformView is the per-engine component residing on the platform thread that
34 // is responsible for all platform specific integrations -- particularly
35 // integration with the platform's accessibility, input, and windowing features.
36 //
37 // PlatformView communicates with the Dart code via "platform messages" handled
38 // in HandlePlatformMessage. This communication is bidirectional. Platform
39 // messages are notably responsible for communication related to input and
40 // external views / windowing.
41 //
42 // The PlatformView implements SessionListener and gets Session events but it
43 // does *not* actually own the Session itself; that is owned by the
44 // FuchsiaExternalViewEmbedder on the raster thread.
45 class PlatformView final : public flutter::PlatformView,
47  private fuchsia::ui::scenic::SessionListener,
48  private fuchsia::ui::input::InputMethodEditorClient {
49  public:
51  std::string debug_label,
52  fuchsia::ui::views::ViewRef view_ref,
53  flutter::TaskRunners task_runners,
54  std::shared_ptr<sys::ServiceDirectory> runner_services,
55  fidl::InterfaceHandle<fuchsia::sys::ServiceProvider>
56  parent_environment_service_provider,
57  fidl::InterfaceRequest<fuchsia::ui::scenic::SessionListener>
58  session_listener_request,
59  fidl::InterfaceHandle<fuchsia::ui::views::Focuser> focuser,
60  fit::closure on_session_listener_error_callback,
61  OnEnableWireframe wireframe_enabled_callback,
62  OnCreateView on_create_view_callback,
63  OnUpdateView on_update_view_callback,
64  OnDestroyView on_destroy_view_callback,
65  OnCreateSurface on_create_surface_callback,
66  std::shared_ptr<flutter::ExternalViewEmbedder> view_embedder,
67  fml::TimeDelta vsync_offset,
68  zx_handle_t vsync_event_handle);
69 
70  ~PlatformView();
71 
72  // |flutter::PlatformView|
73  // |flutter_runner::AccessibilityBridge::Delegate|
74  void SetSemanticsEnabled(bool enabled) override;
75 
76  // |flutter_runner::AccessibilityBridge::Delegate|
77  void DispatchSemanticsAction(int32_t node_id,
79 
80  // |PlatformView|
82 
83  private:
84  void RegisterPlatformMessageHandlers();
85 
86  // |fuchsia::ui::input::InputMethodEditorClient|
87  void DidUpdateState(
88  fuchsia::ui::input::TextInputState state,
89  std::unique_ptr<fuchsia::ui::input::InputEvent> event) override;
90 
91  // |fuchsia::ui::input::InputMethodEditorClient|
92  void OnAction(fuchsia::ui::input::InputMethodAction action) override;
93 
94  // |fuchsia::ui::scenic::SessionListener|
95  void OnScenicError(std::string error) override;
96  void OnScenicEvent(std::vector<fuchsia::ui::scenic::Event> events) override;
97 
98  void OnChildViewConnected(scenic::ResourceId view_holder_id);
99  void OnChildViewDisconnected(scenic::ResourceId view_holder_id);
100  void OnChildViewStateChanged(scenic::ResourceId view_holder_id, bool state);
101 
102  bool OnHandlePointerEvent(const fuchsia::ui::input::PointerEvent& pointer);
103 
104  bool OnHandleKeyboardEvent(const fuchsia::ui::input::KeyboardEvent& keyboard);
105 
106  bool OnHandleFocusEvent(const fuchsia::ui::input::FocusEvent& focus);
107 
108  // Gets a new input method editor from the input connection. Run when both
109  // Scenic has focus and Flutter has requested input with setClient.
110  void ActivateIme();
111 
112  // Detaches the input method editor connection, ending the edit session and
113  // closing the onscreen keyboard. Call when input is no longer desired, either
114  // because Scenic says we lost focus or when Flutter no longer has a text
115  // field focused.
116  void DeactivateIme();
117 
118  // |flutter::PlatformView|
119  std::unique_ptr<flutter::VsyncWaiter> CreateVSyncWaiter() override;
120 
121  // |flutter::PlatformView|
122  std::unique_ptr<flutter::Surface> CreateRenderingSurface() override;
123 
124  // |flutter::PlatformView|
125  void HandlePlatformMessage(
126  fml::RefPtr<flutter::PlatformMessage> message) override;
127 
128  // |flutter::PlatformView|
129  void UpdateSemantics(
132 
133  // Channel handler for kAccessibilityChannel. This is currently not
134  // being used, but it is necessary to handle accessibility messages
135  // that are sent by Flutter when semantics is enabled.
136  void HandleAccessibilityChannelPlatformMessage(
138 
139  // Channel handler for kFlutterPlatformChannel
140  void HandleFlutterPlatformChannelPlatformMessage(
142 
143  // Channel handler for kTextInputChannel
144  void HandleFlutterTextInputChannelPlatformMessage(
146 
147  // Channel handler for kPlatformViewsChannel.
148  void HandleFlutterPlatformViewsChannelPlatformMessage(
150 
151  const std::string debug_label_;
152  // TODO(MI4-2490): remove once ViewRefControl is passed to Scenic and kept
153  // alive there
154  const fuchsia::ui::views::ViewRef view_ref_;
155  fuchsia::ui::views::FocuserPtr focuser_;
156  std::unique_ptr<AccessibilityBridge> accessibility_bridge_;
157 
158  // Logical size and logical->physical ratio. These are optional to provide
159  // an "unset" state during program startup, before Scenic has sent any
160  // metrics-related events to provide initial values for these.
161  //
162  // The engine internally uses a default size of (0.f 0.f) with a default 1.f
163  // ratio, so there is no need to emit events until Scenic has actually sent a
164  // valid size and ratio.
165  std::optional<std::pair<float, float>> view_logical_size_;
166  std::optional<float> view_pixel_ratio_;
167 
168  fidl::Binding<fuchsia::ui::scenic::SessionListener> session_listener_binding_;
169  fit::closure session_listener_error_callback_;
170  OnEnableWireframe wireframe_enabled_callback_;
171  OnCreateView on_create_view_callback_;
172  OnUpdateView on_update_view_callback_;
173  OnDestroyView on_destroy_view_callback_;
174  OnCreateSurface on_create_surface_callback_;
175  std::shared_ptr<flutter::ExternalViewEmbedder> external_view_embedder_;
176 
177  int current_text_input_client_ = 0;
178  fidl::Binding<fuchsia::ui::input::InputMethodEditorClient> ime_client_;
179  fuchsia::ui::input::InputMethodEditorPtr ime_;
180  fuchsia::ui::input::ImeServicePtr text_sync_service_;
181 
182  fuchsia::sys::ServiceProviderPtr parent_environment_service_provider_;
183 
184  // last_text_state_ is the last state of the text input as reported by the IME
185  // or initialized by Flutter. We set it to null if Flutter doesn't want any
186  // input, since then there is no text input state at all.
187  std::unique_ptr<fuchsia::ui::input::TextInputState> last_text_state_;
188 
189  std::set<int> down_pointers_;
190  std::map<
191  std::string /* channel */,
192  fit::function<void(
193  fml::RefPtr<flutter::PlatformMessage> /* message */)> /* handler */>
194  platform_message_handlers_;
195  // These are the channels that aren't registered and have been notified as
196  // such. Notifying via logs multiple times results in log-spam. See:
197  // https://github.com/flutter/flutter/issues/55966
198  std::set<std::string /* channel */> unregistered_channels_;
199 
200  fml::TimeDelta vsync_offset_;
201  zx_handle_t vsync_event_handle_ = 0;
202 
203  FML_DISALLOW_COPY_AND_ASSIGN(PlatformView);
204 };
205 
206 } // namespace flutter_runner
207 
208 #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_PLATFORM_VIEW_H_
fit::function< std::unique_ptr< flutter::Surface >()> OnCreateSurface
Definition: platform_view.h:31
FlMethodResponse GError ** error
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
flutter::PointerDataDispatcherMaker GetDispatcherMaker() override
Returns a platform-specific PointerDataDispatcherMaker so the Engine can construct the PointerDataPac...
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:44
fit::function< void(int64_t)> OnDestroyView
Definition: platform_view.h:30
std::function< void()> closure
Definition: closure.h:14
~PlatformView()
Destroys the platform view. The platform view is owned by the shell and will be destroyed by the same...
PlatformView(flutter::PlatformView::Delegate &delegate, std::string debug_label, fuchsia::ui::views::ViewRef view_ref, flutter::TaskRunners task_runners, std::shared_ptr< sys::ServiceDirectory > runner_services, fidl::InterfaceHandle< fuchsia::sys::ServiceProvider > parent_environment_service_provider, fidl::InterfaceRequest< fuchsia::ui::scenic::SessionListener > session_listener_request, fidl::InterfaceHandle< fuchsia::ui::views::Focuser > focuser, fit::closure on_session_listener_error_callback, OnEnableWireframe wireframe_enabled_callback, OnCreateView on_create_view_callback, OnUpdateView on_update_view_callback, OnDestroyView on_destroy_view_callback, OnCreateSurface on_create_surface_callback, std::shared_ptr< flutter::ExternalViewEmbedder > view_embedder, fml::TimeDelta vsync_offset, zx_handle_t vsync_event_handle)
SemanticsAction action
fit::function< void(int64_t, bool, bool)> OnUpdateView
Definition: platform_view.h:29
GdkEventButton * event
Definition: fl_view.cc:62
fit::function< void(int64_t, bool, bool)> OnCreateView
Definition: platform_view.h:28
void SetSemanticsEnabled(bool enabled) override
Used by embedder to notify the running isolate hosted by the engine on the UI thread that the accessi...
void DispatchSemanticsAction(int32_t node_id, flutter::SemanticsAction action) override
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
Definition: platform_view.h:51
fit::function< void(bool)> OnEnableWireframe
Definition: platform_view.h:27