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>
39 using namespace WrtDB;
42 namespace WidgetInstall {
43 TaskDatabase::TaskDatabase(InstallerContext& context) :
44 DPL::TaskDecl<TaskDatabase>(this),
46 m_handleToRemove(INVALID_WIDGET_HANDLE),
47 m_handle(INVALID_WIDGET_HANDLE)
49 AddStep(&TaskDatabase::StepRegisterExternalFiles);
50 AddStep(&TaskDatabase::StepWrtDBInsert);
51 AddStep(&TaskDatabase::StepAceDBInsert);
52 AddStep(&TaskDatabase::StepRemoveExternalFiles);
54 AddAbortStep(&TaskDatabase::StepAbortDBInsert);
57 void TaskDatabase::StepWrtDBInsert()
61 /* Set install Time */
62 time(&m_context.widgetConfig.installedTime);
64 if (m_context.existingWidgetInfo.isExist) //update
66 m_handleToRemove = WidgetDAOReadOnly::getHandle(
67 m_context.locations->getPkgname());
68 LogInfo("Registering widget... (update)");
69 WidgetDAO::registerOrUpdateWidget(
70 m_context.locations->getPkgname(),
71 m_context.widgetConfig,
72 m_context.wacSecurity);
73 m_handle = WidgetDAOReadOnly::getHandle(
74 m_context.locations->getPkgname());
76 else //new installation
78 LogInfo("Registering widget...");
79 WidgetDAO::registerWidget(
80 m_context.locations->getPkgname(),
81 m_context.widgetConfig,
82 m_context.wacSecurity);
83 m_handle = WidgetDAOReadOnly::getHandle(
84 m_context.locations->getPkgname());
87 FOREACH (cap, m_context.staticPermittedDevCaps) {
88 LogInfo("staticPermittedDevCaps : " << cap->first
89 << " smack status: " << cap->second);
92 LogInfo("Widget registered");
94 Catch(WidgetDAO::Exception::DatabaseError)
96 LogError("Database failure!");
97 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
99 Catch(DPL::DB::SqlConnection::Exception::Base)
101 LogError("Database failure!");
102 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
106 void TaskDatabase::StepAceDBInsert()
108 LogDebug("Inserting Ace database entry. New handle: " << m_handle);
109 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
110 LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
111 if (ACE_OK != ace_unregister_widget(
112 static_cast<ace_widget_handle_t>(m_handleToRemove)))
114 LogWarning("Error while removing ace entry for previous insallation");
118 if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
119 m_context.wacSecurity.getCertificateList()))
121 LogError("ace database insert failed");
122 ThrowMsg(Exceptions::NotAllowed, "Update failure. ace_register_widget failed");
124 LogDebug("Ace data inserted");
126 m_context.job->UpdateProgress(
127 InstallerContext::INSTALL_NEW_DB_INSERT,
128 "New Widget DB UPDATE Finished");
131 void TaskDatabase::StepRegisterExternalFiles()
133 WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
134 if (m_context.existingWidgetInfo.isExist) //update
136 WidgetDAO dao(m_context.locations->getPkgname());
137 WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
138 FOREACH(file, externalLocationsDB)
140 if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
142 m_externalLocationsToRemove.push_back(*file);
146 LogDebug("Registering external files:");
147 FOREACH(file, externalLocationsUpdate)
149 LogDebug(" -> " << *file);
152 //set external locations to be registered
153 m_context.widgetConfig.externalLocations = externalLocationsUpdate;
156 void TaskDatabase::StepRemoveExternalFiles()
158 if(!m_externalLocationsToRemove.empty())
160 LogDebug("Removing external files:");
163 FOREACH(file, m_externalLocationsToRemove)
165 if(WrtUtilFileExists(*file))
167 LogDebug(" -> " << *file);
168 remove(file->c_str());
170 else if(WrtUtilDirExists(*file))
172 LogDebug(" -> " << *file);
173 if(!WrtUtilRemove(*file)){
174 ThrowMsg(Exceptions::RemovingFolderFailure,
175 "Failed to remove external directory");
180 LogWarning(" -> " << *file << "(no such a path)");
185 void TaskDatabase::StepAbortDBInsert()
187 LogWarning("[DB Update Task] Aborting... (DB Clean)");
190 WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
191 LogDebug("Cleaning DB successful!");
193 Catch(DPL::DB::SqlConnection::Exception::Base)
195 LogError("Failed to handle StepAbortDBClean!");
198 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
199 // Remove also old one. If it was already updated nothing wrong will happen,
200 // but if not old widget will be removed.
201 if (INVALID_WIDGET_HANDLE != m_handleToRemove)
202 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
205 } //namespace WidgetInstall