Flutter Engine
The Flutter Engine
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
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
18namespace flutter {
19
20class 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.
28 public:
29 // Creates a new PluginRegistrar. |core_registrar| and the messenger it
30 // provides must remain valid as long as this object exists.
32
33 virtual ~PluginRegistrar();
34
35 // Prevent copying.
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.
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.
77class 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.
87 public:
89
90 // Prevent copying.
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>
103 auto insert_result =
104 registrars_.emplace(registrar_ref, std::make_unique<T>(registrar_ref));
105 auto& registrar_pair = *(insert_result.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:
118
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_
PluginRegistrarManager & operator=(PluginRegistrarManager const &)=delete
T * GetRegistrar(FlutterDesktopPluginRegistrarRef registrar_ref)
static PluginRegistrarManager * GetInstance()
PluginRegistrarManager(PluginRegistrarManager const &)=delete
void AddPlugin(std::unique_ptr< Plugin > plugin)
TextureRegistrar * texture_registrar()
FlutterDesktopPluginRegistrarRef registrar() const
PluginRegistrar & operator=(PluginRegistrar const &)=delete
PluginRegistrar(FlutterDesktopPluginRegistrarRef core_registrar)
BinaryMessenger * messenger()
PluginRegistrar(PluginRegistrar const &)=delete
virtual ~Plugin()=default
struct FlutterDesktopPluginRegistrar * FlutterDesktopPluginRegistrarRef
SI auto map(std::index_sequence< I... >, Fn &&fn, const Args &... args) -> skvx::Vec< sizeof...(I), decltype(fn(args[0]...))>
Definition: SkVx.h:680
#define T
Definition: precompiler.cc:65
void FlutterDesktopPluginRegistrarSetDestructionHandler(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopOnPluginRegistrarDestroyed callback)