2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_new_db_insert.cpp
18 * @author Lukasz Wrzosek(l.wrzosek@samsung.com)
19 * @author Soyoung kim(sy037.kim@samsung.com)
21 * @brief Implementation file for installer task database updating for widget update
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>
37 #include <ace_api_install.h>
38 #include <ace_registration.h>
44 using namespace WrtDB;
47 namespace WidgetInstall {
49 TaskDatabase::TaskDatabase(InstallerContext& context) :
50 DPL::TaskDecl<TaskDatabase>(this),
52 m_handleToRemove(INVALID_WIDGET_HANDLE),
53 m_handle(INVALID_WIDGET_HANDLE)
55 AddStep(&TaskDatabase::StepRegisterExternalFiles);
56 AddStep(&TaskDatabase::StepWrtDBInsert);
57 AddStep(&TaskDatabase::StepAceDBInsert);
58 AddStep(&TaskDatabase::StepRemoveExternalFiles);
59 AddStep(&TaskDatabase::StepCreateVconf);
61 AddAbortStep(&TaskDatabase::StepAbortDBInsert);
64 void TaskDatabase::StepWrtDBInsert()
68 /* Set install Time */
69 time(&m_context.widgetConfig.installedTime);
71 if (m_context.existingWidgetInfo.isExist) //update
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());
83 else //new installation
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());
94 FOREACH (cap, m_context.staticPermittedDevCaps) {
95 LogInfo("staticPermittedDevCaps : " << cap->first
96 << " smack status: " << cap->second);
99 LogInfo("Widget registered");
101 Catch(WidgetDAO::Exception::DatabaseError)
103 LogError("Database failure!");
104 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
106 Catch(DPL::DB::SqlConnection::Exception::Base)
108 LogError("Database failure!");
109 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
113 void TaskDatabase::StepAceDBInsert()
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)))
121 LogWarning("Error while removing ace entry for previous insallation");
125 if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
126 m_context.wacSecurity.getCertificateList()))
128 LogError("ace database insert failed");
129 ThrowMsg(Exceptions::NotAllowed, "Update failure. ace_register_widget failed");
131 LogDebug("Ace data inserted");
133 m_context.job->UpdateProgress(
134 InstallerContext::INSTALL_NEW_DB_INSERT,
135 "New Widget DB UPDATE Finished");
138 void TaskDatabase::StepRegisterExternalFiles()
140 WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
141 if (m_context.existingWidgetInfo.isExist) //update
143 WidgetDAO dao(m_context.locations->getPkgname());
144 WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
145 FOREACH(file, externalLocationsDB)
147 if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
149 m_externalLocationsToRemove.push_back(*file);
153 LogDebug("Registering external files:");
154 FOREACH(file, externalLocationsUpdate)
156 LogDebug(" -> " << *file);
159 //set external locations to be registered
160 m_context.widgetConfig.externalLocations = externalLocationsUpdate;
163 void TaskDatabase::StepRemoveExternalFiles()
165 if(!m_externalLocationsToRemove.empty())
167 LogDebug("Removing external files:");
170 FOREACH(file, m_externalLocationsToRemove)
172 if(WrtUtilFileExists(*file))
174 LogDebug(" -> " << *file);
175 remove(file->c_str());
177 else if(WrtUtilDirExists(*file))
179 LogDebug(" -> " << *file);
180 if(!WrtUtilRemove(*file)){
181 ThrowMsg(Exceptions::RemovingFolderFailure,
182 "Failed to remove external directory");
187 LogWarning(" -> " << *file << "(no such a path)");
192 void TaskDatabase::StepCreateVconf()
194 LogDebug("StepCreateVconf");
195 std::map<std::string, WrtDB::SettingsType> vconfData;
197 WrtDB::VconfConfig::GetVconfKeyPopupUsage(
198 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
200 WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
201 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
203 WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
204 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
206 WrtDB::VconfConfig::GetVconfKeyWebDatabaseUsage(
207 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
209 WrtDB::VconfConfig::GetVconfKeyFilesystemUsage(
210 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
212 WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
213 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_OFF;
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
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());
226 ThrowMsg(Exceptions::CreateVconfFailure, "Failed to create vconf files");
231 void TaskDatabase::StepAbortDBInsert()
233 LogWarning("[DB Update Task] Aborting... (DB Clean)");
236 WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
237 LogDebug("Cleaning DB successful!");
239 Catch(DPL::DB::SqlConnection::Exception::Base)
241 LogError("Failed to handle StepAbortDBClean!");
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));
251 } //namespace WidgetInstall