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