Add Privacy-Guard
[platform/core/security/privacy-guard.git] / server / src / NotificationServer.cpp
1 /*
2  * Copyright (c) 2012 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 <dbus/dbus.h>
18 #include <dbus/dbus-glib-lowlevel.h>
19 #include "NotificationServer.h"
20 #include "PrivacyGuardTypes.h"
21 #include "Utils.h"
22
23 auto DBusConnectionDeleter = [&](DBusConnection* pPtr) { dbus_connection_close(pPtr); pPtr = NULL;};
24 const int MAX_LOCAL_BUF_SIZE = 128;
25
26 NotificationServer::NotificationServer(void)
27         : m_initialized(false)
28         , m_pDBusConnection(NULL)
29 {
30
31 }
32
33 NotificationServer::~NotificationServer(void)
34 {
35         if (m_pDBusConnection)
36         {
37                 dbus_connection_close(m_pDBusConnection);
38                 m_pDBusConnection = NULL;
39         }
40 }
41
42 int
43 NotificationServer::initialize(void)
44 {
45         if (m_initialized)
46                 return PRIV_FLTR_ERROR_SUCCESS;
47         
48         DBusError error;
49         dbus_error_init(&error);
50
51         m_pDBusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
52         TryReturn(m_pDBusConnection != NULL, PRIV_FLTR_ERROR_SYSTEM_ERROR, dbus_error_free(&error), "dbus_bus_get_private : %s", error.message);
53
54         dbus_connection_setup_with_g_main(m_pDBusConnection, NULL);
55         std::unique_ptr < char[] > pRule(new char[MAX_LOCAL_BUF_SIZE]);
56
57         snprintf(pRule.get(), MAX_LOCAL_BUF_SIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH.c_str(), DBUS_SIGNAL_INTERFACE.c_str());
58
59         dbus_bus_add_match(m_pDBusConnection, pRule.get(), &error);
60         TryReturn(!dbus_error_is_set(&error), PRIV_FLTR_ERROR_SYSTEM_ERROR, dbus_error_free(&error), "dbus_bus_add_match : %s", error.message);
61
62         m_initialized = true;
63         return PRIV_FLTR_ERROR_SUCCESS;
64 }
65
66 int
67 NotificationServer::notifySettingChanged(const std::string pkgId, const std::string privacyId)
68 {
69         if (!m_initialized)
70                 return PRIV_FLTR_ERROR_INVALID_STATE;
71
72         char* pPkgId = const_cast <char*> (pkgId.c_str());
73         char* pPrivacyId = const_cast <char*> (privacyId.c_str());
74
75         DBusMessage* pMessage = dbus_message_new_signal(DBUS_PATH.c_str(), DBUS_SIGNAL_INTERFACE.c_str(), DBUS_SIGNAL_SETTING_CHANGED.c_str());
76         TryReturn(pMessage != NULL, PRIV_FLTR_ERROR_IPC_ERROR, , "dbus_message_new_signal");
77
78         dbus_bool_t r;
79         r = dbus_message_append_args(pMessage,
80                 DBUS_TYPE_STRING, &pPkgId,
81                 DBUS_TYPE_STRING, &pPrivacyId,
82                 DBUS_TYPE_INVALID);
83         TryReturn(r, PRIV_FLTR_ERROR_IPC_ERROR, , "dbus_message_append_args");
84
85         r = dbus_connection_send(m_pDBusConnection, pMessage, NULL);
86         TryReturn(r, PRIV_FLTR_ERROR_IPC_ERROR, dbus_message_unref(pMessage);, "dbus_connection_send");
87
88         dbus_connection_flush(m_pDBusConnection);
89         dbus_message_unref(pMessage);
90
91         return PRIV_FLTR_ERROR_SUCCESS;
92 }
93
94 int
95 NotificationServer::notifyPkgRemoved(const std::string pkgId)
96 {
97         if (!m_initialized)
98                 return PRIV_FLTR_ERROR_INVALID_STATE;
99
100         char* pPkgId = const_cast <char*> (pkgId.c_str());
101
102         DBusMessage* pMessage = dbus_message_new_signal(DBUS_PATH.c_str(), DBUS_SIGNAL_INTERFACE.c_str(), DBUS_SIGNAL_PKG_REMOVED.c_str());
103         TryReturn(pMessage != NULL, PRIV_FLTR_ERROR_IPC_ERROR, , "dbus_message_new_signal");
104
105         dbus_bool_t r;
106         r = dbus_message_append_args(pMessage,
107                 DBUS_TYPE_STRING, &pPkgId,
108                 DBUS_TYPE_INVALID);
109         TryReturn(r, PRIV_FLTR_ERROR_IPC_ERROR, , "dbus_message_append_args");
110
111         r = dbus_connection_send(m_pDBusConnection, pMessage, NULL);
112         TryReturn(r, PRIV_FLTR_ERROR_IPC_ERROR, dbus_message_unref(pMessage);, "dbus_connection_send");
113
114         dbus_connection_flush(m_pDBusConnection);
115         dbus_message_unref(pMessage);
116
117         return PRIV_FLTR_ERROR_SUCCESS;
118 }