File: plugins.hpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (230 lines) | stat: -rw-r--r-- 6,338 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.

#ifndef LIBDNF5_PLUGIN_PLUGINS_HPP
#define LIBDNF5_PLUGIN_PLUGINS_HPP

#include "libdnf5/common/exception.hpp"
#include "libdnf5/conf/config_parser.hpp"
#include "libdnf5/plugin/iplugin.hpp"

#include <filesystem>
#include <memory>
#include <string>
#include <vector>


namespace libdnf5::plugin {

class PluginError : public Error {
public:
    using Error::Error;
    const char * get_domain_name() const noexcept override { return "libdnf5::plugin"; }
    const char * get_name() const noexcept override { return "PluginError"; }
};


class Plugin {
public:
    Plugin(IPlugin & iplugin_instance, ConfigParser && parser);
    virtual ~Plugin();

    Plugin(const Plugin &) = delete;
    Plugin(Plugin &&) = delete;
    Plugin & operator=(const Plugin &) = delete;
    Plugin & operator=(Plugin &&) = delete;

    IPlugin * get_iplugin() const noexcept;
    ConfigParser & get_config_parser() noexcept;

    void set_enabled(bool enabled) noexcept;
    bool get_enabled() const noexcept;

    void init();
    void pre_base_setup();
    void post_base_setup();
    void repos_configured();
    void repos_loaded();
    void pre_add_cmdline_packages(const std::vector<std::string> & paths);
    void post_add_cmdline_packages();
    void goal_resolved(const libdnf5::base::Transaction & transaction);
    void pre_transaction(const libdnf5::base::Transaction & transaction);
    void post_transaction(const libdnf5::base::Transaction & transaction);
    void finish() noexcept;

protected:
    Plugin(ConfigParser && parser) : cfg_parser(std::move(parser)) {}
    IPlugin * iplugin_instance{nullptr};
    ConfigParser cfg_parser;
    bool enabled{true};
};


/// Plugin manager
class Plugins {
public:
    Plugins(Base & base);
    ~Plugins();

    /// Registers the plugin passed by the argument.
    void register_plugin(std::unique_ptr<Plugin> && plugin);

    /// Loads plugins defined by configuration files in the directories.
    void load_plugins(const std::vector<std::filesystem::path> & config_dirs);

    /// Returns the number of registered plugins.
    size_t count() const noexcept;

    /// Call init of all allowed plugins.
    bool init();

    /// Call hook of all allowed plugins.
    void pre_base_setup();

    void post_base_setup();

    void repos_configured();

    void repos_loaded();

    void pre_add_cmdline_packages(const std::vector<std::string> & paths);

    void post_add_cmdline_packages();

    void goal_resolved(const libdnf5::base::Transaction & transaction);

    void pre_transaction(const libdnf5::base::Transaction & transaction);

    void post_transaction(const libdnf5::base::Transaction & transaction);

    /// Call finish of all allowed plugins in reverse order.
    void finish() noexcept;

private:
    std::string find_plugin_library(const std::string & plugin_name);

    /// Loads the plugin from the library defined by the file path.
    void load_plugin_library(ConfigParser && parser, const std::string & file_path, const std::string & plugin_name);

    Base * base;
    std::vector<std::unique_ptr<Plugin>> plugins;
};


inline Plugin::Plugin(IPlugin & iplugin_instance, ConfigParser && parser)
    : iplugin_instance{&iplugin_instance},
      cfg_parser(std::move(parser)) {}

inline Plugin::~Plugin() {
    finish();
}

inline IPlugin * Plugin::get_iplugin() const noexcept {
    return iplugin_instance;
}

inline ConfigParser & Plugin::get_config_parser() noexcept {
    return cfg_parser;
}

inline void Plugin::set_enabled(bool enabled) noexcept {
    this->enabled = enabled;
}

inline bool Plugin::get_enabled() const noexcept {
    return enabled;
}

inline void Plugin::init() {
    if (iplugin_instance) {
        iplugin_instance->init();
    }
}

inline void Plugin::pre_base_setup() {
    if (iplugin_instance) {
        iplugin_instance->pre_base_setup();
    }
}

inline void Plugin::post_base_setup() {
    if (iplugin_instance) {
        iplugin_instance->post_base_setup();
    }
}

inline void Plugin::repos_configured() {
    if (iplugin_instance) {
        iplugin_instance->repos_configured();
    }
}

inline void Plugin::repos_loaded() {
    if (iplugin_instance) {
        iplugin_instance->repos_loaded();
    }
}

inline void Plugin::pre_add_cmdline_packages(const std::vector<std::string> & paths) {
    if (iplugin_instance) {
        iplugin_instance->pre_add_cmdline_packages(paths);
    }
}

inline void Plugin::post_add_cmdline_packages() {
    if (iplugin_instance) {
        iplugin_instance->post_add_cmdline_packages();
    }
}

inline void Plugin::goal_resolved(const libdnf5::base::Transaction & transaction) {
    if (iplugin_instance) {
        if (auto iplugin2_1_instance = dynamic_cast<IPlugin2_1 *>(iplugin_instance)) {
            iplugin2_1_instance->goal_resolved(transaction);
        }
    }
}

inline void Plugin::pre_transaction(const libdnf5::base::Transaction & transaction) {
    if (iplugin_instance) {
        iplugin_instance->pre_transaction(transaction);
    }
}

inline void Plugin::post_transaction(const libdnf5::base::Transaction & transaction) {
    if (iplugin_instance) {
        iplugin_instance->post_transaction(transaction);
    }
}

inline void Plugin::finish() noexcept {
    if (iplugin_instance) {
        iplugin_instance->finish();
    }
}

inline Plugins::Plugins(Base & base) : base(&base) {}

inline size_t Plugins::count() const noexcept {
    return plugins.size();
}

}  // namespace libdnf5::plugin

#endif