Update api about private_id
[platform/core/api/notification.git] / notification-ex / dbus_sender.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 #include <unistd.h>
20
21 #include <string>
22 #include <list>
23
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"
29
30 #ifdef LOG_TAG
31 #undef LOG_TAG
32 #endif
33
34 #define LOG_TAG "NOTIFICATION_EX"
35 #define MAX_PACKAGE_STR_SIZE 512
36
37 using namespace std;
38 using namespace tizen_base;
39 namespace notification {
40
41 DBusSender::DBusSender(string path) : impl_(new Impl(this, path)) {
42   LOGW("Created (%s)", path.c_str());
43 }
44
45 DBusSender::Impl::~Impl() = default;
46
47 DBusSender::Impl::Impl(DBusSender* parent, string path)
48     : path_(path), parent_(parent) {
49   LOGI("Dbus_sender impl created");
50 }
51
52 DBusSender::~DBusSender() {
53   LOGW("Destroyed");
54 }
55
56 bool DBusSender::Impl::EmitSignal(string bus_name, string signal_name,
57     GVariant* data) {
58   GError* err = NULL;
59   gboolean result = TRUE;
60
61   result = g_dbus_connection_emit_signal(
62               DBusConnectionManager::GetInst().GetConnection(),
63               bus_name.empty() ? NULL : bus_name.c_str(),
64               path_.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");
69     if (err != NULL) {
70       LOGE("g_dbus_connection_emit_signal() err : %s",
71         err->message);
72       g_error_free(err);
73     }
74   } else {
75     LOGI("Successfully emit signal to %s, %s, %s",
76       bus_name.c_str(), path_.c_str(), signal_name.c_str());
77   }
78   return result;
79 }
80
81 string DBusSender::Impl::GetBusName(string appid, string dest_appid) const {
82   if (!DBusConnectionManager::GetInst().IsDataProviderMaster(appid))
83     return DBusConnectionManager::GetInst().GetDataProviderMasterName();
84
85   return DBusConnectionManager::GetInst().GetBusName(dest_appid);
86 }
87
88 void DBusSender::Notify(const IEventInfo& info, list<Bundle> serialized,
89     string dest_appid) {
90   string signal_name = EventInfo::GetString(info.GetEventType());
91   string appid = util::GetAppId();
92
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()));
97   }
98
99   GVariant* data = g_variant_new("(ssa(s))",
100         appid.c_str(),
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);
106 }
107
108 GDBusMessage* DBusSender::Impl::MethodCall(string appid, string method_name,
109     Bundle serialized) {
110   GError* err = nullptr;
111   GDBusMessage* msg = g_dbus_message_new_method_call(
112       DBusConnectionManager::GetInst().GetDataProviderMasterName().c_str(),
113       path_.c_str(),
114       DBusConnectionManager::GetInst().GetInterfaceName().c_str(),
115       method_name.c_str());
116   if (!msg) {
117     LOGE("Can't allocate new method call");
118     return nullptr;
119   }
120
121   g_dbus_message_set_body(msg,
122       g_variant_new("(ss)",
123         appid.c_str(),
124         reinterpret_cast<char*>(serialized.ToRaw().first.get())
125       )
126   );
127
128   LOGI("send message !! (%s) (%s) (%s)",
129       path_.c_str(), method_name.c_str(), appid.c_str());
130   GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync(
131       DBusConnectionManager::GetInst().GetConnection(), msg,
132       G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
133
134   return reply;
135 }
136
137 std::list<Bundle> DBusSender::Request(const IEventInfo& info) {
138   string appid = info.GetOwner();
139   if (appid.length() == 0)
140     appid = util::GetAppId();
141
142   string method_name = EventInfo::GetString(info.GetEventType());
143   Bundle serialized = info.Serialize();
144
145   GDBusMessage* reply = impl_->MethodCall(appid, method_name, serialized);
146   GVariant *reply_body = g_dbus_message_get_body(reply);
147
148   GVariantIter *iter = nullptr;
149   list<Bundle> ret_list;
150   g_variant_get(reply_body, "(a(s))", &iter);
151   char* raw = nullptr;
152   while (g_variant_iter_loop(iter, "(s)", &raw) && raw != nullptr) {
153     Bundle ret(raw);
154     ret_list.emplace_back(ret);
155   }
156
157   return ret_list;
158 }
159
160 int DBusSender::RequestNumber(const IEventInfo& info) {
161   string appid = info.GetOwner();
162   if (appid.length() == 0)
163     appid = util::GetAppId();
164
165   string method_name = EventInfo::GetString(info.GetEventType());
166   Bundle serialized = info.Serialize();
167
168   GDBusMessage* reply = impl_->MethodCall(appid, method_name, serialized);
169   GVariant *reply_body = g_dbus_message_get_body(reply);
170
171   int num;
172   g_variant_get(reply_body, "(i)", &num);
173   return num;
174 }
175
176 }  // namespace notification