From: hyunho Date: Thu, 7 Mar 2019 23:31:28 +0000 (+0900) Subject: Add manager implementation X-Git-Tag: submit/tizen/20190326.074206~18 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7ddc5ea5dc28108b4286beac65e4a0c72392b3c3;p=platform%2Fcore%2Fapi%2Fnotification.git Add manager implementation Change-Id: I9e9f26145752d8384f43634c25efac1324ddaa87 Signed-off-by: hyunho --- diff --git a/notification-ex/CMakeLists.txt b/notification-ex/CMakeLists.txt index cebea323..ea587cc4 100644 --- a/notification-ex/CMakeLists.txt +++ b/notification-ex/CMakeLists.txt @@ -16,6 +16,8 @@ pkg_check_modules(notification-ex REQUIRED dlog capi-appfw-app-control capi-appfw-app-common + aul + uuid ) FOREACH(flag ${notification-ex_CFLAGS}) diff --git a/notification-ex/abstract_action.cc b/notification-ex/abstract_action.cc index 7b9ec9e6..e37ee52e 100644 --- a/notification-ex/abstract_action.cc +++ b/notification-ex/abstract_action.cc @@ -55,7 +55,7 @@ int AbstractAction::GetType(Bundle b) { return std::stoi(b.GetString(ABSTRACT_ACTION_TYPE_KEY)); } -Bundle AbstractAction::Serialize() { +Bundle AbstractAction::Serialize() const { Bundle b; b.Add(ABSTRACT_ACTION_TYPE_KEY, std::to_string(GetType())); diff --git a/notification-ex/abstract_action.h b/notification-ex/abstract_action.h index cb1063dd..0e001b85 100644 --- a/notification-ex/abstract_action.h +++ b/notification-ex/abstract_action.h @@ -46,7 +46,7 @@ class EXPORT_API AbstractAction { virtual int GetType() const = 0; static int GetType(Bundle b); - virtual Bundle Serialize() = 0; + virtual Bundle Serialize() const = 0; virtual void Deserialize(Bundle b) = 0; virtual bool IsLocal() const = 0; virtual void Execute(std::shared_ptr item) = 0; diff --git a/notification-ex/abstract_item.cc b/notification-ex/abstract_item.cc index 0bdcdcca..8e0edae8 100644 --- a/notification-ex/abstract_item.cc +++ b/notification-ex/abstract_item.cc @@ -15,6 +15,7 @@ */ #include +#include #include #include @@ -23,6 +24,7 @@ #include "notification-ex/abstract_item.h" #include "notification-ex/abstract_item_implementation.h" #include "notification-ex/item_info_internal.h" +#include "notification-ex/ex_util.h" #ifdef LOG_TAG #undef LOG_TAG @@ -32,40 +34,60 @@ #define ABSTRACT_ITEM_TYPE_KEY "__ABSTRACT_ITEM_TYPE_KEY__" #define ABSTRACT_ITEM_ID_KEY "__ABSTRACT_ITEM_ID_KEY__" +#define ABSTRACT_ITEM_SENDER_APPID_KEY "__ABSTRACT_ITEM_SENDER_APPID_KEY__" using namespace std; namespace notification { namespace item { -AbstractItem::AbstractItem(std::shared_ptr action) - : impl_(new Impl(this, action)) { +string AbstractItem::Impl::GenerateItemId() { + char uuid[37]; + uuid_t u; + uuid_generate(u); + uuid_unparse(u, uuid); + string id = util::GetAppId(); + return string(uuid) + ":" + id; +} + +AbstractItem::AbstractItem( + std::shared_ptr action) + : impl_(new Impl(this, action)) { } AbstractItem::AbstractItem(std::string id, std::shared_ptr action) - : impl_(new Impl(this, id, action)) { + : impl_(new Impl(this, id, action)) { } AbstractItem::Impl::Impl(AbstractItem* parent, string id, std::shared_ptr action) - : id_(id), action_(action), parent_(parent), + : id_(id), action_(action), parent_(parent), info_(std::make_shared(this)) { - LOGI("Item created"); + + if (id_.empty()) + id_ = GenerateItemId(); + sender_appid_ = util::GetAppId(); + LOGI("GroupItem created (%s)", id_.c_str()); } AbstractItem::Impl::Impl(AbstractItem* parent, std::shared_ptr action) - : action_(action), parent_(parent), + : action_(action), parent_(parent), info_(std::make_shared(this)) { - LOGI("Item created"); + LOGI("GroupItem created"); + + id_ = GenerateItemId(); + sender_appid_ = util::GetAppId(); + LOGI("GroupItem created (%s)", id_.c_str()); } AbstractItem::~AbstractItem() = default; -Bundle AbstractItem::Serialize() { +Bundle AbstractItem::Serialize() const { Bundle b; b.Add(ABSTRACT_ITEM_TYPE_KEY, to_string(GetType())); b.Add(ABSTRACT_ITEM_ID_KEY, impl_->id_); + b.Add(ABSTRACT_ITEM_SENDER_APPID_KEY, GetSenderAppId().c_str()); return b; } @@ -76,6 +98,7 @@ void AbstractItem::Deserialize(Bundle b) { string id_str = b.GetString(ABSTRACT_ITEM_ID_KEY); impl_->id_ = id_str; + impl_->sender_appid_ = b.GetString(ABSTRACT_ITEM_SENDER_APPID_KEY); } string AbstractItem::GetId() const { @@ -125,7 +148,13 @@ void AbstractItem::RemoveReceiver(std::string receiver_group) { impl_->receiver_group_list_.remove(receiver_group); } +list AbstractItem::GetReceiverList() { + return impl_->receiver_group_list_; +} + bool AbstractItem::CanReceive(std::string id) const { + if (impl_->receiver_group_list_.size() == 0) + return true; list::iterator iter = std::find(impl_->receiver_group_list_.begin(), impl_->receiver_group_list_.end(), id); @@ -155,15 +184,42 @@ void AbstractItem::SetChannel(string channel) { void AbstractItem::SetSoundPath(std::string path) { impl_->sound_path_ = path; } + void AbstractItem::SetVibrationPath(std::string path) { impl_->vibration_path_ = path; } + std::string AbstractItem::GetSoundPath() const { return impl_->sound_path_; } + std::string AbstractItem::GetVibrationPath() const { return impl_->vibration_path_; } +std::string AbstractItem::GetSenderAppId() const { + return impl_->sender_appid_; +} + +void AbstractItem::SetSenderAppId(std::string sender_appid) { + impl_->sender_appid_ = sender_appid; +} + +time_t AbstractItem::GetTime() { + return impl_->time_; +} + +int AbstractItem::GetUid() { + return impl_->uid_; +} + +void AbstractItem::SetUid(int uid) { + impl_->uid_ = uid; +} + +int AbstractItem::GetRequestId() { + return impl_->request_id_; +} + } // namespace item } // namespace notification diff --git a/notification-ex/abstract_item.h b/notification-ex/abstract_item.h index e5b5e7f6..043f6a11 100644 --- a/notification-ex/abstract_item.h +++ b/notification-ex/abstract_item.h @@ -21,6 +21,7 @@ #include #include +#include #include "notification-ex/abstract_action.h" #include "notification-ex/ex_bundle.h" @@ -191,12 +192,12 @@ class EXPORT_API AbstractItem { }; public: - AbstractItem(std::shared_ptr action = - std::shared_ptr({})); - AbstractItem(std::string id, std::shared_ptr action = - std::shared_ptr({})); + AbstractItem( + std::shared_ptr action = std::shared_ptr({})); + AbstractItem(std::string id, + std::shared_ptr action = std::shared_ptr({})); virtual ~AbstractItem() = 0; - virtual Bundle Serialize() = 0; + virtual Bundle Serialize() const = 0; virtual void Deserialize(Bundle b) = 0; virtual AbstractItem& FindByID(std::string id) = 0; virtual int GetType() const = 0; @@ -211,6 +212,7 @@ class EXPORT_API AbstractItem { void SetEnable(bool enable); void AddReceiver(std::string receiver_group); void RemoveReceiver(std::string receiver_group); + std::list GetReceiverList(); bool CanReceive(std::string id) const; void SetPolicy(Policy policy); Policy GetPolicy() const; @@ -223,6 +225,14 @@ class EXPORT_API AbstractItem { std::string GetSoundPath() const; std::string GetVibrationPath() const; std::shared_ptr GetInfo() const; + std::string GetSenderAppId() const; + void SetSenderAppId(std::string sender_appid); + std::string GetTag(); + void SetTag(); + time_t GetTime(); + int GetUid(); + void SetUid(int uid); + int GetRequestId(); private: class Impl; diff --git a/notification-ex/abstract_item_implementation.h b/notification-ex/abstract_item_implementation.h index 4f4b9420..eaada761 100644 --- a/notification-ex/abstract_item_implementation.h +++ b/notification-ex/abstract_item_implementation.h @@ -33,14 +33,14 @@ class AbstractItem::Impl { private: Impl(AbstractItem* parent); - Impl(AbstractItem* parent, std::shared_ptr action = - std::shared_ptr({})); + Impl(AbstractItem* parent, + std::shared_ptr action = std::shared_ptr({})); Impl(AbstractItem* parent, std::string id, - std::shared_ptr action = - std::shared_ptr({})); + std::shared_ptr action = std::shared_ptr({})); private: friend class AbstractItem; + std::string GenerateItemId(); std::string channel_; std::string id_; std::shared_ptr led_ = nullptr; @@ -57,7 +57,13 @@ class AbstractItem::Impl { AbstractItem* parent_; std::string sound_path_; std::string vibration_path_; + std::string sender_appid_; std::shared_ptr info_; + std::list hide_viewer_list_; + std::string tag_; + time_t time_ = 0; + int uid_ = 0; + int request_id_ = 0; }; } // namespace item diff --git a/notification-ex/app_control_action.cc b/notification-ex/app_control_action.cc index df2ec90e..5108c112 100644 --- a/notification-ex/app_control_action.cc +++ b/notification-ex/app_control_action.cc @@ -60,7 +60,7 @@ int AppControlAction::GetType() const { return AbstractAction::AppControl; } -Bundle AppControlAction::Serialize() { +Bundle AppControlAction::Serialize() const { Bundle b; bundle* control_b = NULL; bundle_raw* control_raw; diff --git a/notification-ex/app_control_action.h b/notification-ex/app_control_action.h index 0839eb06..6d53e925 100644 --- a/notification-ex/app_control_action.h +++ b/notification-ex/app_control_action.h @@ -31,7 +31,7 @@ class EXPORT_API AppControlAction : public AbstractAction { virtual ~AppControlAction(); int GetType() const override; - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; bool IsLocal() const override; void Execute(std::shared_ptr item) override; diff --git a/notification-ex/button_item.cc b/notification-ex/button_item.cc index 284b806e..3042d35b 100644 --- a/notification-ex/button_item.cc +++ b/notification-ex/button_item.cc @@ -54,7 +54,7 @@ int ButtonItem::GetType() const { return AbstractItem::Button; } -Bundle ButtonItem::Serialize() { +Bundle ButtonItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); b.Add(BUTTON_TITLE_KEY, impl_->title_); diff --git a/notification-ex/button_item.h b/notification-ex/button_item.h index 6f459d21..6cfd30d7 100644 --- a/notification-ex/button_item.h +++ b/notification-ex/button_item.h @@ -34,7 +34,7 @@ class EXPORT_API ButtonItem : public AbstractItem { std::shared_ptr action = std::shared_ptr({})); virtual ~ButtonItem(); - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; AbstractItem& FindByID(std::string id) override; int GetType() const override; diff --git a/notification-ex/chat_message_item.cc b/notification-ex/chat_message_item.cc index 1970a872..2a94bb1e 100644 --- a/notification-ex/chat_message_item.cc +++ b/notification-ex/chat_message_item.cc @@ -53,7 +53,7 @@ int ChatMessageItem::GetType() const { return AbstractItem::ChatMessage; } -Bundle ChatMessageItem::Serialize() { +Bundle ChatMessageItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); diff --git a/notification-ex/chat_message_item.h b/notification-ex/chat_message_item.h index 1321b0bf..4ccf3138 100644 --- a/notification-ex/chat_message_item.h +++ b/notification-ex/chat_message_item.h @@ -45,7 +45,7 @@ class EXPORT_API ChatMessageItem : public AbstractItem { virtual ~ChatMessageItem(); int GetType() const override; - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; AbstractItem& FindByID(std::string id) override; TextItem& GetNameItem() const; diff --git a/notification-ex/checkbox_item.cc b/notification-ex/checkbox_item.cc index 97cba965..7fb48733 100644 --- a/notification-ex/checkbox_item.cc +++ b/notification-ex/checkbox_item.cc @@ -46,7 +46,7 @@ int CheckBoxItem::GetType() const { return AbstractItem::CheckBox; } -Bundle CheckBoxItem::Serialize() { +Bundle CheckBoxItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); diff --git a/notification-ex/checkbox_item.h b/notification-ex/checkbox_item.h index 27c9fbd2..6eab2ead 100644 --- a/notification-ex/checkbox_item.h +++ b/notification-ex/checkbox_item.h @@ -32,7 +32,7 @@ class EXPORT_API CheckBoxItem : public AbstractItem { std::shared_ptr action = std::shared_ptr({})); virtual ~CheckBoxItem(); - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; AbstractItem& FindByID(std::string id) override; int GetType() const override; diff --git a/notification-ex/common.h b/notification-ex/common.h index 8a0a67da..68e01e9a 100644 --- a/notification-ex/common.h +++ b/notification-ex/common.h @@ -19,7 +19,7 @@ namespace notification { -typedef enum _notification_error { +enum NotificationError { NOTIFICATION_ERROR_NONE = TIZEN_ERROR_NONE, /**< Success */ NOTIFICATION_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ NOTIFICATION_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ @@ -32,7 +32,7 @@ typedef enum _notification_error { NOTIFICATION_ERROR_NOT_EXIST_ID = TIZEN_ERROR_NOTIFICATION | 0x04, /**< Not exist private ID */ NOTIFICATION_ERROR_SERVICE_NOT_READY = TIZEN_ERROR_NOTIFICATION | 0x05, /**< No response from notification service */ NOTIFICATION_ERROR_MAX_EXCEEDED = TIZEN_ERROR_NOTIFICATION | 0x06, /**< Max notification count exceeded (@b Since: 3.0) */ -} notification_error_e; +}; } // namespace notification diff --git a/notification-ex/dbus_connection_manager.cc b/notification-ex/dbus_connection_manager.cc new file mode 100644 index 00000000..5c2abe84 --- /dev/null +++ b/notification-ex/dbus_connection_manager.cc @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "notification-ex/exception.h" +#include "notification-ex/dbus_connection_manager.h" +#include "notification-ex/ex_util.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "NOTIFICATION_EX" + +#define NOTIFICATION_EX_BUS_NAME_PREFIX "org.tizen.notification_ex._" +#define NOTIFICATION_EX_INTERFACE "org.tizen.notification_ex" +#define DATA_PROVIDER_MASTER_ID "data-provider-master" + +using namespace std; +namespace notification { + +DBusConnectionManager::DBusConnectionManager() = default; +DBusConnectionManager::~DBusConnectionManager() = default; +DBusConnectionManager& DBusConnectionManager::GetInst() { + static DBusConnectionManager w_inst; + int ret; + if (w_inst.connection_ == nullptr) { + ret = w_inst.Init(); + if (ret != NOTIFICATION_ERROR_NONE) + THROW(ret); + } + return w_inst; +} + +bool DBusConnectionManager::IsDataProviderMaster() const { + return is_DPM_; +} + +bool DBusConnectionManager::IsDataProviderMaster(string appid) const { + if (appid == DATA_PROVIDER_MASTER_ID) + return true; + return false; +} + +string DBusConnectionManager::GetDataProviderMasterName() const { + return GetBusName(DATA_PROVIDER_MASTER_ID); +} + +string DBusConnectionManager::GetInterfaceName() const { + return NOTIFICATION_EX_INTERFACE; +} + +GDBusConnection* DBusConnectionManager::GetConnection() { + return connection_; +} + +std::string DBusConnectionManager::GetBusName(string appid) const { + if (appid.empty()) + return ""; + + if (IsDataProviderMaster(appid)) + return string(NOTIFICATION_EX_BUS_NAME_PREFIX) + DATA_PROVIDER_MASTER_ID; + + char* encoded_str = g_compute_checksum_for_string(G_CHECKSUM_MD5, + appid.c_str(), -1); + if (encoded_str == NULL) + return ""; + + return string(NOTIFICATION_EX_BUS_NAME_PREFIX) + encoded_str; +} + +int DBusConnectionManager::Init() { + GError* error = nullptr; + connection_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); + if (connection_ == NULL) { + if (error != NULL) { + LOGE("Failed to get dbus [%s]", error->message); + g_error_free(error); + } + return NOTIFICATION_ERROR_IO_ERROR; + } + + string appid = util::GetAppId(); + if (appid.empty()) + return NOTIFICATION_ERROR_IO_ERROR; + + if (IsDataProviderMaster(appid)) + is_DPM_ = true; + + string encoded_name = GetBusName(appid); + LOGI("own name %s", encoded_name.c_str()); + int owner_id = g_bus_own_name_on_connection(connection_, encoded_name.c_str(), + G_BUS_NAME_OWNER_FLAGS_NONE, + NULL, NULL, NULL, NULL); + if (!owner_id) { + g_object_unref(connection_); + LOGE("g_bus_own_name_on_connection, error"); + return NOTIFICATION_ERROR_IO_ERROR; + } + return NOTIFICATION_ERROR_NONE; +} + +} // nampace notification \ No newline at end of file diff --git a/notification-ex/dbus_connection_manager.h b/notification-ex/dbus_connection_manager.h new file mode 100644 index 00000000..9b37f09c --- /dev/null +++ b/notification-ex/dbus_connection_manager.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_DBUS_MANAGER_H_ +#define NOTIFICATION_EX_DBUS_MANAGER_H_ + +#include + +#include + +#include "notification-ex/ex_bundle.h" + +namespace notification { + +class EXPORT_API DBusConnectionManager { + public: + static DBusConnectionManager& GetInst(); + GDBusConnection* GetConnection(); + bool IsDataProviderMaster(std::string appid) const; + std::string GetDataProviderMasterName() const; + std::string GetInterfaceName() const; + std::string GetBusName(std::string appid) const; + + private: + bool IsDataProviderMaster() const; + DBusConnectionManager(); + ~DBusConnectionManager(); + int Init(); + GDBusConnection* connection_ = nullptr; + bool is_DPM_ = false; +}; + +} // namespace notification + +#endif // NOTIFICATION_EX_DBUS_MANAGER_H_ diff --git a/notification-ex/dbus_event_listener.cc b/notification-ex/dbus_event_listener.cc new file mode 100644 index 00000000..455ce932 --- /dev/null +++ b/notification-ex/dbus_event_listener.cc @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "notification-ex/dbus_connection_manager.h" +#include "notification-ex/dbus_event_listener.h" +#include "notification-ex/dbus_event_listener_implementation.h" +#include "notification-ex/exception.h" +#include "notification-ex/ex_util.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "NOTIFICATION_EX" + +#define MAX_PACKAGE_STR_SIZE 512 + +using namespace std; +namespace notification { + +DBusEventListener::DBusEventListener(string path) + : impl_(new Impl(this, path)) { + LOGW("Created (%s)", path.c_str()); +} + +DBusEventListener::~DBusEventListener() { + LOGW("Destroyed"); +} + +DBusEventListener::Impl::~Impl() = default; + +DBusEventListener::Impl::Impl(DBusEventListener* parent, string path) + : subscribe_id_(0), registration_id_(0), path_(path), parent_(parent) { + LOGI("ButtonItem impl created"); +} + +void DBusEventListener::Impl::SignalCb(GDBusConnection* connection, + const gchar* sender_name, + const gchar* object_path, + const gchar* interface_name, + const gchar* signal_name, + GVariant* parameters, + void* user_data) { + DBusEventListener* dl = static_cast(user_data); + char* appid = nullptr; + GVariantIter *iter = nullptr; + char* event_info_raw = nullptr; + g_variant_get(parameters, "(ssa(s))", &appid, &event_info_raw, &iter); + + LOGI("signal callback!! (%s)", appid); + string sender_appid = string(appid); + string cur_appid = util::GetAppId(); + if (sender_appid == cur_appid) + return; + if ((!DBusConnectionManager::GetInst().IsDataProviderMaster(cur_appid) + && !DBusConnectionManager::GetInst().IsDataProviderMaster(sender_appid)) + || (cur_appid == sender_appid)) + return; + + LOGE("%s : %s", cur_appid.c_str(), sender_appid.c_str()); + + char* raw = nullptr; + list ret_list; + while (g_variant_iter_loop(iter, "(s)", &raw) && raw != nullptr) { + Bundle ret(raw); + ret_list.emplace_back(ret); + } + + Bundle b(event_info_raw); + EventInfo info(b); + dl->NotifyObserver(info, ret_list); +} + +void DBusEventListener::Impl::OnMethodCall( + GDBusConnection *conn, const gchar *sender, const gchar *object_path, + const gchar *iface_name, const gchar *method_name, + GVariant *parameters, GDBusMethodInvocation *invocation, + gpointer user_data) { + LOGI("method_name[%s] sender[%s]", method_name, sender); + DBusEventListener* dl = static_cast(user_data); + GVariant *reply_body = NULL; + if (g_strcmp0(method_name, "Get") == 0) { + char* appid = NULL; + char* serialized = NULL; + g_variant_get(parameters, "(&s&s)", &appid, &serialized); + + Bundle b(serialized); + EventInfo info(b); + list result = dl->NotifyObserver(info); + GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)")); + for (auto& i : result) { + g_variant_builder_add(builder, "(s)", + reinterpret_cast(i.ToRaw().first.get())); + } + reply_body = g_variant_new("(a(s))", builder); + g_variant_builder_unref(builder); + } + g_dbus_method_invocation_return_value(invocation, reply_body); +} + +int DBusEventListener::Impl::RegisterGDBusInterface() { + static const GDBusInterfaceVTable interface_vtable = { + OnMethodCall, + nullptr, + nullptr + }; + static gchar introspection_xml[] = + " " + " " + " " + " " + " " + " " + " " + " " + " "; + GError *error = NULL; + GDBusNodeInfo *introspection_data = + g_dbus_node_info_new_for_xml(introspection_xml, &error); + if (!introspection_data) { + LOGE("g_dbus_node_info_new_for_xml is failed."); + if (error != NULL) { + LOGE("g_dbus_node_info_new_for_xml err [%s]", error->message); + g_error_free(error); + } + return NOTIFICATION_ERROR_IO_ERROR; + } + + registration_id_ = g_dbus_connection_register_object( + DBusConnectionManager::GetInst().GetConnection(), + path_.c_str(), introspection_data->interfaces[0], + &interface_vtable, this, NULL, NULL); + g_dbus_node_info_unref(introspection_data); + if (registration_id_ == 0) { + LOGE("register object fail"); + return NOTIFICATION_ERROR_IO_ERROR; + } + + LOGI("RegisterGDBusInterface success"); + return NOTIFICATION_ERROR_NONE; +} + +void DBusEventListener::Impl::UnRegisterGDBusInterface() { + g_dbus_connection_unregister_object( + DBusConnectionManager::GetInst().GetConnection(), + registration_id_); +} + +bool DBusEventListener::Impl::SubscribeSignal() { + subscribe_id_ = g_dbus_connection_signal_subscribe( + DBusConnectionManager::GetInst().GetConnection(), + NULL, + DBusConnectionManager::GetInst().GetInterfaceName().c_str(), + NULL, + path_.c_str(), + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + SignalCb, + this, + NULL); + return subscribe_id_ > 0; +} + +void DBusEventListener::Impl::UnSubscribeSignal() { + g_dbus_connection_signal_unsubscribe( + DBusConnectionManager::GetInst().GetConnection(), subscribe_id_); + subscribe_id_ = 0; +} + +void DBusEventListener::RegisterObserver(IEventObserver* observer) { + if (impl_->subscribe_id_ == 0) { + impl_->SubscribeSignal(); + } else { + impl_->UnSubscribeSignal(); + impl_->SubscribeSignal(); + } + + if (impl_->registration_id_ == 0) { + impl_->RegisterGDBusInterface(); + } else { + impl_->UnRegisterGDBusInterface(); + impl_->RegisterGDBusInterface(); + } + impl_->observer_ = observer; +} + +void DBusEventListener::UnRegisterObserver(IEventObserver* observer) { + impl_->UnSubscribeSignal(); + impl_->observer_ = NULL; +} + +void DBusEventListener::NotifyObserver( + const EventInfo& info, list serialized) { + impl_->observer_->OnEvent(info, serialized); +} + +list DBusEventListener::NotifyObserver(const EventInfo& info) { + return impl_->observer_->OnRequest(info); +} + +} // nampace notification \ No newline at end of file diff --git a/notification-ex/dbus_event_listener.h b/notification-ex/dbus_event_listener.h new file mode 100644 index 00000000..bea87d7c --- /dev/null +++ b/notification-ex/dbus_event_listener.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_DBUS_LISTENER_H_ +#define NOTIFICATION_EX_DBUS_LISTENER_H_ + +#include + +#include "notification-ex/event_listener_interface.h" + +namespace notification { + +class EXPORT_API DBusEventListener : public IEventListener { + public: + DBusEventListener(std::string path); + ~DBusEventListener(); + + void RegisterObserver(IEventObserver* observer) override; + void UnRegisterObserver(IEventObserver* observer) override; + void NotifyObserver( + const EventInfo& info, std::list serialized) override; + std::list NotifyObserver(const EventInfo& info) override; + + private: + class Impl; + std::unique_ptr impl_; +}; + +} // namespace notification + +#endif // NOTIFICATION_EX_DBUS_LISTENER_H_ diff --git a/notification-ex/dbus_event_listener_implementation.h b/notification-ex/dbus_event_listener_implementation.h new file mode 100644 index 00000000..feff2a31 --- /dev/null +++ b/notification-ex/dbus_event_listener_implementation.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_DBUS_EVENT_LISTENER_IMPLEMENTATION_H_ +#define NOTIFICATION_EX_DBUS_EVENT_LISTENER_IMPLEMENTATION_H_ + +#include +#include +#include + +#include "notification-ex/dbus_event_listener.h" + +namespace notification { + +class DBusEventListener::Impl { + public: + virtual ~Impl(); + + private: + friend class DBusEventListener; + Impl(DBusEventListener* parent, std::string path); + + private: + void UnRegisterGDBusInterface(); + int RegisterGDBusInterface(); + static void OnMethodCall( + GDBusConnection *conn, const gchar *sender, const gchar *object_path, + const gchar *iface_name, const gchar *method_name, + GVariant *parameters, GDBusMethodInvocation *invocation, + gpointer user_data); + + static GDBusInterfaceVTable InterfaceVtable; + static void SignalCb(GDBusConnection* connection, + const gchar* sender_name, + const gchar* object_path, + const gchar* interface_name, + const gchar* signal_name, + GVariant* parameters, + void* user_data); + bool SubscribeSignal(); + void UnSubscribeSignal(); + IEventObserver* observer_ = nullptr; + int subscribe_id_; + int registration_id_; + std::string path_; + DBusEventListener* parent_; +}; + +} // nampace notification +#endif // NOTIFICATION_EX_DBUS_EVENT_LISTENER_IMPLEMENTATION_H_ diff --git a/notification-ex/dbus_listener.h b/notification-ex/dbus_listener.h deleted file mode 100644 index e0ad60a8..00000000 --- a/notification-ex/dbus_listener.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NOTIFICATION_EX_DBUS_LISTENER_H_ -#define NOTIFICATION_EX_DBUS_LISTENER_H_ - -#include "notification-ex/observer_interface.h" - -namespace notification { - -class EXPORT_API DBusEventsListener : public IEventListener { - public: - DBusEventsListener(); - ~DBusEventsListener(); - - void RegisterObserver(IEventObserver observer) override; - void UnRegisterObserver(IEventObserver observer) override; - void NotifyAll() override; -}; - -} // namespace notification - -#endif // NOTIFICATION_EX_DBUS_LISTENER_H_ diff --git a/notification-ex/dbus_manager.h b/notification-ex/dbus_manager.h deleted file mode 100644 index 8c1943df..00000000 --- a/notification-ex/dbus_manager.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NOTIFICATION_EX_DBUS_MANAGER_H_ -#define NOTIFICATION_EX_DBUS_MANAGER_H_ - -#include - -namespace notification { - -class DBusConnectionManager { - public: - static DBusConnectionManager& GetInst(); - GDbusConnection& GetConnection(); - - private: - DBusConnectionManager(); - ~DBusConnectionManager(); - GDbusConnection connection_; -}; - -} // namespace notification - -#endif // NOTIFICATION_EX_DBUS_MANAGER_H_ diff --git a/notification-ex/dbus_sender.cc b/notification-ex/dbus_sender.cc new file mode 100644 index 00000000..c3e9991f --- /dev/null +++ b/notification-ex/dbus_sender.cc @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include + +#include "notification-ex/dbus_connection_manager.h" +#include "notification-ex/dbus_sender.h" +#include "notification-ex/dbus_sender_implementation.h" +#include "notification-ex/event_info.h" +#include "notification-ex/ex_util.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "NOTIFICATION_EX" + +#define MAX_PACKAGE_STR_SIZE 512 + +using namespace std; +namespace notification { + +DBusSender::DBusSender(string path) + : impl_(new Impl(this, path)) { + LOGW("Created (%s)", path.c_str()); +} + +DBusSender::Impl::~Impl() = default; + +DBusSender::Impl::Impl(DBusSender* parent, string path) + : path_(path), parent_(parent) { + LOGI("ButtonItem impl created"); +} + +DBusSender::~DBusSender() { + LOGW("Destroyed"); +} + +bool DBusSender::Impl::EmitSignal(string bus_name, + string signal_name, GVariant* data) { + GError* err = NULL; + gboolean result = TRUE; + + result = g_dbus_connection_emit_signal( + DBusConnectionManager::GetInst().GetConnection(), + bus_name.empty() ? NULL : bus_name.c_str(), + path_.c_str(), + DBusConnectionManager::GetInst().GetInterfaceName().c_str(), + signal_name.c_str(), data, &err); + if (result == FALSE) { + LOGE("g_dbus_connection_emit_signal() is failed"); + if (err != NULL) { + LOGE("g_dbus_connection_emit_signal() err : %s", + err->message); + g_error_free(err); + } + } else { + LOGI("Successfully emit signal to %s, %s, %s", + bus_name.c_str(), path_.c_str(), signal_name.c_str()); + } + return result; +} + +void DBusSender::Notify(const EventInfo& info, + list serialized, string dest_appid) { + string signal_name = EventInfo::GetString(info.GetEventType()); + string appid = util::GetAppId(); + string bus_name = ""; + + GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)")); + for (auto& i : serialized) { + g_variant_builder_add(builder, "(s)", + reinterpret_cast(i.ToRaw().first.get())); + } + + GVariant* data = g_variant_new("(ssa(s))", + appid.c_str(), + reinterpret_cast(info.Serialize().ToRaw().first.get()), builder); + if (!DBusConnectionManager::GetInst().IsDataProviderMaster(appid)) { + bus_name = DBusConnectionManager::GetInst().GetDataProviderMasterName(); + impl_->EmitSignal(bus_name, signal_name, data); + } else { + bus_name = DBusConnectionManager::GetInst().GetBusName(dest_appid); + LOGE("bus name %s, %s", dest_appid.c_str(), bus_name.c_str()); + impl_->EmitSignal(bus_name, signal_name, data); + } + g_variant_builder_unref(builder); +} + +GDBusMessage* DBusSender::Impl::MethodCall(string appid, + string method_name, Bundle serialized) { + GError* err = nullptr; + GDBusMessage* msg = g_dbus_message_new_method_call( + DBusConnectionManager::GetInst().GetDataProviderMasterName().c_str(), + path_.c_str(), + DBusConnectionManager::GetInst().GetInterfaceName().c_str(), + method_name.c_str()); + if (!msg) { + LOGE("Can't allocate new method call"); + return nullptr; + } + + g_dbus_message_set_body(msg, + g_variant_new("(ss)", + appid.c_str(), + reinterpret_cast(serialized.ToRaw().first.get()) + ) + ); + + LOGI("send message !! (%s) (%s) (%s)", + path_.c_str(), method_name.c_str(), appid.c_str()); + GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync( + DBusConnectionManager::GetInst().GetConnection(), msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err); + + return reply; +} + +std::list DBusSender::Request(const EventInfo& info) { + string appid = util::GetAppId(); + string method_name = EventInfo::GetString(info.GetEventType()); + Bundle serialized = info.Serialize(); + + GDBusMessage* reply = impl_->MethodCall(appid, method_name, serialized); + GVariant *reply_body = g_dbus_message_get_body(reply); + + GVariantIter *iter = nullptr; + list ret_list; + g_variant_get(reply_body, "(a(s))", &iter); + char* raw = nullptr; + while (g_variant_iter_loop(iter, "(s)", &raw) && raw != nullptr) { + Bundle ret(raw); + ret_list.emplace_back(ret); + } + + return ret_list; +} + +} // nampace notification \ No newline at end of file diff --git a/notification-ex/dbus_sender.h b/notification-ex/dbus_sender.h index b8063be0..a347bcf4 100644 --- a/notification-ex/dbus_sender.h +++ b/notification-ex/dbus_sender.h @@ -17,7 +17,11 @@ #ifndef NOTIFICATION_EX_DBUS_SENDER_H_ #define NOTIFICATION_EX_DBUS_SENDER_H_ +#include + #include "notification-ex/ex_bundle.h" +#include "notification-ex/event_sender_interface.h" +#include "notification-ex/event_info.h" #ifndef EXPORT_API #define EXPORT_API __attribute__((visibility("default"))) @@ -25,12 +29,18 @@ namespace notification { -class EXPORT_API DBusSender : public IEventsSender { +class EXPORT_API DBusSender : public IEventSender { public: - DBusSender(); + DBusSender(std::string path); virtual ~DBusSender(); - void Notify(Bundle serialized) override; + void Notify(const EventInfo& info, std::list serialized, + std::string dest_appid = "") override; + std::list Request(const EventInfo& info) override; + + private: + class Impl; + std::unique_ptr impl_; }; } // namespace notification diff --git a/notification-ex/dbus_sender_implementation.h b/notification-ex/dbus_sender_implementation.h new file mode 100644 index 00000000..e0b61c9e --- /dev/null +++ b/notification-ex/dbus_sender_implementation.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_DBUS_SENDER_IMPLEMENTATION_H_ +#define NOTIFICATION_EX_DBUS_SENDER_IMPLEMENTATION_H_ + +#include +#include +#include + +#include "notification-ex/dbus_sender.h" + +namespace notification { + +class DBusSender::Impl { + public: + virtual ~Impl(); + + private: + friend class DBusSender; + Impl(DBusSender* parent, std::string path); + + private: + bool EmitSignal(std::string bus_name, std::string signal_name, GVariant* data); + GDBusMessage* MethodCall(std::string appid, std::string method_name, Bundle serialized); + std::string path_; + DBusSender* parent_; +}; + +} // nampace notification +#endif // NOTIFICATION_EX_DBUS_SENDER_IMPLEMENTATION_H_ \ No newline at end of file diff --git a/notification-ex/entry_item.cc b/notification-ex/entry_item.cc index 29bf5fc7..dd62de01 100644 --- a/notification-ex/entry_item.cc +++ b/notification-ex/entry_item.cc @@ -53,7 +53,7 @@ int EntryItem::GetType() const { return AbstractItem::Entry; } -Bundle EntryItem::Serialize() { +Bundle EntryItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); diff --git a/notification-ex/entry_item.h b/notification-ex/entry_item.h index d3dcf660..38a32fb4 100644 --- a/notification-ex/entry_item.h +++ b/notification-ex/entry_item.h @@ -36,7 +36,7 @@ class EXPORT_API EntryItem : public AbstractItem { std::shared_ptr action = std::shared_ptr({})); virtual ~EntryItem(); - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; AbstractItem& FindByID(std::string id) override; int GetType() const override; diff --git a/notification-ex/event_info.cc b/notification-ex/event_info.cc new file mode 100644 index 00000000..a0d9f4b9 --- /dev/null +++ b/notification-ex/event_info.cc @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +#include "notification-ex/event_info.h" +#include "notification-ex/event_info_implementation.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "NOTIFICATION_EX" +#define NOTIFICATION_EX_EVENT_TYPE_KEY "__NOTIFICATION_EX_EVENT_TYPE_KEY__" +#define NOTIFICATION_EX_EVENT_OWNER_KEY "__NOTIFICATION_EX_EVENT_OWNER_KEY__" +#define NOTIFICATION_EX_EVENT_CHANNEL_KEY "__NOTIFICATION_EX_EVENT_CHANNEL_KEY__" +#define NOTIFICATION_EX_EVENT_ITEM_ID_KEY "__NOTIFICATION_EX_EVENT_ITEM_ID_KEY__" +#define NOTIFICATION_EX_EVENT_TAG_KEY "__NOTIFICATION_EX_EVENT_TAG_KEY__" + +using namespace std; +namespace notification { + +EventInfo::EventInfo(EventInfo::EventType type, std::string owner, + std::string channel, std::string item_id, std::string tag) + : impl_(new Impl(this, type, owner, channel, item_id, tag)) { +} +EventInfo::~EventInfo() = default; +EventInfo::Impl::~Impl() = default; + +EventInfo::Impl::Impl(EventInfo* parent, EventInfo::EventType type, std::string owner, + std::string channel, std::string item_id, std::string tag) + : type_(type), owner_(owner), channel_(channel), item_id_(item_id), + tag_(tag), parent_(parent) { + LOGI("EventInfo impl created"); +} + +EventInfo::EventInfo(Bundle serialized) { + string event_str = serialized.GetString(NOTIFICATION_EX_EVENT_TYPE_KEY); + impl_->type_ = static_cast(strtol(event_str.c_str(), NULL, 10)); + impl_->owner_ = serialized.GetString(NOTIFICATION_EX_EVENT_OWNER_KEY); + impl_->channel_ = serialized.GetString(NOTIFICATION_EX_EVENT_CHANNEL_KEY); + impl_->item_id_ = serialized.GetString(NOTIFICATION_EX_EVENT_ITEM_ID_KEY); + impl_->tag_ = serialized.GetString(NOTIFICATION_EX_EVENT_TAG_KEY); +} + +string EventInfo::GetString(EventInfo::EventType type) { + switch(type) { + case Post: + return "Post"; + break; + case Update: + return "Update"; + break; + case Delete: + return "Delete"; + break; + case Get: + return "Get"; + break; + } + return ""; +} + +Bundle EventInfo::Serialize() const { + Bundle serialized; + serialized.Add(NOTIFICATION_EX_EVENT_TYPE_KEY, to_string((int)impl_->type_)); + serialized.Add(NOTIFICATION_EX_EVENT_OWNER_KEY, impl_->owner_); + serialized.Add(NOTIFICATION_EX_EVENT_CHANNEL_KEY, impl_->channel_); + serialized.Add(NOTIFICATION_EX_EVENT_ITEM_ID_KEY, impl_->item_id_); + serialized.Add(NOTIFICATION_EX_EVENT_TAG_KEY, impl_->tag_); + + return serialized; +} + +EventInfo::EventType EventInfo::GetEventType() const { + return impl_->type_; +} + +string EventInfo::GetOwner() const { + return impl_->owner_; +} + +string EventInfo::GetChannel() const { + return impl_->channel_; +} + +string EventInfo::GetItemId() const { + return impl_->item_id_; +} + +string EventInfo::GetTag() const { + return impl_->tag_; +} + +} // namespace notification diff --git a/notification-ex/event_info.h b/notification-ex/event_info.h index 55b31b87..5b998b3e 100644 --- a/notification-ex/event_info.h +++ b/notification-ex/event_info.h @@ -18,6 +18,9 @@ #define NOTIFICATION_EX_EVENT_INFO_H_ #include +#include + +#include "notification-ex/ex_bundle.h" #ifndef EXPORT_API #define EXPORT_API __attribute__((visibility("default"))) @@ -27,17 +30,30 @@ namespace notification { class EXPORT_API EventInfo { public: - EventInfo(); + enum EventType { + Post, + Update, + Delete, + Get, + }; + EventInfo(EventType type, std::string owner, + std::string channel = "", + std::string item_id = "", + std::string tag = ""); + EventInfo(Bundle serialized); ~EventInfo(); - int GetEventType() const; + static std::string GetString(EventType type); + EventType GetEventType() const; std::string GetOwner() const; - int GetChannel(); + std::string GetChannel() const; + std::string GetItemId() const; + std::string GetTag() const; + Bundle Serialize() const; private: - int type_; - std::string owner_; - int channel_; + class Impl; + std::unique_ptr impl_; }; } // namespace notification diff --git a/notification-ex/event_info_implementation.h b/notification-ex/event_info_implementation.h new file mode 100644 index 00000000..9ec2fb63 --- /dev/null +++ b/notification-ex/event_info_implementation.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_EVENT_INFO_IMPLEMENTATION_H_ +#define NOTIFICATION_EX_EVENT_INFO_IMPLEMENTATION_H_ + +#include +#include +#include + +#include "notification-ex/dbus_sender.h" + +namespace notification { + +class EventInfo::Impl { + public: + virtual ~Impl(); + + private: + friend class EventInfo; + Impl(EventInfo* parent, EventInfo::EventType type, std::string owner, + std::string channel, std::string item_id, std::string tag); + + private: + EventType type_; + std::string owner_; + std::string channel_; + std::string item_id_; + std::string tag_; + EventInfo* parent_; +}; + +} // nampace notification +#endif // NOTIFICATION_EX_EVENT_INFO_IMPLEMENTATION_H_ \ No newline at end of file diff --git a/notification-ex/event_listener_interface.h b/notification-ex/event_listener_interface.h index 3990271a..7d95807c 100644 --- a/notification-ex/event_listener_interface.h +++ b/notification-ex/event_listener_interface.h @@ -17,7 +17,8 @@ #ifndef NOTIFICATION_EX_EVENT_LISTENER_INTERFACE_H_ #define NOTIFICATION_EX_EVENT_LISTENER_INTERFACE_H_ -#include "notification-ex/observer_interface.h" +#include "notification-ex/event_observer_interface.h" +#include "notification-ex/event_info.h" #ifndef EXPORT_API #define EXPORT_API __attribute__((visibility("default"))) @@ -25,13 +26,13 @@ namespace notification { -class EXPORT_API IEventsListener { +class EXPORT_API IEventListener { public: - virtual ~IEventsListener() = 0; - - virtual void RegisterObserver(IEventObserver observer) = 0; - virtual void UnRegisterObserver(IEventObserver observer) = 0; - virtual void NotifyAll() = 0; + virtual ~IEventListener() = default; + virtual void RegisterObserver(IEventObserver* observer) = 0; + virtual void UnRegisterObserver(IEventObserver* observer) = 0; + virtual void NotifyObserver(const EventInfo& info, std::list serialized) = 0; + virtual std::list NotifyObserver(const EventInfo& info) = 0; }; } // namespace notification diff --git a/notification-ex/event_observer_interface.h b/notification-ex/event_observer_interface.h new file mode 100644 index 00000000..a94abe8e --- /dev/null +++ b/notification-ex/event_observer_interface.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_OBSERVER_INTERFACE_H_ +#define NOTIFICATION_EX_OBSERVER_INTERFACE_H_ + +#include "notification-ex/ex_bundle.h" +#include "notification-ex/event_info.h" + +#ifndef EXPORT_API +#define EXPORT_API __attribute__((visibility("default"))) +#endif + +namespace notification { + +class EXPORT_API IEventObserver { + public: + virtual ~IEventObserver() = default; + virtual void OnEvent(const EventInfo& info, std::list serialized) = 0; + virtual std::list OnRequest(const EventInfo& info) = 0; +}; + +} // namespace notification + +#endif diff --git a/notification-ex/event_sender_interface.h b/notification-ex/event_sender_interface.h index 3c42a4e8..fcefaec2 100644 --- a/notification-ex/event_sender_interface.h +++ b/notification-ex/event_sender_interface.h @@ -17,7 +17,8 @@ #ifndef NOTIFICATION_EX_EVENT_SENDER_INTERFACE_H_ #define NOTIFICATION_EX_EVENT_SENDER_INTERFACE_H_ -#include "notification-ex/bundle.h" +#include "notification-ex/ex_bundle.h" +#include "notification-ex/event_info.h" #ifndef EXPORT_API #define EXPORT_API __attribute__((visibility("default"))) @@ -25,11 +26,11 @@ namespace notification { -class EXPORT_API IEventsSender { +class EXPORT_API IEventSender { public: - virtual ~IEventsSender() = 0; - - virtual void Notify(Bundle serialized) = 0; + virtual void Notify(const EventInfo& info, std::list serialized, + std::string dest_appid = "") = 0; + virtual std::list Request(const EventInfo &info) = 0; }; } // namespace notification diff --git a/notification-ex/ex_bundle.h b/notification-ex/ex_bundle.h index 1640f00a..4eaf47fb 100644 --- a/notification-ex/ex_bundle.h +++ b/notification-ex/ex_bundle.h @@ -37,7 +37,6 @@ #endif namespace notification { -namespace item { class EXPORT_API Bundle final { public: using BundleRaw = @@ -142,6 +141,20 @@ class EXPORT_API Bundle final { return bundle_add_byte(handle_, key.c_str(), val.data(), val.size()); } + int Merge(Bundle& b) { + std::vector keys = b.GetKeys(); + for (auto& k : keys) { + int ret; + if (k.IsArray()) + ret = Add(k.GetName(), b.GetStringArray(k.GetName())); + else + ret = Add(k.GetName(), b.GetString(k.GetName())); + if (ret != 0) + return ret; + } + return 0; + } + int Remove(const std::string& key) { return bundle_del(handle_, key.c_str()); } @@ -212,7 +225,6 @@ class EXPORT_API Bundle final { bool copy_ = true; }; -} // namespace item } // namespace notification #endif // NOTIFICATION_EX_BUNDLE_H_ diff --git a/notification-ex/ex_util.cc b/notification-ex/ex_util.cc new file mode 100644 index 00000000..dca66063 --- /dev/null +++ b/notification-ex/ex_util.cc @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include + +#include "notification-ex/ex_util.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "NOTIFICATION_EX" +#define MAX_PACKAGE_STR_SIZE 512 + +using namespace std; +namespace notification { +namespace util { + GQuark GetQuarkFromString(string str) { + return g_quark_from_string(str.c_str()); + } + + std::string GetQuarkToString(GQuark quark) { + return g_quark_to_string(quark); + } + + string GetAppId() { + static string appid = ""; + char appid_buf[MAX_PACKAGE_STR_SIZE] = {0, }; + + if (!appid.empty()) { + LOGI("appid(%s)", appid.c_str()); + return appid; + } + + int pid = getpid(); + int ret = aul_app_get_appid_bypid(pid, appid_buf, sizeof(appid_buf)); + if (ret == AUL_R_OK) { + appid = string(appid_buf); + } else { + int fd, i; + int last_slash_index = 0; + char proc_buf[MAX_PACKAGE_STR_SIZE] = { 0, }; + + snprintf(proc_buf, sizeof(proc_buf), "/proc/%d/cmdline", pid); + + fd = open(proc_buf, O_RDONLY); + if (fd < 0) { + LOGE("Fail to get appid (%d)", errno); + return ""; + } + + ret = read(fd, appid_buf, sizeof(appid_buf) - 1); + if (ret <= 0) { + LOGE("Fail to get appid (%d)", errno); + close(fd); + return ""; + } + close(fd); + + for (i = 0 ; i < ret ; i++) { + if (appid_buf[i] == '/') + last_slash_index = i; + } + + if (last_slash_index == (ret - 1)) { + LOGE("Fail to get appid (%s)", appid_buf); + return ""; + } + + if (last_slash_index == 0) + appid = string(appid_buf); + else + appid = string(&appid_buf[last_slash_index + 1]); + } + + LOGI("appid(%s)", appid.c_str()); + return appid; + } +} // namespace util +} // namespace watchface_complication diff --git a/notification-ex/ex_util.h b/notification-ex/ex_util.h new file mode 100644 index 00000000..c4250d4d --- /dev/null +++ b/notification-ex/ex_util.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_EX_UTIL_H_ +#define NOTIFICATION_EX_EX_UTIL_H_ + +#include + +#include + +namespace notification { +namespace util { + std::string GetAppId(); + std::string GetAppId(); + GQuark GetQuarkFromString(std::string str); + std::string GetQuarkToString(GQuark quark); +} // namespace util +} // namespace notification + +#endif // NOTIFICATION_EX_EX_UTIL_H_ diff --git a/notification-ex/group_item.cc b/notification-ex/group_item.cc index d6a53daa..6e65f0a7 100644 --- a/notification-ex/group_item.cc +++ b/notification-ex/group_item.cc @@ -61,7 +61,7 @@ int GroupItem::GetType() const { return AbstractItem::Group; } -Bundle GroupItem::Serialize() { +Bundle GroupItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); b.Add(GROUP_DIRECTION_KEY, impl_->is_vertical_ ? diff --git a/notification-ex/group_item.h b/notification-ex/group_item.h index 51d1b651..2989c745 100644 --- a/notification-ex/group_item.h +++ b/notification-ex/group_item.h @@ -34,7 +34,7 @@ class EXPORT_API GroupItem : public AbstractItem { virtual ~GroupItem(); public: - virtual Bundle Serialize() override; + virtual Bundle Serialize() const override; virtual void Deserialize(Bundle b) override; virtual AbstractItem& FindByID(std::string id) override; int GetType() const override; diff --git a/notification-ex/icon_text_item.cc b/notification-ex/icon_text_item.cc index 1006bfae..a237ae94 100644 --- a/notification-ex/icon_text_item.cc +++ b/notification-ex/icon_text_item.cc @@ -48,7 +48,7 @@ int IconTextItem::GetType() const { return AbstractItem::IconText; } -Bundle IconTextItem::Serialize() { +Bundle IconTextItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); diff --git a/notification-ex/icon_text_item.h b/notification-ex/icon_text_item.h index 4fdc44fc..2653076a 100644 --- a/notification-ex/icon_text_item.h +++ b/notification-ex/icon_text_item.h @@ -36,7 +36,7 @@ class EXPORT_API IconTextItem : public AbstractItem { virtual ~IconTextItem(); - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; AbstractItem& FindByID(std::string id) override; int GetType() const override; diff --git a/notification-ex/iitem_info_internal.h b/notification-ex/iitem_info_internal.h index 56f667be..46c7f10d 100644 --- a/notification-ex/iitem_info_internal.h +++ b/notification-ex/iitem_info_internal.h @@ -27,6 +27,9 @@ class IItemInfoInternal : public IItemInfo{ virtual ~IItemInfoInternal() = default; virtual int GetHideTime() const = 0; virtual int GetDeleteTime() const = 0; + virtual void AddHideViewer(std::string appid) = 0; + virtual void RemoveHideViewer(std::string appid) = 0; + virtual std::list GetHideViewerList() const = 0; }; } // namespace item diff --git a/notification-ex/image_item.cc b/notification-ex/image_item.cc index c9e8701e..bce699df 100644 --- a/notification-ex/image_item.cc +++ b/notification-ex/image_item.cc @@ -50,7 +50,7 @@ int ImageItem::GetType() const { return AbstractItem::Image; } -Bundle ImageItem::Serialize() { +Bundle ImageItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); b.Add(IMAGE_PATH_KEY, impl_->imagePath_); diff --git a/notification-ex/image_item.h b/notification-ex/image_item.h index c2bad801..ea536504 100644 --- a/notification-ex/image_item.h +++ b/notification-ex/image_item.h @@ -35,7 +35,7 @@ class EXPORT_API ImageItem : public AbstractItem { virtual ~ImageItem(); int GetType() const override; - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; AbstractItem& FindByID(std::string id) override; std::string GetImagePath() const; diff --git a/notification-ex/input_selector_item.cc b/notification-ex/input_selector_item.cc index 5115552e..37867e23 100644 --- a/notification-ex/input_selector_item.cc +++ b/notification-ex/input_selector_item.cc @@ -55,7 +55,7 @@ int InputSelectorItem::GetType() const { return AbstractItem::InputSelector; } -Bundle InputSelectorItem::Serialize() { +Bundle InputSelectorItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); vector contents_vector { diff --git a/notification-ex/input_selector_item.h b/notification-ex/input_selector_item.h index 6b1220a8..f9a873a6 100644 --- a/notification-ex/input_selector_item.h +++ b/notification-ex/input_selector_item.h @@ -34,7 +34,7 @@ class EXPORT_API InputSelectorItem : public AbstractItem { virtual ~InputSelectorItem(); public: - virtual Bundle Serialize() override; + virtual Bundle Serialize() const override; virtual void Deserialize(Bundle b) override; virtual AbstractItem& FindByID(std::string id) override; int GetType() const override; diff --git a/notification-ex/item_info.cc b/notification-ex/item_info.cc index 2b456ce0..6bf60ff9 100644 --- a/notification-ex/item_info.cc +++ b/notification-ex/item_info.cc @@ -17,6 +17,7 @@ #include #include +#include #include "notification-ex/item_info_internal.h" @@ -48,5 +49,23 @@ int AbstractItem::Impl::ItemInfo::GetDeleteTime() const { return impl_->delete_time_; } +void AbstractItem::Impl::ItemInfo::AddHideViewer(std::string appid) { + if (find(impl_->hide_viewer_list_.begin(), + impl_->hide_viewer_list_.end(), appid) != + impl_->hide_viewer_list_.end()) { + return; + } + impl_->hide_viewer_list_.push_back(appid); +} + +void AbstractItem::Impl::ItemInfo::RemoveHideViewer(std::string appid) { + impl_->hide_viewer_list_.remove(appid); +} + +std::list AbstractItem::Impl::ItemInfo::GetHideViewerList() const { + return impl_->hide_viewer_list_; +} + + } // namespace item } // namespace notification diff --git a/notification-ex/item_info_internal.h b/notification-ex/item_info_internal.h index 5c9d1b1d..918513a9 100644 --- a/notification-ex/item_info_internal.h +++ b/notification-ex/item_info_internal.h @@ -31,6 +31,9 @@ class AbstractItem::Impl::ItemInfo : public IItemInfoInternal { void SetVersion(int ver) override; int GetHideTime() const override; int GetDeleteTime() const override; + void AddHideViewer(std::string appid) override; + void RemoveHideViewer(std::string appid) override; + std::list GetHideViewerList() const override; private: AbstractItem::Impl* impl_; diff --git a/notification-ex/manager.cc b/notification-ex/manager.cc new file mode 100644 index 00000000..d46b1e13 --- /dev/null +++ b/notification-ex/manager.cc @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include + +#include "notification-ex/item_inflator.h" +#include "notification-ex/manager.h" +#include "notification-ex/manager_implementation.h" +#include "notification-ex/event_info.h" +#include "notification-ex/dbus_connection_manager.h" +#include "notification-ex/ex_util.h" +#include "notification-ex/item_info_internal.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "NOTIFICATION_EX" + +#define MAX_PACKAGE_STR_SIZE 512 +#define NOTIFICATION_EX_MANAGER_OBJECT_PATH "/org/tizen/notification_ex_manager" + +using namespace std; +using namespace notification::item; +namespace notification { + +Manager::Manager(IEventSender* sender, IEventListener* listener, string receiver_group) + : impl_(new Impl(this, sender, listener, receiver_group)) { +} +Manager::~Manager() = default; + +Manager::Impl::~Impl() { + listener_->UnRegisterObserver(parent_); +} +Manager::Impl::Impl(Manager* parent, + IEventSender* sender, IEventListener* listener, string receiver_group) + : sender_(sender), listener_(listener), receiver_group_(receiver_group), + parent_(parent) { + LOGI("impl created"); + listener_->RegisterObserver(parent_); +} + +void Manager::Impl::SendNotify(shared_ptr noti, + EventInfo::EventType type) { + Bundle serialized = noti->Serialize(); + EventInfo info(type, util::GetAppId(), noti->GetChannel()); + list serialized_list {serialized}; + + /* Reply to Sender */ + sender_->Notify(info, serialized_list, noti->GetSenderAppId()); +} + +void Manager::Update(shared_ptr noti) { + impl_->SendNotify(noti, EventInfo::Update); +} + +void Manager::Remove(shared_ptr noti) { + impl_->SendNotify(noti, EventInfo::Delete); +} + +void Manager::Hide(shared_ptr noti) { + ((IItemInfoInternal*)noti->GetInfo().get())->AddHideViewer(util::GetAppId()); + impl_->SendNotify(noti, EventInfo::Update); +} + +shared_ptr Manager::FindByRootID(string id) { + EventInfo info(EventInfo::Get, + util::GetAppId(), "", id); + list result = impl_->sender_->Request(info); + if (result.size() == 0) { + LOGE("Fail to get noti"); + return shared_ptr({}); + } + Bundle b = result.front(); + shared_ptr gen_item = ItemInflator::Create(b); + return gen_item; +} + +list> Manager::Get() { + EventInfo info(EventInfo::Get, util::GetAppId(), ""); + list result = impl_->sender_->Request(info); + list> gen_list; + for (auto& i : result) { + shared_ptr gen_item = ItemInflator::Create(i); + gen_list.emplace_back(gen_item); + } + return gen_list; +} + +void Manager::SendEvent(const EventInfo& info, shared_ptr noti) { + Bundle serialized = noti->Serialize(); + Bundle serialized_info = info.Serialize(); + list serialized_list {serialized}; + impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId()); +} + +list Manager::OnRequest(const EventInfo& info) { + list> item_list = OnRequestEvent(info); + list serialized_list; + for (auto& i : item_list) { + serialized_list.push_back(i->Serialize()); + } + return serialized_list; +} + +void Manager::OnEvent(const EventInfo& info, list serialized) { + shared_ptr gen_item; + const EventInfo::EventType type = info.GetEventType(); + switch(type) { + case EventInfo::Post: + { + list> added; + for (auto& i : serialized) { + gen_item = ItemInflator::Create(i); + if (gen_item->CanReceive(impl_->receiver_group_)) + added.emplace_back(gen_item); + } + if (added.size() > 0) + OnAdd(info, added); + break; + } + case EventInfo::Update: + { + for (auto& i : serialized) { + gen_item = ItemInflator::Create(i); + if (gen_item->CanReceive(impl_->receiver_group_)) + OnUpdate(info, gen_item); + } + break; + } + case EventInfo::Delete: + for (auto& i : serialized) { + gen_item = ItemInflator::Create(i); + if (gen_item->CanReceive(impl_->receiver_group_)) + OnDelete(info, gen_item); + } + break; + case EventInfo::Get: + break; + } +} + +void Manager::OnAdd(const EventInfo& info, list> addedItem) { +} + +void Manager::OnUpdate(const EventInfo& info, shared_ptr updatedItem) { +} + +void Manager::OnDelete(const EventInfo& info, shared_ptr deletedItem) { +} + +list> Manager::OnRequestEvent(const EventInfo& info) { + return list>({}); +} + +string Manager::GetPath() { + return NOTIFICATION_EX_MANAGER_OBJECT_PATH; +} + +} // nampace notification \ No newline at end of file diff --git a/notification-ex/manager.h b/notification-ex/manager.h index 8942ba1b..56b2b348 100644 --- a/notification-ex/manager.h +++ b/notification-ex/manager.h @@ -21,6 +21,9 @@ #include #include "notification-ex/abstract_item.h" +#include "notification-ex/event_observer_interface.h" +#include "notification-ex/event_sender_interface.h" +#include "notification-ex/event_listener_interface.h" #ifndef EXPORT_API #define EXPORT_API __attribute__((visibility("default"))) @@ -30,24 +33,30 @@ namespace notification { class EXPORT_API Manager : public IEventObserver { public: - Manager(IEventsSender* sender, IEventsListener* listener); + Manager(IEventSender* sender, IEventListener* listener, std::string receiver_group = ""); virtual ~Manager(); + std::list> Get(); void Update(std::shared_ptr noti); void Remove(std::shared_ptr noti); void Hide(std::shared_ptr noti); std::shared_ptr FindByRootID(std::string id); - void SendEvent(EventInfo info, std::shared_ptr noti); - void OnEvent(EventInfo info, std::shared_ptr notiList) override; + void SendEvent(const EventInfo& info, std::shared_ptr noti); + void OnEvent(const EventInfo& info, std::list serialized) override; + std::list OnRequest(const EventInfo& info) override; + static std::string GetPath(); protected: - virtual void OnAdd(std::list> addedItem); - virtual void OnUpdate(shared_ptr updatedItem); - virtual void OnDelete(shared_ptr deletedItem); + virtual void OnAdd(const EventInfo& info, std::list> addedItem); + virtual void OnUpdate(const EventInfo& info, std::shared_ptr updatedItem); + virtual void OnDelete(const EventInfo& info, std::shared_ptr deletedItem); + virtual std::list> OnRequestEvent( + const EventInfo& info); private: - IEventsSender* sender_; - IEventsListener* receiver_; + class Impl; + std::unique_ptr impl_; + }; } // namespace notification diff --git a/notification-ex/manager_implementation.h b/notification-ex/manager_implementation.h new file mode 100644 index 00000000..9b7c79ee --- /dev/null +++ b/notification-ex/manager_implementation.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_MANAGER_IMPLEMENTATION_H_ +#define NOTIFICATION_EX_MANAGER_IMPLEMENTATION_H_ + +#include +#include +#include + +#include "notification-ex/manager.h" + +namespace notification { + +class Manager::Impl { + public: + virtual ~Impl(); + + private: + Impl(Manager* parent, + IEventSender* sender, IEventListener* listener, std::string receiver_group); + + private: + friend class Manager; + void SendNotify(std::shared_ptr noti, + EventInfo::EventType type); + IEventSender* sender_; + IEventListener* listener_; + std::string receiver_group_; + Manager* parent_; +}; + +} // namespace notification + +#endif // NOTIFICATION_EX_MANAGER_IMPLEMENTATION_H_ diff --git a/notification-ex/mock_sender.h b/notification-ex/mock_sender.h index 769c735d..509a5258 100644 --- a/notification-ex/mock_sender.h +++ b/notification-ex/mock_sender.h @@ -25,7 +25,7 @@ namespace notification { -class EXPORT_API MockSender : public IEventsSender { +class EXPORT_API MockSender : public IEventSender { public: MockSender(); virtual ~MockSender(); diff --git a/notification-ex/notification-ex.pc.in b/notification-ex/notification-ex.pc.in index a4fdc5e7..e51c660a 100644 --- a/notification-ex/notification-ex.pc.in +++ b/notification-ex/notification-ex.pc.in @@ -6,6 +6,6 @@ includedir=@INCLUDEDIR@ Name: notification-ex Description: Support development of the notification Version: @VERSION@ -Libs: -L${libdir} +Libs: -L${libdir} -lnotification-ex Cflags: -I${includedir} -cppflags: -I${includedir} \ No newline at end of file +cppflags: -I${includedir} diff --git a/notification-ex/null_item.cc b/notification-ex/null_item.cc index 819677eb..5d688d23 100644 --- a/notification-ex/null_item.cc +++ b/notification-ex/null_item.cc @@ -44,7 +44,7 @@ int NullItem::GetType() const { return AbstractItem::NullObject; } -Bundle NullItem::Serialize() { +Bundle NullItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); return b; diff --git a/notification-ex/null_item.h b/notification-ex/null_item.h index cf271642..6864c1aa 100644 --- a/notification-ex/null_item.h +++ b/notification-ex/null_item.h @@ -33,7 +33,7 @@ class EXPORT_API NullItem : public AbstractItem { std::shared_ptr action = std::shared_ptr({})); virtual ~NullItem(); int GetType() const override; - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; AbstractItem& FindByID(std::string id) override; }; diff --git a/notification-ex/observer_interface.h b/notification-ex/observer_interface.h deleted file mode 100644 index 7ea61f62..00000000 --- a/notification-ex/observer_interface.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NOTIFICATION_EX_OBSERVER_INTERFACE_H_ -#define NOTIFICATION_EX_OBSERVER_INTERFACE_H_ - -#include "notification-ex/ex_bundle.h" -#include "notification-ex/event_info.h" - -#ifndef EXPORT_API -#define EXPORT_API __attribute__((visibility("default"))) -#endif - -namespace notification { - -class EXPORT_API IEventObserver { - public: - virtual ~IEventObserver() = 0; - - virtual void OnEvent(EventInfo info, std::shared_ptr noti) = 0; -}; - -} // namespace notification - -#endif diff --git a/notification-ex/progress_item.cc b/notification-ex/progress_item.cc index fecefbc3..36dd5dfe 100644 --- a/notification-ex/progress_item.cc +++ b/notification-ex/progress_item.cc @@ -62,7 +62,7 @@ int ProgressItem::GetType() const { return AbstractItem::Progress; } -Bundle ProgressItem::Serialize() { +Bundle ProgressItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); b.Add(PROGRESS_MIN_KEY, to_string(impl_->min_)); diff --git a/notification-ex/progress_item.h b/notification-ex/progress_item.h index d3d4b9e2..04fa3a7d 100644 --- a/notification-ex/progress_item.h +++ b/notification-ex/progress_item.h @@ -35,7 +35,7 @@ class EXPORT_API ProgressItem : public AbstractItem { virtual ~ProgressItem(); public: - virtual Bundle Serialize() override; + virtual Bundle Serialize() const override; virtual void Deserialize(Bundle b) override; virtual AbstractItem& FindByID(std::string id) override; int GetType() const override; diff --git a/notification-ex/reporter.cc b/notification-ex/reporter.cc new file mode 100644 index 00000000..893b6150 --- /dev/null +++ b/notification-ex/reporter.cc @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include + +#include "notification-ex/reporter.h" +#include "notification-ex/reporter_implementation.h" +#include "notification-ex/event_info.h" +#include "notification-ex/item_inflator.h" +#include "notification-ex/dbus_connection_manager.h" +#include "notification-ex/ex_util.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "NOTIFICATION_EX" + +#define MAX_PACKAGE_STR_SIZE 512 +#define NOTIFICATION_EX_REPORTER_OBJECT_PATH "/org/tizen/notification_ex_reporter" + +using namespace std; +using namespace notification::item; +namespace notification { + +Reporter::Reporter(IEventSender* sender, IEventListener* listener) + : impl_(new Impl(this, sender, listener)) { +} +Reporter::~Reporter() = default; + +Reporter::Impl::~Impl() { + listener_->UnRegisterObserver(parent_); +} +Reporter::Impl::Impl(Reporter* parent, + IEventSender* sender, IEventListener* listener) + : sender_(sender), listener_(listener), parent_(parent) { + LOGI("impl created"); + listener_->RegisterObserver(parent_); +} + +void Reporter::Impl::SendNotify(shared_ptr noti, + EventInfo::EventType type) { + Bundle serialized = noti->Serialize(); + EventInfo info(type, util::GetAppId(), noti->GetChannel()); + list serialized_list {serialized}; + sender_->Notify(info, serialized_list); +} + +void Reporter::Post(std::shared_ptr noti) { + LOGI("Post noti"); + impl_->SendNotify(noti, EventInfo::Post); +} + +void Reporter::Post(std::list> notiList) { + EventInfo info(EventInfo::Post, util::GetAppId(), ""); + list serialized_list; + for (auto& i : notiList) { + Bundle b = i->Serialize(); + serialized_list.push_back(b); + } + impl_->sender_->Notify(info, serialized_list); +} + +void Reporter::Update(std::shared_ptr noti) { + impl_->SendNotify(noti, EventInfo::Update); +} + +void Reporter::Remove(std::shared_ptr noti) { + impl_->SendNotify(noti, EventInfo::Delete); +} + +std::shared_ptr Reporter::FindByRootID(std::string id) { + Bundle serialized; + EventInfo info(EventInfo::Get, + util::GetAppId(), "", id); + list result = impl_->sender_->Request(info); + if (result.size() == 0) { + LOGE("Fail to get noti"); + return shared_ptr({}); + } + Bundle b = result.front(); + shared_ptr gen_item = ItemInflator::Create(b); + return gen_item; +} + +void Reporter::SendEvent(const EventInfo& info, shared_ptr noti) { + Bundle serialized = noti->Serialize(); + list serialized_list {serialized}; + impl_->sender_->Notify(info, serialized_list); +} + +void Reporter::OnEvent(const EventInfo& info, list serialized) { + list> item_list; + for (auto& i : serialized) { + shared_ptr gen_item = ItemInflator::Create(i); + item_list.emplace_back(gen_item); + } + OnEvent(info, item_list); +} + +list> Reporter::OnRequestEvent(const EventInfo& info) { + return list>({}); +} + +list Reporter::OnRequest(const EventInfo& info) { + list> item_list = OnRequestEvent(info); + list serialized_list; + for (auto& i : item_list) { + serialized_list.push_back(i->Serialize()); + } + return serialized_list; +} + +void Reporter::OnEvent( + const EventInfo& info, list> notiList) { +} + +string Reporter::GetPath() { + return NOTIFICATION_EX_REPORTER_OBJECT_PATH; +} + +} // nampace notification \ No newline at end of file diff --git a/notification-ex/reporter.h b/notification-ex/reporter.h new file mode 100644 index 00000000..61aa4578 --- /dev/null +++ b/notification-ex/reporter.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_REPOTER_H_ +#define NOTIFICATION_EX_REPOTER_H_ + +#include +#include + +#include "notification-ex/event_info.h" +#include "notification-ex/event_observer_interface.h" +#include "notification-ex/event_sender_interface.h" +#include "notification-ex/event_listener_interface.h" +#include "notification-ex/abstract_item.h" + +#ifndef EXPORT_API +#define EXPORT_API __attribute__((visibility("default"))) +#endif + +namespace notification { + +class EXPORT_API Reporter : public IEventObserver { + public: + Reporter(IEventSender* sender, IEventListener* listener); + virtual ~Reporter(); + + void SendEvent(const EventInfo& info, std::shared_ptr noti); + void Post(std::shared_ptr noti); + void Post(std::list> notiList); + void Update(std::shared_ptr noti); + void Remove(std::shared_ptr noti); + std::shared_ptr FindByRootID(std::string id); + virtual void OnEvent(const EventInfo& info, + std::list> notiList); + virtual std::list> OnRequestEvent( + const EventInfo& info); + void OnEvent(const EventInfo& info, std::list serialized) override; + std::list OnRequest(const EventInfo& info) override; + static std::string GetPath(); + + private: + class Impl; + std::unique_ptr impl_; +}; + +} // namespace notification + +#endif // NOTIFICATION_EX_REPOTER_H_ diff --git a/notification-ex/reporter_implementation.h b/notification-ex/reporter_implementation.h new file mode 100644 index 00000000..378e5079 --- /dev/null +++ b/notification-ex/reporter_implementation.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_EX_REPORTER_IMPLEMENTATION_H_ +#define NOTIFICATION_EX_REPORTER_IMPLEMENTATION_H_ + +#include +#include +#include + +#include "notification-ex/reporter.h" + +namespace notification { + +class Reporter::Impl { + public: + virtual ~Impl(); + + private: + Impl(Reporter* parent, + IEventSender* sender, IEventListener* listener); + + private: + friend class Reporter; + void SendNotify(std::shared_ptr noti, + EventInfo::EventType type); + IEventSender* sender_; + IEventListener* listener_; + Reporter* parent_; +}; + +} // namespace notification + +#endif // NOTIFICATION_EX_REPORTER_IMPLEMENTATION_H_ diff --git a/notification-ex/repoter.h b/notification-ex/repoter.h deleted file mode 100644 index 90cbf6f2..00000000 --- a/notification-ex/repoter.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NOTIFICATION_EX_REPOTER_H_ -#define NOTIFICATION_EX_REPOTER_H_ - -#include -#include - -#include "notification-ex/abstract_item.h" -#include "notification-ex/event_info.h" - -#ifndef EXPORT_API -#define EXPORT_API __attribute__((visibility("default"))) -#endif - -namespace notification { - -class EXPORT_API Repoter : public IEventObserver { - public: - Repoter(IEventsSender* sender, IEventsListener* listener); - virtual ~Repoter(); - - void Post(std::shared_ptr noti); - void Post(std::list> notiList); - void Update(std::shared_ptr noti); - void Remove(std::shared_ptr noti); - std::shared_ptr FindByRootID(std::string id); - void OnEvent(EventInfo info, std::shared_ptr notiList) override; - - private: - IEventsSender* sender_; - IEventsListener* receiver_; -}; - -} // namespace notification - -#endif // NOTIFICATION_EX_REPOTER_H_ diff --git a/notification-ex/text_item.cc b/notification-ex/text_item.cc index f9543ac6..f5d5ed28 100644 --- a/notification-ex/text_item.cc +++ b/notification-ex/text_item.cc @@ -49,7 +49,7 @@ int TextItem::GetType() const { return AbstractItem::Text; } -Bundle TextItem::Serialize() { +Bundle TextItem::Serialize() const { Bundle b; b = AbstractItem::Serialize(); diff --git a/notification-ex/text_item.h b/notification-ex/text_item.h index b7487fac..9f9a987e 100644 --- a/notification-ex/text_item.h +++ b/notification-ex/text_item.h @@ -36,7 +36,7 @@ class EXPORT_API TextItem : public AbstractItem { std::shared_ptr action = std::shared_ptr({})); virtual ~TextItem(); int GetType() const override; - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; AbstractItem& FindByID(std::string id) override; void SetContents(std::string contents); diff --git a/notification-ex/time_item.cc b/notification-ex/time_item.cc index fc7fbf9c..0816373b 100644 --- a/notification-ex/time_item.cc +++ b/notification-ex/time_item.cc @@ -57,7 +57,7 @@ int TimeItem::GetType() const { return AbstractItem::Time; } -Bundle TimeItem::Serialize() { +Bundle TimeItem::Serialize() const { Bundle b; struct tm* timeinfo; char buf[80] = {0,}; diff --git a/notification-ex/time_item.h b/notification-ex/time_item.h index 388a9b4f..29a8d560 100644 --- a/notification-ex/time_item.h +++ b/notification-ex/time_item.h @@ -39,7 +39,7 @@ class EXPORT_API TimeItem : public AbstractItem { virtual ~TimeItem(); int GetType() const override; - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; AbstractItem& FindByID(std::string id) override; time_t GetTime() const; diff --git a/notification-ex/visibility_action.cc b/notification-ex/visibility_action.cc index 3ff4dfe6..1ec90b57 100644 --- a/notification-ex/visibility_action.cc +++ b/notification-ex/visibility_action.cc @@ -49,7 +49,7 @@ int VisibilityAction::GetType() const { return AbstractAction::Visibility; } -Bundle VisibilityAction::Serialize() { +Bundle VisibilityAction::Serialize() const { char buf_key[128]; char buf_data[128]; Bundle b; diff --git a/notification-ex/visibility_action.h b/notification-ex/visibility_action.h index c4e7b7a8..ebc543a1 100644 --- a/notification-ex/visibility_action.h +++ b/notification-ex/visibility_action.h @@ -29,7 +29,7 @@ class EXPORT_API VisibilityAction : public AbstractAction { virtual ~VisibilityAction(); int GetType() const override; - Bundle Serialize() override; + Bundle Serialize() const override; void Deserialize(Bundle b) override; bool IsLocal() const override; void Execute(std::shared_ptr item) override; diff --git a/packaging/notification.spec b/packaging/notification.spec index 52c67d46..6aef59aa 100644 --- a/packaging/notification.spec +++ b/packaging/notification.spec @@ -22,6 +22,7 @@ BuildRequires: pkgconfig(iniparser) BuildRequires: pkgconfig(security-manager) BuildRequires: pkgconfig(libsmack) BuildRequires: pkgconfig(gmock) +BuildRequires: pkgconfig(uuid) BuildRequires: cmake Requires(post): /sbin/ldconfig diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index d56e3bd0..5af53a69 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -6,6 +6,8 @@ pkg_check_modules(notification-ex_unittests REQUIRED dlog gmock capi-appfw-app-control + glib-2.0 + aul ) FOREACH(flag ${notification-ex_unittests_CFLAGS}) diff --git a/unittest/src/test_button_item.cc b/unittest/src/test_button_item.cc index 33208320..7e28128b 100644 --- a/unittest/src/test_button_item.cc +++ b/unittest/src/test_button_item.cc @@ -7,6 +7,7 @@ #include "notification-ex/button_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; using namespace std; diff --git a/unittest/src/test_chat_message_item.cc b/unittest/src/test_chat_message_item.cc index 20a86cfb..086e517c 100644 --- a/unittest/src/test_chat_message_item.cc +++ b/unittest/src/test_chat_message_item.cc @@ -20,6 +20,7 @@ #include "notification-ex/chat_message_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; class ChatMessageItemTest : public ::testing::Test { diff --git a/unittest/src/test_checkbox_item.cc b/unittest/src/test_checkbox_item.cc index 8aa0e6b3..cca8c4d5 100644 --- a/unittest/src/test_checkbox_item.cc +++ b/unittest/src/test_checkbox_item.cc @@ -20,6 +20,7 @@ #include "notification-ex/checkbox_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; class CheckBoxItemTest : public ::testing::Test { diff --git a/unittest/src/test_entry_item.cc b/unittest/src/test_entry_item.cc index da933d33..8bc9d4ed 100644 --- a/unittest/src/test_entry_item.cc +++ b/unittest/src/test_entry_item.cc @@ -20,6 +20,7 @@ #include "notification-ex/entry_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; namespace { diff --git a/unittest/src/test_group_item.cc b/unittest/src/test_group_item.cc index 6288c8df..fb4b63bd 100644 --- a/unittest/src/test_group_item.cc +++ b/unittest/src/test_group_item.cc @@ -9,6 +9,7 @@ #include "notification-ex/item_inflator.h" #include "unittest/mock/app_common.h" +using namespace notification; using namespace notification::item; using namespace std; diff --git a/unittest/src/test_icon_item.cc b/unittest/src/test_icon_item.cc index cd2fdbfa..23704672 100644 --- a/unittest/src/test_icon_item.cc +++ b/unittest/src/test_icon_item.cc @@ -20,6 +20,7 @@ #include "notification-ex/icon_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; class IconItemTest : public ::testing::Test { diff --git a/unittest/src/test_icon_text_item.cc b/unittest/src/test_icon_text_item.cc index 423d0522..20451e2f 100644 --- a/unittest/src/test_icon_text_item.cc +++ b/unittest/src/test_icon_text_item.cc @@ -20,6 +20,7 @@ #include "notification-ex/icon_text_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; class IconTextItemTest : public ::testing::Test { diff --git a/unittest/src/test_image_item.cc b/unittest/src/test_image_item.cc index 5aa45e2d..a12e58c4 100644 --- a/unittest/src/test_image_item.cc +++ b/unittest/src/test_image_item.cc @@ -20,6 +20,7 @@ #include "notification-ex/image_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; class ImageItemTest : public ::testing::Test { diff --git a/unittest/src/test_input_selector_item.cc b/unittest/src/test_input_selector_item.cc index d2db27d9..7a02da8c 100644 --- a/unittest/src/test_input_selector_item.cc +++ b/unittest/src/test_input_selector_item.cc @@ -7,6 +7,7 @@ #include "notification-ex/input_selector_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; using namespace std; diff --git a/unittest/src/test_progress_item.cc b/unittest/src/test_progress_item.cc index d62aab38..a592e448 100644 --- a/unittest/src/test_progress_item.cc +++ b/unittest/src/test_progress_item.cc @@ -7,6 +7,7 @@ #include "notification-ex/progress_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; using namespace std; diff --git a/unittest/src/test_text_item.cc b/unittest/src/test_text_item.cc index 49ea0fc4..5224dfa5 100644 --- a/unittest/src/test_text_item.cc +++ b/unittest/src/test_text_item.cc @@ -20,6 +20,7 @@ #include "notification-ex/text_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; namespace { diff --git a/unittest/src/test_time_item.cc b/unittest/src/test_time_item.cc index f595277a..fd7762df 100644 --- a/unittest/src/test_time_item.cc +++ b/unittest/src/test_time_item.cc @@ -20,6 +20,7 @@ #include "notification-ex/time_item.h" #include "notification-ex/item_inflator.h" +using namespace notification; using namespace notification::item; class TimeItemTest : public ::testing::Test {