Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / plugins / plugin.h
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #ifndef COMMON_PLUGINS_PLUGIN_H_
6 #define COMMON_PLUGINS_PLUGIN_H_
7
8 #include <dlfcn.h>
9 #include <manifest_parser/utils/logging.h>
10 #include <libxml2/libxml/tree.h>
11 #include <pkgmgrinfo_basic.h>
12
13 #include <memory>
14 #include <string>
15
16 #include "common/plugins/plugin_list_parser.h"
17
18 namespace common_installer {
19
20 /**
21  * @brief The Plugin class
22  *        Represents pkgmgr plugin object handling internally managing loading
23  *        of .so shared object from file system. Run() function is being
24  *        overloaded in subclasses to accommodate plugin differences.
25  */
26 class Plugin {
27  public:
28   enum class ActionType {
29       Unknown,
30       Install,
31       Upgrade,
32       Clean,
33       Undo,
34       Uninstall,
35       RecoverInstall,
36       RecoverUpgrade,
37       RecoverUninstall,
38       Removed
39   };
40   enum class ProcessType { Pre, Main, Post };
41
42   virtual ~Plugin();
43
44   template <typename Ret, typename... Args>
45   bool Exec(const std::string& name, Ret* result, Args... args) {
46     using PluginFunctionPtr = Ret (*)(Args...);
47     PluginFunctionPtr function =
48         reinterpret_cast<PluginFunctionPtr>(GetSymbol(name));
49
50     if (!function) {
51       LOG(WARNING) << "Skip to execute symbol: " << name << " (" << dlerror()
52                    << ")";
53       return false;
54     }
55
56     LOG(DEBUG) << "Execute plugin function: " << name << " of "
57                << plugin_info_.path() << "...";
58     *result = function(args...);
59     return true;
60   }
61
62   const PluginInfo& plugin_info() const { return plugin_info_; }
63
64   virtual bool Run(xmlDocPtr doc_ptr, manifest_x* manifest,
65                    ActionType action_type) = 0;
66
67   Plugin(Plugin&&) = default;
68   Plugin& operator=(Plugin&&) = default;
69
70  protected:
71   explicit Plugin(const PluginInfo& plugin_info);
72   bool Load();
73
74   PluginInfo plugin_info_;
75
76  private:
77   void* GetSymbol(const std::string& name) const;
78
79   void* lib_handle_;
80
81   SCOPE_LOG_TAG(Plugin)
82 };
83
84 /**
85  * @brief ActionTypeToPkgmgrActionType
86  *        Helper function to convert app-installer ActionType to pkgmgr action
87  *        type for 'category' and 'metadata' plugins.
88  *
89  * @param action input action type
90  * @return pkgmgr action type or -1 if error
91  */
92 int ActionTypeToPkgmgrActionType(common_installer::Plugin::ActionType action);
93
94 }  // namespace common_installer
95
96 #endif  // COMMON_PLUGINS_PLUGIN_H_