| 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 view_ = std::make_unique<FlutterView>(
36 FlutterDesktopPluginRegistrarGetView(core_registrar));
37 }
38
39 virtual ~PluginRegistrarWindows() {
40 // Must be the first call.
41 ClearPlugins();
42 // Explicitly cleared to facilitate destruction order testing.
43 view_.reset();
44 }
45
46 // Prevent copying.
47 PluginRegistrarWindows(PluginRegistrarWindows const&) = delete;
48 PluginRegistrarWindows& operator=(PluginRegistrarWindows const&) = delete;
49
50 FlutterView* GetView() { return view_.get(); }
51
52 // Registers |delegate| to receive WindowProc callbacks for the top-level
53 // window containing this Flutter instance. Returns an ID that can be used to
54 // unregister the handler.
55 //
56 // Delegates are not guaranteed to be called:
57 // - The application may choose not to delegate WindowProc calls.
58 // - If multiple plugins are registered, the first one that returns a value
59 // from the delegate message will "win", and others will not be called.
60 // The order of delegate calls is not defined.
61 //
62 // Delegates should be implemented as narrowly as possible, only returning
63 // a value in cases where it's important that other delegates not run, to
64 // minimize the chances of conflicts between plugins.
65 int RegisterTopLevelWindowProcDelegate(WindowProcDelegate delegate) {
66 if (window_proc_delegates_.empty()) {
67 FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(
68 registrar(), PluginRegistrarWindows::OnTopLevelWindowProc, this);
69 }
70 int delegate_id = next_window_proc_delegate_id_++;
71 window_proc_delegates_.emplace(delegate_id, std::move(delegate));
72 return delegate_id;
73 }
74
75 // Unregisters a previously registered delegate.
76 void UnregisterTopLevelWindowProcDelegate(int proc_id) {
77 window_proc_delegates_.erase(proc_id);
78 if (window_proc_delegates_.empty()) {
79 FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate(
80 registrar(), PluginRegistrarWindows::OnTopLevelWindowProc);
81 }
82 }
83
84 private:
85 // A FlutterDesktopWindowProcCallback implementation that forwards back to
86 // a PluginRegistarWindows instance provided as |user_data|.
87 static bool OnTopLevelWindowProc(HWND hwnd,
88 UINT message,
89 WPARAM wparam,
90 LPARAM lparam,
91 void* user_data,
92 LRESULT* result) {
93 const auto* registrar = static_cast<PluginRegistrarWindows*>(user_data);
94 std::optional optional_result = registrar->CallTopLevelWindowProcDelegates(
95 hwnd, message, wparam, lparam);
96 if (optional_result) {
97 *result = *optional_result;
98 }
99 return optional_result.has_value();
100 }
101
102 std::optional<LRESULT> CallTopLevelWindowProcDelegates(HWND hwnd,
103 UINT message,
104 WPARAM wparam,
105 LPARAM lparam) const {
106 std::optional<LRESULT> result;
107 for (const auto& pair : window_proc_delegates_) {
108 result = pair.second(hwnd, message, wparam, lparam);
109 // Stop as soon as any delegate indicates that it has handled the message.
110 if (result) {
111 break;
112 }
113 }
114 return result;
115 }
116
117 // The associated FlutterView, if any.
118 std::unique_ptr<FlutterView> view_;
119
120 // The next ID to return from RegisterWindowProcDelegate.
121 int next_window_proc_delegate_id_ = 1;
122
123 std::map<int, WindowProcDelegate> window_proc_delegates_;
124 };
125
126 } // namespace flutter
127
128 #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:76
flutter::PluginRegistrar
Definition: plugin_registrar.h:27
FlutterDesktopPluginRegistrarGetView
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(FlutterDesktopPluginRegistrarRef registrar)
Definition: flutter_windows.cc:249
flutter::PluginRegistrarWindows::RegisterTopLevelWindowProcDelegate
int RegisterTopLevelWindowProcDelegate(WindowProcDelegate delegate)
Definition: plugin_registrar_windows.h:65
FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate
void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate, void *user_data)
Definition: flutter_windows.cc:254
plugin_registrar.h
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:49
flutter::PluginRegistrarWindows::PluginRegistrarWindows
PluginRegistrarWindows(FlutterDesktopPluginRegistrarRef core_registrar)
Definition: plugin_registrar_windows.h:32
flutter::PluginRegistrar::registrar
FlutterDesktopPluginRegistrarRef registrar()
Definition: plugin_registrar.h:57
flutter::PluginRegistrarWindows
Definition: plugin_registrar_windows.h:28
flutter::PluginRegistrar::ClearPlugins
void ClearPlugins()
Definition: plugin_registrar.cc:42
flutter_view.h
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::FlutterView
Definition: flutter_view.h:13
FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate
void FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate)
Definition: flutter_windows.cc:262
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:50
flutter::PluginRegistrarWindows::~PluginRegistrarWindows
virtual ~PluginRegistrarWindows()
Definition: plugin_registrar_windows.h:39