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