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