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/foreach.h>
31 #include <dpl/utils/wrt_utility.h>
32 #include <dpl/log/log.h>
33 #include <dpl/assert.h>
36 #include <ace_api_install.h>
37 #include <ace_registration.h>
41 using namespace WrtDB;
44 namespace WidgetInstall {
45 TaskDatabase::TaskDatabase(InstallerContext& context) :
46 DPL::TaskDecl<TaskDatabase>(this),
48 m_handleToRemove(INVALID_WIDGET_HANDLE),
49 m_handle(INVALID_WIDGET_HANDLE)
51 AddStep(&TaskDatabase::StepRegisterExternalFiles);
52 AddStep(&TaskDatabase::StepWrtDBInsert);
53 AddStep(&TaskDatabase::StepAceDBInsert);
54 AddStep(&TaskDatabase::StepRemoveExternalFiles);
56 AddAbortStep(&TaskDatabase::StepAbortDBInsert);
59 void TaskDatabase::StepWrtDBInsert()
63 /* Set install Time */
64 time(&m_context.widgetConfig.installedTime);
66 if (m_context.existingWidgetInfo.isExist) //update
68 m_handleToRemove = WidgetDAOReadOnly::getHandle(
69 m_context.locations->getPkgname());
70 LogInfo("Registering widget... (update)");
71 WidgetDAO::registerOrUpdateWidget(
72 m_context.locations->getPkgname(),
73 m_context.widgetConfig,
74 m_context.wacSecurity);
75 m_handle = WidgetDAOReadOnly::getHandle(
76 m_context.locations->getPkgname());
78 else //new installation
80 LogInfo("Registering widget...");
81 WidgetDAO::registerWidget(
82 m_context.locations->getPkgname(),
83 m_context.widgetConfig,
84 m_context.wacSecurity);
85 m_handle = WidgetDAOReadOnly::getHandle(
86 m_context.locations->getPkgname());
89 FOREACH (cap, m_context.staticPermittedDevCaps) {
90 LogInfo("staticPermittedDevCaps : " << cap->first
91 << " smack status: " << cap->second);
94 LogInfo("Widget registered");
96 Catch(WidgetDAO::Exception::DatabaseError)
98 LogError("Database failure!");
99 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
101 Catch(DPL::DB::SqlConnection::Exception::Base)
103 LogError("Database failure!");
104 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
108 void TaskDatabase::StepAceDBInsert()
110 LogDebug("Inserting Ace database entry. New handle: " << m_handle);
111 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
112 LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
113 if (ACE_OK != ace_unregister_widget(
114 static_cast<ace_widget_handle_t>(m_handleToRemove)))
116 LogWarning("Error while removing ace entry for previous insallation");
120 if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
121 m_context.wacSecurity.getCertificateList()))
123 LogError("ace database insert failed");
124 ThrowMsg(Exceptions::NotAllowed, "Update failure. ace_register_widget failed");
126 LogDebug("Ace data inserted");
128 m_context.job->UpdateProgress(
129 InstallerContext::INSTALL_NEW_DB_INSERT,
130 "New Widget DB UPDATE Finished");
133 void TaskDatabase::StepRegisterExternalFiles()
135 WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
136 if (m_context.existingWidgetInfo.isExist) //update
138 WidgetDAO dao(m_context.locations->getPkgname());
139 WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
140 FOREACH(file, externalLocationsDB)
142 if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
144 m_externalLocationsToRemove.push_back(*file);
148 LogDebug("Registering external files:");
149 FOREACH(file, externalLocationsUpdate)
151 LogDebug(" -> " << *file);
154 //set external locations to be registered
155 m_context.widgetConfig.externalLocations = externalLocationsUpdate;
158 void TaskDatabase::StepRemoveExternalFiles()
160 if(!m_externalLocationsToRemove.empty())
162 LogDebug("Removing external files:");
165 FOREACH(file, m_externalLocationsToRemove)
167 if(WrtUtilFileExists(*file))
169 LogDebug(" -> " << *file);
170 remove(file->c_str());
172 else if(WrtUtilDirExists(*file))
174 LogDebug(" -> " << *file);
175 if(!WrtUtilRemove(*file)){
176 ThrowMsg(Exceptions::RemovingFolderFailure,
177 "Failed to remove external directory");
182 LogWarning(" -> " << *file << "(no such a path)");
187 void TaskDatabase::StepAbortDBInsert()
189 LogWarning("[DB Update Task] Aborting... (DB Clean)");
192 WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
193 LogDebug("Cleaning DB successful!");
195 Catch(DPL::DB::SqlConnection::Exception::Base)
197 LogError("Failed to handle StepAbortDBClean!");
200 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
201 // Remove also old one. If it was already updated nothing wrong will happen,
202 // but if not old widget will be removed.
203 if (INVALID_WIDGET_HANDLE != m_handleToRemove)
204 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
207 } //namespace WidgetInstall