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
28 #include <widget_install/task_database.h>
29 #include <widget_install/job_widget_install.h>
30 #include <widget_install/widget_install_errors.h>
31 #include <widget_install/widget_install_context.h>
33 #include <web_provider_livebox_info.h>
35 #include <dpl/wrt-dao-rw/widget_dao.h>
36 #include <dpl/foreach.h>
37 #include <dpl/utils/wrt_utility.h>
38 #include <dpl/assert.h>
39 #include <wrt-commons/security-origin-dao/security_origin_dao.h>
40 #include <wrt-commons/widget-interface-dao/widget_interface_dao.h>
41 #include <dpl/wrt-dao-ro/global_config.h>
42 #include <dpl/wrt-dao-ro/widget_dao_types.h>
45 #include <ace_api_install.h>
46 #include <ace_registration.h>
50 #include <installer_log.h>
52 using namespace WrtDB;
55 namespace WidgetInstall {
56 TaskDatabase::TaskDatabase(InstallerContext& context) :
57 DPL::TaskDecl<TaskDatabase>(this),
59 m_handleToRemove(INVALID_WIDGET_HANDLE),
60 m_handle(INVALID_WIDGET_HANDLE)
62 AddStep(&TaskDatabase::StartStep);
63 AddStep(&TaskDatabase::StepRegisterExternalFiles);
64 AddStep(&TaskDatabase::StepWrtDBInsert);
65 AddStep(&TaskDatabase::StepAceDBInsert);
66 AddStep(&TaskDatabase::StepSecurityOriginDBInsert);
67 AddStep(&TaskDatabase::StepWidgetInterfaceDBInsert);
68 AddStep(&TaskDatabase::StepRemoveExternalFiles);
70 AddStep(&TaskDatabase::StepLiveboxDBInsert);
72 AddStep(&TaskDatabase::EndStep);
74 AddAbortStep(&TaskDatabase::StepAbortDBInsert);
75 AddAbortStep(&TaskDatabase::StepAbortAceDBInsert);
76 AddAbortStep(&TaskDatabase::StepAbortWidgetInterfaceDBInsert);
79 void TaskDatabase::StepWrtDBInsert()
83 /* Set install Time */
84 time(&m_context.widgetConfig.installedTime);
86 if (m_context.isUpdateMode) { //update
87 _D("Registering widget... (update)");
90 m_handleToRemove = WidgetDAOReadOnly::getHandle(
91 m_context.widgetConfig.tzAppid);
93 std::string makeAppid =
94 DPL::ToUTF8String(m_context.widgetConfig.tzAppid) + "." +
96 m_backAppId = DPL::FromUTF8String(makeAppid);
98 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
100 _E("Given tizenId not found for update installation (Same GUID?)");
101 ThrowMsg(Exceptions::DatabaseFailure,
102 "Given tizenId not found for update installation");
105 WidgetDAO::updateTizenAppId(m_context.widgetConfig.tzAppid,
107 WidgetDAO::registerWidget(m_context.widgetConfig.tzAppid,
108 m_context.widgetConfig,
109 m_context.widgetSecurity);
111 WidgetDAOReadOnly::getHandle(m_context.widgetConfig.tzAppid);
112 } else { //new installation
113 _D("Registering widget...");
114 WidgetDAO::registerWidget(
115 m_context.widgetConfig.tzAppid,
116 m_context.widgetConfig,
117 m_context.widgetSecurity);
118 m_handle = WidgetDAOReadOnly::getHandle(
119 m_context.widgetConfig.tzAppid);
122 FOREACH(cap, m_context.staticPermittedDevCaps) {
123 _D("staticPermittedDevCaps : %ls smack status: %d", cap->first.c_str(), cap->second);
126 _D("Widget registered");
128 Catch(WidgetDAO::Exception::DatabaseError)
130 _E("Database failure!");
131 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
133 Catch(DPL::DB::SqlConnection::Exception::Base)
135 _E("Database failure!");
136 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
140 void TaskDatabase::StepAceDBInsert()
142 _D("Inserting Ace database entry. New handle: %d", m_handle);
143 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
144 _D("Removing old insallation. Handle: %d", m_handleToRemove);
145 if (ACE_OK != ace_unregister_widget(
146 static_cast<ace_widget_handle_t>(m_handleToRemove)))
148 _W("Error while removing ace entry for previous insallation");
152 if (!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
153 m_context.widgetSecurity.getCertificateList()))
155 _E("ace database insert failed");
156 ThrowMsg(Exceptions::UpdateFailed,
157 "Update failure. ace_register_widget failed");
159 _D("Ace data inserted");
162 void TaskDatabase::StepSecurityOriginDBInsert()
164 _D("Create Security origin database");
165 // automatically create security origin database
166 using namespace SecurityOriginDB;
167 using namespace WrtDB;
169 SecurityOriginDAO dao(m_context.locations->getPkgId());
171 // Checking privilege list for setting security origin exception data
172 FOREACH(it, m_context.widgetConfig.configInfo.privilegeList) {
173 std::map<std::string, Feature>::const_iterator result =
174 g_W3CPrivilegeTextMap.find(DPL::ToUTF8String(it->name));
175 if (result != g_W3CPrivilegeTextMap.end()) {
176 if (result->second == FEATURE_USER_MEDIA) {
177 dao.setPrivilegeSecurityOriginData(result->second, false);
178 } else if (result->second == FEATURE_FULLSCREEN_MODE) {
181 dao.setPrivilegeSecurityOriginData(result->second);
187 void TaskDatabase::StepWidgetInterfaceDBInsert()
189 _D("Create Widget Interface database");
190 using namespace WidgetInterfaceDB;
191 using namespace WrtDB;
193 DbWidgetHandle handle =
194 WidgetDAOReadOnly::getHandle(m_context.widgetConfig.tzAppid);
197 if (m_context.isUpdateMode) {
198 std::string dbPath = WidgetInterfaceDAO::databaseFileName(handle);
199 std::string backupDbPath = dbPath;
200 backupDbPath += GlobalConfig::GetBackupDatabaseSuffix();
201 _D("\"%s\" to \"%s\"", dbPath.c_str(), backupDbPath.c_str());
202 if (0 != std::rename(dbPath.c_str(), backupDbPath.c_str())) {
203 _E("widget interface database backup failed");
204 ThrowMsg(Exceptions::UpdateFailed,
205 "widget interface database backup failed");
211 // automatically create widget interface database
212 WidgetInterfaceDAO dao(handle);
214 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
216 _E("widget interface database create failed");
217 ThrowMsg(Exceptions::UpdateFailed,
218 "widget interface database create failed");
222 void TaskDatabase::StepRegisterExternalFiles()
224 WrtDB::ExternalLocationList externalLocationsUpdate =
225 m_context.locations->listExternalLocations();
226 if (m_context.isUpdateMode) { //update
229 WidgetDAO dao(m_context.widgetConfig.tzAppid);
230 WrtDB::ExternalLocationList externalLocationsDB =
231 dao.getWidgetExternalLocations();
232 FOREACH(file, externalLocationsDB)
234 if (std::find(externalLocationsUpdate.begin(),
235 externalLocationsUpdate.end(),
236 *file) == externalLocationsUpdate.end())
238 m_externalLocationsToRemove.push_back(*file);
242 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
244 _E("Given tizenId not found for update installation (Same GUID?)");
245 ThrowMsg(Exceptions::UpdateFailed,
246 "Given tizenId not found for update installation");
249 _D("Registering external files:");
250 FOREACH(file, externalLocationsUpdate)
252 _D(" -> %s", (*file).c_str());
255 //set external locations to be registered
256 m_context.widgetConfig.externalLocations = externalLocationsUpdate;
259 void TaskDatabase::StepRemoveExternalFiles()
261 if (!m_externalLocationsToRemove.empty()) {
262 _D("Removing external files:");
265 FOREACH(file, m_externalLocationsToRemove)
267 if (WrtUtilFileExists(*file)) {
268 _D(" -> %s", (*file).c_str());
269 if (-1 == TEMP_FAILURE_RETRY(remove(file->c_str()))) {
270 ThrowMsg(Exceptions::RemovingFileFailure,
271 "Failed to remove external file");
273 } else if (WrtUtilDirExists(*file)) {
274 _D(" -> %s", (*file).c_str());
275 if (!WrtUtilRemove(*file)) {
276 ThrowMsg(Exceptions::RemovingFolderFailure,
277 "Failed to remove external directory");
280 _W(" -> %s(no such a path)", (*file).c_str());
285 void TaskDatabase::StepAbortDBInsert()
287 _W("[DB Update Task] Aborting... (DB Clean)");
290 if (m_context.isUpdateMode) {
291 WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
292 WidgetDAO::updateTizenAppId(m_backAppId,
293 m_context.widgetConfig.tzAppid);
295 WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
297 _D("Cleaning DB successful!");
299 Catch(DPL::DB::SqlConnection::Exception::Base)
301 _E("Failed to handle StepAbortDBClean!");
305 void TaskDatabase::StepAbortAceDBInsert()
307 _W("[DB Update Task] ACE DB Aborting... (DB Clean)");
309 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
310 // Remove also old one. If it was already updated nothing wrong will happen,
311 // but if not old widget will be removed.
312 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
313 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
316 if (!AceApi::registerAceWidgetFromDB(m_handleToRemove))
318 _E("ace database restore failed");
320 _D("Ace data inserted");
323 void TaskDatabase::StepAbortWidgetInterfaceDBInsert()
325 _D("[DB Update Task] Widget interface Aborting...");
326 using namespace WidgetInterfaceDB;
327 using namespace WrtDB;
329 DbWidgetHandle handle =
330 WidgetDAOReadOnly::getHandle(m_context.widgetConfig.tzAppid);
331 std::string dbPath = WidgetInterfaceDAO::databaseFileName(handle);
334 if (remove(dbPath.c_str()) != 0) {
335 _W("Fail to remove");
339 if (m_context.isUpdateMode) {
340 std::string backupDbPath = dbPath;
341 backupDbPath += GlobalConfig::GetBackupDatabaseSuffix();
342 _D("\"%s\" to \"%s\"", dbPath.c_str(), backupDbPath.c_str());
343 if (0 != std::rename(backupDbPath.c_str(), dbPath.c_str())) {
344 _W("Fail to rollback");
350 void TaskDatabase::StepLiveboxDBInsert()
352 if (m_context.widgetConfig.configInfo.m_livebox.size() <= 0) {
356 std::string tizenId = DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
358 // insert specific information to web livebox db
359 for (auto it = m_context.widgetConfig.configInfo.m_livebox.begin();
360 it != m_context.widgetConfig.configInfo.m_livebox.end(); ++it)
362 std::string boxId = DPL::ToUTF8String((**it).m_liveboxId);
364 if ((**it).m_type.empty()) {
365 boxType = web_provider_livebox_get_default_type();
367 boxType = DPL::ToUTF8String((**it).m_type);
369 _D("livebox id: %s", boxId.c_str());
370 _D("livebox type: %s", boxType.c_str());
372 int autoLaunch = (**it).m_autoLaunch == L"true" ? 1 : 0;
373 _D("livebox auto-launch: %d", autoLaunch);
375 int mouseEvent = (**it).m_boxInfo.m_boxMouseEvent == L"true" ? 1 : 0;
376 _D("livebox mouse-event: %d", mouseEvent);
378 int pdFastOpen = (**it).m_boxInfo.m_pdFastOpen == L"true" ? 1 : 0;
379 _D("livebox pd fast-open: %d", pdFastOpen);
381 web_provider_livebox_insert_box_info(
382 boxId.c_str(), tizenId.c_str(), boxType.c_str(),
383 autoLaunch, mouseEvent, pdFastOpen);
388 void TaskDatabase::StartStep()
390 _D("--------- <TaskDatabase> : START ----------");
393 void TaskDatabase::EndStep()
395 _D("--------- <TaskDatabase> : END ----------");
397 } //namespace WidgetInstall