b5289cd17ebd2aadc4edf3e71bc05c7082533a82
[platform/core/security/suspicious-activity-monitor.git] / device-agent / daemon / dpm / indexed_property_policy.cpp
1 /**
2  * Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6 /**
7  * @file   indexed_property_policy.cpp
8  * @brief  Implementation of indexed property policy parsing and applying
9  * @date   Created March 22, 2018
10  * @author Mail to: <A HREF="mailto:i.metelytsia@samsung.com">Iurii Metelytsia, i.metelytsia@samsung.com</A>
11  */
12
13 #include "indexed_property_policy.h"
14 #include <jsoncpp/json/reader.h>
15 #include "logging.h"
16 #include "samonitor_tag.h"
17
18 namespace dpm
19 {
20
21 IndexedPropertyPolicy::IndexedPropertyPolicy(PolicyEnforce& policy_enforce, const std::string& policy_name, IdxPropSetter setter, IdxPropGetter getter)
22     : IPolicy(policy_enforce, policy_name), m_setter(setter), m_getter(getter)
23 {
24     if(!m_setter)
25         throw std::runtime_error("Setter is empty.");
26 }
27
28 int IndexedPropertyPolicy::apply(device_policy_manager_h hdpm, const Json::Value& policy)
29 {
30     int res = TIZEN_ERROR_NONE;
31
32     LOG_D(TAG, "IndexedPropertyPolicy::apply");
33
34     if (policy.empty() || !policy.isMember("name") || !policy.isMember("key") || !policy.isMember("value")) {
35         LOG_E(TAG, "Empty policy or invalid policy format");
36         return TIZEN_ERROR_INVALID_PARAMETER;
37     }
38
39     std::string name = policy.get("name", "").asString();
40     std::string key = policy.get("key", "").asString();
41     int value = policy.get("value", 0).asInt();
42     LOG_D(TAG, "Enforce policy [%s] with key [%s] to state %d", name.c_str(), key.c_str(), value);
43
44     try {
45         if ((res = m_setter(hdpm, key.c_str(), value)) != TIZEN_ERROR_NONE) {
46             LOG_E(TAG, "Policy [%s] enforce failed with error: %d", name.c_str(), res);
47         } else {
48             LOG_D(TAG, "Policy [%s] enforced", name.c_str());
49         }
50     } catch (std::exception& e) {
51         LOG_E(TAG, "Policy [%s] enforce failed with exception: %s", name.c_str(), e.what());
52         res = TIZEN_ERROR_UNKNOWN;
53     }
54
55     return res;
56 }
57
58 int IndexedPropertyPolicy::getState(device_policy_manager_h hdpm, Json::Value& policy)
59 {
60     int res = TIZEN_ERROR_NONE;
61
62     LOG_D(TAG, "IndexedPropertyPolicy::getState");
63
64     if (policy.empty() || !policy.isMember("name") || !policy.isMember("key")) {
65         LOG_E(TAG, "Empty policy or invalid policy format");
66         return TIZEN_ERROR_INVALID_PARAMETER;
67     }
68
69     std::string name = policy.get("name", "").asString();
70     std::string key = policy.get("key", "").asString();
71     policy["supported"] = 0;
72     policy["value"] = 0;
73
74     if(!m_getter) {
75         LOG_E(TAG, "Getting policy [%s] state with key [%s] is not supported", name.c_str(), key.c_str());
76         return TIZEN_ERROR_NOT_SUPPORTED;
77     }
78
79     try {
80         int value = 0;
81         if((res = m_getter(hdpm, key.c_str(), &value)) != TIZEN_ERROR_NONE) {
82             LOG_E(TAG, "Getting policy [%s] state failed with error: %d", name.c_str(), res);
83         }
84         else {
85             LOG_D(TAG, "Policy [%s] with key [%s] state [%d]", name.c_str(), key.c_str(), value);
86             policy["supported"] = 1;
87             policy["value"] = value;
88         }
89     } catch (std::exception& e) {
90         LOG_E(TAG, "Getting policy [%s] state failed with exception: %s", name.c_str(), e.what());
91         res = TIZEN_ERROR_UNKNOWN;
92     }
93
94     return res;
95 }
96
97 } // namespace dpm