c5abeda95fc239e1c705443118f735884c5f0667
[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 <tzplatform_config.h>
27
28 #include <memory>
29 #include <utility>
30 #include <string>
31 #include <vector>
32
33 namespace bf = boost::filesystem;
34 namespace bs = boost::system;
35 namespace ci = common_installer;
36
37 namespace {
38
39 const char kHybridConfigLocation[] = "res/wgt/config.xml";
40 const char kTizenManifestLocation[] = "tizen-manifest.xml";
41
42 std::string ReadPkgidFromRecovery(const std::string& recovery_path) {
43   std::unique_ptr<ci::recovery::RecoveryFile> recovery_file =
44       ci::recovery::RecoveryFile::OpenRecoveryFile(recovery_path);
45   recovery_file->Detach();
46   return recovery_file->pkgid();
47 }
48
49 }  // namespace
50
51 namespace wgt {
52
53 std::string WgtAppQueryInterface::GetManifestFileName() const {
54   return "config.xml";
55 }
56
57 std::string WgtAppQueryInterface::GetPkgIdFromPath(
58     const std::string& path) const {
59   bf::path tmp_path = ExtractManifest(path);
60   if (tmp_path.empty())
61     return {};
62   std::vector<std::shared_ptr<parser::ManifestHandler>> handlers = {
63     std::make_shared<wgt::parse::WidgetHandler>(),
64     std::make_shared<wgt::parse::TizenApplicationHandler>()
65   };
66   std::unique_ptr<parser::ManifestHandlerRegistry> registry(
67       new parser::ManifestHandlerRegistry(handlers));
68   std::unique_ptr<parser::ManifestParser> parser(
69       new parser::ManifestParser(std::move(registry)));
70   bf::path config_path = tmp_path / GetManifestFileName();
71   if (!parser->ParseManifest(config_path)) {
72     ci::RemoveAll(tmp_path);
73     return {};
74   }
75   auto info = std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
76       parser->GetManifestData(
77           wgt::application_widget_keys::kTizenApplicationKey));
78   if (!info) {
79     ci::RemoveAll(tmp_path);
80     return {};
81   }
82   std::string pkg_id = info->package();
83
84   ci::RemoveAll(tmp_path);
85   return pkg_id;
86 }
87
88 bool WgtAppQueryInterface::IsHybridApplication(const std::string& arg,
89     uid_t uid) const {
90   std::string info;
91   bool is_recovery = arg.find("wgt-recovery-") != std::string::npos;
92   if (is_recovery)
93     info = ReadPkgidFromRecovery(arg);
94   else
95     info = arg;
96   bf::path rw_package_directory(ci::GetRootAppPath(false, uid));
97   bf::path ro_package_directory;
98   if (uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) || uid == 0)
99     ro_package_directory = ci::GetRootAppPath(true, uid);
100   if ((bf::exists(rw_package_directory / info / kTizenManifestLocation) &&
101         bf::exists(rw_package_directory / info / kHybridConfigLocation)) ||
102        (bf::exists(ro_package_directory / info / kTizenManifestLocation) &&
103         bf::exists(ro_package_directory / info / kHybridConfigLocation))) {
104       return true;
105   } else if (!is_recovery) {
106     bool tizen_manifest_found = false;
107     bool config_xml_found = false;
108     if (!ci::CheckPathInZipArchive(info.c_str(), kTizenManifestLocation,
109                                    &tizen_manifest_found))
110       return false;
111     if (!ci::CheckPathInZipArchive(info.c_str(), kHybridConfigLocation,
112                                    &config_xml_found))
113       return false;
114     if (tizen_manifest_found && config_xml_found)
115       return true;
116   }
117   return false;
118 }
119
120 }  // namespace wgt