Check VCONF value for Maintenance Mode
[platform/core/security/device-policy-client.git] / libs / policy-client.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16
17 #include <system_info.h>
18 #include <klay/filesystem.h>
19
20 #include <klay/dbus/signal.h>
21 #include <klay/audit/logger.h>
22
23 #include "policy-client.h"
24
25 #include <vconf.h>
26
27 #define VCONFKEY_DPM_MODE_STATE "db/dpm/mode_state"
28
29 namespace {
30
31 const std::string SIGNAL_OBJECT_PATH = "/org/tizen/DevicePolicyManger/PIL";
32 const std::string SIGNAL_EVENT_INTERFACE = "org.tizen.DevicePolicyManager.PIL.Event";
33
34 const std::string POLICY_MANAGER_ADDRESS = "/tmp/.device-policy-manager.sock";
35
36 } // namespace
37
38
39 DevicePolicyClient::DevicePolicyClient() noexcept :
40         clientAddress(POLICY_MANAGER_ADDRESS)
41 {
42         mainloop.reset(new ScopedGMainLoop);
43 }
44
45 DevicePolicyClient::~DevicePolicyClient() noexcept
46 {
47         mainloop.reset();
48 }
49
50 int DevicePolicyClient::subscribeSignal(const std::string& name,
51                                                                                 const SignalHandler& handler,
52                                                                                 void* data) noexcept
53 {
54         if (!getMaintenanceMode())
55                 return 0;
56
57         try {
58                 auto dispatch = [name, handler, data](dbus::Variant variant) {
59                         char *state = NULL;
60                         variant.get("(s)", &state);
61
62                         handler(name.c_str(), state, data);
63                 };
64
65                 dbus::signal::Receiver receiver(SIGNAL_OBJECT_PATH, SIGNAL_EVENT_INTERFACE);
66                 return receiver.subscribe(name, dispatch);
67         } catch (runtime::Exception& e) {
68                 ERROR(e.what());
69                 return -1;
70         }
71 }
72
73 int DevicePolicyClient::unsubscribeSignal(int id) noexcept
74 {
75         if (!getMaintenanceMode())
76                 return 0;
77
78         try {
79                 dbus::signal::Receiver receiver(SIGNAL_OBJECT_PATH, SIGNAL_EVENT_INTERFACE);
80                 receiver.unsubscribe(id);
81                 return 0;
82         } catch (runtime::Exception& e) {
83                 ERROR(e.what());
84                 return -1;
85         }
86 }
87
88 int DevicePolicyClient::getMaintenanceMode()
89 {
90         int value = 0;
91
92         ::vconf_get_bool(VCONFKEY_DPM_MODE_STATE, &value);
93
94         return value;
95 }