Initialize Tizen 2.3
[framework/web/wrt-installer.git] / src_wearable / jobs / widget_install / task_recovery.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 /**
17  * @file    task_recovery.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task recovery
21  */
22 #include "task_recovery.h"
23
24 #include <dpl/log/log.h>
25 #include <dpl/errno_string.h>
26 #include <dpl/foreach.h>
27
28 #include <dpl/wrt-dao-ro/widget_config.h>
29 #include <dpl/wrt-dao-ro/global_config.h>
30 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
31 #include <dpl/wrt-dao-rw/widget_dao.h>
32 #include <dpl/utils/wrt_utility.h>
33 #include <dpl/utils/path.h>
34 #include <ace_api_install.h>
35 #include <ace_registration.h>
36 #include <ace-common/ace_api_common.h>
37
38 #include <widget_install/job_widget_install.h>
39 #include <widget_install/widget_install_context.h>
40 #include <widget_install/widget_install_errors.h>
41 #include <installer_log.h>
42
43 using namespace WrtDB;
44
45 namespace {
46 const std::string BACKUP_ID = ".backup";
47 }
48
49 namespace Jobs {
50 namespace WidgetInstall {
51 TaskRecovery::TaskRecovery(InstallerContext& context) :
52     DPL::TaskDecl<TaskRecovery>(this),
53     m_context(context)
54 {
55     AddStep(&TaskRecovery::StartStep);
56     AddStep(&TaskRecovery::StepRecoveryDirectory);
57     AddStep(&TaskRecovery::StepRecoveryDatabase);
58     AddStep(&TaskRecovery::EndStep);
59 }
60
61 void TaskRecovery::StepRecoveryDirectory()
62 {
63     _D("StepRecoveryDirectory ...");
64     // check backup folder
65     DPL::Utils::Path installedPath(WrtDB::GlobalConfig::GetUserInstalledWidgetPath());
66     installedPath /= m_context.widgetConfig.tzPkgid;
67
68     DPL::Utils::Path backupPath(WrtDB::GlobalConfig::GetUserInstalledWidgetPath());
69     backupPath /= DPL::ToUTF8String(m_context.widgetConfig.tzPkgid) + BACKUP_ID;
70
71     _D("installedPath : %s", installedPath.Fullpath().c_str());
72     _D("backupPath : %s", backupPath.Fullpath().c_str());
73
74     if (backupPath.Exists()) {
75         DPL::Utils::TryRemove(installedPath);
76
77         DPL::Utils::Rename(backupPath, installedPath);
78     } else {
79         ThrowMsg(Exceptions::RecoveryFailed, "backup folder doesn't exist");
80     }
81 }
82
83 void TaskRecovery::StepRecoveryDatabase()
84 {
85     _D("StepRecoveryDatabase ... %s", m_context.widgetConfig.tzPkgid.c_str());
86     Try {
87         std::string backupId, deleteId;
88
89         TizenAppId dbAppId = WidgetDAOReadOnly::getTizenAppId(m_context.widgetConfig.tzPkgid);
90         _D("Get appid : %ls", dbAppId.c_str());
91         std::string appId = DPL::ToUTF8String(dbAppId);
92
93         if (0 == appId.compare(appId.size() - BACKUP_ID.length(), BACKUP_ID.length(), BACKUP_ID)) {
94             backupId = appId;
95             deleteId = backupId.substr(0, backupId.length() -
96                     BACKUP_ID.length());
97         } else {
98             backupId = appId + BACKUP_ID;
99             deleteId = appId;
100         }
101
102         if (WrtDB::WidgetDAOReadOnly::isWidgetInstalled(DPL::FromUTF8String(backupId))) {
103             _D("Recovery Database...");
104             _D("backupId %s " , backupId.c_str());
105             _D("deleteId %s " , deleteId.c_str());
106
107             // remove ace
108             ace_unregister_widget(static_cast<ace_widget_handle_t>(
109                         WidgetDAOReadOnly::getHandle(DPL::
110                             FromUTF8String(deleteId))));
111             ace_unregister_widget(static_cast<ace_widget_handle_t>(
112                         WidgetDAOReadOnly::getHandle(DPL::
113                             FromUTF8String(backupId))));
114
115             WidgetDAO::unregisterWidget(DPL::FromUTF8String(deleteId));
116             WidgetDAO::updateTizenAppId(DPL::FromUTF8String(backupId),
117                     DPL::FromUTF8String(deleteId));
118
119             if(!AceApi::registerAceWidgetFromDB(WidgetDAOReadOnly::getHandle(
120                             DPL::FromUTF8String(deleteId)))) {
121                 _E("ace database restore failed");
122             }
123         }
124
125         WidgetDAOReadOnly dao(DPL::FromUTF8String(deleteId));
126         m_context.requestedPath =
127             DPL::ToUTF8String(*dao.getWidgetInstalledPath());
128     }
129     Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
130     {
131         ThrowMsg(Exceptions::RecoveryFailed, "[WidgetNotExist] Failure in recovery db");
132     }
133     Catch(WidgetDAO::Exception::DatabaseError)
134     {
135         ThrowMsg(Exceptions::RecoveryFailed, "[DatabaseError] Failure in recovery db");
136     }
137 }
138
139 void TaskRecovery::StartStep()
140 {
141     LOGD("--------- <TaskRecovery> : START ----------");
142 }
143
144 void TaskRecovery::EndStep()
145 {
146     LOGD("--------- <TaskRecovery> : END ----------");
147 }
148 } //namespace RecoveryInstall
149 } //namespace Jobs