Remove unused logic
[platform/framework/native/installer.git] / src / Step / UninstallStep.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        UninstallStep.cpp
19  * @brief       This is the implementation file for %UninstallStep class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <app2ext_interface.h>
25
26 #include <FIoDirectory.h>
27 #include <FIo_FileImpl.h>
28 #include <FBase_StringConverter.h>
29 #include <FAppPkg_PackageInfoImpl.h>
30
31 #include "InstallationContext.h"
32 #include "UninstallStep.h"
33 #include "InstallerUtil.h"
34 #include "CompatibilityManager.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::App;
39 using namespace Tizen::App::Package;
40 using namespace Tizen::Io;
41
42 UninstallStep::UninstallStep(void)
43 :__state(STATE_GET_PACKAGEINFO)
44 ,__pContext(null)
45 {
46 }
47
48 UninstallStep::~UninstallStep(void)
49 {
50 }
51
52 InstallerError
53 UninstallStep::Run(InstallationContext* pContext)
54 {
55         InstallerError error = INSTALLER_ERROR_NONE;
56         AppLog(" UninstallStep - START");
57
58         __pContext = pContext;
59
60         while (true)
61         {
62                 switch (__state)
63                 {
64                 case STATE_GET_PACKAGEINFO:
65                         AppLog("[STATE_GET_PACKAGEINFO]");
66                         error = OnStateGetPackageInfo();
67                         break;
68
69                 case STATE_POLICY_CHECK:
70                         AppLog("[STATE_POLICY_CHECK]");
71                         error = OnStatePolicyCheck();
72                         break;
73
74                 case STATE_TERMINATE_APP:
75                         AppLog("[STATE_TERMINATE_APP]");
76                         error = OnStateTerminateApp();
77                         break;
78
79                 case STATE_START_TIMER:
80                         AppLog("[STATE_START_TIMER]");
81                         error = OnStateStartTimer();
82                         break;
83
84                 case STATE_DELETE_DIR:
85                         AppLog("[STATE_DELETE_DIR]");
86                         error = OnStateRemoveDir();
87                         break;
88
89                 case STATE_DONE:
90                         AppLog("[STATE_DONE]");
91                         error = OnStateDone();
92                         break;
93
94                 default:
95                         break;
96                 }
97
98                 if (error != INSTALLER_ERROR_NONE)
99                 {
100                         break;
101                 }
102
103                 if (__state > STATE_DONE)
104                 {
105                         AppLog(" UninstallStep - END");
106                         break;
107                 }
108         }
109
110         return error;
111 }
112
113 void
114 UninstallStep::GoNextState(void)
115 {
116         __state++;
117 }
118
119 InstallerError
120 UninstallStep::OnStateGetPackageInfo(void)
121 {
122         InstallerError error = INSTALLER_ERROR_NONE;
123         PackageId packageId = __pContext->__packageId;
124
125         std::unique_ptr< _PackageInfoImpl > pPackageInfoImpl(new (std::nothrow) _PackageInfoImpl());
126         TryReturn(pPackageInfoImpl, INSTALLER_ERROR_OUT_OF_MEMORY, "pPackageInfoImpl is null.");
127
128         AppLog("package = %ls", packageId.GetPointer());
129
130         result r = pPackageInfoImpl->Construct(packageId);
131         TryReturn(r == E_SUCCESS, INSTALLER_ERROR_INTERNAL_STATE, "pPackageInfoImpl->Construct(%ls) failed.", packageId.GetPointer());
132
133 //      bool isUninstallable = pPackageInfoImpl->IsUninstallable();
134 //      if (isUninstallable == false)
135 //      {
136 //              __pContext->__additionalErrorString = L"Thrown when the application cannot be uninstalled because the application was preloaded.";
137 //      }
138 //      TryReturn(isUninstallable == true, INSTALLER_ERROR_PACKAGE_INVALID, "preload app cannot be uninstalled.");
139
140         String rwXmlPath;
141         rwXmlPath.Format(1024, DIR_RW_PACKAGE_SYSTEM_MANIFEST, packageId.GetPointer());
142         if (File::IsFileExist(rwXmlPath) == false)
143         {
144                 AppLog("This is a preload app = [%ls]", rwXmlPath.GetPointer());
145                 __pContext->__isPreloaded = true;
146         }
147
148         __pContext->__rootPath = pPackageInfoImpl->GetAppRootPath();
149
150         if (__pContext->__isHybridService == true)
151         {
152                 AppLog("Uninstallation for HybridService");
153                 __state = STATE_TERMINATE_APP;
154                 return error;
155         }
156
157         if (pPackageInfoImpl->IsInstalledInExternalStorage() == true)
158         {
159                 int res = 0;
160                 app2ext_handle* pHandle = null;
161
162                 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
163                 TryReturn(pPackageId, INSTALLER_ERROR_INTERNAL_STATE, "pAppId is null");
164
165                 pHandle = app2ext_init(APP2EXT_SD_CARD);
166                 TryReturn(pHandle, INSTALLER_ERROR_INTERNAL_STATE, "app2ext_init() failed");
167
168                 res = pHandle->interface.pre_uninstall(pPackageId.get());
169                 TryReturn(res == 0, INSTALLER_ERROR_INTERNAL_STATE, "pHandle->interface.pre_uninstall() failed [%d]", res);
170
171                 __pContext->__pApp2ExtHandle = (void*)pHandle;
172
173                 AppLog("[app2sd] pre_uninstall(%s)", pPackageId.get());
174         }
175
176         std::unique_ptr< ArrayList > pList(pPackageInfoImpl->GetAppInfoListN());
177         TryReturn(pList, INSTALLER_ERROR_DATABASE, "pList is null.");
178
179         for (int i = 0; i < pList.get()->GetCount(); i++)
180         {
181                 _PackageAppInfoImpl* pAppInfoImpl = dynamic_cast<_PackageAppInfoImpl*>(pList.get()->GetAt(i));
182                 if (pAppInfoImpl)
183                 {
184                         std::unique_ptr< AppData > pAppData(new (std::nothrow) AppData);
185                         TryReturn(pAppData, INSTALLER_ERROR_OUT_OF_MEMORY, "pAppData is null.");
186
187                         error = pAppData.get()->Construct();
188                         TryReturn(error == INSTALLER_ERROR_NONE, INSTALLER_ERROR_INTERNAL_STATE, "pAppData.get()->Construct() failed.");
189
190                         pAppData.get()->__appId = pAppInfoImpl->GetPackageName();
191                         pAppData.get()->__feature = pAppInfoImpl->GetAppFeature();
192
193                         __pContext->__pAppDataList->Add(pAppData.release());
194                 }
195         }
196
197         GoNextState();
198         return error;
199 }
200
201 InstallerError
202 UninstallStep::OnStatePolicyCheck(void)
203 {
204         InstallerError error = INSTALLER_ERROR_NONE;
205
206         GoNextState();
207         return error;
208 }
209
210 InstallerError
211 UninstallStep::OnStateTerminateApp(void)
212 {
213         InstallerError error = INSTALLER_ERROR_NONE;
214
215         InstallerUtil::TerminateApps(__pContext->__packageId);
216
217         GoNextState();
218         return error;
219 }
220
221 InstallerError
222 UninstallStep::OnStateStartTimer(void)
223 {
224         InstallerError error = INSTALLER_ERROR_NONE;
225
226         GoNextState();
227         return error;
228 }
229
230 InstallerError
231 UninstallStep::OnStateRemoveDir(void)
232 {
233         InstallerError error = INSTALLER_ERROR_NONE;
234         bool res = true;
235
236         CompatibilityManager compatibilityManager;
237         compatibilityManager.Construct(__pContext);
238
239         String rootPath;
240         rootPath = __pContext->__rootPath;
241         AppLog("rootPath = [%ls]", rootPath.GetPointer());
242
243         String compatPath(rootPath);
244         compatPath.Append(L"/info/compat.info");
245         bool ospCompat = File::IsFileExist(compatPath);
246         result r = GetLastResult();
247         if (r == E_SUCCESS && ospCompat == true)
248         {
249                 if (compatibilityManager.FinalizeDataCaging(rootPath) == false)
250                 {
251                         AppLog("[Tizen::Io] Failed to unmount directories for 2.0 application, appRootPath: %ls",
252                                         rootPath.GetPointer());
253                         return INSTALLER_ERROR_UNMOUNT_FAILED;
254                 }
255         }
256         else if (r != E_SUCCESS)
257         {
258                 AppLog("[Tizen::Io] Failed to access %ls", compatPath.GetPointer());
259                 return INSTALLER_ERROR_UNMOUNT_FAILED;
260         }
261
262         compatibilityManager.CleanDirectories(rootPath, __pContext->__packageId);
263
264
265         String virtualRootInfoFile = rootPath + VIRTUAL_ROOT_INFO_FILE;
266         if (File::IsFileExist(virtualRootInfoFile) == true)
267         {
268                 res = compatibilityManager.FinalizeVirtualRoot(rootPath, __pContext->__packageId);
269                 TryReturn(res == true, INSTALLER_ERROR_UNMOUNT_FAILED, "compatibilityManager.FinalizeVirtualRoot(%ls) failed.", rootPath.GetPointer());
270         }
271
272         AppLog("Directory::Remove - START");
273
274         if (__pContext->__isHybridService == true)
275         {
276                 AppLog("Uninstallation for HybridService - skip Remove");
277
278         }
279         else
280         {
281                 String realPath;
282                 if (InstallerUtil::IsSymlink(rootPath) == true)
283                 {
284                         if (InstallerUtil::GetRealPath(rootPath, realPath) == true)
285                         {
286                                 InstallerUtil::Remove(realPath);
287                         }
288                 }
289
290                 InstallerUtil::Remove(rootPath);
291         }
292
293         IListT<AppData*>* pAppDataList = __pContext->__pAppDataList;
294         TryReturn(pAppDataList, INSTALLER_ERROR_INTERNAL_STATE, "pAppDataList is null");
295
296         int count = pAppDataList->GetCount();
297         for (int i = 0; i < count; i++)
298         {
299                 AppData* pAppData = null;
300                 pAppDataList->GetAt(i, pAppData);
301
302                 if (pAppData)
303                 {
304                         if (pAppData->__feature == CATEGORY_TYPE_IME)
305                         {
306                                 String symlinkPath;
307                                 symlinkPath.Format(1024, L"%s/%ls.so", IME_PATH, pAppData->__appId.GetPointer());
308                                 InstallerUtil::Remove(symlinkPath);
309                         }
310                 }
311         }
312
313         AppLog("Directory::Remove - END");
314
315         GoNextState();
316         return error;
317 }
318
319 InstallerError
320 UninstallStep::OnStateDone(void)
321 {
322         InstallerError error = INSTALLER_ERROR_NONE;
323
324         GoNextState();
325         return error;
326 }