Add new func for extentsion data
[platform/core/api/notification.git] / notification-ex / dbus_connection_manager.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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 <dlog.h>
18 #include <glib.h>
19
20 #include "notification-ex/exception.h"
21 #include "notification-ex/dbus_connection_manager.h"
22 #include "notification-ex/ex_util.h"
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27
28 #define LOG_TAG "NOTIFICATION_EX"
29
30 #define NOTIFICATION_EX_BUS_NAME_PREFIX "org.tizen.notification_ex._"
31 #define NOTIFICATION_EX_INTERFACE "org.tizen.notification_ex"
32 #define DATA_PROVIDER_MASTER_ID "data-provider-master"
33
34 using namespace std;
35 namespace notification {
36
37 DBusConnectionManager::DBusConnectionManager() = default;
38 DBusConnectionManager::~DBusConnectionManager() = default;
39
40 DBusConnectionManager& DBusConnectionManager::GetInst() {
41   static DBusConnectionManager w_inst;
42   int ret;
43   if (w_inst.connection_ == nullptr) {
44     ret = w_inst.Init();
45     if (ret != ERROR_NONE)
46       THROW(ret);
47   }
48   return w_inst;
49 }
50
51 bool DBusConnectionManager::IsDataProviderMaster() const {
52   return is_DPM_;
53 }
54
55 bool DBusConnectionManager::IsDataProviderMaster(string appid) const {
56   if (appid == DATA_PROVIDER_MASTER_ID)
57     return true;
58   return false;
59 }
60
61 string DBusConnectionManager::GetDataProviderMasterName() const {
62   return GetBusName(DATA_PROVIDER_MASTER_ID);
63 }
64
65 string DBusConnectionManager::GetInterfaceName() const {
66   return NOTIFICATION_EX_INTERFACE;
67 }
68
69 GDBusConnection* DBusConnectionManager::GetConnection() {
70   return connection_;
71 }
72
73 std::string DBusConnectionManager::GetBusName(string appid) const {
74   if (appid.empty())
75     return "";
76
77   if (IsDataProviderMaster(appid))
78     return string(NOTIFICATION_EX_BUS_NAME_PREFIX) + DATA_PROVIDER_MASTER_ID;
79
80   char* encoded_str = g_compute_checksum_for_string(G_CHECKSUM_MD5,
81                 appid.c_str(), -1);
82   if (encoded_str == NULL)
83     return "";
84
85   string bus_name = string(NOTIFICATION_EX_BUS_NAME_PREFIX) + encoded_str;
86   g_free(encoded_str);
87
88   return bus_name;
89 }
90
91 int DBusConnectionManager::Init() {
92   GError* error = nullptr;
93   connection_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
94   if (connection_ == NULL) {
95     if (error != NULL) {
96       LOGE("Failed to get dbus [%s]", error->message);
97       g_error_free(error);
98     }
99     return ERROR_IO_ERROR;
100   }
101
102   string appid = util::GetAppId();
103   if (appid.empty())
104     return ERROR_IO_ERROR;
105
106   if (IsDataProviderMaster(appid))
107     is_DPM_ = true;
108
109   string encoded_name = GetBusName(appid);
110   LOGI("own name %s", encoded_name.c_str());
111   int owner_id = g_bus_own_name_on_connection(connection_, encoded_name.c_str(),
112                                           G_BUS_NAME_OWNER_FLAGS_NONE,
113                                           NULL, NULL, NULL, NULL);
114   if (!owner_id) {
115     g_object_unref(connection_);
116     LOGE("g_bus_own_name_on_connection, error");
117     return ERROR_IO_ERROR;
118   }
119   return ERROR_NONE;
120 }
121
122 }  // namespace notification