Fixing issues found by prevent
[framework/web/wrt-installer.git] / src / wrt-installer / wrt_installer_api.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        wrt_installer_api.cpp
18  * @author      Chung Jihoon (jihoon.chung@samsung.com)
19  * @version     1.0
20  * @brief       This file contains definitions of wrt installer api
21  */
22 #include <stdlib.h>
23 #include <list>
24 #include <string>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <dpl/exception.h>
29 #include <dpl/log/log.h>
30 #include <dpl/assert.h>
31 #include <dpl/semaphore.h>
32 #include <dpl/sstream.h>
33 #include <dpl/errno_string.h>
34 #include <libxml/parser.h>
35 #include <vconf.h>
36
37 #include <wrt_installer_api.h>
38 #include <installer_callbacks_translate.h>
39 #include <installer_controller.h>
40 #include <language_subtag_rst_tree.h>
41 #include <dpl/wrt-dao-ro/global_config.h>
42 #include <dpl/utils/widget_version.h>
43 #include <wrt_type.h>
44 #include <dpl/localization/w3c_file_localization.h>
45 #include <dpl/wrt-dao-ro/WrtDatabase.h>
46 #include <vcore/VCore.h>
47 #include <installer_main_thread.h>
48
49 using namespace WrtDB;
50
51 #ifdef __cplusplus
52
53 #define EXPORT_API __attribute__((visibility("default")))
54 extern "C"
55 {
56 #endif
57 inline InstallMode::Type translateInstallMode(
58     WrtInstallMode installMode)
59 {
60     if (WRT_INSTALL_MODE_INSTALL_WGT == installMode) {
61         return InstallMode::INSTALL_MODE_WGT;
62     } else if (WRT_INSTALL_MODE_INSTALL_DIRECTORY == installMode) {
63         return InstallMode::INSTALL_MODE_DIRECTORY;
64     } else if (WRT_INSTALL_MODE_INSTALL_PRELOAD == installMode) {
65         return InstallMode::INSTALL_MODE_PRELOAD;
66     } else if (WRT_INSTALL_MODE_REINSTALL == installMode) {
67         return InstallMode::REINSTALL_MODE_DIRECTORY;
68     }
69     Assert(true && "wrong argument is inputed");
70     return InstallMode::WRONG_INSTALL_MODE;
71 }
72
73 static std::string cutOffFileName(const std::string& path)
74 {
75     size_t found = path.find_last_of("/");
76     if (found == std::string::npos) {
77         return path;
78     } else {
79         return path.substr(0, found);
80     }
81 }
82
83 static bool checkPath(const std::string& path)
84 {
85     struct stat st;
86     if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
87         return true;
88     }
89     LogError("Cannot access directory [ " << path << " ]");
90     return false;
91 }
92
93 static bool checkPaths()
94 {
95     bool if_ok = true;
96
97     if_ok &= (checkPath(cutOffFileName(GlobalConfig::GetWrtDatabaseFilePath())));
98     if_ok &= (checkPath(GlobalConfig::GetDevicePluginPath()));
99     if_ok &= (checkPath(GlobalConfig::GetUserInstalledWidgetPath()));
100     if_ok &= (checkPath(GlobalConfig::GetUserPreloadedWidgetPath()));
101
102     return if_ok;
103 }
104
105 EXPORT_API void wrt_installer_init(void *userdata,
106                                   WrtInstallerInitCallback callback)
107 {
108     // Set DPL/LOG MID
109     DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
110
111     try {
112         LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
113         LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
114
115         // Touch InstallerController Singleton
116         InstallerMainThreadSingleton::Instance().TouchArchitecture();
117
118         // Check paths
119         if (!checkPaths()) {
120             if (callback) {
121                 callback(WRT_INSTALLER_ERROR_FATAL_ERROR, userdata);
122             }
123             return;
124         }
125
126         // Initialize ValidationCore - this must be done before AttachDatabases
127         ValidationCore::VCoreInit(
128             std::string(GlobalConfig::GetFingerprintListFile()),
129             std::string(GlobalConfig::GetFingerprintListSchema()),
130             std::string(GlobalConfig::GetVCoreDatabaseFilePath()));
131
132         InstallerMainThreadSingleton::Instance().AttachDatabases();
133
134         LogInfo("Prepare libxml2 to work in multithreaded program.");
135         xmlInitParser();
136
137         // Initialize Language Subtag registry
138         LanguageSubtagRstTreeSingleton::Instance().Initialize();
139
140         // Installer init
141         CONTROLLER_POST_SYNC_EVENT(
142             Logic::InstallerController,
143             InstallerControllerEvents::
144                 InitializeEvent());
145
146         // Install deferred widget packages
147         CONTROLLER_POST_EVENT(
148             Logic::InstallerController,
149             InstallerControllerEvents::
150                 InstallDeferredWidgetPackagesEvent());
151
152         if (callback) {
153             LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
154             callback(WRT_SUCCESS, userdata);
155         }
156     } catch (const DPL::Exception& ex) {
157         LogError("Internal Error during Init:");
158         DPL::Exception::DisplayKnownException(ex);
159         if (callback) {
160             callback(WRT_INSTALLER_ERROR_FATAL_ERROR, userdata);
161             return;
162         }
163     }
164     return;
165 }
166
167 EXPORT_API void wrt_installer_shutdown()
168 {
169     try {
170         LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
171
172         // Installer termination
173         CONTROLLER_POST_SYNC_EVENT(
174             Logic::InstallerController,
175             InstallerControllerEvents::
176                 TerminateEvent());
177
178         InstallerMainThreadSingleton::Instance().DetachDatabases();
179
180         // This must be done after DetachDatabase
181         ValidationCore::VCoreDeinit();
182
183         // Global deinit check
184         LogInfo("Cleanup libxml2 global values.");
185         xmlCleanupParser();
186     } catch (const DPL::Exception& ex) {
187         LogError("Internal Error during Shutdown:");
188         DPL::Exception::DisplayKnownException(ex);
189     }
190 }
191
192 EXPORT_API void wrt_install_widget(
193     const char *path,
194     void* userdata,
195     WrtInstallerStatusCallback status_cb,
196     WrtProgressCallback progress_cb,
197     WrtInstallMode installMode,
198     bool quiet,
199     std::shared_ptr<PackageManager::
200                         IPkgmgrSignal> pkgmgrInterface
201     )
202 {
203     UNHANDLED_EXCEPTION_HANDLER_BEGIN
204     {
205         LogInfo("[WRT-API] INSTALL WIDGET: " << path);
206         // Post installation event
207         CONTROLLER_POST_EVENT(
208             Logic::InstallerController,
209             InstallerControllerEvents::InstallWidgetEvent(
210                 path, WidgetInstallationStruct(
211                     InstallerCallbacksTranslate::installFinishedCallback,
212                     InstallerCallbacksTranslate::installProgressCallback,
213                     new InstallerCallbacksTranslate::StatusCallbackStruct(
214                         userdata, status_cb, progress_cb),
215                     translateInstallMode(installMode),
216                     quiet,
217                     pkgmgrInterface)));
218     }
219     UNHANDLED_EXCEPTION_HANDLER_END
220 }
221
222 EXPORT_API void wrt_uninstall_widget(
223     const char * const tzAppid,
224     void* userdata,
225     WrtInstallerStatusCallback status_cb,
226     WrtProgressCallback progress_cb,
227     std::shared_ptr<PackageManager::
228                         IPkgmgrSignal> pkgmgrSignalInterface)
229 {
230     UNHANDLED_EXCEPTION_HANDLER_BEGIN
231     {
232         std::string tizenAppid(tzAppid);
233         LogInfo("[WRT-API] UNINSTALL WIDGET: " << tizenAppid);
234         // Post uninstallation event
235         CONTROLLER_POST_EVENT(
236             Logic::InstallerController,
237             InstallerControllerEvents::UninstallWidgetEvent(
238                 tizenAppid,
239                 WidgetUninstallationStruct(
240                     InstallerCallbacksTranslate::uninstallFinishedCallback,
241                     InstallerCallbacksTranslate::installProgressCallback,
242                     new InstallerCallbacksTranslate::StatusCallbackStruct(
243                         userdata, status_cb, progress_cb),
244                     pkgmgrSignalInterface
245                     )
246                 )
247             );
248     }
249     UNHANDLED_EXCEPTION_HANDLER_END
250 }
251
252 EXPORT_API void wrt_install_plugin(
253     const char *pluginDir,
254     void *user_param,
255     WrtPluginInstallerStatusCallback status_cb,
256     WrtProgressCallback progress_cb)
257 {
258     UNHANDLED_EXCEPTION_HANDLER_BEGIN
259     {
260         LogInfo("[WRT-API] INSTALL PLUGIN: " << pluginDir);
261         //Private data for status callback
262         //Resource is free in pluginInstallFinishedCallback
263         InstallerCallbacksTranslate::PluginStatusCallbackStruct*
264         callbackStruct =
265             new InstallerCallbacksTranslate::PluginStatusCallbackStruct(
266                 user_param, status_cb, progress_cb);
267
268         CONTROLLER_POST_EVENT(
269             Logic::InstallerController,
270             InstallerControllerEvents::InstallPluginEvent(
271                 std::string(pluginDir),
272                 PluginInstallerStruct(
273                     InstallerCallbacksTranslate::
274                         pluginInstallFinishedCallback,
275                     InstallerCallbacksTranslate::
276                         installProgressCallback, callbackStruct)));
277     }
278     UNHANDLED_EXCEPTION_HANDLER_END
279 }
280
281 #ifdef __cplusplus
282 }
283 #endif