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