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
27 #include <widget_install/task_database.h>
28 #include <widget_install/job_widget_install.h>
29 #include <widget_install/widget_install_errors.h>
30 #include <widget_install/widget_install_context.h>
31 #include <web-provider-info.h>
32 #include <dpl/wrt-dao-rw/widget_dao.h>
33 #include <dpl/wrt-dao-ro/vconf_config.h>
34 #include <dpl/foreach.h>
35 #include <dpl/utils/wrt_utility.h>
36 #include <dpl/log/log.h>
37 #include <dpl/assert.h>
38 #include <wrt-commons/security-origin-dao/security_origin_dao.h>
41 #include <ace_api_install.h>
42 #include <ace_registration.h>
48 using namespace WrtDB;
51 namespace WidgetInstall {
52 TaskDatabase::TaskDatabase(InstallerContext& context) :
53 DPL::TaskDecl<TaskDatabase>(this),
55 m_handleToRemove(INVALID_WIDGET_HANDLE),
56 m_handle(INVALID_WIDGET_HANDLE)
58 AddStep(&TaskDatabase::StepRegisterExternalFiles);
59 AddStep(&TaskDatabase::StepWrtDBInsert);
60 AddStep(&TaskDatabase::StepAceDBInsert);
61 AddStep(&TaskDatabase::StepSecurityOriginDBInsert);
62 AddStep(&TaskDatabase::StepRemoveExternalFiles);
63 AddStep(&TaskDatabase::StepCreateVconf);
64 AddStep(&TaskDatabase::StepLiveboxDBInsert);
66 AddAbortStep(&TaskDatabase::StepAbortDBInsert);
69 void TaskDatabase::StepWrtDBInsert()
73 /* Set install Time */
74 time(&m_context.widgetConfig.installedTime);
76 if (m_context.existingWidgetInfo.isExist) { //update
77 LogInfo("Registering widget... (update)");
80 m_handleToRemove = WidgetDAOReadOnly::getHandle(
81 m_context.widgetConfig.tzAppid);
83 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
86 "Given tizenId not found for update installation (Same GUID?)");
87 ThrowMsg(Exceptions::InvalidPackage,
88 "Given tizenId not found for update installation");
90 WidgetDAO::registerOrUpdateWidget(
91 m_context.widgetConfig.tzAppid,
92 m_context.widgetConfig,
93 m_context.wacSecurity);
94 m_handle = WidgetDAOReadOnly::getHandle(
95 m_context.widgetConfig.tzAppid);
96 } else { //new installation
97 LogInfo("Registering widget...");
98 WidgetDAO::registerWidget(
99 m_context.widgetConfig.tzAppid,
100 m_context.widgetConfig,
101 m_context.wacSecurity);
102 m_handle = WidgetDAOReadOnly::getHandle(
103 m_context.widgetConfig.tzAppid);
106 FOREACH(cap, m_context.staticPermittedDevCaps) {
108 "staticPermittedDevCaps : " << cap->first
109 << " smack status: " <<
113 LogInfo("Widget registered");
115 Catch(WidgetDAO::Exception::DatabaseError)
117 LogError("Database failure!");
118 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
120 Catch(DPL::DB::SqlConnection::Exception::Base)
122 LogError("Database failure!");
123 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
127 void TaskDatabase::StepAceDBInsert()
129 LogDebug("Inserting Ace database entry. New handle: " << m_handle);
130 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
131 LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
132 if (ACE_OK != ace_unregister_widget(
133 static_cast<ace_widget_handle_t>(m_handleToRemove)))
136 "Error while removing ace entry for previous insallation");
140 if (!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
141 m_context.wacSecurity.getCertificateList()))
143 LogError("ace database insert failed");
144 ThrowMsg(Exceptions::UpdateFailed,
145 "Update failure. ace_register_widget failed");
147 LogDebug("Ace data inserted");
150 void TaskDatabase::StepSecurityOriginDBInsert()
152 LogDebug("Create Security origin database");
153 // automatically create security origin database
154 using namespace SecurityOriginDB;
155 SecurityOriginDAO dao(m_context.locations->getPkgId());
157 // Checking privilege list for setting security origin exception data
158 FOREACH(it, m_context.widgetConfig.configInfo.privilegeList) {
159 std::map<std::string, Feature>::const_iterator result =
160 g_W3CPrivilegeTextMap.find(DPL::ToUTF8String(it->name));
161 if (result != g_W3CPrivilegeTextMap.end()) {
162 if (result->second == FEATURE_USER_MEDIA) {
163 dao.setPrivilegeSecurityOriginData(result->second, false);
165 dao.setPrivilegeSecurityOriginData(result->second);
170 m_context.job->UpdateProgress(
171 InstallerContext::INSTALL_NEW_DB_INSERT,
172 "New Widget DB UPDATE Finished");
175 void TaskDatabase::StepRegisterExternalFiles()
177 WrtDB::ExternalLocationList externalLocationsUpdate =
178 m_context.locations->listExternalLocations();
179 if (m_context.existingWidgetInfo.isExist) { //update
182 WidgetDAO dao(m_context.widgetConfig.tzAppid);
183 WrtDB::ExternalLocationList externalLocationsDB =
184 dao.getWidgetExternalLocations();
185 FOREACH(file, externalLocationsDB)
187 if (std::find(externalLocationsUpdate.begin(),
188 externalLocationsUpdate.end(),
189 *file) == externalLocationsUpdate.end())
191 m_externalLocationsToRemove.push_back(*file);
195 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
198 "Given tizenId not found for update installation (Same GUID?)");
199 ThrowMsg(Exceptions::UpdateFailed,
200 "Given tizenId not found for update installation");
203 LogDebug("Registering external files:");
204 FOREACH(file, externalLocationsUpdate)
206 LogDebug(" -> " << *file);
209 //set external locations to be registered
210 m_context.widgetConfig.externalLocations = externalLocationsUpdate;
213 void TaskDatabase::StepRemoveExternalFiles()
215 if (!m_externalLocationsToRemove.empty()) {
216 LogDebug("Removing external files:");
219 FOREACH(file, m_externalLocationsToRemove)
221 if (WrtUtilFileExists(*file)) {
222 LogDebug(" -> " << *file);
223 if (-1 == TEMP_FAILURE_RETRY(remove(file->c_str()))) {
224 ThrowMsg(Exceptions::RemovingFileFailure,
225 "Failed to remove external file");
227 } else if (WrtUtilDirExists(*file)) {
228 LogDebug(" -> " << *file);
229 if (!WrtUtilRemove(*file)) {
230 ThrowMsg(Exceptions::RemovingFolderFailure,
231 "Failed to remove external directory");
234 LogWarning(" -> " << *file << "(no such a path)");
239 void TaskDatabase::StepCreateVconf()
241 LogDebug("StepCreateVconf");
242 std::map<std::string, WrtDB::SettingsType> vconfData;
244 WrtDB::VconfConfig::GetVconfKeyPopupUsage(
245 m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
247 WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
248 m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
250 WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
251 m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
253 WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
254 m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_OFF;
256 // vconftool -g 5000 set -t int <path> initialize value
257 // Current installer should use vconftool for setting group ID
258 // In case of install application by pkgcmd, permission for others
260 FOREACH(it, vconfData) {
261 std::ostringstream command;
262 command << "vconftool -g 5000 set -t int ";
263 command << (*it).first;
264 command << " \"" << static_cast<int>((*it).second) << "\"";
265 int ret = system(command.str().c_str());
267 ThrowMsg(Exceptions::CreateVconfFailure,
268 "Failed to create vconf files");
273 void TaskDatabase::StepAbortDBInsert()
275 LogWarning("[DB Update Task] Aborting... (DB Clean)");
278 WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
279 LogDebug("Cleaning DB successful!");
281 Catch(DPL::DB::SqlConnection::Exception::Base)
283 LogError("Failed to handle StepAbortDBClean!");
286 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
287 // Remove also old one. If it was already updated nothing wrong will happen,
288 // but if not old widget will be removed.
289 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
290 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
294 void TaskDatabase::StepLiveboxDBInsert()
296 if (m_context.widgetConfig.configInfo.m_livebox.size() <= 0) {
300 std::string tizenId = DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
302 for (auto it = m_context.widgetConfig.configInfo.m_livebox.begin();
303 it != m_context.widgetConfig.configInfo.m_livebox.end(); ++it)
305 std::string boxId = DPL::ToUTF8String((**it).m_liveboxId);
307 if ((**it).m_type == L"") {
308 boxType = web_provider_info_get_default_type();
310 boxType = DPL::ToUTF8String((**it).m_type);
312 LogInfo("livebox id: " << boxId);
313 LogInfo("livebox type: " << boxType);
316 web_provider_info_insert_box_type(
317 boxId.c_str(), tizenId.c_str(), boxType.c_str());
320 LogDebug("failed to set type of livebox: " << boxId);
324 } //namespace WidgetInstall