Fix for update-period
[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         !strcmp(argv[i], "-w") || !strcmp(argv[i], "-y")) {
45       if (i + 1 < argc) {
46         path = argv[i + 1];
47         break;
48       }
49     }
50   }
51   return path;
52 }
53
54 std::string GetPkgIdFromPath(const std::string& path) {
55   if (!bf::exists(path))
56     return {};
57   bf::path tmp_path = common_installer::GenerateTmpDir("/tmp");
58   bs::error_code code;
59   bf::create_directories(tmp_path, code);
60   if (code)
61     return {};
62   if (!common_installer::ExtractToTmpDir(path.c_str(), tmp_path,
63       "config.xml")) {
64     bf::remove_all(tmp_path, code);
65     return {};
66   }
67   bf::path config_path = tmp_path / "config.xml";
68   std::vector<std::shared_ptr<parser::ManifestHandler>> handlers = {
69     std::make_shared<wgt::parse::WidgetHandler>(),
70     std::make_shared<wgt::parse::TizenApplicationHandler>()
71   };
72   std::unique_ptr<parser::ManifestHandlerRegistry> registry(
73       new parser::ManifestHandlerRegistry(handlers));
74   std::unique_ptr<parser::ManifestParser> parser(
75       new parser::ManifestParser(std::move(registry)));
76   if (!parser->ParseManifest(config_path)) {
77     bf::remove_all(tmp_path, code);
78     return {};
79   }
80   auto info = std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
81       parser->GetManifestData(
82           wgt::application_widget_keys::kTizenApplicationKey));
83   if (!info) {
84     bf::remove_all(tmp_path, code);
85     return {};
86   }
87   std::string pkg_id = info->package();
88
89   bf::remove_all(tmp_path, code);
90   return pkg_id;
91 }
92
93 std::string ReadPkgidFromRecovery(const std::string& recovery_path) {
94   std::unique_ptr<ci::recovery::RecoveryFile> recovery_file =
95       ci::recovery::RecoveryFile::OpenRecoveryFileForPath(recovery_path);
96   recovery_file->Detach();
97   return recovery_file->pkgid();
98 }
99
100 }  // namespace
101
102 namespace wgt {
103
104 bool WgtAppQueryInterface::IsAppInstalledByArgv(int argc, char** argv) {
105   std::string arg = GetInstallationRequestInfo(argc, argv);
106   if (arg.empty())
107     return false;
108
109   // argument from commandline is package id
110   if (ci::IsPackageInstalled(arg, ci::GetRequestMode()))
111     return true;
112
113   // argument from commandline is path to file
114   std::string pkg_id = GetPkgIdFromPath(arg);
115   if (pkg_id.empty())
116     return false;
117   return ci::IsPackageInstalled(pkg_id, ci::GetRequestMode());
118 }
119
120 bool WgtAppQueryInterface::IsHybridApplication(int argc, char** argv) {
121   std::string arg = GetInstallationRequestInfo(argc, argv);
122   if (arg.find("apps_rw/recovery-") != std::string::npos)
123     arg = ReadPkgidFromRecovery(arg);
124   if (ci::IsPackageInstalled(arg, ci::GetRequestMode())) {
125     bf::path package_directory(ci::GetRootAppPath(false));
126     if (bf::exists(package_directory / arg / kTizenManifestLocation) &&
127         bf::exists(package_directory / arg / kHybridConfigLocation))
128       return true;
129   } else {
130     bool tizen_manifest_found = false;
131     bool config_xml_found = false;
132     if (!ci::CheckPathInZipArchive(arg.c_str(), kTizenManifestLocation,
133                                    &tizen_manifest_found))
134       return false;
135     if (!ci::CheckPathInZipArchive(arg.c_str(), kHybridConfigLocation,
136                                    &config_xml_found))
137       return false;
138     if (tizen_manifest_found && config_xml_found)
139       return true;
140   }
141   return false;
142 }
143
144 }  // namespace wgt