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