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