Change pkgname to appid for docomo request. PART1
[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 & tizenAppId,
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.tzAppid = tizenAppId;
71     m_context.isExternalWidget = getExternalWidgetFlag();
72
73     Try
74     {
75         WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(tizenAppId));
76         m_context.locations = WidgetLocation(m_context.tzAppid);
77         m_context.tzPkgid = DPL::ToUTF8String(dao.getTizenPkgId());
78
79         LogInfo("Widget model exists. Pkg name: " << m_context.tzAppid);
80
81         AddTask(new TaskSmack(m_context));
82         AddTask(new TaskCheck(m_context));
83
84         if (dao.getPackagingType() == PKG_TYPE_HYBRID_WEB_APP) {
85             AddTask(new TaskUninstallOspsvc(m_context));
86         }
87         AddTask(new TaskRemoveFiles(m_context));
88         AddTask(new TaskDbUpdate(m_context));
89         AddTask(new TaskRemoveCustomHandlers(m_context));
90         AddTask(new TaskDeleteCertificates(m_context));
91
92         // send start signal of pkgmgr
93         if (getInstallerStruct().pkgmgrInterface->setPkgname(m_context.tzAppid)) {
94             getInstallerStruct().pkgmgrInterface->sendSignal(
95                     PKGMGR_START_KEY,
96                     PKGMGR_START_UNINSTALL);
97         }
98     } Catch (WidgetDAOReadOnly::Exception::WidgetNotExist) {
99         AddTask(new UninstallerTaskFail(true));
100     } Catch (WidgetDAOReadOnly::Exception::Base) {
101         AddTask(new UninstallerTaskFail(false));
102     }
103 }
104
105 std::string JobWidgetUninstall::getRemovedTizenId() const
106 {
107     return m_context.tzAppid;
108 }
109
110 bool JobWidgetUninstall::getRemoveStartedFlag() const
111 {
112     return m_context.removeStarted;
113 }
114
115 bool JobWidgetUninstall::getRemoveFinishedFlag() const
116 {
117     return m_context.removeFinished;
118 }
119
120 void JobWidgetUninstall::SendProgress()
121 {
122     using namespace PackageManager;
123     if (!getRemoveStartedFlag() ||
124         (getRemoveStartedFlag() && getRemoveFinishedFlag())) {
125         if (NULL != getInstallerStruct().progressCallback) {
126             // send progress signal of pkgmgr
127             std::ostringstream percent;
128             percent << static_cast<int>(GetProgressPercent());
129             getInstallerStruct().pkgmgrInterface->sendSignal(
130                         PKGMGR_PROGRESS_KEY,
131                         percent.str());
132
133             LogDebug("Call widget uninstall progressCallback");
134             getInstallerStruct().progressCallback(
135                     getInstallerStruct().userParam,
136                     GetProgressPercent(), GetProgressDescription());
137         }
138     }
139 }
140
141 void JobWidgetUninstall::SendFinishedSuccess()
142 {
143     using namespace PackageManager;
144     // send signal of pkgmgr
145     getInstallerStruct().pkgmgrInterface->sendSignal(
146                 PKGMGR_END_KEY,
147                 PKGMGR_END_SUCCESS);
148
149     LogDebug("Call widget uninstall success finishedCallback");
150     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
151             getRemovedTizenId(),Exceptions::Success);
152 }
153
154 void JobWidgetUninstall::SendFinishedFailure()
155 {
156     using namespace PackageManager;
157     LogError("Error in uninstallation step: " << m_exceptionCaught);
158     LogError("Message: " << m_exceptionMessage);
159
160     // send signal of pkgmgr
161     getInstallerStruct().pkgmgrInterface->sendSignal(
162                 PKGMGR_END_KEY,
163                 PKGMGR_END_FAILURE);
164
165     LogDebug("Call widget uninstall failure finishedCallback");
166     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
167             getRemovedTizenId(), m_exceptionCaught); //TODO
168     LogDebug("[JobWidgetUninstall] Asynchronous failure callback status sent");
169 }
170
171 void JobWidgetUninstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
172 {
173     m_exceptionCaught = static_cast<Exceptions::Type>(e.getParam());
174     m_exceptionMessage = e.GetMessage();
175 }
176
177 bool JobWidgetUninstall::getExternalWidgetFlag() const
178 {
179
180     LogDebug("Get external widget");
181     if (APP2EXT_SD_CARD == app2ext_get_app_location(m_context.tzAppid.c_str())) {
182         LogDebug("This widget is in external stroage");
183         return true;
184     }
185     return false;
186 }
187
188 } //namespace WidgetUninstall
189 } //namespace Jobs