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>
42 using namespace WrtDB;
45 namespace WidgetInstall {
48 const char * const VCONF_KEY_PREFIX = "file/private/";
49 const char * const VCONF_KEY_GROUP = "/security";
50 const char * const VCONF_KEY_POPUP_USAGE = "/popup_usage";
51 const char * const VCONF_KEY_GEOLOCATION_USAGE = "/geolocation_usage";
52 const char * const VCONF_KEY_WEB_NOTIFICATION_USAGE = "/web_notification_usage";
53 const char * const VCONF_KEY_WEB_DATABASE_USAGE = "/web_database_usage";
54 const char * const VCONF_KEY_FILESYSTEM_USAGE = "/filesystem_usage";
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::StepRegisterExternalFiles);
63 AddStep(&TaskDatabase::StepWrtDBInsert);
64 AddStep(&TaskDatabase::StepAceDBInsert);
65 AddStep(&TaskDatabase::StepRemoveExternalFiles);
66 AddStep(&TaskDatabase::StepCreateVconf);
68 AddAbortStep(&TaskDatabase::StepAbortDBInsert);
71 void TaskDatabase::StepWrtDBInsert()
75 /* Set install Time */
76 time(&m_context.widgetConfig.installedTime);
78 if (m_context.existingWidgetInfo.isExist) //update
80 m_handleToRemove = WidgetDAOReadOnly::getHandle(
81 m_context.locations->getPkgname());
82 LogInfo("Registering widget... (update)");
83 WidgetDAO::registerOrUpdateWidget(
84 m_context.locations->getPkgname(),
85 m_context.widgetConfig,
86 m_context.wacSecurity);
87 m_handle = WidgetDAOReadOnly::getHandle(
88 m_context.locations->getPkgname());
90 else //new installation
92 LogInfo("Registering widget...");
93 WidgetDAO::registerWidget(
94 m_context.locations->getPkgname(),
95 m_context.widgetConfig,
96 m_context.wacSecurity);
97 m_handle = WidgetDAOReadOnly::getHandle(
98 m_context.locations->getPkgname());
101 FOREACH (cap, m_context.staticPermittedDevCaps) {
102 LogInfo("staticPermittedDevCaps : " << cap->first
103 << " smack status: " << cap->second);
106 LogInfo("Widget registered");
108 Catch(WidgetDAO::Exception::DatabaseError)
110 LogError("Database failure!");
111 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
113 Catch(DPL::DB::SqlConnection::Exception::Base)
115 LogError("Database failure!");
116 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
120 void TaskDatabase::StepAceDBInsert()
122 LogDebug("Inserting Ace database entry. New handle: " << m_handle);
123 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
124 LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
125 if (ACE_OK != ace_unregister_widget(
126 static_cast<ace_widget_handle_t>(m_handleToRemove)))
128 LogWarning("Error while removing ace entry for previous insallation");
132 if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
133 m_context.wacSecurity.getCertificateList()))
135 LogError("ace database insert failed");
136 ThrowMsg(Exceptions::NotAllowed, "Update failure. ace_register_widget failed");
138 LogDebug("Ace data inserted");
140 m_context.job->UpdateProgress(
141 InstallerContext::INSTALL_NEW_DB_INSERT,
142 "New Widget DB UPDATE Finished");
145 void TaskDatabase::StepRegisterExternalFiles()
147 WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
148 if (m_context.existingWidgetInfo.isExist) //update
150 WidgetDAO dao(m_context.locations->getPkgname());
151 WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
152 FOREACH(file, externalLocationsDB)
154 if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
156 m_externalLocationsToRemove.push_back(*file);
160 LogDebug("Registering external files:");
161 FOREACH(file, externalLocationsUpdate)
163 LogDebug(" -> " << *file);
166 //set external locations to be registered
167 m_context.widgetConfig.externalLocations = externalLocationsUpdate;
170 void TaskDatabase::StepRemoveExternalFiles()
172 if(!m_externalLocationsToRemove.empty())
174 LogDebug("Removing external files:");
177 FOREACH(file, m_externalLocationsToRemove)
179 if(WrtUtilFileExists(*file))
181 LogDebug(" -> " << *file);
182 remove(file->c_str());
184 else if(WrtUtilDirExists(*file))
186 LogDebug(" -> " << *file);
187 if(!WrtUtilRemove(*file)){
188 ThrowMsg(Exceptions::RemovingFolderFailure,
189 "Failed to remove external directory");
194 LogWarning(" -> " << *file << "(no such a path)");
199 void TaskDatabase::StepCreateVconf()
201 LogDebug("StepCreateVconf");
202 std::string keyPrefix =
204 + DPL::ToUTF8String(m_context.locations->getPkgname())
206 std::string securityPopupUsageKey = keyPrefix + VCONF_KEY_POPUP_USAGE;
207 std::string geolocationUsageKey = keyPrefix + VCONF_KEY_GEOLOCATION_USAGE;
208 std::string webNotificationUsageKey = keyPrefix + VCONF_KEY_WEB_NOTIFICATION_USAGE;
209 std::string webDatabaseUsageKey = keyPrefix + VCONF_KEY_WEB_DATABASE_USAGE;
210 std::string filesystemUsageKey = keyPrefix + VCONF_KEY_FILESYSTEM_USAGE;
212 vconf_set_int(securityPopupUsageKey.c_str(),
213 static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
214 // prevent permission error
215 vconf_unset(securityPopupUsageKey.c_str());
216 vconf_set_int(securityPopupUsageKey.c_str(),
217 static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
218 vconf_set_int(geolocationUsageKey.c_str(),
219 static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
220 vconf_set_int(webNotificationUsageKey.c_str(),
221 static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
222 vconf_set_int(webDatabaseUsageKey.c_str(),
223 static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
224 vconf_set_int(filesystemUsageKey.c_str(),
225 static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
228 void TaskDatabase::StepAbortDBInsert()
230 LogWarning("[DB Update Task] Aborting... (DB Clean)");
233 WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
234 LogDebug("Cleaning DB successful!");
236 Catch(DPL::DB::SqlConnection::Exception::Base)
238 LogError("Failed to handle StepAbortDBClean!");
241 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
242 // Remove also old one. If it was already updated nothing wrong will happen,
243 // but if not old widget will be removed.
244 if (INVALID_WIDGET_HANDLE != m_handleToRemove)
245 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
248 } //namespace WidgetInstall