Fix query IsHybridApplication()
[platform/core/appfw/wgt-backend.git] / src / wgt / wgt_app_query_interface.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "wgt/wgt_app_query_interface.h"
6
7 #include <unistd.h>
8 #include <sys/types.h>
9
10 #include <boost/filesystem/operations.hpp>
11 #include <boost/filesystem/path.hpp>
12 #include <boost/system/error_code.hpp>
13
14 #include <common/pkgmgr_interface.h>
15 #include <common/pkgmgr_query.h>
16 #include <common/recovery_file.h>
17 #include <common/request.h>
18 #include <common/utils/file_util.h>
19
20 #include <manifest_parser/manifest_parser.h>
21 #include <manifest_parser/utils/logging.h>
22 #include <wgt_manifest_handlers/application_manifest_constants.h>
23 #include <wgt_manifest_handlers/tizen_application_handler.h>
24 #include <wgt_manifest_handlers/widget_handler.h>
25
26 #include <memory>
27 #include <utility>
28 #include <string>
29 #include <vector>
30
31 namespace bf = boost::filesystem;
32 namespace bs = boost::system;
33 namespace ci = common_installer;
34
35 namespace {
36
37 const char kHybridConfigLocation[] = "res/wgt/config.xml";
38 const char kTizenManifestLocation[] = "tizen-manifest.xml";
39
40 std::string GetPkgIdFromPath(const std::string& path) {
41   if (!bf::exists(path))
42     return {};
43   bf::path tmp_path = common_installer::GenerateTmpDir("/tmp");
44   bs::error_code code;
45   bf::create_directories(tmp_path, code);
46   if (code)
47     return {};
48   if (!common_installer::ExtractToTmpDir(path.c_str(), tmp_path,
49       "config.xml")) {
50     ci::RemoveAll(tmp_path);
51     return {};
52   }
53   bf::path config_path = tmp_path / "config.xml";
54   std::vector<std::shared_ptr<parser::ManifestHandler>> handlers = {
55     std::make_shared<wgt::parse::WidgetHandler>(),
56     std::make_shared<wgt::parse::TizenApplicationHandler>()
57   };
58   std::unique_ptr<parser::ManifestHandlerRegistry> registry(
59       new parser::ManifestHandlerRegistry(handlers));
60   std::unique_ptr<parser::ManifestParser> parser(
61       new parser::ManifestParser(std::move(registry)));
62   if (!parser->ParseManifest(config_path)) {
63     ci::RemoveAll(tmp_path);
64     return {};
65   }
66   auto info = std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
67       parser->GetManifestData(
68           wgt::application_widget_keys::kTizenApplicationKey));
69   if (!info) {
70     ci::RemoveAll(tmp_path);
71     return {};
72   }
73   std::string pkg_id = info->package();
74
75   ci::RemoveAll(tmp_path);
76   return pkg_id;
77 }
78
79 std::string ReadPkgidFromRecovery(const std::string& recovery_path) {
80   std::unique_ptr<ci::recovery::RecoveryFile> recovery_file =
81       ci::recovery::RecoveryFile::OpenRecoveryFileForPath(recovery_path);
82   recovery_file->Detach();
83   return recovery_file->pkgid();
84 }
85
86 }  // namespace
87
88 namespace wgt {
89
90 bool WgtAppQueryInterface::IsPkgInstalled(const std::string& arg, uid_t uid) {
91   // argument from commandline is package id
92   if (ci::QueryIsPackageInstalled(arg, ci::GetRequestMode(uid), uid))
93     return true;
94
95   // argument from commandline is path to file
96   std::string pkg_id = GetPkgIdFromPath(arg);
97   if (pkg_id.empty())
98     return false;
99   return ci::QueryIsPackageInstalled(pkg_id, ci::GetRequestMode(uid), uid);
100 }
101
102 bool WgtAppQueryInterface::IsHybridApplication(const std::string& arg,
103     uid_t uid) {
104   std::string info;
105   bool is_recovery = arg.find("wgt-recovery-") != std::string::npos;
106   if (is_recovery)
107     info = ReadPkgidFromRecovery(arg);
108   else
109     info = arg;
110
111   if (ci::QueryIsPackageInstalled(info, ci::GetRequestMode(uid), uid)) {
112     bf::path package_directory(ci::GetRootAppPath(false, uid));
113     if (bf::exists(package_directory / info / kTizenManifestLocation) &&
114         bf::exists(package_directory / info / kHybridConfigLocation))
115       return true;
116   } else if (!is_recovery) {
117     bool tizen_manifest_found = false;
118     bool config_xml_found = false;
119     if (!ci::CheckPathInZipArchive(info.c_str(), kTizenManifestLocation,
120                                    &tizen_manifest_found))
121       return false;
122     if (!ci::CheckPathInZipArchive(info.c_str(), kHybridConfigLocation,
123                                    &config_xml_found))
124       return false;
125     if (tizen_manifest_found && config_xml_found)
126       return true;
127   }
128   return false;
129 }
130
131 std::string WgtAppQueryInterface::GetPkgId(const std::string& arg) {
132   return GetPkgIdFromPath(arg);
133 }
134
135 }  // namespace wgt