1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/plugins/tag_plugin.h"
10 namespace common_installer {
12 const char TagPlugin::kType[] = "tag";
14 std::unique_ptr<TagPlugin> TagPlugin::Create(
15 const PluginInfo& plugin_info, Plugin::ActionType action_type) {
16 std::unique_ptr<TagPlugin> plugin(new TagPlugin(plugin_info, action_type));
22 std::string TagPlugin::GetFunctionName(ProcessType process,
23 ActionType action) const {
24 static std::map<std::pair<ActionType, ProcessType>, std::string> names {
25 {{ActionType::Install, ProcessType::Pre}, "PKGMGR_PARSER_PLUGIN_PRE_INSTALL"}, // NOLINT
26 {{ActionType::Upgrade, ProcessType::Pre}, "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE"}, // NOLINT
27 {{ActionType::Uninstall, ProcessType::Pre}, "PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL"}, // NOLINT
28 {{ActionType::RecoverInstall, ProcessType::Pre}, "PKGMGR_PARSER_PLUGIN_PRE_RECOVERINSTALL"}, // NOLINT
29 {{ActionType::RecoverUpgrade, ProcessType::Pre}, "PKGMGR_PARSER_PLUGIN_PRE_RECOVERUPGRADE"}, // NOLINT
30 {{ActionType::RecoverUninstall, ProcessType::Pre}, "PKGMGR_PARSER_PLUGIN_PRE_RECOVERUNINSTALL"}, // NOLINT
31 {{ActionType::Install, ProcessType::Main}, "PKGMGR_PARSER_PLUGIN_INSTALL"}, // NOLINT
32 {{ActionType::Upgrade, ProcessType::Main}, "PKGMGR_PARSER_PLUGIN_UPGRADE"}, // NOLINT
33 {{ActionType::Uninstall, ProcessType::Main}, "PKGMGR_PARSER_PLUGIN_UNINSTALL"}, // NOLINT
34 {{ActionType::Removed, ProcessType::Main}, "PKGMGR_PARSER_PLUGIN_REMOVED"}, // NOLINT
35 {{ActionType::RecoverInstall, ProcessType::Main}, "PKGMGR_PARSER_PLUGIN_RECOVERINSTALL"}, // NOLINT
36 {{ActionType::RecoverUpgrade, ProcessType::Main}, "PKGMGR_PARSER_PLUGIN_RECOVERUPGRADE"}, // NOLINT
37 {{ActionType::RecoverUninstall, ProcessType::Main}, "PKGMGR_PARSER_PLUGIN_RECOVERUNINSTALL"}, // NOLINT
38 {{ActionType::Clean, ProcessType::Main}, "PKGMGR_PARSER_PLUGIN_CLEAN"}, // NOLINT
39 {{ActionType::Undo, ProcessType::Main}, "PKGMGR_PARSER_PLUGIN_UNDO"}, // NOLINT
40 {{ActionType::Install, ProcessType::Post}, "PKGMGR_PARSER_PLUGIN_POST_INSTALL"}, // NOLINT
41 {{ActionType::Upgrade, ProcessType::Post}, "PKGMGR_PARSER_PLUGIN_POST_UPGRADE"}, // NOLINT
42 {{ActionType::Uninstall, ProcessType::Post}, "PKGMGR_PARSER_PLUGIN_POST_UNINSTALL"}, // NOLINT
43 {{ActionType::RecoverInstall, ProcessType::Post}, "PKGMGR_PARSER_PLUGIN_POST_RECOVERINSTALL"}, // NOLINT
44 {{ActionType::RecoverUpgrade, ProcessType::Post}, "PKGMGR_PARSER_PLUGIN_POST_RECOVERUPGRADE"}, // NOLINT
45 {{ActionType::RecoverUninstall, ProcessType::Post}, "PKGMGR_PARSER_PLUGIN_POST_RECOVERUNINSTALL"}, // NOLINT
48 auto pos = names.find(std::make_pair(action, process));
49 if (pos == names.end()) {
50 LOG(INFO) << "Function name not defined";
56 xmlNodePtr MakePluginNode(xmlNodePtr node, const std::string& tag_name) {
57 xmlNodePtr ret_node = nullptr;
58 if (tag_name == reinterpret_cast<const char*>(node->name)) {
59 ret_node = xmlCopyNode(node, 1);
63 for (xmlNodePtr child = xmlFirstElementChild(node);
64 child != nullptr; child = xmlNextElementSibling(child)) {
65 xmlNodePtr child_node = MakePluginNode(child, tag_name);
69 ret_node = xmlCopyNode(node, 0);
70 xmlAddChild(ret_node, child_node);
75 xmlDocPtr TagPlugin::CreateDocPtrForPlugin(xmlDocPtr doc_ptr,
76 const std::string& tag_name) const {
77 // Make copy of document and root node
78 xmlNodePtr root_node = xmlDocGetRootElement(doc_ptr);
80 LOG(ERROR) << "Original document is empty. Cannot create copy for plugin";
83 xmlDocPtr plugin_doc_ptr = xmlCopyDoc(doc_ptr, 0);
84 xmlNodePtr plugin_root_node = MakePluginNode(root_node, tag_name);
85 if (!plugin_root_node) {
86 LOG(ERROR) << "make plugin node fail";
89 xmlDocSetRootElement(plugin_doc_ptr, plugin_root_node);
90 xmlSetTreeDoc(plugin_root_node, plugin_doc_ptr);
91 return plugin_doc_ptr;
94 bool TagPlugin::Run(xmlDocPtr doc_ptr, manifest_x* manifest,
95 ActionType action_type) {
96 xmlDocPtr plugin_doc_ptr = CreateDocPtrForPlugin(doc_ptr,
98 if (!plugin_doc_ptr && action_type_ != Plugin::ActionType::Removed)
103 (action_type_ != ActionType::Unknown &&
104 action_type != ActionType::Undo &&
105 action_type != ActionType::Clean) ? action_type_ : action_type;
106 std::string pre_function = GetFunctionName(ProcessType::Pre, type);
107 Exec(pre_function, &result, manifest->package);
109 LOG(ERROR) << "Function: " << pre_function << " of plugin "
110 << plugin_info_.path() << " failed";
111 xmlFreeDoc(plugin_doc_ptr);
115 std::string main_function = GetFunctionName(ProcessType::Main, type);
116 Exec(main_function, &result, plugin_doc_ptr, manifest->package);
118 LOG(ERROR) << "Function: " << main_function << " of plugin "
119 << plugin_info_.path() << " failed";
120 xmlFreeDoc(plugin_doc_ptr);
124 std::string post_function = GetFunctionName(ProcessType::Post, type);
125 Exec(post_function, &result, manifest->package);
127 LOG(ERROR) << "Function: " << post_function << " of plugin "
128 << plugin_info_.path() << " failed";
129 xmlFreeDoc(plugin_doc_ptr);
133 xmlFreeDoc(plugin_doc_ptr);
138 } // namespace common_installer