Flutter Engine
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.
29  public:
30  // Creates a new PluginRegistrar. |core_registrar| and the messenger it
31  // provides must remain valid as long as this object exists.
33  FlutterDesktopPluginRegistrarRef core_registrar)
34  : PluginRegistrar(core_registrar) {
35  view_ = std::make_unique<FlutterView>(
36  FlutterDesktopPluginRegistrarGetView(core_registrar));
37  }
38 
40  // Must be the first call.
41  ClearPlugins();
42  // Explicitly cleared to facilitate destruction order testing.
43  view_.reset();
44  }
45 
46  // Prevent copying.
49 
50  FlutterView* GetView() { return view_.get(); }
51 
52  // Registers |delegate| to recieve 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.
66  if (window_proc_delegates_.empty()) {
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.
77  window_proc_delegates_.erase(proc_id);
78  if (window_proc_delegates_.empty()) {
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_
int RegisterTopLevelWindowProcDelegate(WindowProcDelegate delegate)
void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate, void *user_data)
Dart_NativeFunction function
Definition: fuchsia.cc:51
PluginRegistrarWindows(FlutterDesktopPluginRegistrarRef core_registrar)
void UnregisterTopLevelWindowProcDelegate(int proc_id)
PluginRegistrarWindows & operator=(PluginRegistrarWindows const &)=delete
void FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate)
G_BEGIN_DECLS FlMethodCall gpointer user_data
std::function< std::optional< LRESULT >(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)> WindowProcDelegate
FlutterDesktopPluginRegistrarRef registrar()
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(FlutterDesktopPluginRegistrarRef controller)