[Release] wrt-installer_0.0.90
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_database.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    task_new_db_insert.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @author  Soyoung kim(sy037.kim@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for installer task database updating for widget update
22  */
23 #include <time.h>
24 #include <sys/stat.h>
25 #include <widget_install/task_database.h>
26 #include <widget_install/job_widget_install.h>
27 #include <widget_install/widget_install_errors.h>
28 #include <widget_install/widget_install_context.h>
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30 #include <dpl/wrt-dao-ro/vconf_config.h>
31 #include <dpl/foreach.h>
32 #include <dpl/utils/wrt_utility.h>
33 #include <dpl/log/log.h>
34 #include <dpl/assert.h>
35 #include <string>
36 #include <sstream>
37 #include <ace_api_install.h>
38 #include <ace_registration.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <vconf.h>
42 #include <map>
43
44 using namespace WrtDB;
45
46 namespace Jobs {
47 namespace WidgetInstall {
48
49 TaskDatabase::TaskDatabase(InstallerContext& context) :
50     DPL::TaskDecl<TaskDatabase>(this),
51     m_context(context),
52     m_handleToRemove(INVALID_WIDGET_HANDLE),
53     m_handle(INVALID_WIDGET_HANDLE)
54 {
55     AddStep(&TaskDatabase::StepRegisterExternalFiles);
56     AddStep(&TaskDatabase::StepWrtDBInsert);
57     AddStep(&TaskDatabase::StepAceDBInsert);
58     AddStep(&TaskDatabase::StepRemoveExternalFiles);
59     AddStep(&TaskDatabase::StepCreateVconf);
60
61     AddAbortStep(&TaskDatabase::StepAbortDBInsert);
62 }
63
64 void TaskDatabase::StepWrtDBInsert()
65 {
66     Try
67     {
68         /* Set install Time */
69         time(&m_context.widgetConfig.installedTime);
70
71         if (m_context.existingWidgetInfo.isExist) //update
72         {
73             m_handleToRemove = WidgetDAOReadOnly::getHandle(
74                 m_context.locations->getPkgname());
75             LogInfo("Registering widget... (update)");
76             WidgetDAO::registerOrUpdateWidget(
77                     m_context.locations->getPkgname(),
78                     m_context.widgetConfig,
79                     m_context.wacSecurity);
80             m_handle = WidgetDAOReadOnly::getHandle(
81                 m_context.locations->getPkgname());
82         }
83         else //new installation
84         {
85             LogInfo("Registering widget...");
86             WidgetDAO::registerWidget(
87                     m_context.locations->getPkgname(),
88                     m_context.widgetConfig,
89                     m_context.wacSecurity);
90             m_handle = WidgetDAOReadOnly::getHandle(
91                 m_context.locations->getPkgname());
92         }
93
94         FOREACH (cap, m_context.staticPermittedDevCaps) {
95             LogInfo("staticPermittedDevCaps : " << cap->first
96                     << " smack status: " << cap->second);
97         }
98
99         LogInfo("Widget registered");
100     }
101     Catch(WidgetDAO::Exception::DatabaseError)
102     {
103         LogError("Database failure!");
104         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
105     }
106     Catch(DPL::DB::SqlConnection::Exception::Base)
107     {
108         LogError("Database failure!");
109         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
110     }
111 }
112
113 void TaskDatabase::StepAceDBInsert()
114 {
115     LogDebug("Inserting Ace database entry. New handle: " << m_handle);
116     if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
117         LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
118         if (ACE_OK != ace_unregister_widget(
119                 static_cast<ace_widget_handle_t>(m_handleToRemove)))
120         {
121             LogWarning("Error while removing ace entry for previous insallation");
122         };
123     }
124
125     if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
126                                   m_context.wacSecurity.getCertificateList()))
127     {
128         LogError("ace database insert failed");
129         ThrowMsg(Exceptions::NotAllowed, "Update failure. ace_register_widget failed");
130     }
131     LogDebug("Ace data inserted");
132
133     m_context.job->UpdateProgress(
134         InstallerContext::INSTALL_NEW_DB_INSERT,
135         "New Widget DB UPDATE Finished");
136 }
137
138 void TaskDatabase::StepRegisterExternalFiles()
139 {
140     WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
141     if (m_context.existingWidgetInfo.isExist) //update
142     {
143         WidgetDAO dao(m_context.locations->getPkgname());
144         WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
145         FOREACH(file, externalLocationsDB)
146         {
147             if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
148             {
149                 m_externalLocationsToRemove.push_back(*file);
150             }
151         }
152     }
153     LogDebug("Registering external files:");
154     FOREACH(file, externalLocationsUpdate)
155     {
156         LogDebug("  -> " << *file);
157     }
158
159     //set external locations to be registered
160     m_context.widgetConfig.externalLocations = externalLocationsUpdate;
161 }
162
163 void TaskDatabase::StepRemoveExternalFiles()
164 {
165     if(!m_externalLocationsToRemove.empty())
166     {
167         LogDebug("Removing external files:");
168     }
169
170     FOREACH(file, m_externalLocationsToRemove)
171     {
172         if(WrtUtilFileExists(*file))
173         {
174             LogDebug("  -> " << *file);
175             remove(file->c_str());
176         }
177         else if(WrtUtilDirExists(*file))
178         {
179             LogDebug("  -> " << *file);
180             if(!WrtUtilRemove(*file)){
181                 ThrowMsg(Exceptions::RemovingFolderFailure,
182                         "Failed to remove external directory");
183             }
184         }
185         else
186         {
187             LogWarning("  -> " << *file << "(no such a path)");
188         }
189     }
190 }
191
192 void TaskDatabase::StepCreateVconf()
193 {
194     LogDebug("StepCreateVconf");
195     std::map<std::string, WrtDB::SettingsType> vconfData;
196     vconfData[
197         WrtDB::VconfConfig::GetVconfKeyPopupUsage(
198             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
199     vconfData[
200         WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
201             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
202     vconfData[
203         WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
204             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
205     vconfData[
206         WrtDB::VconfConfig::GetVconfKeyWebDatabaseUsage(
207             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
208     vconfData[
209         WrtDB::VconfConfig::GetVconfKeyFilesystemUsage(
210             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
211     vconfData[
212         WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
213             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_OFF;
214
215     // vconftool -g 5000 set -t int <path> initialize value
216     // Current installer should use vconftool for setting group ID
217     // In case of install application by pkgcmd, permission for others
218     // set to read-only
219     FOREACH(it, vconfData) {
220         std::ostringstream command;
221         command << "vconftool -g 5000 set -t int ";
222         command << (*it).first;
223         command << " \"" << static_cast<int>((*it).second) << "\"";
224         int ret = system(command.str().c_str());
225         if (-1 == ret) {
226             ThrowMsg(Exceptions::CreateVconfFailure, "Failed to create vconf files");
227         }
228     }
229 }
230
231 void TaskDatabase::StepAbortDBInsert()
232 {
233     LogWarning("[DB Update Task] Aborting... (DB Clean)");
234     Try
235     {
236         WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
237         LogDebug("Cleaning DB successful!");
238     }
239     Catch(DPL::DB::SqlConnection::Exception::Base)
240     {
241         LogError("Failed to handle StepAbortDBClean!");
242     }
243
244     ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
245     // Remove also old one. If it was already updated nothing wrong will happen,
246     // but if not old widget will be removed.
247     if (INVALID_WIDGET_HANDLE != m_handleToRemove)
248         ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
249 }
250
251 } //namespace WidgetInstall
252 } //namespace Jobs