| Flutter Windows Embedder |

plugin_registrar.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_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_H_
7
8 #include <flutter_plugin_registrar.h>
9
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <string>
14
15 #include "binary_messenger.h"
16 #include "texture_registrar.h"
17
18 namespace flutter {
19
20 class Plugin;
21
22 // A object managing the registration of a plugin for various events.
23 //
24 // Currently this class has very limited functionality, but is expected to
25 // expand over time to more closely match the functionality of
26 // the Flutter mobile plugin APIs' plugin registrars.
27 class PluginRegistrar {
28 public:
29 // Creates a new PluginRegistrar. |core_registrar| and the messenger it
30 // provides must remain valid as long as this object exists.
31 explicit PluginRegistrar(FlutterDesktopPluginRegistrarRef core_registrar);
32
33 virtual ~PluginRegistrar();
34
35 // Prevent copying.
36 PluginRegistrar(PluginRegistrar const&) = delete;
37 PluginRegistrar& operator=(PluginRegistrar const&) = delete;
38
39 // Returns the messenger to use for creating channels to communicate with the
40 // Flutter engine.
41 //
42 // This pointer will remain valid for the lifetime of this instance.
43 BinaryMessenger* messenger() { return messenger_.get(); }
44
45 // Returns the texture registrar to use for the plugin to render a pixel
46 // buffer.
47 TextureRegistrar* texture_registrar() { return texture_registrar_.get(); }
48
49 // Takes ownership of |plugin|.
50 //
51 // Plugins are not required to call this method if they have other lifetime
52 // management, but this is a convenient place for plugins to be owned to
53 // ensure that they stay valid for any registered callbacks.
54 void AddPlugin(std::unique_ptr<Plugin> plugin);
55
56 protected:
57 FlutterDesktopPluginRegistrarRef registrar() const { return registrar_; }
58
59 // Destroys all owned plugins. Subclasses should call this at the beginning of
60 // their destructors to prevent the possibility of an owned plugin trying to
61 // access destroyed state during its own destruction.
62 void ClearPlugins();
63
64 private:
65 // Handle for interacting with the C API's registrar.
66 FlutterDesktopPluginRegistrarRef registrar_;
67
68 std::unique_ptr<BinaryMessenger> messenger_;
69
70 std::unique_ptr<TextureRegistrar> texture_registrar_;
71
72 // Plugins registered for ownership.
73 std::set<std::unique_ptr<Plugin>> plugins_;
74 };
75
76 // A plugin that can be registered for ownership by a PluginRegistrar.
77 class Plugin {
78 public:
79 virtual ~Plugin() = default;
80 };
81
82 // A singleton to own PluginRegistrars. This is intended for use in plugins,
83 // where there is no higher-level object to own a PluginRegistrar that can
84 // own plugin instances and ensure that they live as long as the engine they
85 // are registered with.
86 class PluginRegistrarManager {
87 public:
88 static PluginRegistrarManager* GetInstance();
89
90 // Prevent copying.
91 PluginRegistrarManager(PluginRegistrarManager const&) = delete;
92 PluginRegistrarManager& operator=(PluginRegistrarManager const&) = delete;
93
94 // Returns a plugin registrar wrapper of type T, which must be a kind of
95 // PluginRegistrar, creating it if necessary. The returned registrar will
96 // live as long as the underlying FlutterDesktopPluginRegistrarRef, so
97 // can be used to own plugin instances.
98 //
99 // Calling this multiple times for the same registrar_ref with different
100 // template types results in undefined behavior.
101 template <class T>
102 T* GetRegistrar(FlutterDesktopPluginRegistrarRef registrar_ref) {
103 auto insert_result =
104 registrars_.emplace(registrar_ref, std::make_unique<T>(registrar_ref));
105 auto& registrar_pair = *(insert_result.first);
106 FlutterDesktopPluginRegistrarSetDestructionHandler(registrar_pair.first,
107 OnRegistrarDestroyed);
108 return static_cast<T*>(registrar_pair.second.get());
109 }
110
111 // Destroys all registrar wrappers created by the manager.
112 //
113 // This is intended primarily for use in tests.
114 void Reset() { registrars_.clear(); }
115
116 private:
117 PluginRegistrarManager();
118
119 using WrapperMap = std::map<FlutterDesktopPluginRegistrarRef,
120 std::unique_ptr<PluginRegistrar>>;
121
122 static void OnRegistrarDestroyed(FlutterDesktopPluginRegistrarRef registrar);
123
124 WrapperMap* registrars() { return &registrars_; }
125
126 WrapperMap registrars_;
127 };
128
129 } // namespace flutter
130
131 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_H_
binary_messenger.h
flutter::BinaryMessenger
Definition: binary_messenger.h:29
flutter::Plugin
Definition: plugin_registrar.h:77
flutter::Plugin::~Plugin
virtual ~Plugin()=default
flutter::PluginRegistrar
Definition: plugin_registrar.h:27
flutter::PluginRegistrar::AddPlugin
void AddPlugin(std::unique_ptr< Plugin > plugin)
Definition: plugin_registrar.cc:38
flutter::PluginRegistrar::registrar
FlutterDesktopPluginRegistrarRef registrar() const
Definition: plugin_registrar.h:57
flutter::PluginRegistrar::messenger
BinaryMessenger * messenger()
Definition: plugin_registrar.h:43
flutter::PluginRegistrar::PluginRegistrar
PluginRegistrar(FlutterDesktopPluginRegistrarRef core_registrar)
Definition: plugin_registrar.cc:19
flutter::PluginRegistrar::~PluginRegistrar
virtual ~PluginRegistrar()
Definition: plugin_registrar.cc:30
flutter::PluginRegistrar::texture_registrar
TextureRegistrar * texture_registrar()
Definition: plugin_registrar.h:47
flutter::PluginRegistrar::ClearPlugins
void ClearPlugins()
Definition: plugin_registrar.cc:42
flutter::PluginRegistrar::PluginRegistrar
PluginRegistrar(PluginRegistrar const &)=delete
flutter::PluginRegistrar::operator=
PluginRegistrar & operator=(PluginRegistrar const &)=delete
flutter::PluginRegistrarManager
Definition: plugin_registrar.h:86
flutter::PluginRegistrarManager::operator=
PluginRegistrarManager & operator=(PluginRegistrarManager const &)=delete
flutter::PluginRegistrarManager::Reset
void Reset()
Definition: plugin_registrar.h:114
flutter::PluginRegistrarManager::GetInstance
static PluginRegistrarManager * GetInstance()
Definition: plugin_registrar.cc:49
flutter::PluginRegistrarManager::PluginRegistrarManager
PluginRegistrarManager(PluginRegistrarManager const &)=delete
flutter::PluginRegistrarManager::GetRegistrar
T * GetRegistrar(FlutterDesktopPluginRegistrarRef registrar_ref)
Definition: plugin_registrar.h:102
flutter::TextureRegistrar
Definition: texture_registrar.h:85
flutter_plugin_registrar.h
FlutterDesktopPluginRegistrarRef
struct FlutterDesktopPluginRegistrar * FlutterDesktopPluginRegistrarRef
Definition: flutter_plugin_registrar.h:20
FlutterDesktopPluginRegistrarSetDestructionHandler
void FlutterDesktopPluginRegistrarSetDestructionHandler(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopOnPluginRegistrarDestroyed callback)
Definition: flutter_windows.cc:349
flutter
Definition: accessibility_bridge_windows.cc:11
FlutterDesktopPluginRegistrar
Definition: window_state.h:23
texture_registrar.h