1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "xwalk/application/tools/tizen/xwalk_package_installer_helper.h"
9 #include <pkgmgr/pkgmgr_parser.h>
13 #include "base/files/file_path.h"
14 #include "base/file_util.h"
18 typedef int (*PkgParser)(const char*, char* const*);
20 const base::FilePath kIconDir("/opt/share/icons/default/small/");
21 const base::FilePath kXmlDir("/opt/share/packages/");
22 const base::FilePath kXWalkLauncherBinary("/usr/bin/xwalk-launcher");
23 const std::string kServicePrefix("xwalk-service.");
24 const std::string kXmlFileExt(".xml");
25 const std::string kPngFileExt(".png");
27 // Package type sent in every signal.
28 const char PKGMGR_PKG_TYPE[] = "rpm";
30 // Notification about operation start.
31 const char PKGMGR_START_KEY[] = "start";
33 // Value for new installation.
34 const char PKGMGR_START_INSTALL[] = "install";
36 // Value for uninstallation.
37 const char PKGMGR_START_UNINSTALL[] = "uninstall";
40 const char PKGMGR_START_UPDATE[] = "update";
42 // Notification about end of installation with status.
43 const char PKGMGR_END_KEY[] = "end";
45 // Success value of end of installation.
46 const char PKGMGR_END_SUCCESS[] = "ok";
48 // Failure value of end of installation.
49 const char PKGMGR_END_FAILURE[] = "fail";
51 const std::string kAppIdPrefix("xwalk.");
55 FileDeleter(const base::FilePath& path, bool recursive)
57 recursive_(recursive) {}
63 base::DeleteFile(path_, recursive_);
75 const char* ToEndStatus(bool result) {
76 return result ? PKGMGR_END_SUCCESS : PKGMGR_END_FAILURE;
79 inline base::FilePath GetDestFilePath(const base::FilePath& DirPath,
80 const std::string& appid,
81 const std::string& file_ext) {
82 return DirPath.Append(kServicePrefix + std::string(appid) + file_ext);
85 bool CopyFileToDst(const base::FilePath& file_src,
86 const base::FilePath& file_dst) {
87 if (!base::CopyFile(file_src, file_dst)) {
88 fprintf(stdout, "Couldn't copy application file to '%s'\n",
89 file_dst.value().c_str());
97 PackageInstallerHelper::PackageInstallerHelper(const std::string& appid)
100 fprintf(stdout, "Invalid app id is provided for pkg installer.\n");
102 handle_ = pkgmgr_installer_new();
104 fprintf(stdout, "Fail to get package manager installer handle.\n");
107 PackageInstallerHelper::~PackageInstallerHelper() {
109 pkgmgr_installer_free(handle_);
112 bool PackageInstallerHelper::InstallApplication(
113 const std::string& xmlpath,
114 const std::string& iconpath) {
115 SendSignal(PKGMGR_START_KEY, PKGMGR_START_INSTALL);
116 bool ret = InstallApplicationInternal(xmlpath, iconpath);
117 SendSignal(PKGMGR_END_KEY, ToEndStatus(ret));
121 bool PackageInstallerHelper::UninstallApplication() {
122 SendSignal(PKGMGR_START_KEY, PKGMGR_START_UNINSTALL);
123 bool ret = UninstallApplicationInternal();
124 SendSignal(PKGMGR_END_KEY, ToEndStatus(ret));
128 bool PackageInstallerHelper::UpdateApplication(
129 const std::string& xmlpath,
130 const std::string& iconpath) {
131 SendSignal(PKGMGR_START_KEY, PKGMGR_START_UPDATE);
132 bool ret = UpdateApplicationInternal(xmlpath, iconpath);
133 SendSignal(PKGMGR_END_KEY, ToEndStatus(ret));
137 bool PackageInstallerHelper::InstallApplicationInternal(
138 const std::string& xmlpath,
139 const std::string& iconpath) {
140 if (xmlpath.empty() || iconpath.empty()) {
141 fprintf(stdout, "Invalid xml path or icon path for installation\n");
144 // FIXME(vcgomes): Add support for more icon types
145 base::FilePath xml_dst = GetDestFilePath(kXmlDir, appid_, kXmlFileExt);
146 base::FilePath icon_dst = GetDestFilePath(kIconDir, appid_, kPngFileExt);
147 FileDeleter xml_cleaner(xml_dst, false);
148 FileDeleter icon_cleaner(icon_dst, false);
151 if (!CopyFileToDst(base::FilePath(xmlpath), xml_dst)
152 || !CopyFileToDst(base::FilePath(iconpath), icon_dst))
155 if (pkgmgr_parser_parse_manifest_for_installation(xmlpath.c_str(), NULL)) {
156 fprintf(stdout, "Couldn't parse manifest XML '%s'\n", xmlpath.c_str());
160 xml_cleaner.Dismiss();
161 icon_cleaner.Dismiss();
166 bool PackageInstallerHelper::UninstallApplicationInternal() {
169 // FIXME(vcgomes): Add support for more icon types
170 base::FilePath iconpath = GetDestFilePath(kIconDir, appid_, kPngFileExt);
171 base::FilePath xmlpath = GetDestFilePath(kXmlDir, appid_, kXmlFileExt);
172 FileDeleter icon_cleaner(iconpath, false);
173 FileDeleter xml_cleaner(xmlpath, false);
175 std::string xmlpath_str = xmlpath.MaybeAsASCII();
176 assert(!xmlpath_str.empty());
177 if (pkgmgr_parser_parse_manifest_for_uninstallation(
178 xmlpath_str.c_str(), NULL)) {
179 fprintf(stdout, "Couldn't parse manifest XML '%s'\n", xmlpath_str.c_str());
180 icon_cleaner.Dismiss();
181 xml_cleaner.Dismiss();
187 bool PackageInstallerHelper::UpdateApplicationInternal(
188 const std::string& xmlpath,
189 const std::string& iconpath) {
190 if (xmlpath.empty() || iconpath.empty()) {
191 fprintf(stdout, "Invalid xml path or icon path for update\n");
194 // FIXME(vcgomes): Add support for more icon types
195 base::FilePath xml_dst = GetDestFilePath(kXmlDir, appid_, kXmlFileExt);
196 base::FilePath icon_dst = GetDestFilePath(kIconDir, appid_, kPngFileExt);
197 FileDeleter xml_cleaner(xml_dst, false);
198 FileDeleter icon_cleaner(icon_dst, false);
201 if (!CopyFileToDst(base::FilePath(xmlpath), xml_dst)
202 || !CopyFileToDst(base::FilePath(iconpath), icon_dst))
205 if (pkgmgr_parser_parse_manifest_for_upgrade(xmlpath.c_str(), NULL)) {
206 fprintf(stdout, "Couldn't parse manifest XML '%s'\n", xmlpath.c_str());
210 xml_cleaner.Dismiss();
211 icon_cleaner.Dismiss();
216 bool PackageInstallerHelper::SendSignal(
217 const std::string& key,
218 const std::string& value) {
220 fprintf(stdout, "The package install manager is not initialized.\n");
224 if (key.empty() || value.empty()) {
225 fprintf(stdout, " Fail to send signal, key/value is empty.\n");
229 if (pkgmgr_installer_send_signal(
230 handle_, PKGMGR_PKG_TYPE, appid_.c_str(),
231 key.c_str(), value.c_str())) {
232 fprintf(stdout, "Fail to send package manager signal.\n");