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.
24 #include "notification-ex/dbus_connection_manager.h"
25 #include "notification-ex/dbus_sender.h"
26 #include "notification-ex/dbus_sender_implementation.h"
27 #include "notification-ex/event_info_internal.h"
28 #include "notification-ex/ex_util.h"
34 #define LOG_TAG "NOTIFICATION_EX"
35 #define MAX_PACKAGE_STR_SIZE 512
38 using namespace tizen_base;
39 namespace notification {
41 DBusSender::DBusSender(string path) : impl_(new Impl(this, path)) {
42 LOGW("Created (%s)", path.c_str());
45 DBusSender::Impl::~Impl() = default;
47 DBusSender::Impl::Impl(DBusSender* parent, string path)
48 : path_(path), parent_(parent) {
49 LOGI("Dbus_sender impl created");
52 DBusSender::~DBusSender() {
56 bool DBusSender::Impl::EmitSignal(string bus_name, string signal_name,
59 gboolean result = TRUE;
61 result = g_dbus_connection_emit_signal(
62 DBusConnectionManager::GetInst().GetConnection(),
63 bus_name.empty() ? NULL : bus_name.c_str(),
65 DBusConnectionManager::GetInst().GetInterfaceName().c_str(),
66 signal_name.c_str(), data, &err);
67 if (result == FALSE) {
68 LOGE("g_dbus_connection_emit_signal() is failed");
70 LOGE("g_dbus_connection_emit_signal() err : %s",
75 LOGI("Successfully emit signal to %s, %s, %s",
76 bus_name.c_str(), path_.c_str(), signal_name.c_str());
81 string DBusSender::Impl::GetBusName(string appid, string dest_appid) const {
82 if (!DBusConnectionManager::GetInst().IsDataProviderMaster(appid))
83 return DBusConnectionManager::GetInst().GetDataProviderMasterName();
85 return DBusConnectionManager::GetInst().GetBusName(dest_appid);
88 void DBusSender::Notify(const IEventInfo& info, list<Bundle> serialized,
90 string signal_name = EventInfo::GetString(info.GetEventType());
91 string appid = util::GetAppId();
93 GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
94 for (auto& i : serialized) {
95 g_variant_builder_add(builder, "(s)",
96 reinterpret_cast<char*>(i.ToRaw().first.get()));
99 GVariant* data = g_variant_new("(ssa(s))",
101 reinterpret_cast<char*>(info.Serialize().ToRaw().first.get()), builder);
102 string bus_name = impl_->GetBusName(appid, dest_appid);
103 LOGI("bus name %s, %s", dest_appid.c_str(), bus_name.c_str());
104 impl_->EmitSignal(bus_name, signal_name, data);
105 g_variant_builder_unref(builder);
108 GDBusMessage* DBusSender::Impl::MethodCall(string appid, string method_name,
110 GDBusMessage* msg = g_dbus_message_new_method_call(
111 DBusConnectionManager::GetInst().GetDataProviderMasterName().c_str(),
113 DBusConnectionManager::GetInst().GetInterfaceName().c_str(),
114 method_name.c_str());
116 LOGE("Can't allocate new method call");
120 g_dbus_message_set_body(msg,
121 g_variant_new("(ss)",
123 reinterpret_cast<char*>(serialized.ToRaw().first.get())
127 LOGI("send message !! (%s) (%s) (%s)",
128 path_.c_str(), method_name.c_str(), appid.c_str());
130 GError* err = nullptr;
131 GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync(
132 DBusConnectionManager::GetInst().GetConnection(), msg,
133 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
135 LOGE("MethodCall, err[%s]", err->message);
136 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
137 set_last_result(ERROR_PERMISSION_DENIED);
139 set_last_result(ERROR_IO_ERROR);
146 GDBusMessage* DBusSender::Impl::MethodCall(string appid, const IEventInfo& info,
147 list<Bundle> serialized) {
148 GDBusMessage* msg = g_dbus_message_new_method_call(
149 DBusConnectionManager::GetInst().GetDataProviderMasterName().c_str(),
151 DBusConnectionManager::GetInst().GetInterfaceName().c_str(),
152 EventInfo::GetString(info.GetEventType()).c_str());
154 LOGE("Can't allocate new method call");
158 GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
159 for (auto& i : serialized) {
160 g_variant_builder_add(builder, "(s)",
161 reinterpret_cast<char*>(i.ToRaw().first.get()));
164 g_dbus_message_set_body(msg,
165 g_variant_new("(ssa(s))",
167 reinterpret_cast<char*>(info.Serialize().ToRaw().first.get()),
172 LOGI("send message !! (%s) (%s) (%s)",
173 path_.c_str(), EventInfo::GetString(info.GetEventType()).c_str(), appid.c_str());
175 GError* err = nullptr;
176 GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync(
177 DBusConnectionManager::GetInst().GetConnection(), msg,
178 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
180 LOGE("MethodCall, err[%s]", err->message);
181 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
182 set_last_result(ERROR_PERMISSION_DENIED);
184 set_last_result(ERROR_IO_ERROR);
188 g_variant_builder_unref(builder);
192 int DBusSender::SendMessage(const IEventInfo& info, list<Bundle> serialized,
194 string appid = info.GetOwner();
195 if (appid.length() == 0)
196 appid = util::GetAppId();
198 GDBusMessage* reply = impl_->MethodCall(appid, info, serialized);
200 return ERROR_IO_ERROR;
202 int ret = ERROR_NONE;
203 GError* err = nullptr;
204 if (g_dbus_message_to_gerror(reply, &err)) {
205 LOGE("SendMessage got error %s", err->message);
206 if (err->code == G_DBUS_ERROR_ACCESS_DENIED) {
207 set_last_result(ERROR_PERMISSION_DENIED);
208 ret = ERROR_PERMISSION_DENIED;
210 set_last_result(ERROR_IO_ERROR);
211 ret = ERROR_IO_ERROR;
216 set_last_result(ERROR_NONE);
219 GVariant *reply_body = g_dbus_message_get_body(reply);
220 g_variant_get(reply_body, "(i)", &ret);
224 std::list<Bundle> DBusSender::Request(const IEventInfo& info) {
225 string appid = info.GetOwner();
226 if (appid.length() == 0)
227 appid = util::GetAppId();
229 string method_name = EventInfo::GetString(info.GetEventType());
230 Bundle serialized = info.Serialize();
232 list<Bundle> ret_list;
233 GDBusMessage* reply = impl_->MethodCall(appid, method_name, serialized);
237 GError* err = nullptr;
238 if (g_dbus_message_to_gerror(reply, &err)) {
239 LOGE("Request got error %s", err->message);
240 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
241 set_last_result(ERROR_PERMISSION_DENIED);
243 set_last_result(ERROR_IO_ERROR);
248 set_last_result(ERROR_NONE);
251 GVariant *reply_body = g_dbus_message_get_body(reply);
252 GVariantIter *iter = nullptr;
253 g_variant_get(reply_body, "(a(s))", &iter);
255 while (g_variant_iter_loop(iter, "(s)", &raw) && raw != nullptr) {
257 ret_list.emplace_back(ret);
263 int DBusSender::RequestNumber(const IEventInfo& info) {
264 string appid = info.GetOwner();
265 if (appid.length() == 0)
266 appid = util::GetAppId();
268 string method_name = EventInfo::GetString(info.GetEventType());
269 Bundle serialized = info.Serialize();
271 GDBusMessage* reply = impl_->MethodCall(appid, method_name, serialized);
275 GError* err = nullptr;
276 if (g_dbus_message_to_gerror(reply, &err)) {
277 LOGE("RequestNumber got error %s", err->message);
278 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
279 set_last_result(ERROR_PERMISSION_DENIED);
281 set_last_result(ERROR_IO_ERROR);
286 set_last_result(ERROR_NONE);
290 GVariant *reply_body = g_dbus_message_get_body(reply);
291 g_variant_get(reply_body, "(i)", &num);
295 } // namespace notification