Add OOM exception handler
[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     if (!privilege)
53       return Status::ERROR;
54     privilege->type = strdup(common_installer::kWebPrivilegeType);
55     if (!privilege->type) {
56       common_installer::FreePrivilegeX(privilege);
57       return Status::ERROR;
58     }
59     privilege->value = strdup(common_installer::privileges::kImePrivilegeName);
60     if (!privilege->value) {
61       common_installer::FreePrivilegeX(privilege);
62       return Status::ERROR;
63     }
64     context_->manifest_data.get()->privileges =
65         g_list_append(context_->manifest_data.get()->privileges, privilege);
66   }
67
68   return Status::OK;
69 }
70
71 common_installer::Step::Status StepCheckWgtImePrivilege::Check23Api() const {
72   const auto &ime = context_->manifest_plugins_data.get().ime_info.get();
73   if (ime.uuid().empty()) {
74     LOG(ERROR) << "Missing IME tag.";
75     return Status::CONFIG_ERROR;
76   }
77
78   // ime priv not supported in 2.3
79   return CheckImePrivilege() != Status::OK ?
80       Status::OK : Status::PRIVILEGE_ERROR;
81 }
82
83 common_installer::Step::Status StepCheckWgtImePrivilege::Check24Api() const {
84   return CheckImePrivilege();
85 }
86
87 common_installer::Step::Status
88 StepCheckWgtImePrivilege::CheckImePrivilege() const {
89   for (privilege_x* privilege :
90       GListRange<privilege_x*>(context_->manifest_data.get()->privileges)) {
91     if (!strcmp(privilege->value,
92                 common_installer::privileges::kImePrivilegeName))
93       return Status::OK;
94   }
95
96   LOG(DEBUG) << "Missing IME privilege.";
97   return Status::PRIVILEGE_ERROR;
98 }
99 }  // namespace security
100 }  // namespace wgt