0dc1b02aacddd9ea6d2e3cc92ef7696638ca1870
[platform/core/appfw/app-installers.git] / src / common / step / pkgmgr / step_run_parser_plugins.cc
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 #include "common/step/pkgmgr/step_run_parser_plugins.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "common/pkgmgr_registration.h"
11 #include "common/plugins/plugin_manager.h"
12
13 namespace common_installer {
14 namespace pkgmgr {
15
16 bool StepRunParserPlugin::InitPluginManager(
17     const boost::filesystem::path& xml_path, manifest_x* manifest, uid_t uid) {
18   // PLUGINS_LIST_INSTALL_PATH path generated from cmake
19   const std::string listPath(PLUGINS_LIST_INSTALL_PATH);
20   plugin_manager_.reset(
21     new PluginManager(xml_path.string(), listPath, context_->pkgid.get(),
22       manifest, uid));
23   if (!plugin_manager_.get()) {
24     LOG(ERROR) << "plugin_manager_ reset fail, plugin_manager_ is NULL";
25     return false;
26   }
27
28   if (!plugin_manager_.get()->LoadPlugins(action_type_)) {
29     LOG(ERROR) << "Loading plugins failed in progress";
30     return false;
31   }
32
33   return true;
34 }
35
36 StepRunParserPlugin::StepRunParserPlugin(
37     InstallerContext* context, Plugin::ActionType action_type)
38     : Step(context), action_type_(action_type) {}
39
40 bool StepRunParserPlugin::ProcessPlugins(
41     const boost::filesystem::path& xml_path, manifest_x* manifest,
42     Plugin::ActionType action_type) {
43   if (!plugin_manager_.get()) {
44     if (!InitPluginManager(xml_path, manifest, context_->uid.get()))
45       return false;
46   }
47   if (!plugin_manager_.get()->RunPlugins(action_type))
48     return false;
49
50   LOG(INFO) << "Processing plugins done";
51   return true;
52 }
53
54 Step::Status StepRunParserPlugin::process() {
55   if (context_->force_clean_from_db.get())
56       return Status::OK;
57   if (!ProcessPlugins(context_->xml_path.get(), context_->manifest_data.get(),
58                      action_type_))
59     return Status::ERROR;
60
61   switch (action_type_) {
62     case Plugin::ActionType::Install:
63       if (!RegisterPluginInfo(context_->manifest_data.get(),
64                               context_->uid.get(),
65                               context_->request_mode.get())) {
66         LOG(ERROR) << "Failed to register plugin execution info";
67         return Step::Status::REGISTER_ERROR;
68       }
69       break;
70     case Plugin::ActionType::Upgrade:
71       if (!UpdatePluginInfo(context_->manifest_data.get(),
72                             context_->uid.get(),
73                             context_->request_mode.get())) {
74         LOG(ERROR) << "Failed to update plugin execution info";
75         return Step::Status::REGISTER_ERROR;
76       }
77       break;
78     case Plugin::ActionType::Uninstall:
79       if (!UnregisterPluginInfo(context_->pkgid.get(),
80                                 context_->uid.get(),
81                                 context_->request_mode.get())) {
82         LOG(ERROR) << "Failed to remove plugin execution info";
83         return Step::Status::REGISTER_ERROR;
84       }
85       break;
86     default:
87       break;
88   }
89   return Step::Status::OK;
90 }
91
92 Step::Status StepRunParserPlugin::undo() {
93   Step::Status ret = Status::OK;
94   switch (action_type_) {
95     case Plugin::ActionType::Install:
96       if (!UnregisterPluginInfo(context_->pkgid.get(),
97                                 context_->uid.get(),
98                                 context_->request_mode.get())) {
99         LOG(ERROR) << "Failed to undo registering plugin execution info";
100         ret = Step::Status::REGISTER_ERROR;
101       }
102       break;
103     case Plugin::ActionType::Upgrade:
104       if (!UnregisterPluginInfo(context_->pkgid.get(),
105                                 context_->uid.get(),
106                                 context_->request_mode.get())) {
107         LOG(ERROR) << "Failed to revert plugin execution info";
108         ret = Step::Status::REGISTER_ERROR;
109       }
110
111       if (!RegisterPluginInfo(context_->old_manifest_data.get(),
112                               context_->uid.get(),
113                               context_->request_mode.get())) {
114         LOG(ERROR) << "Failed to revert plugin execution info";
115         ret = Step::Status::REGISTER_ERROR;
116       }
117       break;
118     case Plugin::ActionType::Uninstall:
119       if (!RegisterPluginInfo(context_->manifest_data.get(),
120                               context_->uid.get(),
121                               context_->request_mode.get())) {
122         LOG(ERROR) << "Failed to remove plugin execution info";
123         ret = Step::Status::REGISTER_ERROR;
124       }
125       break;
126     default:
127       break;
128   }
129
130   // For update, we need to reread configuration of old version of widget
131   // so running whole process from beginning
132   if (action_type_ == Plugin::ActionType::Install) {
133     if (!ProcessPlugins(context_->xml_path.get(),
134                         context_->manifest_data.get(),
135                         Plugin::ActionType::Undo))
136       ret = Status::ERROR;
137   } else if (action_type_ == Plugin::ActionType::Upgrade) {
138     plugin_manager_.reset();
139     if (!ProcessPlugins(context_->backup_xml_path.get(),
140                         context_->old_manifest_data.get(),
141                         Plugin::ActionType::Undo))
142       ret = Status::ERROR;
143   }
144   return ret;
145 }
146
147 Step::Status StepRunParserPlugin::clean() {
148   ProcessPlugins(context_->xml_path.get(),
149                  context_->manifest_data.get(),
150                  Plugin::ActionType::Clean);
151
152   return Status::OK;
153 }
154
155 }  // namespace pkgmgr
156 }  // namespace common_installer