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 LogInfo("Registering widget... (update)");
76 m_handleToRemove = WidgetDAOReadOnly::getHandle(
77 m_context.locations->getPkgname());
79 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
81 LogError("Given tizenId not found for update installation (Same GUID?)");
82 ThrowMsg(Exceptions::InvalidPackage, "Given tizenId not found for update installation");
84 WidgetDAO::registerOrUpdateWidget(
85 m_context.locations->getPkgname(),
86 m_context.widgetConfig,
87 m_context.wacSecurity);
88 m_handle = WidgetDAOReadOnly::getHandle(
89 m_context.locations->getPkgname());
91 else //new installation
93 LogInfo("Registering widget...");
94 WidgetDAO::registerWidget(
95 m_context.locations->getPkgname(),
96 m_context.widgetConfig,
97 m_context.wacSecurity);
98 m_handle = WidgetDAOReadOnly::getHandle(
99 m_context.locations->getPkgname());
102 FOREACH (cap, m_context.staticPermittedDevCaps) {
103 LogInfo("staticPermittedDevCaps : " << cap->first
104 << " smack status: " << cap->second);
107 LogInfo("Widget registered");
109 Catch(WidgetDAO::Exception::DatabaseError)
111 LogError("Database failure!");
112 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
114 Catch(DPL::DB::SqlConnection::Exception::Base)
116 LogError("Database failure!");
117 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
121 void TaskDatabase::StepAceDBInsert()
123 LogDebug("Inserting Ace database entry. New handle: " << m_handle);
124 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
125 LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
126 if (ACE_OK != ace_unregister_widget(
127 static_cast<ace_widget_handle_t>(m_handleToRemove)))
129 LogWarning("Error while removing ace entry for previous insallation");
133 if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
134 m_context.wacSecurity.getCertificateList()))
136 LogError("ace database insert failed");
137 ThrowMsg(Exceptions::UpdateFailed, "Update failure. ace_register_widget failed");
139 LogDebug("Ace data inserted");
141 m_context.job->UpdateProgress(
142 InstallerContext::INSTALL_NEW_DB_INSERT,
143 "New Widget DB UPDATE Finished");
146 void TaskDatabase::StepRegisterExternalFiles()
148 WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
149 if (m_context.existingWidgetInfo.isExist) //update
153 WidgetDAO dao(m_context.locations->getPkgname());
154 WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
155 FOREACH(file, externalLocationsDB)
157 if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
159 m_externalLocationsToRemove.push_back(*file);
163 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
165 LogError("Given tizenId not found for update installation (Same GUID?)");
166 ThrowMsg(Exceptions::UpdateFailed, "Given tizenId not found for update installation");
169 LogDebug("Registering external files:");
170 FOREACH(file, externalLocationsUpdate)
172 LogDebug(" -> " << *file);
175 //set external locations to be registered
176 m_context.widgetConfig.externalLocations = externalLocationsUpdate;
179 void TaskDatabase::StepRemoveExternalFiles()
181 if(!m_externalLocationsToRemove.empty())
183 LogDebug("Removing external files:");
186 FOREACH(file, m_externalLocationsToRemove)
188 if(WrtUtilFileExists(*file))
190 LogDebug(" -> " << *file);
191 remove(file->c_str());
193 else if(WrtUtilDirExists(*file))
195 LogDebug(" -> " << *file);
196 if(!WrtUtilRemove(*file)){
197 ThrowMsg(Exceptions::RemovingFolderFailure,
198 "Failed to remove external directory");
203 LogWarning(" -> " << *file << "(no such a path)");
208 void TaskDatabase::StepCreateVconf()
210 LogDebug("StepCreateVconf");
211 std::map<std::string, WrtDB::SettingsType> vconfData;
213 WrtDB::VconfConfig::GetVconfKeyPopupUsage(
214 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
216 WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
217 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
219 WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
220 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
222 WrtDB::VconfConfig::GetVconfKeyWebDatabaseUsage(
223 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
225 WrtDB::VconfConfig::GetVconfKeyFilesystemUsage(
226 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
228 WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
229 m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_OFF;
231 // vconftool -g 5000 set -t int <path> initialize value
232 // Current installer should use vconftool for setting group ID
233 // In case of install application by pkgcmd, permission for others
235 FOREACH(it, vconfData) {
236 std::ostringstream command;
237 command << "vconftool -g 5000 set -t int ";
238 command << (*it).first;
239 command << " \"" << static_cast<int>((*it).second) << "\"";
240 int ret = system(command.str().c_str());
242 ThrowMsg(Exceptions::CreateVconfFailure, "Failed to create vconf files");
247 void TaskDatabase::StepAbortDBInsert()
249 LogWarning("[DB Update Task] Aborting... (DB Clean)");
252 WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
253 LogDebug("Cleaning DB successful!");
255 Catch(DPL::DB::SqlConnection::Exception::Base)
257 LogError("Failed to handle StepAbortDBClean!");
260 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
261 // Remove also old one. If it was already updated nothing wrong will happen,
262 // but if not old widget will be removed.
263 if (INVALID_WIDGET_HANDLE != m_handleToRemove)
264 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
267 } //namespace WidgetInstall