Merge "Remove unnecessary free to prevent double free" into tizen
[platform/core/api/notification.git] / notification-ex / 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 #include <unistd.h>
20
21 #include <list>
22
23 #include "notification-ex/item_inflator.h"
24 #include "notification-ex/manager.h"
25 #include "notification-ex/manager_implementation.h"
26 #include "notification-ex/event_info_internal.h"
27 #include "notification-ex/dbus_connection_manager.h"
28 #include "notification-ex/ex_util.h"
29 #include "notification-ex/item_info_internal.h"
30 #include "notification-ex/null_item.h"
31
32 #ifdef LOG_TAG
33 #undef LOG_TAG
34 #endif
35
36 #define LOG_TAG "NOTIFICATION_EX"
37
38 #define MAX_PACKAGE_STR_SIZE 512
39 #define NOTIFICATION_EX_MANAGER_OBJECT_PATH "/org/tizen/notification_ex_manager"
40 #define DATA_PROVIDER_MASTER_ID "data-provider-master"
41
42 using namespace std;
43 using namespace tizen_base;
44 using namespace notification::item;
45 namespace notification {
46
47 Manager::Manager(unique_ptr<IEventSender> sender,
48     unique_ptr<IEventListener> listener, string receiver_group)
49   : impl_(new Impl(this, move(sender), move(listener), receiver_group)) {
50 }
51 Manager::~Manager() = default;
52
53 Manager::Impl::~Impl() {
54   listener_->UnRegisterObserver(parent_);
55
56   list<Bundle> dummy_list {};
57   EventInfo info(EventInfo::Unregister, util::GetAppId(), "", "");
58   sender_->Notify(info, dummy_list, DATA_PROVIDER_MASTER_ID);
59 }
60 Manager::Impl::Impl(Manager* parent,
61     unique_ptr<IEventSender> sender,
62     unique_ptr<IEventListener> listener, string receiver_group)
63   : sender_(move(sender)), listener_(move(listener)),
64     receiver_group_(receiver_group),
65     parent_(parent) {
66   LOGI("impl created");
67   listener_->RegisterObserver(parent_);
68
69   list<Bundle> serialized_list {};
70   EventInfo info(EventInfo::Register, util::GetAppId(), receiver_group, "");
71   sender_->Notify(info, serialized_list, DATA_PROVIDER_MASTER_ID);
72 }
73
74 int Manager::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
75     IEventInfo::EventType type) {
76   Bundle serialized = noti->Serialize();
77   EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
78   list<Bundle> serialized_list {serialized};
79
80   /* Reply to Sender */
81   sender_->Notify(info, serialized_list, noti->GetSenderAppId());
82   return info.GetRequestId();
83 }
84
85 void Manager::SendError(const IEventInfo& info, NotificationError error) {
86   list<Bundle> serialized_list {};
87   IEventInfo& i = const_cast<IEventInfo&>(info);
88   static_cast<IEventInfoInternal&>(i).SetError(error);
89   static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
90   impl_->sender_->Notify(info, serialized_list, info.GetOwner());
91 }
92
93 int Manager::Update(shared_ptr<item::AbstractItem> noti) {
94   return impl_->SendNotify(noti, EventInfo::Update);
95 }
96
97 int Manager::Delete(shared_ptr<item::AbstractItem> noti) {
98   return impl_->SendNotify(noti, EventInfo::Delete);
99 }
100
101 int Manager::DeleteAll() {
102   Bundle serialized;
103   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
104   list<Bundle> serialized_list {serialized};
105   impl_->sender_->Notify(info, serialized_list, util::GetAppId());
106   return info.GetRequestId();
107 }
108
109 int Manager::GetCount() const {
110   EventInfo info(EventInfo::Count, util::GetAppId(), "");
111   int num = impl_->sender_->RequestNumber(info);
112   return num;
113 }
114
115 int Manager::Hide(shared_ptr<item::AbstractItem> noti) {
116   (reinterpret_cast<IItemInfoInternal*>(noti->GetInfo().get()))->AddHideViewer(util::GetAppId());
117   return impl_->SendNotify(noti, EventInfo::Update);
118 }
119
120 unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
121   EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
122   list<Bundle> result = impl_->sender_->Request(info);
123   if (result.size() == 0) {
124     LOGE("Fail to get noti");
125     return unique_ptr<item::AbstractItem>{};
126   }
127   Bundle b = result.front();
128   unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
129   return move(gen_item);
130 }
131
132 list<unique_ptr<item::AbstractItem>> Manager::Get(string channel) {
133   EventInfo info(EventInfo::Get, util::GetAppId(), channel);
134   list<Bundle> result = impl_->sender_->Request(info);
135   list<unique_ptr<item::AbstractItem>> gen_list;
136   for (auto& i : result) {
137     unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
138     gen_list.push_back(move(gen_item));
139   }
140   return move(gen_list);
141 }
142
143 int Manager::SendEvent(const IEventInfo& info,
144     shared_ptr<item::AbstractItem> noti) {
145   Bundle serialized = noti->Serialize();
146   Bundle serialized_info = info.Serialize();
147   list<Bundle> serialized_list {serialized};
148   impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
149   return info.GetRequestId();
150 }
151
152 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
153   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
154   list<Bundle> serialized_list;
155   for (auto& i : item_list) {
156     if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
157           ->CanReceive(impl_->receiver_group_))
158       serialized_list.push_back(i->Serialize());
159   }
160   return serialized_list;
161 }
162
163 int Manager::OnRequestNumber(const IEventInfo& info) {
164   return OnRequestNumberEvent(info);
165 }
166
167 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
168   shared_ptr<AbstractItem> gen_item;
169   int type = info.GetEventType();
170   NotificationError error =
171       (static_cast<const IEventInfoInternal&>(info)).GetError();
172   if (error != ERROR_NONE) {
173     LOGE("Handling error event (%d)", error);
174     OnError(error, info.GetRequestId());
175     return;
176   }
177
178   switch (type) {
179     case EventInfo::Post: {
180       list<shared_ptr<item::AbstractItem>> added;
181       for (auto& i : serialized) {
182         gen_item = ItemInflator::Create(i);
183         if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
184               ->CanReceive(impl_->receiver_group_))
185           added.emplace_back(gen_item);
186       }
187       if (added.size() > 0)
188         OnAdd(info, added);
189       break;
190     }
191     case EventInfo::Update: {
192       for (auto& i : serialized) {
193         gen_item = ItemInflator::Create(i);
194         if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
195               ->CanReceive(impl_->receiver_group_))
196           OnUpdate(info, gen_item);
197       }
198       break;
199     }
200     case EventInfo::Delete: {
201       for (auto& i : serialized) {
202         gen_item = ItemInflator::Create(i);
203         if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
204               ->CanReceive(impl_->receiver_group_))
205           OnDelete(info, gen_item);
206       }
207       break;
208     }
209     case EventInfo::Get:
210       break;
211     case EventInfo::Error:
212       break;
213     case EventInfo::Count:
214       break;
215     case EventInfo::DeleteAll:
216       OnDelete(info, shared_ptr<AbstractItem>(new NullItem()));
217       break;
218     case EventInfo::Custom:
219       break;
220   }
221 }
222
223 void Manager::OnAdd(const IEventInfo& info,
224       list<shared_ptr<item::AbstractItem>> addedItem) {
225 }
226
227 void Manager::OnUpdate(const IEventInfo& info,
228       shared_ptr<item::AbstractItem> updatedItem) {
229 }
230
231 void Manager::OnDelete(const IEventInfo& info,
232       shared_ptr<item::AbstractItem> deletedItem) {
233 }
234
235 void Manager::OnError(NotificationError error, int requestId) {
236 }
237
238 list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
239   return list<shared_ptr<item::AbstractItem>>({});
240 }
241
242 int Manager::OnRequestNumberEvent(const IEventInfo& info) {
243   return 0;
244 }
245
246 string Manager::GetPath() {
247   return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
248 }
249
250 }  // namespace notification