[Release] wrt-installer_0.0.89
[framework/web/wrt-installer.git] / src / jobs / widget_uninstall / job_widget_uninstall.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 #include <widget_uninstall/job_widget_uninstall.h>
18 #include <widget_uninstall/widget_uninstall_errors.h>
19 #include <widget_uninstall/task_check.h>
20 #include <widget_uninstall/task_db_update.h>
21 #include <widget_uninstall/task_remove_files.h>
22 #include <widget_uninstall/task_smack.h>
23 #include <widget_uninstall/task_uninstall_ospsvc.h>
24 #include <widget_uninstall/task_delete_certificates.h>
25 #include <pkg-manager/pkgmgr_signal.h>
26 #include <app2ext_interface.h>
27
28 using namespace WrtDB;
29
30 namespace { //anonymous
31 class UninstallerTaskFail :
32     public DPL::TaskDecl<UninstallerTaskFail>
33 {
34   private:
35     bool m_uninstalled;
36
37     void StepFail()
38     {
39         if(m_uninstalled) {
40             ThrowMsg(Jobs::WidgetUninstall::Exceptions::WidgetNotExist,
41                     "Widget does not exist");
42         } else {
43             Throw(Jobs::WidgetUninstall::Exceptions::Base);
44         }
45     }
46
47   public:
48     UninstallerTaskFail(bool uninstalled) :
49         DPL::TaskDecl<UninstallerTaskFail>(this),
50         m_uninstalled(uninstalled)
51     {
52         AddStep(&UninstallerTaskFail::StepFail);
53     }
54 };
55 }
56
57 namespace Jobs {
58 namespace WidgetUninstall {
59 JobWidgetUninstall::JobWidgetUninstall(const std::string & widgetPkgName,
60         const WidgetUninstallationStruct &uninstallerStruct) :
61     Job(Uninstallation),
62     JobContextBase<WidgetUninstallationStruct>(uninstallerStruct)
63 {
64     using namespace PackageManager;
65     m_context.removeStarted = false;
66     m_context.removeFinished = false;
67     m_context.uninstallStep = UninstallerContext::UNINSTALL_START;
68     m_context.job = this;
69     m_context.pkgname = widgetPkgName;
70     m_context.isExternalWidget = getExternalWidgetFlag();
71
72     Try
73     {
74         WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(widgetPkgName));
75         m_context.locations = WidgetLocation(m_context.pkgname);
76
77         LogInfo("Widget model exists. Pkg name: " << m_context.pkgname);
78
79         AddTask(new TaskSmack(m_context));
80         AddTask(new TaskCheck(m_context));
81
82         if (dao.getPackagingType() == PKG_TYPE_HYBRID_WEB_APP) {
83             AddTask(new TaskUninstallOspsvc(m_context));
84         }
85         AddTask(new TaskRemoveFiles(m_context));
86         AddTask(new TaskDbUpdate(m_context));
87         AddTask(new TaskDeleteCertificates(m_context));
88
89         // send start signal of pkgmgr
90         if (getInstallerStruct().pkgmgrInterface->setPkgname(m_context.pkgname)) {
91             getInstallerStruct().pkgmgrInterface->sendSignal(
92                     PKGMGR_START_KEY,
93                     PKGMGR_START_UNINSTALL);
94         }
95     } Catch (WidgetDAOReadOnly::Exception::WidgetNotExist) {
96         AddTask(new UninstallerTaskFail(true));
97     } Catch (WidgetDAOReadOnly::Exception::Base) {
98         AddTask(new UninstallerTaskFail(false));
99     }
100 }
101
102 std::string JobWidgetUninstall::getRemovedTizenId() const
103 {
104     return m_context.pkgname;
105 }
106
107 bool JobWidgetUninstall::getRemoveStartedFlag() const
108 {
109     return m_context.removeStarted;
110 }
111
112 bool JobWidgetUninstall::getRemoveFinishedFlag() const
113 {
114     return m_context.removeFinished;
115 }
116
117 void JobWidgetUninstall::SendProgress()
118 {
119     using namespace PackageManager;
120     if (!getRemoveStartedFlag() ||
121         (getRemoveStartedFlag() && getRemoveFinishedFlag())) {
122         if (NULL != getInstallerStruct().progressCallback) {
123             // send progress signal of pkgmgr
124             std::ostringstream percent;
125             percent << static_cast<int>(GetProgressPercent());
126             getInstallerStruct().pkgmgrInterface->sendSignal(
127                         PKGMGR_PROGRESS_KEY,
128                         percent.str());
129
130             LogDebug("Call widget uninstall progressCallback");
131             getInstallerStruct().progressCallback(
132                     getInstallerStruct().userParam,
133                     GetProgressPercent(), GetProgressDescription());
134         }
135     }
136 }
137
138 void JobWidgetUninstall::SendFinishedSuccess()
139 {
140     using namespace PackageManager;
141     // send signal of pkgmgr
142     getInstallerStruct().pkgmgrInterface->sendSignal(
143                 PKGMGR_END_KEY,
144                 PKGMGR_END_SUCCESS);
145
146     LogDebug("Call widget uninstall success finishedCallback");
147     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
148             getRemovedTizenId(),Exceptions::Success);
149 }
150
151 void JobWidgetUninstall::SendFinishedFailure()
152 {
153     using namespace PackageManager;
154     LogError("Error in uninstallation step: " << m_exceptionCaught);
155     LogError("Message: " << m_exceptionMessage);
156
157     // send signal of pkgmgr
158     getInstallerStruct().pkgmgrInterface->sendSignal(
159                 PKGMGR_END_KEY,
160                 PKGMGR_END_FAILURE);
161
162     LogDebug("Call widget uninstall failure finishedCallback");
163     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
164             getRemovedTizenId(), m_exceptionCaught); //TODO
165     LogDebug("[JobWidgetUninstall] Asynchronous failure callback status sent");
166 }
167
168 void JobWidgetUninstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
169 {
170     m_exceptionCaught = static_cast<Exceptions::Type>(e.getParam());
171     m_exceptionMessage = e.GetMessage();
172 }
173
174 bool JobWidgetUninstall::getExternalWidgetFlag() const
175 {
176
177     LogDebug("Get external widget");
178     if (APP2EXT_SD_CARD == app2ext_get_app_location(m_context.pkgname.c_str())) {
179         LogDebug("This widget is in external stroage");
180         return true;
181     }
182     return false;
183 }
184
185 } //namespace WidgetUninstall
186 } //namespace Jobs