Fix in StepWgtPatchStorageDirectory
[platform/core/appfw/wgt-backend.git] / src / wgt / step / security / step_check_wgt_ime_privilege.cc
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include <wgt/step/security/step_check_wgt_ime_privilege.h>
6
7 #include <manifest_parser/utils/version_number.h>
8 #include <manifest_parser/utils/logging.h>
9
10 #include <common/utils/glist_range.h>
11 #include <common/privileges.h>
12
13 #include <string>
14
15 namespace {
16 const char kImeCategoryName[] = "http://tizen.org/category/ime";
17 }
18
19 namespace wgt {
20 namespace security {
21
22 common_installer::Step::Status StepCheckWgtImePrivilege::process() {
23   utils::VersionNumber apiVersion(context_->manifest_data.get()->api_version);
24
25   const auto version23 = apiVersion < utils::VersionNumber("2.4");
26   auto has_ime = false;
27
28   for (const auto app :
29       GListRange<application_x*>(context_->manifest_data.get()->application)) {
30     for (const auto category : GListRange<char *>(app->category)) {
31       if (!strcmp(category, kImeCategoryName)) {
32         has_ime = true;
33
34         const auto result = version23 ? Check23Api() : Check24Api();
35         if (result != Status::OK) {
36           LOG(ERROR) << "Insufficient privileges for IME application.";
37           return result;
38         }
39
40         break;
41       }
42     }
43   }
44
45   if (!has_ime) {
46     // be sure no ime data is present without the category
47     context_->manifest_plugins_data.get().ime_info.get().setUuid(std::string());
48   } else if (version23) {
49     // be sure there's a privilege in manifest
50     privilege_x* privilege =
51         reinterpret_cast<privilege_x*>(calloc(1, sizeof(privilege_x)));
52     privilege->type = strdup(common_installer::kWebPrivilegeType);
53     privilege->value = strdup(common_installer::privileges::kImePrivilegeName);
54     context_->manifest_data.get()->privileges =
55         g_list_append(context_->manifest_data.get()->privileges, privilege);
56   }
57
58   return Status::OK;
59 }
60
61 common_installer::Step::Status StepCheckWgtImePrivilege::Check23Api() const {
62   const auto &ime = context_->manifest_plugins_data.get().ime_info.get();
63   if (ime.uuid().empty()) {
64     LOG(ERROR) << "Missing IME tag.";
65     return Status::CONFIG_ERROR;
66   }
67
68   // ime priv not supported in 2.3
69   return CheckImePrivilege() != Status::OK ?
70       Status::OK : Status::PRIVILEGE_ERROR;
71 }
72
73 common_installer::Step::Status StepCheckWgtImePrivilege::Check24Api() const {
74   return CheckImePrivilege();
75 }
76
77 common_installer::Step::Status
78 StepCheckWgtImePrivilege::CheckImePrivilege() const {
79   for (privilege_x* privilege :
80       GListRange<privilege_x*>(context_->manifest_data.get()->privileges)) {
81     if (!strcmp(privilege->value,
82                 common_installer::privileges::kImePrivilegeName))
83       return Status::OK;
84   }
85
86   LOG(DEBUG) << "Missing IME privilege.";
87   return Status::PRIVILEGE_ERROR;
88 }
89 }  // namespace security
90 }  // namespace wgt