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_pkg_installer.h"
10 #include <pkgmgr/pkgmgr_parser.h>
15 #include "base/files/file_path.h"
16 #include "base/file_util.h"
20 typedef int (*PkgParser)(const char*, char* const*);
22 const base::FilePath kIconDir("/opt/share/icons/default/small/");
23 const base::FilePath kXmlDir("/opt/share/packages/");
24 const base::FilePath kXWalkLauncherBinary("/usr/bin/xwalk-launcher");
25 const std::string kServicePrefix("xwalk-service.");
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";
39 // Notification about end of installation with status.
40 const char PKGMGR_END_KEY[] = "end";
42 // Success value of end of installation.
43 const char PKGMGR_END_SUCCESS[] = "ok";
45 // Failure value of end of installation.
46 const char PKGMGR_END_FAILURE[] = "fail";
48 const std::string kAppIdPrefix("xwalk.");
52 FileDeleter(const base::FilePath& path, bool recursive)
54 recursive_(recursive) {}
60 base::DeleteFile(path_, recursive_);
74 PkgInstaller::PkgInstaller(const std::string& appid)
77 fprintf(stdout, "Invalid app id is provided for pkg installer.\n");
80 handle_ = pkgmgr_installer_new();
82 fprintf(stdout, "Fail to get package manager installer handle.\n");
86 PkgInstaller::~PkgInstaller() {
89 pkgmgr_installer_free(handle_);
93 bool PkgInstaller::InstallApplication(const std::string& xmlpath,
94 const std::string& iconpath) {
95 if (xmlpath.empty() || iconpath.empty()) {
96 fprintf(stdout, "Invalid xml path or icon path for installation\n");
99 base::FilePath icon_src(iconpath);
100 // icon_dst == /opt/share/icons/default/small/xwalk-service.<appid>.png
101 // FIXME(vcgomes): Add support for more icon types
102 base::FilePath icon_dst = kIconDir.Append(
103 kServicePrefix + std::string(appid_) + ".png");
104 if (!base::CopyFile(icon_src, icon_dst)) {
105 fprintf(stdout, "Couldn't copy application icon to '%s'\n",
106 icon_dst.value().c_str());
110 FileDeleter icon_cleaner(icon_dst, false);
112 base::FilePath xml_src(xmlpath);
113 base::FilePath xml_dst = kXmlDir.Append(
114 kServicePrefix + std::string(appid_) + ".xml");
115 if (!base::CopyFile(xml_src, xml_dst)) {
116 fprintf(stdout, "Couldn't copy application XML metadata to '%s'\n",
117 xml_dst.value().c_str());
121 #if defined(OS_TIZEN)
122 if (!ParseManifest(PkgInstaller::INSTALL, xmlpath)) {
123 fprintf(stdout, "Couldn't install %s'\n", appid_.c_str());
128 FileDeleter xml_cleaner(xml_dst, false);
129 icon_cleaner.Dismiss();
130 xml_cleaner.Dismiss();
135 bool PkgInstaller::UninstallApplication() {
138 // FIXME(vcgomes): Add support for more icon types
139 base::FilePath icon_dst = kIconDir.Append(
140 kServicePrefix + appid_ + ".png");
141 if (!base::DeleteFile(icon_dst, false)) {
142 fprintf(stdout, "Couldn't delete '%s'\n", icon_dst.value().c_str());
146 base::FilePath xmlpath(kXmlDir);
147 xmlpath = xmlpath.Append(kServicePrefix + std::string(appid_) + ".xml");
149 #if defined(OS_TIZEN)
150 if (!ParseManifest(PkgInstaller::UNINSTALL, xmlpath.MaybeAsASCII())) {
151 fprintf(stdout, "Couldn't uninstall %s'\n", appid_.c_str());
156 if (!base::DeleteFile(xmlpath, false)) {
157 fprintf(stdout, "Couldn't delete '%s'\n", xmlpath.value().c_str());
164 #if defined(OS_TIZEN)
165 bool PkgInstaller::ParseManifest(PkgInstaller::RequestType type,
166 const std::string& xmlpath) {
167 std::string pkgmgr_cmd;
170 case PkgInstaller::INSTALL:
171 pkgmgr_cmd = PKGMGR_START_INSTALL;
172 parser = pkgmgr_parser_parse_manifest_for_installation;
175 case PkgInstaller::UNINSTALL:
176 pkgmgr_cmd = PKGMGR_START_UNINSTALL;
177 parser = pkgmgr_parser_parse_manifest_for_uninstallation;
182 fprintf(stdout, "Setting unknown command for package manager.");
186 SendSignal(PKGMGR_START_KEY, pkgmgr_cmd);
188 bool result = parser(xmlpath.c_str(), NULL);
191 fprintf(stdout, "Couldn't parse manifest XML '%s'\n", xmlpath.c_str());
192 SendSignal(PKGMGR_END_KEY, PKGMGR_END_FAILURE);
195 SendSignal(PKGMGR_END_KEY, PKGMGR_END_SUCCESS);
201 bool PkgInstaller::SendSignal(
202 const std::string& key,
203 const std::string& value) {
205 fprintf(stdout, "The package install manager is not initialized.\n");
209 if (key.empty() || value.empty()) {
210 fprintf(stdout, " Fail to send signal, key/value is empty.\n");
214 if (pkgmgr_installer_send_signal(
215 handle_, PKGMGR_PKG_TYPE, appid_.c_str(),
216 key.c_str(), value.c_str())) {
217 fprintf(stdout, "Fail to send package manager signal.\n");