| Flutter Windows Embedder |

plugin_registrar_windows.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_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_WINDOWS_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_WINDOWS_H_
7
8 #include <flutter_windows.h>
9 #include <windows.h>
10
11 #include <memory>
12 #include <optional>
13
14 #include "flutter_view.h"
15 #include "plugin_registrar.h"
16
17 namespace flutter {
18
19 // A delegate callback for WindowProc delegation.
20 //
21 // Implementations should return a value only if they have handled the message
22 // and want to stop all further handling.
23 using WindowProcDelegate = std::function<std::optional<
24 LRESULT>(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)>;
25
26 // An extension to PluginRegistrar providing access to Windows-specific
27 // functionality.
28 class PluginRegistrarWindows : public PluginRegistrar {
29 public:
30 // Creates a new PluginRegistrar. |core_registrar| and the messenger it
31 // provides must remain valid as long as this object exists.
32 explicit PluginRegistrarWindows(
33 FlutterDesktopPluginRegistrarRef core_registrar)
34 : PluginRegistrar(core_registrar) {
35 FlutterDesktopViewRef implicit_view =
36 FlutterDesktopPluginRegistrarGetView(core_registrar);
37 if (implicit_view) {
38 implicit_view_ = std::make_unique<FlutterView>(implicit_view);
39 }
40 }
41
42 virtual ~PluginRegistrarWindows() {
43 // Must be the first call.
44 ClearPlugins();
45 // Explicitly cleared to facilitate destruction order testing.
46 implicit_view_.reset();
47 }
48
49 // Prevent copying.
50 PluginRegistrarWindows(PluginRegistrarWindows const&) = delete;
51 PluginRegistrarWindows& operator=(PluginRegistrarWindows const&) = delete;
52
53 // Returns the implicit view, or nullptr if there is no implicit view.
54 //
55 // See:
56 // https://api.flutter.dev/flutter/dart-ui/PlatformDispatcher/implicitView.html
57 //
58 // DEPRECATED: Use |GetViewById| instead.
59 FlutterView* GetView() { return implicit_view_.get(); }
60
61 // Returns the view with the given ID, or nullptr if the view does not exist.
62 //
63 // Destroying the shared pointer destroys the reference to the view; it does
64 // not destroy the underlying view.
65 std::shared_ptr<FlutterView> GetViewById(FlutterViewId view_id) const {
66 FlutterDesktopViewRef view =
67 FlutterDesktopPluginRegistrarGetViewById(registrar(), view_id);
68 if (!view) {
69 return nullptr;
70 }
71
72 return std::make_shared<FlutterView>(view);
73 }
74
75 // Registers |delegate| to receive WindowProc callbacks for the top-level
76 // window containing this Flutter instance. Returns an ID that can be used to
77 // unregister the handler.
78 //
79 // Delegates are not guaranteed to be called:
80 // - The application may choose not to delegate WindowProc calls.
81 // - If multiple plugins are registered, the first one that returns a value
82 // from the delegate message will "win", and others will not be called.
83 // The order of delegate calls is not defined.
84 //
85 // Delegates should be implemented as narrowly as possible, only returning
86 // a value in cases where it's important that other delegates not run, to
87 // minimize the chances of conflicts between plugins.
88 int RegisterTopLevelWindowProcDelegate(WindowProcDelegate delegate) {
89 if (window_proc_delegates_.empty()) {
90 FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(
91 registrar(), PluginRegistrarWindows::OnTopLevelWindowProc, this);
92 }
93 int delegate_id = next_window_proc_delegate_id_++;
94 window_proc_delegates_.emplace(delegate_id, std::move(delegate));
95 return delegate_id;
96 }
97
98 // Unregisters a previously registered delegate.
99 void UnregisterTopLevelWindowProcDelegate(int proc_id) {
100 window_proc_delegates_.erase(proc_id);
101 if (window_proc_delegates_.empty()) {
102 FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate(
103 registrar(), PluginRegistrarWindows::OnTopLevelWindowProc);
104 }
105 }
106
107 private:
108 // A FlutterDesktopWindowProcCallback implementation that forwards back to
109 // a PluginRegistarWindows instance provided as |user_data|.
110 static bool OnTopLevelWindowProc(HWND hwnd,
111 UINT message,
112 WPARAM wparam,
113 LPARAM lparam,
114 void* user_data,
115 LRESULT* result) {
116 const auto* registrar = static_cast<PluginRegistrarWindows*>(user_data);
117 std::optional optional_result = registrar->CallTopLevelWindowProcDelegates(
118 hwnd, message, wparam, lparam);
119 if (optional_result) {
120 *result = *optional_result;
121 }
122 return optional_result.has_value();
123 }
124
125 std::optional<LRESULT> CallTopLevelWindowProcDelegates(HWND hwnd,
126 UINT message,
127 WPARAM wparam,
128 LPARAM lparam) const {
129 std::optional<LRESULT> result;
130 for (const auto& pair : window_proc_delegates_) {
131 result = pair.second(hwnd, message, wparam, lparam);
132 // Stop as soon as any delegate indicates that it has handled the message.
133 if (result) {
134 break;
135 }
136 }
137 return result;
138 }
139
140 // The associated FlutterView, if any.
141 std::unique_ptr<FlutterView> implicit_view_;
142
143 // The next ID to return from RegisterWindowProcDelegate.
144 int next_window_proc_delegate_id_ = 1;
145
146 std::map<int, WindowProcDelegate> window_proc_delegates_;
147 };
148
149 } // namespace flutter
150
151 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_WINDOWS_H_
flutter::PluginRegistrarWindows::UnregisterTopLevelWindowProcDelegate
void UnregisterTopLevelWindowProcDelegate(int proc_id)
Definition: plugin_registrar_windows.h:99
flutter::PluginRegistrar
Definition: plugin_registrar.h:27
FlutterDesktopPluginRegistrarGetView
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(FlutterDesktopPluginRegistrarRef registrar)
Definition: flutter_windows.cc:290
flutter::PluginRegistrarWindows::RegisterTopLevelWindowProcDelegate
int RegisterTopLevelWindowProcDelegate(WindowProcDelegate delegate)
Definition: plugin_registrar_windows.h:88
FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate
void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate, void *user_data)
Definition: flutter_windows.cc:301
plugin_registrar.h
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:53
flutter::PluginRegistrarWindows::PluginRegistrarWindows
PluginRegistrarWindows(FlutterDesktopPluginRegistrarRef core_registrar)
Definition: plugin_registrar_windows.h:32
flutter::PluginRegistrarWindows
Definition: plugin_registrar_windows.h:28
flutter::PluginRegistrarWindows::GetViewById
std::shared_ptr< FlutterView > GetViewById(FlutterViewId view_id) const
Definition: plugin_registrar_windows.h:65
flutter::PluginRegistrar::registrar
FlutterDesktopPluginRegistrarRef registrar() const
Definition: plugin_registrar.h:57
flutter::PluginRegistrar::ClearPlugins
void ClearPlugins()
Definition: plugin_registrar.cc:42
FlutterDesktopViewRef
struct FlutterDesktopView * FlutterDesktopViewRef
Definition: flutter_windows.h:29
flutter_view.h
flutter::FlutterViewId
int64_t FlutterViewId
Definition: flutter_view.h:13
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::FlutterView
Definition: flutter_view.h:16
FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate
void FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate)
Definition: flutter_windows.cc:309
flutter::WindowProcDelegate
std::function< std::optional< LRESULT >(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)> WindowProcDelegate
Definition: plugin_registrar_windows.h:24
flutter_windows.h
delegate_id
int delegate_id
Definition: keyboard_key_handler_unittests.cc:113
message
Win32Message message
Definition: keyboard_unittests.cc:137
FlutterDesktopPluginRegistrar
Definition: window_state.h:23
flutter::PluginRegistrarWindows::operator=
PluginRegistrarWindows & operator=(PluginRegistrarWindows const &)=delete
flutter::PluginRegistrarWindows::GetView
FlutterView * GetView()
Definition: plugin_registrar_windows.h:59
FlutterDesktopPluginRegistrarGetViewById
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopViewId view_id)
Definition: flutter_windows.cc:295
flutter::PluginRegistrarWindows::~PluginRegistrarWindows
virtual ~PluginRegistrarWindows()
Definition: plugin_registrar_windows.h:42