Add error handling feature
[platform/core/api/notification.git] / notification-ex / reporter.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/reporter.h"
24 #include "notification-ex/reporter_implementation.h"
25 #include "notification-ex/event_info_internal.h"
26 #include "notification-ex/item_inflator.h"
27 #include "notification-ex/dbus_connection_manager.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 #define NOTIFICATION_EX_REPORTER_OBJECT_PATH "/org/tizen/notification_ex_reporter"
37
38 using namespace std;
39 using namespace notification::item;
40 namespace notification {
41
42 Reporter::Reporter(
43     unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
44   : impl_(new Impl(this, move(sender), move(listener))) {
45 }
46 Reporter::~Reporter() = default;
47
48 Reporter::Impl::~Impl() {
49   listener_->UnRegisterObserver(parent_);
50 }
51 Reporter::Impl::Impl(Reporter* parent,
52     unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
53   : sender_(move(sender)), listener_(move(listener)), parent_(parent) {
54   LOGI("impl created");
55   listener_->RegisterObserver(parent_);
56 }
57
58 int Reporter::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
59     IEventInfo::EventType type) {
60   Bundle serialized = noti->Serialize();
61   EventInfo info(type, util::GetAppId(), noti->GetChannel());
62   list<Bundle> serialized_list {serialized};
63   sender_->Notify(info, serialized_list);
64   return info.GetRequestId();
65 }
66
67 void Reporter::SendError(const IEventInfo& info, NotificationError error) {
68   list<Bundle> serialized_list {};
69   IEventInfo& i = const_cast<IEventInfo&>(info);
70   static_cast<IEventInfoInternal&>(i).SetError(error);
71   static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
72   impl_->sender_->Notify(info, serialized_list, info.GetOwner());
73 }
74
75 int Reporter::Post(std::shared_ptr<item::AbstractItem> noti) {
76   LOGI("Post noti");
77   return impl_->SendNotify(noti, EventInfo::Post);
78 }
79
80 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
81   EventInfo info(EventInfo::Post, util::GetAppId(), "");
82   list<Bundle> serialized_list;
83   for (auto& i : notiList) {
84     Bundle b = i->Serialize();
85     serialized_list.push_back(b);
86   }
87   impl_->sender_->Notify(info, serialized_list);
88   return info.GetRequestId();
89 }
90
91 int Reporter::Update(std::shared_ptr<AbstractItem> noti) {
92   return impl_->SendNotify(noti, EventInfo::Update);
93 }
94
95 int Reporter::Delete(std::shared_ptr<AbstractItem> noti) {
96   return impl_->SendNotify(noti, EventInfo::Delete);
97 }
98
99 std::shared_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
100   Bundle serialized;
101   EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
102   list<Bundle> result = impl_->sender_->Request(info);
103   if (result.size() == 0) {
104     LOGE("Fail to get noti");
105     return shared_ptr<item::AbstractItem>({});
106   }
107   Bundle b = result.front();
108   shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
109   return gen_item;
110 }
111
112 int Reporter::SendEvent(const IEventInfo& info,
113     shared_ptr<item::AbstractItem> noti) {
114   Bundle serialized = noti->Serialize();
115   list<Bundle> serialized_list {serialized};
116   impl_->sender_->Notify(info, serialized_list);
117   return info.GetRequestId();
118 }
119
120 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
121   NotificationError error =
122       (static_cast<const IEventInfoInternal&>(info)).GetError();
123   if (info.GetEventType() == EventInfo::Error) {
124     OnError(error, info.GetRequestId());
125     return;
126   }
127
128   list<shared_ptr<item::AbstractItem>> item_list;
129   for (auto& i : serialized) {
130     shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
131     item_list.emplace_back(gen_item);
132   }
133   OnEvent(info, item_list);
134 }
135
136 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
137   return list<shared_ptr<item::AbstractItem>>({});
138 }
139
140 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
141   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
142   list<Bundle> serialized_list;
143   for (auto& i : item_list) {
144     serialized_list.push_back(i->Serialize());
145   }
146   return serialized_list;
147 }
148
149 void Reporter::OnEvent(
150     const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
151 }
152
153 void Reporter::OnError(NotificationError error, int requestId) {
154 }
155
156 string Reporter::GetPath() {
157   return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
158 }
159
160 }  // namespace notification