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