2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "notification-ex/exception.h"
21 #include "notification-ex/dbus_connection_manager.h"
22 #include "notification-ex/ex_util.h"
28 #define LOG_TAG "NOTIFICATION_EX"
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"
35 namespace notification {
37 DBusConnectionManager::DBusConnectionManager() = default;
38 DBusConnectionManager::~DBusConnectionManager() = default;
40 DBusConnectionManager& DBusConnectionManager::GetInst() {
41 static DBusConnectionManager w_inst;
43 if (w_inst.connection_ == nullptr) {
45 if (ret != ERROR_NONE)
51 bool DBusConnectionManager::IsDataProviderMaster() const {
55 bool DBusConnectionManager::IsDataProviderMaster(string appid) const {
56 if (appid == DATA_PROVIDER_MASTER_ID)
61 string DBusConnectionManager::GetDataProviderMasterName() const {
62 return GetBusName(DATA_PROVIDER_MASTER_ID);
65 string DBusConnectionManager::GetInterfaceName() const {
66 return NOTIFICATION_EX_INTERFACE;
69 GDBusConnection* DBusConnectionManager::GetConnection() {
73 std::string DBusConnectionManager::GetBusName(string appid) const {
77 if (IsDataProviderMaster(appid))
78 return string(NOTIFICATION_EX_BUS_NAME_PREFIX) + DATA_PROVIDER_MASTER_ID;
80 char* encoded_str = g_compute_checksum_for_string(G_CHECKSUM_MD5,
82 if (encoded_str == NULL)
85 string bus_name = string(NOTIFICATION_EX_BUS_NAME_PREFIX) + encoded_str;
91 int DBusConnectionManager::Init() {
92 GError* error = nullptr;
93 connection_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
94 if (connection_ == NULL) {
96 LOGE("Failed to get dbus [%s]", error->message);
99 return ERROR_IO_ERROR;
102 string appid = util::GetAppId();
104 return ERROR_IO_ERROR;
106 if (IsDataProviderMaster(appid))
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);
115 g_object_unref(connection_);
116 LOGE("g_bus_own_name_on_connection, error");
117 return ERROR_IO_ERROR;
122 } // namespace notification