Popup removal 2
[platform/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     std::shared_ptr<PackageManager::
199                         IPkgmgrSignal> pkgmgrInterface
200     )
201 {
202     UNHANDLED_EXCEPTION_HANDLER_BEGIN
203     {
204         if ( WRT_INSTALL_MODE_INSTALL_PRELOAD == installMode) {
205             DPL::Log::OldStyleLogProvider *oldStyleProvider =
206                 new DPL::Log::OldStyleLogProvider(false, false, false, true,
207                         false, true);
208             DPL::Log::LogSystemSingleton::Instance().AddProvider(oldStyleProvider);
209         }
210
211         LogInfo("[WRT-API] INSTALL WIDGET: " << path);
212         // Post installation event
213         CONTROLLER_POST_EVENT(
214             Logic::InstallerController,
215             InstallerControllerEvents::InstallWidgetEvent(
216                 path, WidgetInstallationStruct(
217                     InstallerCallbacksTranslate::installFinishedCallback,
218                     InstallerCallbacksTranslate::installProgressCallback,
219                     new InstallerCallbacksTranslate::StatusCallbackStruct(
220                         userdata, status_cb, progress_cb),
221                     translateInstallMode(installMode),
222                     pkgmgrInterface)));
223     }
224     UNHANDLED_EXCEPTION_HANDLER_END
225 }
226
227 EXPORT_API void wrt_uninstall_widget(
228     const char * const tzAppid,
229     void* userdata,
230     WrtInstallerStatusCallback status_cb,
231     WrtProgressCallback progress_cb,
232     std::shared_ptr<PackageManager::
233                         IPkgmgrSignal> pkgmgrSignalInterface)
234 {
235     UNHANDLED_EXCEPTION_HANDLER_BEGIN
236     {
237         std::string tizenAppid(tzAppid);
238         LogInfo("[WRT-API] UNINSTALL WIDGET: " << tizenAppid);
239         // Post uninstallation event
240         CONTROLLER_POST_EVENT(
241             Logic::InstallerController,
242             InstallerControllerEvents::UninstallWidgetEvent(
243                 tizenAppid,
244                 WidgetUninstallationStruct(
245                     InstallerCallbacksTranslate::uninstallFinishedCallback,
246                     InstallerCallbacksTranslate::installProgressCallback,
247                     new InstallerCallbacksTranslate::StatusCallbackStruct(
248                         userdata, status_cb, progress_cb),
249                     pkgmgrSignalInterface
250                     )
251                 )
252             );
253     }
254     UNHANDLED_EXCEPTION_HANDLER_END
255 }
256
257 EXPORT_API void wrt_install_plugin(
258     const char *pluginDir,
259     void *user_param,
260     WrtPluginInstallerStatusCallback status_cb,
261     WrtProgressCallback progress_cb)
262 {
263     UNHANDLED_EXCEPTION_HANDLER_BEGIN
264     {
265         LogInfo("[WRT-API] INSTALL PLUGIN: " << pluginDir);
266         //Private data for status callback
267         //Resource is free in pluginInstallFinishedCallback
268         InstallerCallbacksTranslate::PluginStatusCallbackStruct*
269         callbackStruct =
270             new InstallerCallbacksTranslate::PluginStatusCallbackStruct(
271                 user_param, status_cb, progress_cb);
272
273         CONTROLLER_POST_EVENT(
274             Logic::InstallerController,
275             InstallerControllerEvents::InstallPluginEvent(
276                 std::string(pluginDir),
277                 PluginInstallerStruct(
278                     InstallerCallbacksTranslate::
279                         pluginInstallFinishedCallback,
280                     InstallerCallbacksTranslate::
281                         installProgressCallback, callbackStruct)));
282     }
283     UNHANDLED_EXCEPTION_HANDLER_END
284 }
285
286 #ifdef __cplusplus
287 }
288 #endif