[Release] wrt-installer_0.1.114
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_database.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    task_new_db_insert.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @author  Soyoung kim(sy037.kim@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for installer task database updating for widget
22  * update
23  */
24 #include <unistd.h>
25 #include <time.h>
26 #include <sys/stat.h>
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>
39 #include <string>
40 #include <sstream>
41 #include <ace_api_install.h>
42 #include <ace_registration.h>
43 #include <errno.h>
44 #include <string.h>
45 #include <map>
46
47 using namespace WrtDB;
48
49 namespace Jobs {
50 namespace WidgetInstall {
51 TaskDatabase::TaskDatabase(InstallerContext& context) :
52     DPL::TaskDecl<TaskDatabase>(this),
53     m_context(context),
54     m_handleToRemove(INVALID_WIDGET_HANDLE),
55     m_handle(INVALID_WIDGET_HANDLE)
56 {
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);
65
66     AddAbortStep(&TaskDatabase::StepAbortDBInsert);
67     AddAbortStep(&TaskDatabase::StepAbortAceDBInsert);
68 }
69
70 void TaskDatabase::StepWrtDBInsert()
71 {
72     Try
73     {
74         /* Set install Time */
75         time(&m_context.widgetConfig.installedTime);
76
77         if (m_context.isUpdateMode) { //update
78             LogDebug("Registering widget... (update)");
79             Try
80             {
81                 m_handleToRemove = WidgetDAOReadOnly::getHandle(
82                         m_context.widgetConfig.tzAppid);
83
84                 std::string makeAppid =
85                     DPL::ToUTF8String(m_context.widgetConfig.tzAppid) + "." +
86                     "backup";
87                 m_backAppId = DPL::FromUTF8String(makeAppid);
88             }
89             Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
90             {
91                 LogError(
92                     "Given tizenId not found for update installation (Same GUID?)");
93                 ThrowMsg(Exceptions::DatabaseFailure,
94                          "Given tizenId not found for update installation");
95             }
96
97             WidgetDAO::updateTizenAppId(m_context.widgetConfig.tzAppid,
98                                         m_backAppId);
99             WidgetDAO::registerWidget(m_context.widgetConfig.tzAppid,
100                                       m_context.widgetConfig,
101                                       m_context.widgetSecurity);
102             m_handle =
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);
112         }
113
114         FOREACH(cap, m_context.staticPermittedDevCaps) {
115             LogDebug(
116                 "staticPermittedDevCaps : " << cap->first
117                                             << " smack status: " <<
118                 cap->second);
119         }
120
121         LogDebug("Widget registered");
122     }
123     Catch(WidgetDAO::Exception::DatabaseError)
124     {
125         LogError("Database failure!");
126         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
127     }
128     Catch(DPL::DB::SqlConnection::Exception::Base)
129     {
130         LogError("Database failure!");
131         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
132     }
133 }
134
135 void TaskDatabase::StepAceDBInsert()
136 {
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)))
142         {
143             LogWarning(
144                 "Error while removing ace entry for previous insallation");
145         }
146     }
147
148     if (!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
149                                    m_context.widgetSecurity.getCertificateList()))
150     {
151         LogError("ace database insert failed");
152         ThrowMsg(Exceptions::UpdateFailed,
153                  "Update failure. ace_register_widget failed");
154     }
155     LogDebug("Ace data inserted");
156 }
157
158 void TaskDatabase::StepSecurityOriginDBInsert()
159 {
160     LogDebug("Create Security origin database");
161     // automatically create security origin database
162     using namespace SecurityOriginDB;
163     using namespace WrtDB;
164
165     SecurityOriginDAO dao(m_context.locations->getPkgId());
166
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) {
175                 continue;
176             } else {
177                 dao.setPrivilegeSecurityOriginData(result->second);
178             }
179         }
180     }
181 }
182
183 void TaskDatabase::StepRegisterExternalFiles()
184 {
185     WrtDB::ExternalLocationList externalLocationsUpdate =
186         m_context.locations->listExternalLocations();
187     if (m_context.isUpdateMode) { //update
188         Try
189         {
190             WidgetDAO dao(m_context.widgetConfig.tzAppid);
191             WrtDB::ExternalLocationList externalLocationsDB =
192                 dao.getWidgetExternalLocations();
193             FOREACH(file, externalLocationsDB)
194             {
195                 if (std::find(externalLocationsUpdate.begin(),
196                               externalLocationsUpdate.end(),
197                               *file) == externalLocationsUpdate.end())
198                 {
199                     m_externalLocationsToRemove.push_back(*file);
200                 }
201             }
202         }
203         Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
204         {
205             LogError(
206                 "Given tizenId not found for update installation (Same GUID?)");
207             ThrowMsg(Exceptions::UpdateFailed,
208                      "Given tizenId not found for update installation");
209         }
210     }
211     LogDebug("Registering external files:");
212     FOREACH(file, externalLocationsUpdate)
213     {
214         LogDebug("  -> " << *file);
215     }
216
217     //set external locations to be registered
218     m_context.widgetConfig.externalLocations = externalLocationsUpdate;
219 }
220
221 void TaskDatabase::StepRemoveExternalFiles()
222 {
223     if (!m_externalLocationsToRemove.empty()) {
224         LogDebug("Removing external files:");
225     }
226
227     FOREACH(file, m_externalLocationsToRemove)
228     {
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");
234             }
235         } else if (WrtUtilDirExists(*file)) {
236             LogDebug("  -> " << *file);
237             if (!WrtUtilRemove(*file)) {
238                 ThrowMsg(Exceptions::RemovingFolderFailure,
239                          "Failed to remove external directory");
240             }
241         } else {
242             LogWarning("  -> " << *file << "(no such a path)");
243         }
244     }
245 }
246
247 void TaskDatabase::StepAbortDBInsert()
248 {
249     LogWarning("[DB Update Task] Aborting... (DB Clean)");
250     Try
251     {
252         if (m_context.isUpdateMode) {
253             WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
254             WidgetDAO::updateTizenAppId(m_backAppId,
255                                         m_context.widgetConfig.tzAppid);
256         } else {
257             WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
258         }
259         LogDebug("Cleaning DB successful!");
260     }
261     Catch(DPL::DB::SqlConnection::Exception::Base)
262     {
263         LogError("Failed to handle StepAbortDBClean!");
264     }
265 }
266
267 void TaskDatabase::StepAbortAceDBInsert()
268 {
269     LogWarning("[DB Update Task] ACE DB Aborting... (DB Clean)");
270
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));
276     }
277
278     if (!AceApi::registerAceWidgetFromDB(m_handleToRemove))
279     {
280         LogError("ace database restore failed");
281     }
282     LogDebug("Ace data inserted");
283 }
284
285 void TaskDatabase::StepLiveboxDBInsert()
286 {
287     if (m_context.widgetConfig.configInfo.m_livebox.size() <= 0) {
288         return;
289     }
290
291     std::string tizenId = DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
292
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)
296     {
297         std::string boxId = DPL::ToUTF8String((**it).m_liveboxId);
298         std::string boxType;
299         if ((**it).m_type.empty()) {
300             boxType = web_provider_livebox_get_default_type();
301         } else {
302             boxType = DPL::ToUTF8String((**it).m_type);
303         }
304         LogDebug("livebox id: " << boxId);
305         LogDebug("livebox type: " << boxType);
306
307         int autoLaunch = (**it).m_autoLaunch == L"true" ? 1 : 0;
308         LogDebug("livebox auto-launch: " << autoLaunch);
309
310         int mouseEvent = (**it).m_boxInfo.m_boxMouseEvent == L"true" ? 1 : 0;
311         LogDebug("livebox mouse-event: " << mouseEvent);
312
313         int pdFastOpen = (**it).m_boxInfo.m_pdFastOpen == L"true" ? 1 : 0;
314         LogDebug("livebox pd fast-open: " << pdFastOpen);
315
316         web_provider_livebox_insert_box_info(
317                 boxId.c_str(), tizenId.c_str(), boxType.c_str(),
318                 autoLaunch, mouseEvent, pdFastOpen);
319     }
320 }
321
322 void TaskDatabase::StartStep()
323 {
324     LogDebug("--------- <TaskDatabase> : START ----------");
325 }
326
327 void TaskDatabase::EndStep()
328 {
329     LogDebug("--------- <TaskDatabase> : END ----------");
330 }
331 } //namespace WidgetInstall
332 } //namespace Jobs