[Release] wrt-installer_0.1.47
[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/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>
39 #include <dpl/wrt-dao-ro/widget_dao_types.h>
40 #include <string>
41 #include <sstream>
42 #include <ace_api_install.h>
43 #include <ace_registration.h>
44 #include <errno.h>
45 #include <string.h>
46 #include <vconf.h>
47 #include <map>
48
49 using namespace WrtDB;
50
51 namespace Jobs {
52 namespace WidgetInstall {
53 TaskDatabase::TaskDatabase(InstallerContext& context) :
54     DPL::TaskDecl<TaskDatabase>(this),
55     m_context(context),
56     m_handleToRemove(INVALID_WIDGET_HANDLE),
57     m_handle(INVALID_WIDGET_HANDLE)
58 {
59     AddStep(&TaskDatabase::StepRegisterExternalFiles);
60     AddStep(&TaskDatabase::StepWrtDBInsert);
61     AddStep(&TaskDatabase::StepAceDBInsert);
62     AddStep(&TaskDatabase::StepSecurityOriginDBInsert);
63     AddStep(&TaskDatabase::StepRemoveExternalFiles);
64     AddStep(&TaskDatabase::StepCreateVconf);
65     AddStep(&TaskDatabase::StepLiveboxDBInsert);
66
67     AddAbortStep(&TaskDatabase::StepAbortDBInsert);
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             LogInfo("Registering widget... (update)");
79             Try
80             {
81                 m_handleToRemove = WidgetDAOReadOnly::getHandle(
82                         m_context.widgetConfig.tzAppid);
83             }
84             Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
85             {
86                 LogError(
87                     "Given tizenId not found for update installation (Same GUID?)");
88                 ThrowMsg(Exceptions::DatabaseFailure,
89                          "Given tizenId not found for update installation");
90             }
91             WidgetDAO::registerOrUpdateWidget(
92                 m_context.widgetConfig.tzAppid,
93                 m_context.widgetConfig,
94                 m_context.wacSecurity);
95             m_handle = WidgetDAOReadOnly::getHandle(
96                     m_context.widgetConfig.tzAppid);
97         } else { //new installation
98             LogInfo("Registering widget...");
99             WidgetDAO::registerWidget(
100                 m_context.widgetConfig.tzAppid,
101                 m_context.widgetConfig,
102                 m_context.wacSecurity);
103             m_handle = WidgetDAOReadOnly::getHandle(
104                     m_context.widgetConfig.tzAppid);
105         }
106
107         FOREACH(cap, m_context.staticPermittedDevCaps) {
108             LogInfo(
109                 "staticPermittedDevCaps : " << cap->first
110                                             << " smack status: " <<
111                 cap->second);
112         }
113
114         LogInfo("Widget registered");
115     }
116     Catch(WidgetDAO::Exception::DatabaseError)
117     {
118         LogError("Database failure!");
119         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
120     }
121     Catch(DPL::DB::SqlConnection::Exception::Base)
122     {
123         LogError("Database failure!");
124         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
125     }
126 }
127
128 void TaskDatabase::StepAceDBInsert()
129 {
130     LogDebug("Inserting Ace database entry. New handle: " << m_handle);
131     if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
132         LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
133         if (ACE_OK != ace_unregister_widget(
134                 static_cast<ace_widget_handle_t>(m_handleToRemove)))
135         {
136             LogWarning(
137                 "Error while removing ace entry for previous insallation");
138         }
139     }
140
141     if (!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
142                                    m_context.wacSecurity.getCertificateList()))
143     {
144         LogError("ace database insert failed");
145         ThrowMsg(Exceptions::UpdateFailed,
146                  "Update failure. ace_register_widget failed");
147     }
148     LogDebug("Ace data inserted");
149 }
150
151 void TaskDatabase::StepSecurityOriginDBInsert()
152 {
153     LogDebug("Create Security origin database");
154     // automatically create security origin database
155     using namespace SecurityOriginDB;
156     using namespace WrtDB;
157
158     SecurityOriginDAO dao(m_context.locations->getPkgId());
159
160     // Checking privilege list for setting security origin exception data
161     FOREACH(it, m_context.widgetConfig.configInfo.privilegeList) {
162         std::map<std::string, Feature>::const_iterator result =
163             g_W3CPrivilegeTextMap.find(DPL::ToUTF8String(it->name));
164         if (result != g_W3CPrivilegeTextMap.end()) {
165             if (result->second == FEATURE_USER_MEDIA) {
166                 dao.setPrivilegeSecurityOriginData(result->second, false);
167             } else if (result->second == FEATURE_FULLSCREEN_MODE) {
168                 continue;
169             } else {
170                 dao.setPrivilegeSecurityOriginData(result->second);
171             }
172         }
173     }
174
175     m_context.job->UpdateProgress(
176         InstallerContext::INSTALL_NEW_DB_INSERT,
177         "New Widget DB UPDATE Finished");
178 }
179
180 void TaskDatabase::StepRegisterExternalFiles()
181 {
182     WrtDB::ExternalLocationList externalLocationsUpdate =
183         m_context.locations->listExternalLocations();
184     if (m_context.isUpdateMode) { //update
185         Try
186         {
187             WidgetDAO dao(m_context.widgetConfig.tzAppid);
188             WrtDB::ExternalLocationList externalLocationsDB =
189                 dao.getWidgetExternalLocations();
190             FOREACH(file, externalLocationsDB)
191             {
192                 if (std::find(externalLocationsUpdate.begin(),
193                               externalLocationsUpdate.end(),
194                               *file) == externalLocationsUpdate.end())
195                 {
196                     m_externalLocationsToRemove.push_back(*file);
197                 }
198             }
199         }
200         Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
201         {
202             LogError(
203                 "Given tizenId not found for update installation (Same GUID?)");
204             ThrowMsg(Exceptions::UpdateFailed,
205                      "Given tizenId not found for update installation");
206         }
207     }
208     LogDebug("Registering external files:");
209     FOREACH(file, externalLocationsUpdate)
210     {
211         LogDebug("  -> " << *file);
212     }
213
214     //set external locations to be registered
215     m_context.widgetConfig.externalLocations = externalLocationsUpdate;
216 }
217
218 void TaskDatabase::StepRemoveExternalFiles()
219 {
220     if (!m_externalLocationsToRemove.empty()) {
221         LogDebug("Removing external files:");
222     }
223
224     FOREACH(file, m_externalLocationsToRemove)
225     {
226         if (WrtUtilFileExists(*file)) {
227             LogDebug("  -> " << *file);
228             if (-1 == TEMP_FAILURE_RETRY(remove(file->c_str()))) {
229                 ThrowMsg(Exceptions::RemovingFileFailure,
230                          "Failed to remove external file");
231             }
232         } else if (WrtUtilDirExists(*file)) {
233             LogDebug("  -> " << *file);
234             if (!WrtUtilRemove(*file)) {
235                 ThrowMsg(Exceptions::RemovingFolderFailure,
236                          "Failed to remove external directory");
237             }
238         } else {
239             LogWarning("  -> " << *file << "(no such a path)");
240         }
241     }
242 }
243
244 void TaskDatabase::StepCreateVconf()
245 {
246     LogDebug("StepCreateVconf");
247     std::map<std::string, WrtDB::SettingsType> vconfData;
248     vconfData[
249         WrtDB::VconfConfig::GetVconfKeyPopupUsage(
250             m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
251     vconfData[
252         WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
253             m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
254     vconfData[
255         WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
256             m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_ON;
257     vconfData[
258         WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
259             m_context.widgetConfig.tzAppid)] = WrtDB::SETTINGS_TYPE_OFF;
260
261     // vconftool -g 5000 set -t int <path> initialize value
262     // Current installer should use vconftool for setting group ID
263     // In case of install application by pkgcmd, permission for others
264     // set to read-only
265     FOREACH(it, vconfData) {
266         std::ostringstream command;
267         command << "vconftool -g 5000 set -t int ";
268         command << (*it).first;
269         command << " \"" << static_cast<int>((*it).second) << "\"";
270         int ret = system(command.str().c_str());
271         if (-1 == ret) {
272             ThrowMsg(Exceptions::CreateVconfFailure,
273                      "Failed to create vconf files");
274         }
275     }
276 }
277
278 void TaskDatabase::StepAbortDBInsert()
279 {
280     LogWarning("[DB Update Task] Aborting... (DB Clean)");
281     Try
282     {
283         WidgetDAO::unregisterWidget(m_context.widgetConfig.tzAppid);
284         LogDebug("Cleaning DB successful!");
285     }
286     Catch(DPL::DB::SqlConnection::Exception::Base)
287     {
288         LogError("Failed to handle StepAbortDBClean!");
289     }
290
291     ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
292     // Remove also old one. If it was already updated nothing wrong will happen,
293     // but if not old widget will be removed.
294     if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
295         ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
296     }
297 }
298
299 void TaskDatabase::StepLiveboxDBInsert()
300 {
301     if (m_context.widgetConfig.configInfo.m_livebox.size() <= 0) {
302         return;
303     }
304
305     std::string tizenId = DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
306
307     // insert specific information to web livebox db
308     for (auto it = m_context.widgetConfig.configInfo.m_livebox.begin();
309          it != m_context.widgetConfig.configInfo.m_livebox.end(); ++it)
310     {
311         std::string boxId = DPL::ToUTF8String((**it).m_liveboxId);
312         std::string boxType;
313         if ((**it).m_type.empty()) {
314             boxType = web_provider_livebox_get_default_type();
315         } else {
316             boxType = DPL::ToUTF8String((**it).m_type);
317         }
318         LogInfo("livebox id: " << boxId);
319         LogInfo("livebox type: " << boxType);
320
321         int autoLaunch = (**it).m_autoLaunch == L"true" ? 1 : 0;
322         LogInfo("livebox auto-launch: " << autoLaunch);
323
324         int mouseEvent = (**it).m_boxInfo.m_boxMouseEvent == L"true" ? 1 : 0;
325         LogInfo("livebox mouse-event: " << mouseEvent);
326
327         int pdFastOpen = (**it).m_boxInfo.m_pdFastOpen == L"true" ? 1 : 0;
328         LogInfo("livebox pd fast-open: " << pdFastOpen);
329
330         web_provider_livebox_insert_box_info(
331                 boxId.c_str(), tizenId.c_str(), boxType.c_str(),
332                 autoLaunch, mouseEvent, pdFastOpen);
333     }
334 }
335 } //namespace WidgetInstall
336 } //namespace Jobs