Add send signal if there is not enough memory during web app installation
[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/assert.h>
30 #include <dpl/semaphore.h>
31 #include <dpl/sstream.h>
32 #include <dpl/errno_string.h>
33 #include <libxml/parser.h>
34
35 #include <wrt_installer_api.h>
36 #include <installer_callbacks_translate.h>
37 #include <installer_controller.h>
38 #include <language_subtag_rst_tree.h>
39 #include <dpl/wrt-dao-ro/global_config.h>
40 #include <dpl/utils/widget_version.h>
41 #include <wrt_type.h>
42 #include <dpl/localization/w3c_file_localization.h>
43 #include <dpl/wrt-dao-ro/WrtDatabase.h>
44 #include <vcore/VCore.h>
45 #include <installer_main_thread.h>
46 #include <wrt_install_mode.h>
47 #include <installer_log.h>
48
49 using namespace WrtDB;
50
51 static std::string cutOffFileName(const std::string& path)
52 {
53     size_t found = path.find_last_of("/");
54     if (found == std::string::npos) {
55         return path;
56     } else {
57         return path.substr(0, found);
58     }
59 }
60
61 static bool checkPath(const std::string& path)
62 {
63     struct stat st;
64     if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
65         return true;
66     }
67     _E("Cannot access directory [ %s ]", path.c_str());
68     return false;
69 }
70
71 static bool checkPaths()
72 {
73     bool if_ok = true;
74
75     if_ok &= (checkPath(cutOffFileName(GlobalConfig::GetWrtDatabaseFilePath())));
76     if_ok &= (checkPath(GlobalConfig::GetDevicePluginPath()));
77     if_ok &= (checkPath(GlobalConfig::GetUserInstalledWidgetPath()));
78     if_ok &= (checkPath(GlobalConfig::GetUserPreloadedWidgetPath()));
79
80     return if_ok;
81 }
82
83 void wrt_installer_init(void *userdata,
84                                   WrtInstallerInitCallback callback)
85 {
86     try {
87         _D("[WRT-API] INITIALIZING WRT INSTALLER...");
88         _D("[WRT-API] BUILD: %s", __TIMESTAMP__);
89
90         // Touch InstallerController Singleton
91         InstallerMainThreadSingleton::Instance().TouchArchitecture();
92
93         // Check paths
94         if (!checkPaths()) {
95             if (callback) {
96                 callback(WRT_INSTALLER_ERROR_FATAL_ERROR, userdata);
97             }
98             return;
99         }
100
101         // Initialize ValidationCore - this must be done before AttachDatabases
102         ValidationCore::VCoreInit(
103             std::string(GlobalConfig::GetFingerprintListFile()),
104             std::string(GlobalConfig::GetFingerprintListSchema()),
105             std::string(GlobalConfig::GetVCoreDatabaseFilePath()));
106
107         InstallerMainThreadSingleton::Instance().AttachDatabases();
108
109         _D("Prepare libxml2 to work in multithreaded program.");
110         xmlInitParser();
111
112         // Initialize Language Subtag registry
113         LanguageSubtagRstTreeSingleton::Instance().Initialize();
114
115         // Installer init
116         CONTROLLER_POST_SYNC_EVENT(
117             Logic::InstallerController,
118             InstallerControllerEvents::
119                 InitializeEvent());
120
121         if (callback) {
122             _D("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
123             callback(WRT_SUCCESS, userdata);
124         }
125     } catch (const DPL::Exception& ex) {
126         _E("Internal Error during Init:");
127         DPL::Exception::DisplayKnownException(ex);
128         if (callback) {
129             callback(WRT_INSTALLER_ERROR_FATAL_ERROR, userdata);
130             return;
131         }
132     }
133     return;
134 }
135
136 void wrt_installer_shutdown()
137 {
138     try {
139         _D("[WRT-API] DEINITIALIZING WRT INSTALLER...");
140
141         // Installer termination
142         CONTROLLER_POST_SYNC_EVENT(
143             Logic::InstallerController,
144             InstallerControllerEvents::
145                 TerminateEvent());
146
147         InstallerMainThreadSingleton::Instance().DetachDatabases();
148
149         // This must be done after DetachDatabase
150         ValidationCore::VCoreDeinit();
151
152         // Global deinit check
153         _D("Cleanup libxml2 global values.");
154         xmlCleanupParser();
155     } catch (const DPL::Exception& ex) {
156         _E("Internal Error during Shutdown:");
157         DPL::Exception::DisplayKnownException(ex);
158     }
159 }
160
161 void wrt_install_widget(
162     const char *path,
163     const char *tzPkgId,
164     void* userdata,
165     WrtInstallerStatusCallback status_cb,
166     WrtProgressCallback progress_cb,
167     InstallMode installMode,
168     std::shared_ptr<PackageManager::
169                         IPkgmgrSignal> pkgmgrInterface
170     )
171 {
172     UNHANDLED_EXCEPTION_HANDLER_BEGIN
173     {
174         if (InstallMode::InstallTime::PRELOAD == installMode.installTime) {
175             DPL::Log::OldStyleLogProvider *oldStyleProvider =
176                 new DPL::Log::OldStyleLogProvider(false, false, false, true,
177                         false, true);
178             DPL::Log::LogSystemSingleton::Instance().AddProvider(oldStyleProvider);
179         }
180
181         _D("[WRT-API] INSTALL WIDGET: %s", path);
182         // Post installation event
183         CONTROLLER_POST_EVENT(
184             Logic::InstallerController,
185             InstallerControllerEvents::InstallWidgetEvent(
186                 path, tzPkgId, Jobs::WidgetInstall::WidgetInstallationStruct(
187                     InstallerCallbacksTranslate::installFinishedCallback,
188                     InstallerCallbacksTranslate::installProgressCallback,
189                     new InstallerCallbacksTranslate::StatusCallbackStruct(
190                         userdata, status_cb, progress_cb),
191                     installMode,
192                     pkgmgrInterface)));
193     }
194     UNHANDLED_EXCEPTION_HANDLER_END
195 }
196
197 void wrt_uninstall_widget(
198     const char * const tzAppid,
199     void* userdata,
200     WrtInstallerStatusCallback status_cb,
201     WrtProgressCallback progress_cb,
202     std::shared_ptr<PackageManager::
203                         IPkgmgrSignal> pkgmgrSignalInterface)
204 {
205     UNHANDLED_EXCEPTION_HANDLER_BEGIN
206     {
207         std::string tizenAppid(tzAppid);
208         _D("[WRT-API] UNINSTALL WIDGET: %s", tizenAppid.c_str());
209         // Post uninstallation event
210         CONTROLLER_POST_EVENT(
211             Logic::InstallerController,
212             InstallerControllerEvents::UninstallWidgetEvent(
213                 tizenAppid,
214                 WidgetUninstallationStruct(
215                     InstallerCallbacksTranslate::uninstallFinishedCallback,
216                     InstallerCallbacksTranslate::installProgressCallback,
217                     new InstallerCallbacksTranslate::StatusCallbackStruct(
218                         userdata, status_cb, progress_cb),
219                     pkgmgrSignalInterface
220                     )
221                 )
222             );
223     }
224     UNHANDLED_EXCEPTION_HANDLER_END
225 }
226
227 void wrt_install_plugin(
228     const char *pluginDir,
229     void *user_param,
230     WrtPluginInstallerStatusCallback status_cb,
231     WrtProgressCallback progress_cb)
232 {
233     UNHANDLED_EXCEPTION_HANDLER_BEGIN
234     {
235         _D("[WRT-API] INSTALL PLUGIN: %s", pluginDir);
236         //Private data for status callback
237         //Resource is free in pluginInstallFinishedCallback
238         InstallerCallbacksTranslate::PluginStatusCallbackStruct*
239         callbackStruct =
240             new InstallerCallbacksTranslate::PluginStatusCallbackStruct(
241                 user_param, status_cb, progress_cb);
242
243         CONTROLLER_POST_EVENT(
244             Logic::InstallerController,
245             InstallerControllerEvents::InstallPluginEvent(
246                 std::string(pluginDir),
247                 PluginInstallerStruct(
248                     InstallerCallbacksTranslate::
249                         pluginInstallFinishedCallback,
250                     InstallerCallbacksTranslate::
251                         installProgressCallback, callbackStruct)));
252     }
253     UNHANDLED_EXCEPTION_HANDLER_END
254 }