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 dao.setPrivilegeSecurityOriginData(result->second);
166 m_context.job->UpdateProgress(
167 InstallerContext::INSTALL_NEW_DB_INSERT,
168 "New Widget DB UPDATE Finished");
171 void TaskDatabase::StepRegisterExternalFiles()
173 WrtDB::ExternalLocationList externalLocationsUpdate =
174 m_context.locations->listExternalLocations();
175 if (m_context.existingWidgetInfo.isExist) { //update
178 WidgetDAO dao(m_context.widgetConfig.tzAppid);
179 WrtDB::ExternalLocationList externalLocationsDB =
180 dao.getWidgetExternalLocations();
181 FOREACH(file, externalLocationsDB)
183 if (std::find(externalLocationsUpdate.begin(),
184 externalLocationsUpdate.end(),
185 *file) == externalLocationsUpdate.end())
187 m_externalLocationsToRemove.push_back(*file);
191 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
194 "Given tizenId not found for update installation (Same GUID?)");
195 ThrowMsg(Exceptions::UpdateFailed,
196 "Given tizenId not found for update installation");
199 LogDebug("Registering external files:");
200 FOREACH(file, externalLocationsUpdate)
202 LogDebug(" -> " << *file);
205 //set external locations to be registered
206 m_context.widgetConfig.externalLocations = externalLocationsUpdate;
209 void TaskDatabase::StepRemoveExternalFiles()
211 if (!m_externalLocationsToRemove.empty()) {
212 LogDebug("Removing external files:");
215 FOREACH(file, m_externalLocationsToRemove)
217 if (WrtUtilFileExists(*file)) {
218 LogDebug(" -> " << *file);
219 if (-1 == TEMP_FAILURE_RETRY(remove(file->c_str()))) {
220 ThrowMsg(Exceptions::RemovingFileFailure,
221 "Failed to remove external file");
223 } else if (WrtUtilDirExists(*file)) {
224 LogDebug(" -> " << *file);
225 if (!WrtUtilRemove(*file)) {
226 ThrowMsg(Exceptions::RemovingFolderFailure,
227 "Failed to remove external directory");
230 LogWarning(" -> " << *file << "(no such a path)");
235 void TaskDatabase::StepCreateVconf()
237 LogDebug("StepCreateVconf");
238 std::map<std::string, WrtDB::SettingsType> vconfData;
240 WrtDB::VconfConfig::GetVconfKeyPopupUsage(
241 m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
243 WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
244 m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
246 WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
247 m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
249 WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
250 m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_OFF;
252 // vconftool -g 5000 set -t int <path> initialize value
253 // Current installer should use vconftool for setting group ID
254 // In case of install application by pkgcmd, permission for others
256 FOREACH(it, vconfData) {
257 std::ostringstream command;
258 command << "vconftool -g 5000 set -t int ";
259 command << (*it).first;
260 command << " \"" << static_cast<int>((*it).second) << "\"";
261 int ret = system(command.str().c_str());
263 ThrowMsg(Exceptions::CreateVconfFailure,
264 "Failed to create vconf files");
269 void TaskDatabase::StepAbortDBInsert()
271 LogWarning("[DB Update Task] Aborting... (DB Clean)");
274 WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
275 LogDebug("Cleaning DB successful!");
277 Catch(DPL::DB::SqlConnection::Exception::Base)
279 LogError("Failed to handle StepAbortDBClean!");
282 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
283 // Remove also old one. If it was already updated nothing wrong will happen,
284 // but if not old widget will be removed.
285 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
286 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
290 void TaskDatabase::StepLiveboxDBInsert()
292 if (m_context.widgetConfig.configInfo.m_livebox.size() <= 0) {
296 std::string tizenId = DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
298 for (auto it = m_context.widgetConfig.configInfo.m_livebox.begin();
299 it != m_context.widgetConfig.configInfo.m_livebox.end(); ++it)
301 std::string boxId = DPL::ToUTF8String((**it).m_liveboxId);
303 if ((**it).m_type == L"") {
304 boxType = web_provider_info_get_default_type();
306 boxType = DPL::ToUTF8String((**it).m_type);
308 LogInfo("livebox id: " << boxId);
309 LogInfo("livebox type: " << boxType);
312 web_provider_info_insert_box_type(
313 boxId.c_str(), tizenId.c_str(), boxType.c_str());
316 LogDebug("failed to set type of livebox: " << boxId);
320 } //namespace WidgetInstall