2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @author Yunchan Cho (yunchan.cho@samsung.com)
22 #include <dpl/lexical_cast.h>
23 #include <dpl/wrt-dao-ro/global_config.h>
25 #include <pkgmgr_installer.h>
26 #include <pkg-manager/pkgmgr_signal.h>
27 #include <installer_log.h>
32 // package type sent in every signal
33 const char PKGMGR_WEBAPP_TYPE[] = "wgt";
35 // notification about opoeration start
36 const char PKGMGR_START_KEY[] = "start";
38 // value for new installation
39 const char PKGMGR_START_INSTALL[] = "install";
41 // value for update installation
42 const char PKGMGR_START_UPDATE[] = "update";
44 // value for uninstallation
45 const char PKGMGR_START_UNINSTALL[] = "uninstall";
47 // notification about progress of installation with percentage number
48 const char PKGMGR_PROGRESS_KEY[] = "install_percent";
50 // notification about icon path for installation frontend
51 const char PKGMGR_ICON_PATH[] = "icon_path";
53 // notification about error before end with given error code
54 // (currently, same as backend exit status)
55 const char PKGMGR_ERROR[] = "error";
57 // notification about end of installation with status
58 const char PKGMGR_END_KEY[] = "end";
60 // success value of end of installation
61 const char PKGMGR_END_SUCCESS[] = "ok";
63 // failure value of end of installation
64 const char PKGMGR_END_FAILURE[] = "fail";
67 namespace PackageManager {
68 PkgmgrSignal::PkgmgrSignal() :
71 m_reqType(RequestType::UNSUPPORTED),
75 PkgmgrSignal::~PkgmgrSignal()
80 bool PkgmgrSignal::initialize(int argc, char* argv[])
83 _D("Release already allocated pkgmgr handle");
84 pkgmgr_installer_free(m_handle);
88 m_handle = pkgmgr_installer_new();
90 _E("Fail to get pkgmgr installer handle");
94 // set information from pkgmgr
95 if (!pkgmgr_installer_receive_request(
96 m_handle, argc, argv))
98 auto pkgmgrtype = pkgmgr_installer_get_request_type(m_handle);
101 case PKGMGR_REQ_INSTALL:
102 m_reqType = RequestType::INSTALL;
104 case PKGMGR_REQ_UNINSTALL:
105 m_reqType = RequestType::UNINSTALL;
107 case PKGMGR_REQ_REINSTALL:
108 m_reqType = RequestType::REINSTALL;
111 m_reqType = RequestType::UNSUPPORTED;
115 if (m_reqType == RequestType::UNSUPPORTED)
117 _E("Fail to get request type of pkgmgr");
118 pkgmgr_installer_free(m_handle);
122 const char *callerId = pkgmgr_installer_get_caller_pkgid(m_handle);
124 m_callerId = callerId;
127 _E("Fail to get information of pkgmgr's request");
128 pkgmgr_installer_free(m_handle);
133 m_type = PKGMGR_WEBAPP_TYPE;
134 m_initialized = true;
138 bool PkgmgrSignal::deinitialize()
140 if (!m_initialized) {
141 _E("PkgmgrSingal not yet intialized");
145 if (!m_recoveryFile.empty() && (0 != unlink(m_recoveryFile.c_str()))) {
146 _E("Failed to remove %s", m_recoveryFile.c_str());
150 pkgmgr_installer_free(m_handle);
154 m_initialized = false;
158 bool PkgmgrSignal::setPkgname(const std::string& name)
160 if (!m_initialized) {
161 _E("PkgmgrSingal not yet intialized");
171 _D("Success to set tizen package name: %s", m_pkgname.c_str());
177 void PkgmgrSignal::setRecoveryFile()
179 std::string filePath = WrtDB::GlobalConfig::GetTempInstallInfoPath();
180 filePath += "/" + m_pkgname;
182 m_recoveryFile = filePath;
183 _D("SetRecoveryFile... %s", filePath.c_str());
184 if (access(filePath.c_str(), F_OK) != 0) {
185 FILE *file = fopen(filePath.c_str(), "w");
190 _D("Recovery File : %s is already exist", filePath.c_str());
194 bool PkgmgrSignal::startJob(Jobs::InstallationType type)
198 case Jobs::InstallationType::NewInstallation:
199 sendSignal(PKGMGR_START_KEY, PKGMGR_START_INSTALL);
201 case Jobs::InstallationType::UpdateInstallation:
202 sendSignal(PKGMGR_START_KEY, PKGMGR_START_UPDATE);
204 case Jobs::InstallationType::Uninstallation:
205 sendSignal(PKGMGR_START_KEY, PKGMGR_START_UNINSTALL);
208 _E("Trying to send unknown installation type to pkgmgr");
214 bool PkgmgrSignal::endJob(Jobs::Exceptions::Type type, const char* message)
216 if(type == Jobs::Exceptions::Type::Success)
218 return sendSignal(PKGMGR_END_KEY, PKGMGR_END_SUCCESS);
222 std::string ecode = DPL::lexical_cast<std::string>(type);
223 if (message != NULL) {
224 char buf[BUF_SIZE] = {'\0'};
226 snprintf(buf, BUF_SIZE - 1, "%s:%s", ecode.c_str(), message);
227 sendSignal(PKGMGR_ERROR, buf);
229 return sendSignal(PKGMGR_END_KEY, PKGMGR_END_FAILURE);
233 bool PkgmgrSignal::sendProgress(int percent)
235 if (m_percent == percent) {
240 return sendSignal(PKGMGR_PROGRESS_KEY, DPL::lexical_cast<std::string>(percent));
243 bool PkgmgrSignal::sendIconPath(const std::string & iconpath)
245 return sendSignal(PKGMGR_ICON_PATH, iconpath);
248 bool PkgmgrSignal::sendSignal(const std::string& key,
249 const std::string& value) const
251 if (!m_initialized) {
252 _E("PkgmgrSingal not yet intialized");
256 if (key.empty() || value.empty()) {
257 _D("key or value is empty");
261 if (m_handle == NULL || m_type.empty()) {
262 _E("Some data of PkgmgrSignal is empty");
266 // send pkgmgr signal
267 if (pkgmgr_installer_send_signal(
268 m_handle, m_type.c_str(), m_pkgname.c_str(),
269 key.c_str(), value.c_str()))
271 _E("Fail to send pkgmgr signal");
275 _D("Success to send pkgmgr signal: %s - %s", key.c_str(), value.c_str());
279 std::string PkgmgrSignal::getPkgname() const
281 if (!m_initialized) {
282 _E("PkgmgrSingal not yet intialized");
288 PkgmgrSignal::RequestType PkgmgrSignal::getRequestedType() const
290 if (!m_initialized) {
291 _E("PkgmgrSingal not yet intialized");
297 std::string PkgmgrSignal::getCallerId() const
299 if (!m_initialized) {
300 _E("PkgmgrSingal not yet intialized");