Implement reinstall mode
[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 }
71
72 static std::string cutOffFileName(const std::string& path)
73 {
74     size_t found = path.find_last_of("/");
75     if (found == std::string::npos) {
76         return path;
77     } else {
78         return path.substr(0, found);
79     }
80 }
81
82 static bool checkPath(const std::string& path)
83 {
84     struct stat st;
85     if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
86         return true;
87     }
88     LogError("Cannot access directory [ " << path << " ]");
89     return false;
90 }
91
92 static bool checkPaths()
93 {
94     bool if_ok = true;
95
96     if_ok &= (checkPath(cutOffFileName(GlobalConfig::GetWrtDatabaseFilePath())));
97     if_ok &= (checkPath(GlobalConfig::GetDevicePluginPath()));
98     if_ok &= (checkPath(GlobalConfig::GetUserInstalledWidgetPath()));
99     if_ok &= (checkPath(GlobalConfig::GetUserPreloadedWidgetPath()));
100
101     return if_ok;
102 }
103
104 EXPORT_API void wrt_installer_init(void *userdata,
105                                   WrtInstallerInitCallback callback)
106 {
107     // Set DPL/LOG MID
108     DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
109
110     try {
111         LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
112         LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
113
114         // Touch InstallerController Singleton
115         InstallerMainThreadSingleton::Instance().TouchArchitecture();
116
117         // Check paths
118         if (!checkPaths()) {
119             if (callback) {
120                 callback(WRT_INSTALLER_ERROR_FATAL_ERROR, userdata);
121             }
122             return;
123         }
124
125         // Initialize ValidationCore - this must be done before AttachDatabases
126         ValidationCore::VCoreInit(
127             std::string(GlobalConfig::GetFingerprintListFile()),
128             std::string(GlobalConfig::GetFingerprintListSchema()),
129             std::string(GlobalConfig::GetVCoreDatabaseFilePath()));
130
131         InstallerMainThreadSingleton::Instance().AttachDatabases();
132
133         LogInfo("Prepare libxml2 to work in multithreaded program.");
134         xmlInitParser();
135
136         // Initialize Language Subtag registry
137         LanguageSubtagRstTreeSingleton::Instance().Initialize();
138
139         // Installer init
140         CONTROLLER_POST_SYNC_EVENT(
141             Logic::InstallerController,
142             InstallerControllerEvents::
143                 InitializeEvent());
144
145         // Install deferred widget packages
146         CONTROLLER_POST_EVENT(
147             Logic::InstallerController,
148             InstallerControllerEvents::
149                 InstallDeferredWidgetPackagesEvent());
150
151         if (callback) {
152             LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
153             callback(WRT_SUCCESS, userdata);
154         }
155     } catch (const DPL::Exception& ex) {
156         LogError("Internal Error during Init:");
157         DPL::Exception::DisplayKnownException(ex);
158         if (callback) {
159             callback(WRT_INSTALLER_ERROR_FATAL_ERROR, userdata);
160             return;
161         }
162     }
163     return;
164 }
165
166 EXPORT_API void wrt_installer_shutdown()
167 {
168     try {
169         LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
170
171         // Installer termination
172         CONTROLLER_POST_SYNC_EVENT(
173             Logic::InstallerController,
174             InstallerControllerEvents::
175                 TerminateEvent());
176
177         InstallerMainThreadSingleton::Instance().DetachDatabases();
178
179         // This must be done after DetachDatabase
180         ValidationCore::VCoreDeinit();
181
182         // Global deinit check
183         LogInfo("Cleanup libxml2 global values.");
184         xmlCleanupParser();
185     } catch (const DPL::Exception& ex) {
186         LogError("Internal Error during Shutdown:");
187         DPL::Exception::DisplayKnownException(ex);
188     }
189 }
190
191 EXPORT_API void wrt_install_widget(
192     const char *path,
193     void* userdata,
194     WrtInstallerStatusCallback status_cb,
195     WrtProgressCallback progress_cb,
196     WrtInstallMode installMode,
197     bool quiet,
198     std::shared_ptr<PackageManager::
199                         IPkgmgrSignal> pkgmgrInterface
200     )
201 {
202     UNHANDLED_EXCEPTION_HANDLER_BEGIN
203     {
204         LogInfo("[WRT-API] INSTALL WIDGET: " << path);
205         // Post installation event
206         CONTROLLER_POST_EVENT(
207             Logic::InstallerController,
208             InstallerControllerEvents::InstallWidgetEvent(
209                 path, WidgetInstallationStruct(
210                     InstallerCallbacksTranslate::installFinishedCallback,
211                     InstallerCallbacksTranslate::installProgressCallback,
212                     new InstallerCallbacksTranslate::StatusCallbackStruct(
213                         userdata, status_cb, progress_cb),
214                     translateInstallMode(installMode),
215                     quiet,
216                     pkgmgrInterface)));
217     }
218     UNHANDLED_EXCEPTION_HANDLER_END
219 }
220
221 EXPORT_API void wrt_uninstall_widget(
222     const char * const tzAppid,
223     void* userdata,
224     WrtInstallerStatusCallback status_cb,
225     WrtProgressCallback progress_cb,
226     std::shared_ptr<PackageManager::
227                         IPkgmgrSignal> pkgmgrSignalInterface)
228 {
229     UNHANDLED_EXCEPTION_HANDLER_BEGIN
230     {
231         std::string tizenAppid(tzAppid);
232         LogInfo("[WRT-API] UNINSTALL WIDGET: " << tizenAppid);
233         // Post uninstallation event
234         CONTROLLER_POST_EVENT(
235             Logic::InstallerController,
236             InstallerControllerEvents::UninstallWidgetEvent(
237                 tizenAppid,
238                 WidgetUninstallationStruct(
239                     InstallerCallbacksTranslate::uninstallFinishedCallback,
240                     InstallerCallbacksTranslate::installProgressCallback,
241                     new InstallerCallbacksTranslate::StatusCallbackStruct(
242                         userdata, status_cb, progress_cb),
243                     pkgmgrSignalInterface
244                     )
245                 )
246             );
247     }
248     UNHANDLED_EXCEPTION_HANDLER_END
249 }
250
251 EXPORT_API void wrt_install_plugin(
252     const char *pluginDir,
253     void *user_param,
254     WrtPluginInstallerStatusCallback status_cb,
255     WrtProgressCallback progress_cb)
256 {
257     UNHANDLED_EXCEPTION_HANDLER_BEGIN
258     {
259         LogInfo("[WRT-API] INSTALL PLUGIN: " << pluginDir);
260         //Private data for status callback
261         //Resource is free in pluginInstallFinishedCallback
262         InstallerCallbacksTranslate::PluginStatusCallbackStruct*
263         callbackStruct =
264             new InstallerCallbacksTranslate::PluginStatusCallbackStruct(
265                 user_param, status_cb, progress_cb);
266
267         CONTROLLER_POST_EVENT(
268             Logic::InstallerController,
269             InstallerControllerEvents::InstallPluginEvent(
270                 std::string(pluginDir),
271                 PluginInstallerStruct(
272                     InstallerCallbacksTranslate::
273                         pluginInstallFinishedCallback,
274                     InstallerCallbacksTranslate::
275                         installProgressCallback, callbackStruct)));
276     }
277     UNHANDLED_EXCEPTION_HANDLER_END
278 }
279
280 #ifdef __cplusplus
281 }
282 #endif