[Release] wrt-installer_0.1.114
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_database.cpp
index ea2cd3c..49020fc 100644 (file)
  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
  * @author  Soyoung kim(sy037.kim@samsung.com)
  * @version 1.0
- * @brief   Implementation file for installer task database updating for widget update
+ * @brief   Implementation file for installer task database updating for widget
+ * update
  */
+#include <unistd.h>
 #include <time.h>
 #include <sys/stat.h>
 #include <widget_install/task_database.h>
 #include <widget_install/job_widget_install.h>
 #include <widget_install/widget_install_errors.h>
 #include <widget_install/widget_install_context.h>
-#include <web-provider-info.h>
+#include <web_provider_livebox_info.h>
 #include <dpl/wrt-dao-rw/widget_dao.h>
-#include <dpl/wrt-dao-ro/vconf_config.h>
 #include <dpl/foreach.h>
 #include <dpl/utils/wrt_utility.h>
 #include <dpl/log/log.h>
 #include <dpl/assert.h>
+#include <wrt-commons/security-origin-dao/security_origin_dao.h>
+#include <dpl/wrt-dao-ro/widget_dao_types.h>
 #include <string>
 #include <sstream>
 #include <ace_api_install.h>
 #include <ace_registration.h>
 #include <errno.h>
 #include <string.h>
-#include <vconf.h>
 #include <map>
 
 using namespace WrtDB;
 
 namespace Jobs {
 namespace WidgetInstall {
-
 TaskDatabase::TaskDatabase(InstallerContext& context) :
     DPL::TaskDecl<TaskDatabase>(this),
     m_context(context),
     m_handleToRemove(INVALID_WIDGET_HANDLE),
     m_handle(INVALID_WIDGET_HANDLE)
 {
+    AddStep(&TaskDatabase::StartStep);
     AddStep(&TaskDatabase::StepRegisterExternalFiles);
     AddStep(&TaskDatabase::StepWrtDBInsert);
     AddStep(&TaskDatabase::StepAceDBInsert);
+    AddStep(&TaskDatabase::StepSecurityOriginDBInsert);
     AddStep(&TaskDatabase::StepRemoveExternalFiles);
-    AddStep(&TaskDatabase::StepCreateVconf);
     AddStep(&TaskDatabase::StepLiveboxDBInsert);
+    AddStep(&TaskDatabase::EndStep);
 
     AddAbortStep(&TaskDatabase::StepAbortDBInsert);
+    AddAbortStep(&TaskDatabase::StepAbortAceDBInsert);
 }
 
 void TaskDatabase::StepWrtDBInsert()
@@ -70,43 +74,51 @@ void TaskDatabase::StepWrtDBInsert()
         /* Set install Time */
         time(&m_context.widgetConfig.installedTime);
 
-        if (m_context.existingWidgetInfo.isExist) //update
-        {
-            LogInfo("Registering widget... (update)");
+        if (m_context.isUpdateMode) { //update
+            LogDebug("Registering widget... (update)");
             Try
             {
                 m_handleToRemove = WidgetDAOReadOnly::getHandle(
-                    m_context.widgetConfig.tzAppid);
+                        m_context.widgetConfig.tzAppid);
+
+                std::string makeAppid =
+                    DPL::ToUTF8String(m_context.widgetConfig.tzAppid) + "." +
+                    "backup";
+                m_backAppId = DPL::FromUTF8String(makeAppid);
             }
             Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
             {
-                LogError("Given tizenId not found for update installation (Same GUID?)");
-                ThrowMsg(Exceptions::InvalidPackage, "Given tizenId not found for update installation");
+                LogError(
+                    "Given tizenId not found for update installation (Same GUID?)");
+                ThrowMsg(Exceptions::DatabaseFailure,
+                         "Given tizenId not found for update installation");
             }
-            WidgetDAO::registerOrUpdateWidget(
-                    m_context.widgetConfig.tzAppid,
-                    m_context.widgetConfig,
-                    m_context.wacSecurity);
-            m_handle = WidgetDAOReadOnly::getHandle(
-                m_context.widgetConfig.tzAppid);
-        }
-        else //new installation
-        {
-            LogInfo("Registering widget...");
+
+            WidgetDAO::updateTizenAppId(m_context.widgetConfig.tzAppid,
+                                        m_backAppId);
+            WidgetDAO::registerWidget(m_context.widgetConfig.tzAppid,
+                                      m_context.widgetConfig,
+                                      m_context.widgetSecurity);
+            m_handle =
+                WidgetDAOReadOnly::getHandle(m_context.widgetConfig.tzAppid);
+        } else { //new installation
+            LogDebug("Registering widget...");
             WidgetDAO::registerWidget(
-                    m_context.widgetConfig.tzAppid,
-                    m_context.widgetConfig,
-                    m_context.wacSecurity);
+                m_context.widgetConfig.tzAppid,
+                m_context.widgetConfig,
+                m_context.widgetSecurity);
             m_handle = WidgetDAOReadOnly::getHandle(
-                m_context.widgetConfig.tzAppid);
+                    m_context.widgetConfig.tzAppid);
         }
 
-        FOREACH (cap, m_context.staticPermittedDevCaps) {
-            LogInfo("staticPermittedDevCaps : " << cap->first
-                    << " smack status: " << cap->second);
+        FOREACH(cap, m_context.staticPermittedDevCaps) {
+            LogDebug(
+                "staticPermittedDevCaps : " << cap->first
+                                            << " smack status: " <<
+                cap->second);
         }
 
-        LogInfo("Widget registered");
+        LogDebug("Widget registered");
     }
     Catch(WidgetDAO::Exception::DatabaseError)
     {
@@ -128,35 +140,61 @@ void TaskDatabase::StepAceDBInsert()
         if (ACE_OK != ace_unregister_widget(
                 static_cast<ace_widget_handle_t>(m_handleToRemove)))
         {
-            LogWarning("Error while removing ace entry for previous insallation");
-        };
+            LogWarning(
+                "Error while removing ace entry for previous insallation");
+        }
     }
 
-    if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
-                                  m_context.wacSecurity.getCertificateList()))
+    if (!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
+                                   m_context.widgetSecurity.getCertificateList()))
     {
         LogError("ace database insert failed");
-        ThrowMsg(Exceptions::UpdateFailed, "Update failure. ace_register_widget failed");
+        ThrowMsg(Exceptions::UpdateFailed,
+                 "Update failure. ace_register_widget failed");
     }
     LogDebug("Ace data inserted");
+}
+
+void TaskDatabase::StepSecurityOriginDBInsert()
+{
+    LogDebug("Create Security origin database");
+    // automatically create security origin database
+    using namespace SecurityOriginDB;
+    using namespace WrtDB;
 
-    m_context.job->UpdateProgress(
-        InstallerContext::INSTALL_NEW_DB_INSERT,
-        "New Widget DB UPDATE Finished");
+    SecurityOriginDAO dao(m_context.locations->getPkgId());
+
+    // Checking privilege list for setting security origin exception data
+    FOREACH(it, m_context.widgetConfig.configInfo.privilegeList) {
+        std::map<std::string, Feature>::const_iterator result =
+            g_W3CPrivilegeTextMap.find(DPL::ToUTF8String(it->name));
+        if (result != g_W3CPrivilegeTextMap.end()) {
+            if (result->second == FEATURE_USER_MEDIA) {
+                dao.setPrivilegeSecurityOriginData(result->second, false);
+            } else if (result->second == FEATURE_FULLSCREEN_MODE) {
+                continue;
+            } else {
+                dao.setPrivilegeSecurityOriginData(result->second);
+            }
+        }
+    }
 }
 
 void TaskDatabase::StepRegisterExternalFiles()
 {
-    WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
-    if (m_context.existingWidgetInfo.isExist) //update
-    {
+    WrtDB::ExternalLocationList externalLocationsUpdate =
+        m_context.locations->listExternalLocations();
+    if (m_context.isUpdateMode) { //update
         Try
         {
             WidgetDAO dao(m_context.widgetConfig.tzAppid);
-            WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
+            WrtDB::ExternalLocationList externalLocationsDB =
+                dao.getWidgetExternalLocations();
             FOREACH(file, externalLocationsDB)
             {
-                if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
+                if (std::find(externalLocationsUpdate.begin(),
+                              externalLocationsUpdate.end(),
+                              *file) == externalLocationsUpdate.end())
                 {
                     m_externalLocationsToRemove.push_back(*file);
                 }
@@ -164,8 +202,10 @@ void TaskDatabase::StepRegisterExternalFiles()
         }
         Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
         {
-            LogError("Given tizenId not found for update installation (Same GUID?)");
-            ThrowMsg(Exceptions::UpdateFailed, "Given tizenId not found for update installation");
+            LogError(
+                "Given tizenId not found for update installation (Same GUID?)");
+            ThrowMsg(Exceptions::UpdateFailed,
+                     "Given tizenId not found for update installation");
         }
     }
     LogDebug("Registering external files:");
@@ -180,93 +220,66 @@ void TaskDatabase::StepRegisterExternalFiles()
 
 void TaskDatabase::StepRemoveExternalFiles()
 {
-    if(!m_externalLocationsToRemove.empty())
-    {
+    if (!m_externalLocationsToRemove.empty()) {
         LogDebug("Removing external files:");
     }
 
     FOREACH(file, m_externalLocationsToRemove)
     {
-        if(WrtUtilFileExists(*file))
-        {
+        if (WrtUtilFileExists(*file)) {
             LogDebug("  -> " << *file);
             if (-1 == TEMP_FAILURE_RETRY(remove(file->c_str()))) {
                 ThrowMsg(Exceptions::RemovingFileFailure,
                          "Failed to remove external file");
             }
-        }
-        else if(WrtUtilDirExists(*file))
-        {
+        } else if (WrtUtilDirExists(*file)) {
             LogDebug("  -> " << *file);
-            if(!WrtUtilRemove(*file)){
+            if (!WrtUtilRemove(*file)) {
                 ThrowMsg(Exceptions::RemovingFolderFailure,
-                        "Failed to remove external directory");
+                         "Failed to remove external directory");
             }
-        }
-        else
-        {
+        } else {
             LogWarning("  -> " << *file << "(no such a path)");
         }
     }
 }
 
-void TaskDatabase::StepCreateVconf()
-{
-    LogDebug("StepCreateVconf");
-    std::map<std::string, WrtDB::SettingsType> vconfData;
-    vconfData[
-        WrtDB::VconfConfig::GetVconfKeyPopupUsage(
-            m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
-    vconfData[
-        WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
-            m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
-    vconfData[
-        WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
-            m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
-    vconfData[
-        WrtDB::VconfConfig::GetVconfKeyWebDatabaseUsage(
-            m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
-    vconfData[
-        WrtDB::VconfConfig::GetVconfKeyFilesystemUsage(
-            m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
-    vconfData[
-        WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
-            m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_OFF;
-
-    // vconftool -g 5000 set -t int <path> initialize value
-    // Current installer should use vconftool for setting group ID
-    // In case of install application by pkgcmd, permission for others
-    // set to read-only
-    FOREACH(it, vconfData) {
-        std::ostringstream command;
-        command << "vconftool -g 5000 set -t int ";
-        command << (*it).first;
-        command << " \"" << static_cast<int>((*it).second) << "\"";
-        int ret = system(command.str().c_str());
-        if (-1 == ret) {
-            ThrowMsg(Exceptions::CreateVconfFailure, "Failed to create vconf files");
-        }
-    }
-}
-
 void TaskDatabase::StepAbortDBInsert()
 {
     LogWarning("[DB Update Task] Aborting... (DB Clean)");
     Try
     {
-        WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
+        if (m_context.isUpdateMode) {
+            WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
+            WidgetDAO::updateTizenAppId(m_backAppId,
+                                        m_context.widgetConfig.tzAppid);
+        } else {
+            WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
+        }
         LogDebug("Cleaning DB successful!");
     }
     Catch(DPL::DB::SqlConnection::Exception::Base)
     {
         LogError("Failed to handle StepAbortDBClean!");
     }
+}
+
+void TaskDatabase::StepAbortAceDBInsert()
+{
+    LogWarning("[DB Update Task] ACE DB Aborting... (DB Clean)");
 
     ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
     // Remove also old one. If it was already updated nothing wrong will happen,
     // but if not old widget will be removed.
-    if (INVALID_WIDGET_HANDLE != m_handleToRemove)
+    if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
         ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
+    }
+
+    if (!AceApi::registerAceWidgetFromDB(m_handleToRemove))
+    {
+        LogError("ace database restore failed");
+    }
+    LogDebug("Ace data inserted");
 }
 
 void TaskDatabase::StepLiveboxDBInsert()
@@ -277,28 +290,43 @@ void TaskDatabase::StepLiveboxDBInsert()
 
     std::string tizenId = DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
 
+    // insert specific information to web livebox db
     for (auto it = m_context.widgetConfig.configInfo.m_livebox.begin();
-            it != m_context.widgetConfig.configInfo.m_livebox.end(); ++it)
+         it != m_context.widgetConfig.configInfo.m_livebox.end(); ++it)
     {
         std::string boxId = DPL::ToUTF8String((**it).m_liveboxId);
         std::string boxType;
-        if ((**it).m_type == L"") {
-            boxType = web_provider_info_get_default_type();
+        if ((**it).m_type.empty()) {
+            boxType = web_provider_livebox_get_default_type();
         } else {
             boxType = DPL::ToUTF8String((**it).m_type);
         }
-        LogInfo("livebox id: " << boxId);
-        LogInfo("livebox type: " << boxType);
+        LogDebug("livebox id: " << boxId);
+        LogDebug("livebox type: " << boxType);
 
-        int ret =
-            web_provider_info_insert_box_type(
-                    boxId.c_str(), tizenId.c_str(), boxType.c_str());
+        int autoLaunch = (**it).m_autoLaunch == L"true" ? 1 : 0;
+        LogDebug("livebox auto-launch: " << autoLaunch);
 
-        if (ret < 0) {
-            LogDebug("failed to set type of livebox: " << boxId);
-        }
+        int mouseEvent = (**it).m_boxInfo.m_boxMouseEvent == L"true" ? 1 : 0;
+        LogDebug("livebox mouse-event: " << mouseEvent);
+
+        int pdFastOpen = (**it).m_boxInfo.m_pdFastOpen == L"true" ? 1 : 0;
+        LogDebug("livebox pd fast-open: " << pdFastOpen);
+
+        web_provider_livebox_insert_box_info(
+                boxId.c_str(), tizenId.c_str(), boxType.c_str(),
+                autoLaunch, mouseEvent, pdFastOpen);
     }
 }
 
+void TaskDatabase::StartStep()
+{
+    LogDebug("--------- <TaskDatabase> : START ----------");
+}
+
+void TaskDatabase::EndStep()
+{
+    LogDebug("--------- <TaskDatabase> : END ----------");
+}
 } //namespace WidgetInstall
 } //namespace Jobs