392106fe9c7a643e450efd0c128334677f329945
[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(const std::string & widgetPkgName,
59         const WidgetUninstallationStruct &uninstallerStruct) :
60     Job(Uninstallation),
61     JobContextBase<WidgetUninstallationStruct>(uninstallerStruct)
62 {
63     using namespace PackageManager;
64     m_context.removeStarted = false;
65     m_context.removeFinished = false;
66     m_context.uninstallStep = UninstallerContext::UNINSTALL_START;
67     m_context.job = this;
68     m_context.pkgname = widgetPkgName;
69
70     Try
71     {
72         WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(widgetPkgName));
73         m_context.locations = WidgetLocation(m_context.pkgname);
74
75         LogInfo("Widget model exists. Pkg name: " << m_context.pkgname);
76
77         AddTask(new TaskSmack(m_context));
78         AddTask(new TaskCheck(m_context));
79
80         if (dao.getPackagingType() == PKG_TYPE_HYBRID_WEB_APP) {
81             AddTask(new TaskUninstallOspsvc(m_context));
82         }
83         AddTask(new TaskRemoveFiles(m_context));
84         AddTask(new TaskDbUpdate(m_context));
85         AddTask(new TaskDeleteCertificates(m_context));
86
87         // send start signal of pkgmgr
88         if (PkgmgrSignalSingleton::Instance().setPkgname(m_context.pkgname)) {
89             PkgmgrSignalSingleton::Instance().sendSignal(
90                     PKGMGR_START_KEY,
91                     PKGMGR_START_UNINSTALL);
92         }
93     } Catch (WidgetDAOReadOnly::Exception::WidgetNotExist) {
94         AddTask(new UninstallerTaskFail(true));
95     } Catch (WidgetDAOReadOnly::Exception::Base) {
96         AddTask(new UninstallerTaskFail(false));
97     }
98 }
99
100 std::string JobWidgetUninstall::getRemovedTizenId() const
101 {
102     return m_context.pkgname;
103 }
104
105 bool JobWidgetUninstall::getRemoveStartedFlag() const
106 {
107     return m_context.removeStarted;
108 }
109
110 bool JobWidgetUninstall::getRemoveFinishedFlag() const
111 {
112     return m_context.removeFinished;
113 }
114
115 void JobWidgetUninstall::SendProgress()
116 {
117     using namespace PackageManager;
118     if (!getRemoveStartedFlag() ||
119         (getRemoveStartedFlag() && getRemoveFinishedFlag())) {
120         if (NULL != getInstallerStruct().progressCallback) {
121             // send progress signal of pkgmgr
122             std::ostringstream percent;
123             percent << static_cast<int>(GetProgressPercent());
124             PkgmgrSignalSingleton::Instance().sendSignal(
125                         PKGMGR_PROGRESS_KEY,
126                         percent.str());
127
128             LogDebug("Call widget uninstall progressCallback");
129             getInstallerStruct().progressCallback(
130                     getInstallerStruct().userParam,
131                     GetProgressPercent(), GetProgressDescription());
132         }
133     }
134 }
135
136 void JobWidgetUninstall::SendFinishedSuccess()
137 {
138     using namespace PackageManager;
139     // send signal of pkgmgr
140     PkgmgrSignalSingleton::Instance().sendSignal(
141                 PKGMGR_END_KEY,
142                 PKGMGR_END_SUCCESS);
143
144     LogDebug("Call widget uninstall success finishedCallback");
145     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
146             getRemovedTizenId(),Exceptions::Success);
147 }
148
149 void JobWidgetUninstall::SendFinishedFailure()
150 {
151     using namespace PackageManager;
152     LogError("Error in uninstallation step: " << m_exceptionCaught);
153     LogError("Message: " << m_exceptionMessage);
154
155     // send signal of pkgmgr
156     PkgmgrSignalSingleton::Instance().sendSignal(
157                 PKGMGR_END_KEY,
158                 PKGMGR_END_FAILURE);
159
160     LogDebug("Call widget uninstall failure finishedCallback");
161     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
162             getRemovedTizenId(), m_exceptionCaught); //TODO
163     LogDebug("[JobWidgetUninstall] Asynchronous failure callback status sent");
164 }
165
166 void JobWidgetUninstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
167 {
168     m_exceptionCaught = static_cast<Exceptions::Type>(e.getParam());
169     m_exceptionMessage = e.GetMessage();
170 }
171 } //namespace WidgetUninstall
172 } //namespace Jobs