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