[Path usage Unification] Plugin Installation part unification
[framework/web/wrt-installer.git] / src / logic / installer_logic.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #include <installer_logic.h>
17 #include <installer_controller.h>
18 #include <dpl/string.h>
19 #include <dpl/foreach.h>
20 #include <dpl/wrt-dao-rw/feature_dao.h>
21 #include <dpl/wrt-dao-rw/plugin_dao.h>
22 #include <widget_install/job_widget_install.h>
23 #include <widget_uninstall/job_widget_uninstall.h>
24 #include <plugin_install/job_plugin_install.h>
25 #include <job_exception_base.h>
26 #include <plugin_install/plugin_objects.h>
27
28 using namespace WrtDB;
29
30 namespace Logic {
31 InstallerLogic::InstallerLogic() :
32     m_NextHandle(0),m_job(0)
33 {}
34
35 InstallerLogic::~InstallerLogic()
36 {
37         Assert(!m_job && "There are still running job");
38     //FIXME what should be done here?
39 }
40
41 void InstallerLogic::Initialize()
42 {
43     LogDebug("Done");
44 }
45
46 void InstallerLogic::Terminate()
47 {
48     //TODO how to delete, if it is still running, paused and so on
49     if(m_job)
50         m_job->SetPaused(true);
51
52     LogDebug("Done");
53 }
54
55 Jobs::JobHandle InstallerLogic::AddAndStartJob()
56 {
57     Jobs::JobHandle handle = GetNewJobHandle();
58     m_job->SetJobHandle(handle);
59     //Start job
60     CONTROLLER_POST_EVENT(InstallerController,
61                           InstallerControllerEvents::NextStepEvent(m_job));
62
63     return handle;
64 }
65
66 //InstallWidget, UninstallWidget InstallPlugin method are almost the same
67 // But each Job has different constructor, so creating new Job is specific
68 Jobs::JobHandle InstallerLogic::InstallWidget(
69     const std::string & widgetPath,
70     const WidgetInstallationStruct &
71     installerStruct)
72 {
73     if(m_job)
74     {
75         LogError("Job is in progress. It is impossible to add new job");
76         return -1;
77     }
78
79     LogDebug("New Widget Installation:");
80
81     m_job =
82         new Jobs::WidgetInstall::JobWidgetInstall(widgetPath, installerStruct);
83
84
85     return AddAndStartJob();
86 }
87
88 Jobs::JobHandle InstallerLogic::UninstallWidget(
89     const std::string & widgetPkgName,
90     const
91     WidgetUninstallationStruct &uninstallerStruct)
92 {
93     if(m_job)
94     {
95         LogError("Job is in progress. It is impossible to add new job");
96         return -1;
97     }
98
99     LogDebug("New Widget Uninstallation");
100
101     m_job  =
102         new Jobs::WidgetUninstall::JobWidgetUninstall(widgetPkgName,
103                                                       uninstallerStruct);
104
105       return AddAndStartJob();
106 }
107
108 Jobs::JobHandle InstallerLogic::InstallPlugin(
109     std::string const & pluginPath,
110     const PluginInstallerStruct &
111     installerStruct)
112 {
113     if(m_job)
114     {
115         LogError("Job is in progress. It is impossible to add new job");
116         return -1;
117     }
118
119     LogDebug("New Plugin Installation");
120
121     //Conversion to DPL::Utils::Path is temporary
122     m_job =
123         new Jobs::PluginInstall::JobPluginInstall(DPL::Utils::Path(pluginPath), installerStruct);
124
125     // before start install plugin, reset plugin data which is stopped
126     // during installing. (PluginDAO::INSTALLATION_IN_PROGRESS)
127     ResetProgressPlugins();
128
129     return AddAndStartJob();
130 }
131
132 #define TRANSLATE_JOB_EXCEPTION() \
133     _rethrown_exception.getParam()
134 #define TRANSLATE_JOB_MESSAGE() \
135     _rethrown_exception.GetMessage()
136
137 bool InstallerLogic::NextStep(Jobs::Job *job)
138 {
139     Try {
140         bool stepSucceded = job->NextStep();
141
142         job->SendProgress();
143
144         if (stepSucceded) {
145             return !job->IsPaused();
146         }
147
148         if (!job->GetUndoType()) {
149             //job successfully finished
150
151             //send finished callback
152             job->SendFinishedSuccess();
153
154             switch (job->GetInstallationType()) {
155             case Jobs::PluginInstallation:
156                 //todo move it somewhere
157                 InstallWaitingPlugins();
158                 break;
159             default: //because of warning
160                 break;
161             }
162         } else {
163             //job abort process completed
164             job->SendFinishedFailure();
165         }
166
167         //clean job
168         delete job;
169         m_job=0;
170
171         return false;
172     } catch (Jobs::JobExceptionBase &exc) {
173         //start revert job
174         LogInfo("Exception occured: " << exc.getParam() <<
175                 ". Reverting job...");
176         bool hasAbortSteps = job->Abort();
177         job->SetUndoType(true);
178         job->SaveExceptionData(exc);
179
180         if (!hasAbortSteps) {
181             //no AbortSteps
182             job->SendFinishedFailure();
183
184             //clean job
185             delete job;
186             m_job=0;
187         }
188         return hasAbortSteps;
189     }
190 }
191
192 void InstallerLogic::InstallWaitingPlugins()
193 {
194     PluginHandleSetPtr waitingPlugins;
195
196     waitingPlugins =
197         PluginDAO::getPluginHandleByStatus(PluginDAO::INSTALLATION_WAITING);
198
199     FOREACH(it, *waitingPlugins)
200     {
201         resolvePluginDependencies(*it);
202     }
203 }
204
205 void InstallerLogic::ResetProgressPlugins()
206 {
207     PluginHandleSetPtr progressPlugins;
208
209     progressPlugins =
210         PluginDAO::getPluginHandleByStatus(PluginDAO::INSTALLATION_IN_PROGRESS);
211
212     FOREACH(it, *progressPlugins) {
213         FeatureHandleListPtr featureListPtr =
214             FeatureDAOReadOnly::GetFeatureHandleListForPlugin(*it);
215         FOREACH(ItFeature, *featureListPtr) {
216             FeatureDAO::UnregisterFeature(*ItFeature);
217         }
218         PluginDAO::unregisterPlugin(*it);
219     }
220 }
221
222 bool InstallerLogic::resolvePluginDependencies(PluginHandle handle)
223 {
224     PluginHandleSetPtr dependencies(new PluginHandleSet);
225
226     PluginObjects::ObjectsPtr requiredObjects =
227         PluginDAO::getRequiredObjectsForPluginHandle(handle);
228
229     PluginHandle depHandle =
230         Jobs::PluginInstall::JobPluginInstall::INVALID_HANDLE;
231
232     FOREACH(requiredObject, *requiredObjects)
233     {
234         depHandle =
235             PluginDAO::getPluginHandleForImplementedObject(*requiredObject);
236
237         if (depHandle ==
238             Jobs::PluginInstall::JobPluginInstall::INVALID_HANDLE)
239         {
240             LogError("Library implementing: " <<
241                      *requiredObject << " NOT FOUND");
242
243             //PluginDAO::SetPluginInstallationStatus(INSTALLATION_WAITING);
244             return false;
245         }
246         dependencies->insert(depHandle);
247     }
248
249     PluginDAO::registerPluginLibrariesDependencies(handle, dependencies);
250     PluginDAO::setPluginInstallationStatus(handle,
251                                            PluginDAO::INSTALLATION_COMPLETED);
252
253     return true;
254 }
255 }
256