Add send signal if there is not enough memory during web app installation
[framework/web/wrt-installer.git] / src / logic / installer_logic.cpp
index 10cda89..df67764 100644 (file)
 #include <plugin_install/job_plugin_install.h>
 #include <job_exception_base.h>
 #include <plugin_install/plugin_objects.h>
+#include <installer_log.h>
 
 using namespace WrtDB;
 
 namespace Logic {
 InstallerLogic::InstallerLogic() :
+    m_job(0),
     m_NextHandle(0)
 {}
 
 InstallerLogic::~InstallerLogic()
 {
-    Assert(m_jobs.empty() && "There are still running jobs");
+        Assert(!m_job && "There are still running job");
     //FIXME what should be done here?
 }
 
 void InstallerLogic::Initialize()
 {
-    LogDebug("Done");
+    _D("Done");
 }
 
 void InstallerLogic::Terminate()
 {
     //TODO how to delete, if it is still running, paused and so on
-    FOREACH(it, m_jobs)
-    {
-        it->second->SetPaused(true); //FIXME this is not enough!
-    }
+    if(m_job)
+        m_job->SetPaused(true);
 
-    LogDebug("Done");
+    _D("Done");
 }
 
-Jobs::JobHandle InstallerLogic::AddAndStartJob(Jobs::Job *job)
+Jobs::JobHandle InstallerLogic::AddAndStartJob()
 {
     Jobs::JobHandle handle = GetNewJobHandle();
-    job->SetJobHandle(handle);
-
-    m_jobs.insert(std::make_pair(handle, job));
-
+    m_job->SetJobHandle(handle);
     //Start job
     CONTROLLER_POST_EVENT(InstallerController,
-                          InstallerControllerEvents::NextStepEvent(job));
+                          InstallerControllerEvents::NextStepEvent(m_job));
 
     return handle;
 }
@@ -72,15 +69,21 @@ Jobs::JobHandle InstallerLogic::AddAndStartJob(Jobs::Job *job)
 // But each Job has different constructor, so creating new Job is specific
 Jobs::JobHandle InstallerLogic::InstallWidget(
     const std::string & widgetPath,
-    const WidgetInstallationStruct &
+    const std::string & pkgId,
+    const Jobs::WidgetInstall::WidgetInstallationStruct &
     installerStruct)
 {
-    LogDebug("New Widget Installation:");
+    if(m_job)
+    {
+        _E("Job is in progress. It is impossible to add new job");
+        return -1;
+    }
 
-    Jobs::Job *job =
-        new Jobs::WidgetInstall::JobWidgetInstall(widgetPath, installerStruct);
+    _D("New Widget Installation:");
 
-    return AddAndStartJob(job);
+    m_job = new Jobs::WidgetInstall::JobWidgetInstall(widgetPath, pkgId, installerStruct);
+
+    return AddAndStartJob();
 }
 
 Jobs::JobHandle InstallerLogic::UninstallWidget(
@@ -88,28 +91,43 @@ Jobs::JobHandle InstallerLogic::UninstallWidget(
     const
     WidgetUninstallationStruct &uninstallerStruct)
 {
-    LogDebug("New Widget Uninstallation");
+    if(m_job)
+    {
+        _E("Job is in progress. It is impossible to add new job");
+        return -1;
+    }
+
+    _D("New Widget Uninstallation");
 
-    Jobs::Job *job =
+    m_job  =
         new Jobs::WidgetUninstall::JobWidgetUninstall(widgetPkgName,
                                                       uninstallerStruct);
 
-    return AddAndStartJob(job);
+      return AddAndStartJob();
 }
 
 Jobs::JobHandle InstallerLogic::InstallPlugin(
-    std::string const & pluginPath,
+    std::string const & pluginPath,     // TODO change type to PluginPath
     const PluginInstallerStruct &
     installerStruct)
 {
-    LogDebug("New Plugin Installation");
+    if(m_job)
+    {
+        _E("Job is in progress. It is impossible to add new job");
+        return -1;
+    }
+
+    _D("New Plugin Installation");
+
+    // TODO Conversion to PluginPath is temporary
+    m_job =
+        new Jobs::PluginInstall::JobPluginInstall(PluginPath(pluginPath), installerStruct);
 
-    Jobs::Job *job =
-        new Jobs::PluginInstall::JobPluginInstall(pluginPath, installerStruct);
     // before start install plugin, reset plugin data which is stopped
     // during installing. (PluginDAO::INSTALLATION_IN_PROGRESS)
     ResetProgressPlugins();
-    return AddAndStartJob(job);
+
+    return AddAndStartJob();
 }
 
 #define TRANSLATE_JOB_EXCEPTION() \
@@ -128,7 +146,7 @@ bool InstallerLogic::NextStep(Jobs::Job *job)
             return !job->IsPaused();
         }
 
-        if (!job->GetUndoType()) {
+        if (!job->GetAbortStarted()) {
             //job successfully finished
 
             //send finished callback
@@ -136,7 +154,6 @@ bool InstallerLogic::NextStep(Jobs::Job *job)
 
             switch (job->GetInstallationType()) {
             case Jobs::PluginInstallation:
-                //todo move it somewhere
                 InstallWaitingPlugins();
                 break;
             default: //because of warning
@@ -148,16 +165,15 @@ bool InstallerLogic::NextStep(Jobs::Job *job)
         }
 
         //clean job
-        m_jobs.erase(job->GetJobHandle());
         delete job;
+        m_job=0;
 
         return false;
     } catch (Jobs::JobExceptionBase &exc) {
         //start revert job
-        LogInfo("Exception occured: " << exc.getParam() <<
-                ". Reverting job...");
+        _D("Exception occured: %d. Reverting job...", exc.getParam());
         bool hasAbortSteps = job->Abort();
-        job->SetUndoType(true);
+        job->SetAbortStarted(true);
         job->SaveExceptionData(exc);
 
         if (!hasAbortSteps) {
@@ -165,8 +181,8 @@ bool InstallerLogic::NextStep(Jobs::Job *job)
             job->SendFinishedFailure();
 
             //clean job
-            m_jobs.erase(job->GetJobHandle());
             delete job;
+            m_job=0;
         }
         return hasAbortSteps;
     }
@@ -220,8 +236,7 @@ bool InstallerLogic::resolvePluginDependencies(PluginHandle handle)
         if (depHandle ==
             Jobs::PluginInstall::JobPluginInstall::INVALID_HANDLE)
         {
-            LogError("Library implementing: " <<
-                     *requiredObject << " NOT FOUND");
+            _E("Library implementing: %s NOT FOUND", (*requiredObject).c_str());
 
             //PluginDAO::SetPluginInstallationStatus(INSTALLATION_WAITING);
             return false;