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