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