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_livebox_info.h>
32 #include <dpl/wrt-dao-rw/widget_dao.h>
33 #include <dpl/foreach.h>
34 #include <dpl/utils/wrt_utility.h>
35 #include <dpl/log/log.h>
36 #include <dpl/assert.h>
37 #include <wrt-commons/security-origin-dao/security_origin_dao.h>
38 #include <dpl/wrt-dao-ro/widget_dao_types.h>
41 #include <ace_api_install.h>
42 #include <ace_registration.h>
47 using namespace WrtDB;
50 namespace WidgetInstall {
51 TaskDatabase::TaskDatabase(InstallerContext& context) :
52 DPL::TaskDecl<TaskDatabase>(this),
54 m_handleToRemove(INVALID_WIDGET_HANDLE),
55 m_handle(INVALID_WIDGET_HANDLE)
57 AddStep(&TaskDatabase::StartStep);
58 AddStep(&TaskDatabase::StepRegisterExternalFiles);
59 AddStep(&TaskDatabase::StepWrtDBInsert);
60 AddStep(&TaskDatabase::StepAceDBInsert);
61 AddStep(&TaskDatabase::StepSecurityOriginDBInsert);
62 AddStep(&TaskDatabase::StepRemoveExternalFiles);
63 AddStep(&TaskDatabase::StepLiveboxDBInsert);
64 AddStep(&TaskDatabase::EndStep);
66 AddAbortStep(&TaskDatabase::StepAbortDBInsert);
67 AddAbortStep(&TaskDatabase::StepAbortAceDBInsert);
70 void TaskDatabase::StepWrtDBInsert()
74 /* Set install Time */
75 time(&m_context.widgetConfig.installedTime);
77 if (m_context.isUpdateMode) { //update
78 LogDebug("Registering widget... (update)");
81 m_handleToRemove = WidgetDAOReadOnly::getHandle(
82 m_context.widgetConfig.tzAppid);
84 std::string makeAppid =
85 DPL::ToUTF8String(m_context.widgetConfig.tzAppid) + "." +
87 m_backAppId = DPL::FromUTF8String(makeAppid);
89 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
92 "Given tizenId not found for update installation (Same GUID?)");
93 ThrowMsg(Exceptions::DatabaseFailure,
94 "Given tizenId not found for update installation");
97 WidgetDAO::updateTizenAppId(m_context.widgetConfig.tzAppid,
99 WidgetDAO::registerWidget(m_context.widgetConfig.tzAppid,
100 m_context.widgetConfig,
101 m_context.widgetSecurity);
103 WidgetDAOReadOnly::getHandle(m_context.widgetConfig.tzAppid);
104 } else { //new installation
105 LogDebug("Registering widget...");
106 WidgetDAO::registerWidget(
107 m_context.widgetConfig.tzAppid,
108 m_context.widgetConfig,
109 m_context.widgetSecurity);
110 m_handle = WidgetDAOReadOnly::getHandle(
111 m_context.widgetConfig.tzAppid);
114 FOREACH(cap, m_context.staticPermittedDevCaps) {
116 "staticPermittedDevCaps : " << cap->first
117 << " smack status: " <<
121 LogDebug("Widget registered");
123 Catch(WidgetDAO::Exception::DatabaseError)
125 LogError("Database failure!");
126 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
128 Catch(DPL::DB::SqlConnection::Exception::Base)
130 LogError("Database failure!");
131 ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
135 void TaskDatabase::StepAceDBInsert()
137 LogDebug("Inserting Ace database entry. New handle: " << m_handle);
138 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
139 LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
140 if (ACE_OK != ace_unregister_widget(
141 static_cast<ace_widget_handle_t>(m_handleToRemove)))
144 "Error while removing ace entry for previous insallation");
148 if (!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
149 m_context.widgetSecurity.getCertificateList()))
151 LogError("ace database insert failed");
152 ThrowMsg(Exceptions::UpdateFailed,
153 "Update failure. ace_register_widget failed");
155 LogDebug("Ace data inserted");
158 void TaskDatabase::StepSecurityOriginDBInsert()
160 LogDebug("Create Security origin database");
161 // automatically create security origin database
162 using namespace SecurityOriginDB;
163 using namespace WrtDB;
165 SecurityOriginDAO dao(m_context.locations->getPkgId());
167 // Checking privilege list for setting security origin exception data
168 FOREACH(it, m_context.widgetConfig.configInfo.privilegeList) {
169 std::map<std::string, Feature>::const_iterator result =
170 g_W3CPrivilegeTextMap.find(DPL::ToUTF8String(it->name));
171 if (result != g_W3CPrivilegeTextMap.end()) {
172 if (result->second == FEATURE_USER_MEDIA) {
173 dao.setPrivilegeSecurityOriginData(result->second, false);
174 } else if (result->second == FEATURE_FULLSCREEN_MODE) {
177 dao.setPrivilegeSecurityOriginData(result->second);
183 void TaskDatabase::StepRegisterExternalFiles()
185 WrtDB::ExternalLocationList externalLocationsUpdate =
186 m_context.locations->listExternalLocations();
187 if (m_context.isUpdateMode) { //update
190 WidgetDAO dao(m_context.widgetConfig.tzAppid);
191 WrtDB::ExternalLocationList externalLocationsDB =
192 dao.getWidgetExternalLocations();
193 FOREACH(file, externalLocationsDB)
195 if (std::find(externalLocationsUpdate.begin(),
196 externalLocationsUpdate.end(),
197 *file) == externalLocationsUpdate.end())
199 m_externalLocationsToRemove.push_back(*file);
203 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
206 "Given tizenId not found for update installation (Same GUID?)");
207 ThrowMsg(Exceptions::UpdateFailed,
208 "Given tizenId not found for update installation");
211 LogDebug("Registering external files:");
212 FOREACH(file, externalLocationsUpdate)
214 LogDebug(" -> " << *file);
217 //set external locations to be registered
218 m_context.widgetConfig.externalLocations = externalLocationsUpdate;
221 void TaskDatabase::StepRemoveExternalFiles()
223 if (!m_externalLocationsToRemove.empty()) {
224 LogDebug("Removing external files:");
227 FOREACH(file, m_externalLocationsToRemove)
229 if (WrtUtilFileExists(*file)) {
230 LogDebug(" -> " << *file);
231 if (-1 == TEMP_FAILURE_RETRY(remove(file->c_str()))) {
232 ThrowMsg(Exceptions::RemovingFileFailure,
233 "Failed to remove external file");
235 } else if (WrtUtilDirExists(*file)) {
236 LogDebug(" -> " << *file);
237 if (!WrtUtilRemove(*file)) {
238 ThrowMsg(Exceptions::RemovingFolderFailure,
239 "Failed to remove external directory");
242 LogWarning(" -> " << *file << "(no such a path)");
247 void TaskDatabase::StepAbortDBInsert()
249 LogWarning("[DB Update Task] Aborting... (DB Clean)");
252 if (m_context.isUpdateMode) {
253 WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
254 WidgetDAO::updateTizenAppId(m_backAppId,
255 m_context.widgetConfig.tzAppid);
257 WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
259 LogDebug("Cleaning DB successful!");
261 Catch(DPL::DB::SqlConnection::Exception::Base)
263 LogError("Failed to handle StepAbortDBClean!");
267 void TaskDatabase::StepAbortAceDBInsert()
269 LogWarning("[DB Update Task] ACE DB Aborting... (DB Clean)");
271 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
272 // Remove also old one. If it was already updated nothing wrong will happen,
273 // but if not old widget will be removed.
274 if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
275 ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
278 if (!AceApi::registerAceWidgetFromDB(m_handleToRemove))
280 LogError("ace database restore failed");
282 LogDebug("Ace data inserted");
285 void TaskDatabase::StepLiveboxDBInsert()
287 if (m_context.widgetConfig.configInfo.m_livebox.size() <= 0) {
291 std::string tizenId = DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
293 // insert specific information to web livebox db
294 for (auto it = m_context.widgetConfig.configInfo.m_livebox.begin();
295 it != m_context.widgetConfig.configInfo.m_livebox.end(); ++it)
297 std::string boxId = DPL::ToUTF8String((**it).m_liveboxId);
299 if ((**it).m_type.empty()) {
300 boxType = web_provider_livebox_get_default_type();
302 boxType = DPL::ToUTF8String((**it).m_type);
304 LogDebug("livebox id: " << boxId);
305 LogDebug("livebox type: " << boxType);
307 int autoLaunch = (**it).m_autoLaunch == L"true" ? 1 : 0;
308 LogDebug("livebox auto-launch: " << autoLaunch);
310 int mouseEvent = (**it).m_boxInfo.m_boxMouseEvent == L"true" ? 1 : 0;
311 LogDebug("livebox mouse-event: " << mouseEvent);
313 int pdFastOpen = (**it).m_boxInfo.m_pdFastOpen == L"true" ? 1 : 0;
314 LogDebug("livebox pd fast-open: " << pdFastOpen);
316 web_provider_livebox_insert_box_info(
317 boxId.c_str(), tizenId.c_str(), boxType.c_str(),
318 autoLaunch, mouseEvent, pdFastOpen);
322 void TaskDatabase::StartStep()
324 LogDebug("--------- <TaskDatabase> : START ----------");
327 void TaskDatabase::EndStep()
329 LogDebug("--------- <TaskDatabase> : END ----------");
331 } //namespace WidgetInstall