Fix build warning
[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 #include <installer_log.h>
28
29 using namespace WrtDB;
30
31 namespace Logic {
32 InstallerLogic::InstallerLogic() :
33     m_job(0),
34     m_NextHandle(0)
35 {}
36
37 InstallerLogic::~InstallerLogic()
38 {
39         Assert(!m_job && "There are still running job");
40     //FIXME what should be done here?
41 }
42
43 void InstallerLogic::Initialize()
44 {
45     _D("Done");
46 }
47
48 void InstallerLogic::Terminate()
49 {
50     //TODO how to delete, if it is still running, paused and so on
51     if(m_job)
52         m_job->SetPaused(true);
53
54     _D("Done");
55 }
56
57 Jobs::JobHandle InstallerLogic::AddAndStartJob()
58 {
59     Jobs::JobHandle handle = GetNewJobHandle();
60     m_job->SetJobHandle(handle);
61     //Start job
62     CONTROLLER_POST_EVENT(InstallerController,
63                           InstallerControllerEvents::NextStepEvent(m_job));
64
65     return handle;
66 }
67
68 //InstallWidget, UninstallWidget InstallPlugin method are almost the same
69 // But each Job has different constructor, so creating new Job is specific
70 Jobs::JobHandle InstallerLogic::InstallWidget(
71     const std::string & widgetPath,
72     const Jobs::WidgetInstall::WidgetInstallationStruct &
73     installerStruct)
74 {
75     if(m_job)
76     {
77         _E("Job is in progress. It is impossible to add new job");
78         return -1;
79     }
80
81     _D("New Widget Installation:");
82
83     m_job =
84         new Jobs::WidgetInstall::JobWidgetInstall(widgetPath, installerStruct);
85
86
87     return AddAndStartJob();
88 }
89
90 Jobs::JobHandle InstallerLogic::UninstallWidget(
91     const std::string & widgetPkgName,
92     const
93     WidgetUninstallationStruct &uninstallerStruct)
94 {
95     if(m_job)
96     {
97         _E("Job is in progress. It is impossible to add new job");
98         return -1;
99     }
100
101     _D("New Widget Uninstallation");
102
103     m_job  =
104         new Jobs::WidgetUninstall::JobWidgetUninstall(widgetPkgName,
105                                                       uninstallerStruct);
106
107       return AddAndStartJob();
108 }
109
110 Jobs::JobHandle InstallerLogic::InstallPlugin(
111     std::string const & pluginPath,     // TODO change type to PluginPath
112     const PluginInstallerStruct &
113     installerStruct)
114 {
115     if(m_job)
116     {
117         _E("Job is in progress. It is impossible to add new job");
118         return -1;
119     }
120
121     _D("New Plugin Installation");
122
123     // TODO Conversion to PluginPath is temporary
124     m_job =
125         new Jobs::PluginInstall::JobPluginInstall(PluginPath(pluginPath), installerStruct);
126
127     // before start install plugin, reset plugin data which is stopped
128     // during installing. (PluginDAO::INSTALLATION_IN_PROGRESS)
129     ResetProgressPlugins();
130
131     return AddAndStartJob();
132 }
133
134 #define TRANSLATE_JOB_EXCEPTION() \
135     _rethrown_exception.getParam()
136 #define TRANSLATE_JOB_MESSAGE() \
137     _rethrown_exception.GetMessage()
138
139 bool InstallerLogic::NextStep(Jobs::Job *job)
140 {
141     Try {
142         bool stepSucceded = job->NextStep();
143
144         job->SendProgress();
145
146         if (stepSucceded) {
147             return !job->IsPaused();
148         }
149
150         if (!job->GetAbortStarted()) {
151             //job successfully finished
152
153             //send finished callback
154             job->SendFinishedSuccess();
155
156             switch (job->GetInstallationType()) {
157             case Jobs::PluginInstallation:
158                 InstallWaitingPlugins();
159                 break;
160             default: //because of warning
161                 break;
162             }
163         } else {
164             //job abort process completed
165             job->SendFinishedFailure();
166         }
167
168         //clean job
169         delete job;
170         m_job=0;
171
172         return false;
173     } catch (Jobs::JobExceptionBase &exc) {
174         //start revert job
175         _D("Exception occured: %d. Reverting job...", exc.getParam());
176         bool hasAbortSteps = job->Abort();
177         job->SetAbortStarted(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             _E("Library implementing: %s NOT FOUND", (*requiredObject).c_str());
241
242             //PluginDAO::SetPluginInstallationStatus(INSTALLATION_WAITING);
243             return false;
244         }
245         dependencies->insert(depHandle);
246     }
247
248     PluginDAO::registerPluginLibrariesDependencies(handle, dependencies);
249     PluginDAO::setPluginInstallationStatus(handle,
250                                            PluginDAO::INSTALLATION_COMPLETED);
251
252     return true;
253 }
254 }
255