Rename invalid WRT_SMACK_LABEL macro to WRT_SMACK_ENABLED
[platform/framework/web/wrt-installer.git] / src / jobs / widget_install / job_widget_install.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  * @file    job_widget_install.cpp
18  * @author  Radoslaw Wicik r.wicik@samsung.com
19  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for main installer task
22  */
23 #include <string>
24 #include <sys/time.h>
25 #include <ctime>
26 #include <cstdlib>
27 #include <limits.h>
28 #include <regex.h>
29
30 #include <dpl/utils/wrt_utility.h>
31 #include <dpl/utils/path.h>
32 #include <dpl/localization/w3c_file_localization.h>
33
34 #include <pkg-manager/pkgmgr_signal.h>
35 #include <app_manager.h>
36
37 #include "root_parser.h"
38 #include "widget_parser.h"
39 #include "parser_runner.h"
40 #include <widget_install/job_widget_install.h>
41 #include <widget_install/task_certify.h>
42 #include <widget_install/task_certify_level.h>
43 #include <widget_install/task_process_config.h>
44 #include <widget_install/task_file_manipulation.h>
45 #include <widget_install/task_ace_check.h>
46 #include <widget_install/task_smack.h>
47 #include <widget_install/task_manifest_file.h>
48 #include <widget_install/task_prepare_files.h>
49 #include <widget_install/task_recovery.h>
50 #include <widget_install/task_install_ospsvc.h>
51 #include <widget_install/task_update_files.h>
52 #include <widget_install/task_database.h>
53 #include <widget_install/task_remove_backup.h>
54 #include <widget_install/task_encrypt_resource.h>
55 #include <widget_install/task_pkg_info_update.h>
56 #include <widget_install/task_commons.h>
57 #include <widget_install/task_prepare_reinstall.h>
58 #include <widget_install/task_configuration.h>
59
60 #include <widget_install_to_external.h>
61 #include <widget_install/widget_unzip.h>
62
63 #include <installer_log.h>
64
65 using namespace WrtDB;
66 using namespace Jobs::Exceptions;
67
68 namespace Jobs {
69 namespace WidgetInstall {
70
71 JobWidgetInstall::JobWidgetInstall(
72     std::string const &widgetPath,
73     const Jobs::WidgetInstall::WidgetInstallationStruct &
74     installerStruct) :
75     Job(UnknownInstallation),
76     JobContextBase<Jobs::WidgetInstall::WidgetInstallationStruct>(installerStruct),
77     m_exceptionCaught(Jobs::Exceptions::Success)
78 {
79     m_installerContext.mode = m_jobStruct.m_installMode;
80     m_installerContext.requestedPath = widgetPath;
81
82     //start configuration of installation
83     AddTask(new TaskConfiguration(m_installerContext));
84
85     // Init installer context
86     m_installerContext.installStep = InstallerContext::INSTALL_START;
87     m_installerContext.job = this;
88
89     m_installerContext.callerPkgId
90             = DPL::FromUTF8String(m_jobStruct.pkgmgrInterface->getCallerId());
91     _D("Caller Package Id : %s", DPL::ToUTF8String(m_installerContext.callerPkgId).c_str());
92 }
93
94 void JobWidgetInstall::appendNewInstallationTaskList()
95 {
96     _D("Configure installation succeeded");
97     m_installerContext.job->SetProgressFlag(true);
98
99     AddTask(new TaskRecovery(m_installerContext));
100
101     AddTask(new TaskProcessConfig(m_installerContext));
102     if (m_installerContext.widgetConfig.packagingType ==
103         WrtDB::PKG_TYPE_HOSTED_WEB_APP)
104     {
105         AddTask(new TaskPrepareFiles(m_installerContext));
106     }
107     AddTask(new TaskCertify(m_installerContext));
108     AddTask(new TaskCertifyLevel(m_installerContext));
109     if (m_installerContext.needEncryption) {
110         AddTask(new TaskEncryptResource(m_installerContext));
111     }
112     AddTask(new TaskFileManipulation(m_installerContext));
113     AddTask(new TaskManifestFile(m_installerContext));
114     if (m_installerContext.widgetConfig.packagingType ==
115         PKG_TYPE_HYBRID_WEB_APP)
116     {
117         AddTask(new TaskInstallOspsvc(m_installerContext));
118     }
119     AddTask(new TaskDatabase(m_installerContext));
120     AddTask(new TaskAceCheck(m_installerContext));
121     AddTask(new TaskSmack(m_installerContext));
122     AddTask(new TaskPkgInfoUpdate(m_installerContext));
123 }
124
125 void JobWidgetInstall::appendUpdateInstallationTaskList()
126 {
127     _D("Configure installation updated");
128     _D("Widget Update");
129     m_installerContext.job->SetProgressFlag(true);
130
131     if (m_installerContext.mode.command ==
132         InstallMode::Command::REINSTALL)
133     {
134         AddTask(new TaskPrepareReinstall(m_installerContext));
135     }
136
137     AddTask(new TaskProcessConfig(m_installerContext));
138
139     if (m_installerContext.widgetConfig.packagingType ==
140         WrtDB::PKG_TYPE_HOSTED_WEB_APP)
141     {
142         AddTask(new TaskPrepareFiles(m_installerContext));
143     }
144
145     AddTask(new TaskCertify(m_installerContext));
146     AddTask(new TaskCertifyLevel(m_installerContext));
147     if (m_installerContext.needEncryption) {
148         AddTask(new TaskEncryptResource(m_installerContext));
149     }
150
151     if (m_installerContext.mode.extension !=
152             InstallMode::ExtensionType::DIR) {
153         AddTask(new TaskUpdateFiles(m_installerContext));
154         AddTask(new TaskFileManipulation(m_installerContext));
155     }
156
157     AddTask(new TaskManifestFile(m_installerContext));
158     if (m_installerContext.widgetConfig.packagingType ==
159         PKG_TYPE_HYBRID_WEB_APP)
160     {
161         AddTask(new TaskInstallOspsvc(m_installerContext));
162     }
163
164     AddTask(new TaskDatabase(m_installerContext));
165     AddTask(new TaskAceCheck(m_installerContext));
166     //TODO: remove widgetHandle from this task and move before database task
167     // by now widget handle is needed in ace check
168     // Any error in acecheck while update will break widget
169     AddTask(new TaskSmack(m_installerContext));
170     AddTask(new TaskRemoveBackupFiles(m_installerContext));
171     AddTask(new TaskPkgInfoUpdate(m_installerContext));
172 }
173
174 void JobWidgetInstall::SendProgress()
175 {
176     using namespace PackageManager;
177     if (GetProgressFlag() != false) {
178         if (GetInstallerStruct().progressCallback != NULL) {
179             // send progress signal of pkgmgr
180             GetInstallerStruct().pkgmgrInterface->sendProgress(GetProgressPercent());
181
182             _D("Call widget install progressCallback");
183             GetInstallerStruct().progressCallback(
184                 GetInstallerStruct().userParam,
185                 GetProgressPercent(),
186                 GetProgressDescription());
187         }
188     }
189 }
190
191 void JobWidgetInstall::SendProgressIconPath(const std::string &path)
192 {
193     using namespace PackageManager;
194     if (GetProgressFlag() != false) {
195         if (GetInstallerStruct().progressCallback != NULL) {
196             // send progress signal of pkgmgr
197             GetInstallerStruct().pkgmgrInterface->sendIconPath(path);
198         }
199     }
200 }
201
202 void JobWidgetInstall::SendFinishedSuccess()
203 {
204     using namespace PackageManager;
205     // TODO : sync should move to separate task.
206     sync();
207
208     if (INSTALL_LOCATION_TYPE_EXTERNAL == m_installerContext.locationType) {
209         if (m_installerContext.isUpdateMode) {
210             WidgetInstallToExtSingleton::Instance().postUpgrade(true);
211         } else {
212             WidgetInstallToExtSingleton::Instance().postInstallation(true);
213         }
214         WidgetInstallToExtSingleton::Instance().deinitialize();
215     }
216
217     // remove widget install information file
218     unlink(m_installerContext.installInfo.c_str());
219
220     //inform widget info
221     JobWidgetInstall::displayWidgetInfo();
222
223     TizenAppId& tizenId = m_installerContext.widgetConfig.tzAppid;
224
225     // send signal of pkgmgr
226     GetInstallerStruct().pkgmgrInterface->endJob(m_exceptionCaught);
227
228     _D("Call widget install successfinishedCallback");
229     GetInstallerStruct().finishedCallback(GetInstallerStruct().userParam,
230                                           DPL::ToUTF8String(
231                                               tizenId), Jobs::Exceptions::Success);
232 }
233
234 void JobWidgetInstall::SendFinishedFailure()
235 {
236     using namespace PackageManager;
237     // remove widget install information file
238     unlink(m_installerContext.installInfo.c_str());
239
240     _E("Error number: %d", m_exceptionCaught);
241     _E("Message: %s", m_exceptionMessage.c_str());
242     TizenAppId & tizenId = m_installerContext.widgetConfig.tzAppid;
243
244     _D("Call widget install failure finishedCallback");
245
246     // send signal of pkgmgr
247     GetInstallerStruct().pkgmgrInterface->endJob(m_exceptionCaught);
248
249     GetInstallerStruct().finishedCallback(GetInstallerStruct().userParam,
250                                           DPL::ToUTF8String(
251                                               tizenId), m_exceptionCaught);
252 }
253
254 void JobWidgetInstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
255 {
256     m_exceptionCaught = static_cast<Jobs::Exceptions::Type>(e.getParam());
257     m_exceptionMessage = e.GetMessage();
258 }
259
260 void JobWidgetInstall::displayWidgetInfo()
261 {
262     WidgetDAOReadOnly dao(m_installerContext.widgetConfig.tzAppid);
263
264     std::ostringstream out;
265     WidgetLocalizedInfo localizedInfo =
266         W3CFileLocalization::getLocalizedInfo(dao.getTzAppId());
267
268     out << std::endl <<
269     "===================================== INSTALLED WIDGET INFO =========" \
270     "============================";
271     out << std::endl << "Name:                        " << localizedInfo.name;
272     out << std::endl << "AppId:                     " << dao.getTzAppId();
273     WidgetSize size = dao.getPreferredSize();
274     out << std::endl << "Width:                       " << size.width;
275     out << std::endl << "Height:                      " << size.height;
276     out << std::endl << "Start File:                  " <<
277     W3CFileLocalization::getStartFile(dao.getTzAppId());
278     out << std::endl << "Version:                     " << dao.getVersion();
279     out << std::endl << "Licence:                     " <<
280     localizedInfo.license;
281     out << std::endl << "Licence Href:                " <<
282     localizedInfo.licenseHref;
283     out << std::endl << "Description:                 " <<
284     localizedInfo.description;
285     out << std::endl << "Widget Id:                   " << dao.getGUID();
286     out << std::endl << "Widget recognized:           " << dao.isRecognized();
287     out << std::endl << "Widget distributor signed:   " <<
288     dao.isDistributorSigned();
289     out << std::endl << "Widget trusted:              " << dao.isTrusted();
290
291     OptionalWidgetIcon icon = W3CFileLocalization::getIcon(dao.getTzAppId());
292     DPL::OptionalString iconSrc =
293         !!icon ? icon->src : DPL::OptionalString::Null;
294     out << std::endl << "Icon:                        " << iconSrc;
295
296     out << std::endl << "Preferences:";
297     {
298         PropertyDAOReadOnly::WidgetPreferenceList list = dao.getPropertyList();
299         FOREACH(it, list)
300         {
301             out << std::endl << "  Key:                       " <<
302             it->key_name;
303             out << std::endl << "      Readonly:              " <<
304             it->readonly;
305         }
306     }
307
308     out << std::endl << "Features:";
309     {
310         WidgetFeatureSet list = dao.getFeaturesList();
311         FOREACH(it, list)
312         {
313             out << std::endl << "  Name:                      " << it->name;
314         }
315     }
316
317     out << std::endl;
318
319     _D("%s", out.str().c_str());
320 }
321 } //namespace WidgetInstall
322 } //namespace Jobs