Fix WGT handlers includes
[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_registration.h>
15 #include <common/recovery_file.h>
16 #include <common/request.h>
17 #include <common/utils/file_util.h>
18
19 #include <manifest_parser/manifest_parser.h>
20 #include <manifest_parser/utils/logging.h>
21 #include <wgt_manifest_handlers/application_manifest_constants.h>
22 #include <wgt_manifest_handlers/tizen_application_handler.h>
23 #include <wgt_manifest_handlers/widget_handler.h>
24
25 #include <memory>
26 #include <string>
27 #include <vector>
28
29
30 namespace bf = boost::filesystem;
31 namespace bs = boost::system;
32 namespace ci = common_installer;
33
34 namespace {
35
36 const char kHybridConfigLocation[] = "res/wgt/config.xml";
37 const char kTizenManifestLocation[] = "tizen-manifest.xml";
38
39 std::string GetInstallationRequestInfo(int argc, char** argv) {
40   std::string path;
41   for (int i = 0; i < argc; ++i) {
42     if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "-r") ||
43         !strcmp(argv[i], "-d") || !strcmp(argv[i], "-b")) {
44       if (i + 1 < argc) {
45         path = argv[i + 1];
46         break;
47       }
48     }
49   }
50   return path;
51 }
52
53 std::string GetPkgIdFromPath(const std::string& path) {
54   if (!bf::exists(path))
55     return {};
56   bf::path tmp_path = common_installer::GenerateTmpDir("/tmp");
57   bs::error_code code;
58   bf::create_directories(tmp_path, code);
59   if (code)
60     return {};
61   if (!common_installer::ExtractToTmpDir(path.c_str(), tmp_path,
62       "config.xml")) {
63     bf::remove_all(tmp_path, code);
64     return {};
65   }
66   bf::path config_path = tmp_path / "config.xml";
67   std::vector<parser::ManifestHandler*> handlers = {
68     new wgt::parse::WidgetHandler(),
69     new wgt::parse::TizenApplicationHandler()
70   };
71   std::unique_ptr<parser::ManifestHandlerRegistry> registry(
72       new parser::ManifestHandlerRegistry(handlers));
73   std::unique_ptr<parser::ManifestParser> parser(
74       new parser::ManifestParser(std::move(registry)));
75   if (!parser->ParseManifest(config_path)) {
76     bf::remove_all(tmp_path, code);
77     return {};
78   }
79   auto info = std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
80       parser->GetManifestData(
81           wgt::application_widget_keys::kTizenApplicationKey));
82   if (!info) {
83     bf::remove_all(tmp_path, code);
84     return {};
85   }
86   std::string pkg_id = info->package();
87
88   bf::remove_all(tmp_path, code);
89   return pkg_id;
90 }
91
92 std::string ReadPkgidFromRecovery(const std::string& recovery_path) {
93   std::unique_ptr<ci::recovery::RecoveryFile> recovery_file =
94       ci::recovery::RecoveryFile::OpenRecoveryFileForPath(recovery_path);
95   recovery_file->Detach();
96   return recovery_file->pkgid();
97 }
98
99 }  // namespace
100
101 namespace wgt {
102
103 bool WgtAppQueryInterface::IsAppInstalledByArgv(int argc, char** argv) {
104   std::string path = GetInstallationRequestInfo(argc, argv);
105   if (path.empty())
106     return false;
107   std::string pkg_id = GetPkgIdFromPath(path);
108   if (pkg_id.empty())
109     return false;
110   return ci::IsPackageInstalled(pkg_id, ci::GetRequestMode());
111 }
112
113 bool WgtAppQueryInterface::IsHybridApplication(int argc, char** argv) {
114   std::string arg = GetInstallationRequestInfo(argc, argv);
115   if (arg.find("apps_rw/recovery-") != std::string::npos)
116     arg = ReadPkgidFromRecovery(arg);
117   if (ci::IsPackageInstalled(arg, ci::GetRequestMode())) {
118     bf::path package_directory(ci::GetRootAppPath());
119     if (bf::exists(package_directory / arg / kTizenManifestLocation) &&
120         bf::exists(package_directory / arg / kHybridConfigLocation))
121       return true;
122   } else {
123     bool tizen_manifest_found = false;
124     bool config_xml_found = false;
125     if (!ci::CheckPathInZipArchive(arg.c_str(), kTizenManifestLocation,
126                                    &tizen_manifest_found))
127       return false;
128     if (!ci::CheckPathInZipArchive(arg.c_str(), kHybridConfigLocation,
129                                    &config_xml_found))
130       return false;
131     if (tizen_manifest_found && config_xml_found)
132       return true;
133   }
134   return false;
135 }
136
137 }  // namespace wgt