696a9628cb37f33196feedf334e9d3d82c7e6040
[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 update
22  */
23 #include <time.h>
24 #include <sys/stat.h>
25 #include <widget_install/task_database.h>
26 #include <widget_install/job_widget_install.h>
27 #include <widget_install/widget_install_errors.h>
28 #include <widget_install/widget_install_context.h>
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30 #include <dpl/wrt-dao-ro/vconf_config.h>
31 #include <dpl/foreach.h>
32 #include <dpl/utils/wrt_utility.h>
33 #include <dpl/log/log.h>
34 #include <dpl/assert.h>
35 #include <string>
36 #include <sstream>
37 #include <ace_api_install.h>
38 #include <ace_registration.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <vconf.h>
42
43 using namespace WrtDB;
44
45 namespace Jobs {
46 namespace WidgetInstall {
47
48 TaskDatabase::TaskDatabase(InstallerContext& context) :
49     DPL::TaskDecl<TaskDatabase>(this),
50     m_context(context),
51     m_handleToRemove(INVALID_WIDGET_HANDLE),
52     m_handle(INVALID_WIDGET_HANDLE)
53 {
54     AddStep(&TaskDatabase::StepRegisterExternalFiles);
55     AddStep(&TaskDatabase::StepWrtDBInsert);
56     AddStep(&TaskDatabase::StepAceDBInsert);
57     AddStep(&TaskDatabase::StepRemoveExternalFiles);
58     AddStep(&TaskDatabase::StepCreateVconf);
59
60     AddAbortStep(&TaskDatabase::StepAbortDBInsert);
61 }
62
63 void TaskDatabase::StepWrtDBInsert()
64 {
65     Try
66     {
67         /* Set install Time */
68         time(&m_context.widgetConfig.installedTime);
69
70         if (m_context.existingWidgetInfo.isExist) //update
71         {
72             m_handleToRemove = WidgetDAOReadOnly::getHandle(
73                 m_context.locations->getPkgname());
74             LogInfo("Registering widget... (update)");
75             WidgetDAO::registerOrUpdateWidget(
76                     m_context.locations->getPkgname(),
77                     m_context.widgetConfig,
78                     m_context.wacSecurity);
79             m_handle = WidgetDAOReadOnly::getHandle(
80                 m_context.locations->getPkgname());
81         }
82         else //new installation
83         {
84             LogInfo("Registering widget...");
85             WidgetDAO::registerWidget(
86                     m_context.locations->getPkgname(),
87                     m_context.widgetConfig,
88                     m_context.wacSecurity);
89             m_handle = WidgetDAOReadOnly::getHandle(
90                 m_context.locations->getPkgname());
91         }
92
93         FOREACH (cap, m_context.staticPermittedDevCaps) {
94             LogInfo("staticPermittedDevCaps : " << cap->first
95                     << " smack status: " << cap->second);
96         }
97
98         LogInfo("Widget registered");
99     }
100     Catch(WidgetDAO::Exception::DatabaseError)
101     {
102         LogError("Database failure!");
103         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
104     }
105     Catch(DPL::DB::SqlConnection::Exception::Base)
106     {
107         LogError("Database failure!");
108         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
109     }
110 }
111
112 void TaskDatabase::StepAceDBInsert()
113 {
114     LogDebug("Inserting Ace database entry. New handle: " << m_handle);
115     if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
116         LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
117         if (ACE_OK != ace_unregister_widget(
118                 static_cast<ace_widget_handle_t>(m_handleToRemove)))
119         {
120             LogWarning("Error while removing ace entry for previous insallation");
121         };
122     }
123
124     if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
125                                   m_context.wacSecurity.getCertificateList()))
126     {
127         LogError("ace database insert failed");
128         ThrowMsg(Exceptions::NotAllowed, "Update failure. ace_register_widget failed");
129     }
130     LogDebug("Ace data inserted");
131
132     m_context.job->UpdateProgress(
133         InstallerContext::INSTALL_NEW_DB_INSERT,
134         "New Widget DB UPDATE Finished");
135 }
136
137 void TaskDatabase::StepRegisterExternalFiles()
138 {
139     WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
140     if (m_context.existingWidgetInfo.isExist) //update
141     {
142         WidgetDAO dao(m_context.locations->getPkgname());
143         WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
144         FOREACH(file, externalLocationsDB)
145         {
146             if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
147             {
148                 m_externalLocationsToRemove.push_back(*file);
149             }
150         }
151     }
152     LogDebug("Registering external files:");
153     FOREACH(file, externalLocationsUpdate)
154     {
155         LogDebug("  -> " << *file);
156     }
157
158     //set external locations to be registered
159     m_context.widgetConfig.externalLocations = externalLocationsUpdate;
160 }
161
162 void TaskDatabase::StepRemoveExternalFiles()
163 {
164     if(!m_externalLocationsToRemove.empty())
165     {
166         LogDebug("Removing external files:");
167     }
168
169     FOREACH(file, m_externalLocationsToRemove)
170     {
171         if(WrtUtilFileExists(*file))
172         {
173             LogDebug("  -> " << *file);
174             remove(file->c_str());
175         }
176         else if(WrtUtilDirExists(*file))
177         {
178             LogDebug("  -> " << *file);
179             if(!WrtUtilRemove(*file)){
180                 ThrowMsg(Exceptions::RemovingFolderFailure,
181                         "Failed to remove external directory");
182             }
183         }
184         else
185         {
186             LogWarning("  -> " << *file << "(no such a path)");
187         }
188     }
189 }
190
191 void TaskDatabase::StepCreateVconf()
192 {
193     LogDebug("StepCreateVconf");
194     std::string popupUsageKey =
195         WrtDB::VconfConfig::GetVconfKeyPopupUsage(
196             m_context.locations->getPkgname());
197     std::string geolocationUsageKey =
198         WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
199             m_context.locations->getPkgname());
200     std::string webNotificationUsageKey =
201         WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
202             m_context.locations->getPkgname());
203     std::string webDatabaseUsageKey =
204         WrtDB::VconfConfig::GetVconfKeyWebDatabaseUsage(
205             m_context.locations->getPkgname());
206     std::string filesystemUsageKey =
207         WrtDB::VconfConfig::GetVconfKeyFilesystemUsage(
208             m_context.locations->getPkgname());
209     std::string memorySavingModeKey =
210         WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
211             m_context.locations->getPkgname());
212
213     vconf_set_int(popupUsageKey.c_str(),
214                   static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
215     // prevent permission error
216     vconf_unset(popupUsageKey.c_str());
217     vconf_set_int(popupUsageKey.c_str(),
218                   static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
219     vconf_set_int(geolocationUsageKey.c_str(),
220                   static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
221     vconf_set_int(webNotificationUsageKey.c_str(),
222                   static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
223     vconf_set_int(webDatabaseUsageKey.c_str(),
224                   static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
225     vconf_set_int(filesystemUsageKey.c_str(),
226                   static_cast<int>(WrtDB::SETTINGS_TYPE_ON));
227     vconf_set_int(memorySavingModeKey.c_str(),
228                   static_cast<int>(WrtDB::SETTINGS_TYPE_OFF));
229 }
230
231 void TaskDatabase::StepAbortDBInsert()
232 {
233     LogWarning("[DB Update Task] Aborting... (DB Clean)");
234     Try
235     {
236         WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
237         LogDebug("Cleaning DB successful!");
238     }
239     Catch(DPL::DB::SqlConnection::Exception::Base)
240     {
241         LogError("Failed to handle StepAbortDBClean!");
242     }
243
244     ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
245     // Remove also old one. If it was already updated nothing wrong will happen,
246     // but if not old widget will be removed.
247     if (INVALID_WIDGET_HANDLE != m_handleToRemove)
248         ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
249 }
250
251 } //namespace WidgetInstall
252 } //namespace Jobs