2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 <plugin_install/job_plugin_install.h>
23 #include <job_exception_base.h>
24 #include <plugin_install/plugin_objects.h>
26 using namespace WrtDB;
30 InstallerLogic::InstallerLogic() :
35 InstallerLogic::~InstallerLogic()
37 Assert(m_jobs.empty() && "There are still running jobs");
38 //FIXME what should be done here?
41 void InstallerLogic::Initialize()
46 void InstallerLogic::Terminate()
48 //TODO how to delete, if it is still running, paused and so on
51 it->second->SetPaused(true); //FIXME this is not enough!
57 Jobs::JobHandle InstallerLogic::AddAndStartJob(Jobs::Job *job)
59 Jobs::JobHandle handle = GetNewJobHandle();
60 job->SetJobHandle(handle);
62 m_jobs.insert(std::make_pair(handle, job));
65 CONTROLLER_POST_EVENT(InstallerController,
66 InstallerControllerEvents::NextStepEvent(job));
71 //InstallWidget, UninstallWidget InstallPlugin method are almost the same
72 // But each Job has different constructor, so creating new Job is specific
73 // i.e. widgetHandle, path etc...
74 //Jobs::JobHandle InstallerLogic::InstallWidget(std::string const & widgetPath,
75 // const WidgetInstallationStruct &installerStruct)
77 // LogDebug("New Widget Installation:");
80 // new Jobs::WidgetInstall::JobWidgetInstall(widgetPath, installerStruct);
82 // return AddAndStartJob(job);
85 //Jobs::JobHandle InstallerLogic::UninstallWidget(WidgetHandle widgetHandle,
86 // const WidgetUninstallationStruct &uninstallerStruct)
88 // LogDebug("New Widget Uninstallation");
91 // new Jobs::WidgetUninstall::JobWidgetUninstall(widgetHandle,
92 // uninstallerStruct);
94 // return AddAndStartJob(job);
97 Jobs::JobHandle InstallerLogic::InstallPlugin(std::string const & pluginPath,
98 const PluginInstallerStruct &installerStruct)
100 LogDebug("New Plugin Installation");
103 new Jobs::PluginInstall::JobPluginInstall(pluginPath, installerStruct);
105 return AddAndStartJob(job);
108 #define TRANSLATE_JOB_EXCEPTION() \
109 _rethrown_exception.getParam()
110 #define TRANSLATE_JOB_MESSAGE() \
111 _rethrown_exception.GetMessage()
113 bool InstallerLogic::NextStep(Jobs::Job *job)
116 bool stepSucceded = job->NextStep();
121 return !job->IsPaused();
124 if (!job->GetUndoType()) {
125 //job successfully finished
127 //send finished callback
128 job->SendFinishedSuccess();
130 switch (job->GetInstallationType()) {
131 case Jobs::PluginInstallation:
132 //todo move it somewhere
133 InstallWaitingPlugins();
135 default: //because of warning
139 //job abort process completed
140 job->SendFinishedFailure();
144 m_jobs.erase(job->GetJobHandle());
149 catch (Jobs::JobExceptionBase &exc) {
151 LogInfo("Exception occured: " << exc.getParam() <<
152 ". Reverting job...");
153 bool hasAbortSteps = job->Abort();
154 job->SetUndoType(true);
155 job->SaveExceptionData(exc);
157 if (!hasAbortSteps) {
159 job->SendFinishedFailure();
162 m_jobs.erase(job->GetJobHandle());
165 return hasAbortSteps;
169 //TODO this should be moved somewhere...when it should take place? after widget
171 //void InstallerLogic::InstallDeferredWidgetPackages()
173 // LogWarning("Not implemented");
174 // // LogInfo("Installing deferred widget packages...");
176 // // WidgetPackageList packages = GlobalDAO::GetDefferedWidgetPackageInstallationList();
178 // // LogInfo(packages.size() << " widget package(s) to install");
180 // // // Make a copy of widget packages to install, because some
181 // // // widget packages may still fail because they are running
182 // // m_packagesToInstall = packages;
184 // // // Start processing
185 // // InstallSingleDeferredPackage();
188 //void InstallerLogic::InstallSingleDeferredPackage()
190 // LogWarning("Not implemented");
191 // // if (m_packagesToInstall.empty())
194 // // // Take single package
195 // // DPL::String widgetPackage = m_packagesToInstall.front();
196 // // m_packagesToInstall.pop_front();
198 // // // Remove it from DB
199 // // GlobalDAO::RemoveDefferedWidgetPackageInstallation(widgetPackage);
201 // // // Begin installation
202 // // LogInfo("Installing deferred widget package: " << widgetPackage);
204 // // // Post installation
205 // // CONTROLLER_POST_EVENT(
206 // // InstallerController, InstallerControllerEvents::InstallWidgetEvent(
207 // // DPL::ToUTF8String(widgetPackage).c_str(), WidgetInstallationStruct(
208 // // &DummyInstallCallback, &DummyProgressCallback, NULL,
209 // // WidgetUpdateMode::PolicyWac)));
212 void InstallerLogic::InstallWaitingPlugins()
214 PluginHandleSetPtr waitingPlugins;
217 PluginDAO::getPluginHandleByStatus(PluginDAO::INSTALLATION_WAITING);
219 FOREACH(it, *waitingPlugins)
221 resolvePluginDependencies(*it);
225 bool InstallerLogic::resolvePluginDependencies(PluginHandle handle)
227 PluginHandleSetPtr dependencies(new PluginHandleSet);
229 PluginObjects::ObjectsPtr requiredObjects =
230 PluginDAO::getRequiredObjectsForPluginHandle(handle);
232 PluginHandle depHandle =
233 Jobs::PluginInstall::JobPluginInstall::INVALID_HANDLE;
235 FOREACH(requiredObject, *requiredObjects)
238 PluginDAO::getPluginHandleForImplementedObject(*requiredObject);
241 Jobs::PluginInstall::JobPluginInstall::INVALID_HANDLE) {
242 LogError("Library implementing: " <<
243 *requiredObject << " NOT FOUND");
245 //PluginDAO::SetPluginInstallationStatus(INSTALLATION_WAITING);
248 dependencies->insert(depHandle);
251 PluginDAO::registerPluginLibrariesDependencies(handle, dependencies);
252 PluginDAO::setPluginInstallationStatus(handle,
253 PluginDAO::INSTALLATION_COMPLETED);