From e43a4a4c1d8a40ec6b0a1414780f282417a23746 Mon Sep 17 00:00:00 2001
From: Piotr Kosko
Date: Thu, 25 Aug 2016 11:33:31 +0200
Subject: [PATCH] [Push] refactored module for using multiple managers
[Feature] This commit is the first step for applying new policy
about push module. There would be used different behaviour, dependant
to current version of application. Currently module was refactored to
hold tizen 2.4 behaviour. Tizen 3.0 would be implemented in next commits.
[Verification] Code compiles without errors.
Passrate didn't change.
Change-Id: If26e03352db134b2a5fb5b1a8def46343732c41b
Signed-off-by: Piotr Kosko
---
src/push/push.gyp | 4 +-
src/push/push_extension.cc | 5 -
src/push/push_extension.h | 2 -
src/push/push_instance.cc | 21 ++--
src/push/push_instance.h | 1 +
src/push/push_manager.cc | 189 +++++++++-----------------------
src/push/push_manager.h | 17 +--
src/push/push_manager_common.cc | 143 ++++++++++++++++++++++++
src/push/push_manager_common.h | 70 ++++++++++++
9 files changed, 279 insertions(+), 173 deletions(-)
create mode 100644 src/push/push_manager_common.cc
create mode 100644 src/push/push_manager_common.h
diff --git a/src/push/push.gyp b/src/push/push.gyp
index 317c0a69..5cdb2ad1 100644
--- a/src/push/push.gyp
+++ b/src/push/push.gyp
@@ -16,7 +16,9 @@
'push_instance.cc',
'push_instance.h',
'push_manager.cc',
- 'push_manager.h'
+ 'push_manager.h',
+ 'push_manager_common.cc',
+ 'push_manager_common.h'
],
'includes': [
'../common/pkg-config.gypi',
diff --git a/src/push/push_extension.cc b/src/push/push_extension.cc
index e2028e2f..e231f0b3 100644
--- a/src/push/push_extension.cc
+++ b/src/push/push_extension.cc
@@ -30,11 +30,6 @@ PushExtension::PushExtension() {
PushExtension::~PushExtension() {}
-PushManager& PushExtension::manager() {
- // Initialize API on first request
- return PushManager::getInstance();
-}
-
common::Instance* PushExtension::CreateInstance() {
return new PushInstance;
}
diff --git a/src/push/push_extension.h b/src/push/push_extension.h
index 4a86d6a7..4118bb22 100644
--- a/src/push/push_extension.h
+++ b/src/push/push_extension.h
@@ -28,8 +28,6 @@ class PushExtension : public common::Extension {
PushExtension();
virtual ~PushExtension();
- PushManager& manager();
-
private:
virtual common::Instance* CreateInstance();
};
diff --git a/src/push/push_instance.cc b/src/push/push_instance.cc
index c588132a..cb347f81 100644
--- a/src/push/push_instance.cc
+++ b/src/push/push_instance.cc
@@ -54,7 +54,8 @@ PushInstance::PushInstance(): m_ignoreNotificationEvents(true) {
std::bind(&PushInstance::getUnreadNotifications, this, _1, _2));
REGISTER_SYNC("Push_getPushMessage",
std::bind(&PushInstance::getPushMessage, this, _1, _2));
- PushManager::getInstance().setListener(this);
+ impl = new PushManager();
+ impl->setListener(this);
#undef REGISTER_ASYNC
#undef REGISTER_SYNC
@@ -65,7 +66,7 @@ void PushInstance::registerApplication(const picojson::value& args,
LoggerD("Enter");
CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
- common::PlatformResult result = PushManager::getInstance().registerApplication(
+ common::PlatformResult result = impl->registerApplication(
args.get("callbackId").get());
if (result.IsError()) {
LogAndReportError(result, &out, ("Error occured"));
@@ -81,8 +82,8 @@ void PushInstance::unregisterApplication(const picojson::value& args,
CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
- common::PlatformResult result = PushManager::getInstance()
- .unregisterApplication(args.get("callbackId").get());
+ common::PlatformResult result = impl->unregisterApplication(
+ args.get("callbackId").get());
if (result.IsError()) {
LogAndReportError(result, &out, ("Error occured"));
} else {
@@ -120,8 +121,7 @@ void PushInstance::getRegistrationId(const picojson::value& args,
CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
std::string id;
- common::PlatformResult result = PushManager::getInstance()
- .getRegistrationId(id);
+ common::PlatformResult result = impl->getRegistrationId(id);
if (result.IsError()) {
// this method should fail silently and return null
picojson::value res = picojson::value();
@@ -138,8 +138,7 @@ void PushInstance::getUnreadNotifications(const picojson::value& args,
CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
- common::PlatformResult result = PushManager::getInstance()
- .getUnreadNotifications();
+ common::PlatformResult result = impl->getUnreadNotifications();
if (result.IsError()) {
LogAndReportError(result, &out, ("Error occured"));
} else {
@@ -155,7 +154,7 @@ void PushInstance::getPushMessage(const picojson::value& args,
CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
picojson::value msg;
- common::PlatformResult result = PushManager::getInstance().getPushMessage(&msg);
+ common::PlatformResult result = impl->getPushMessage(&msg);
if (result.IsError()) {
LoggerE("Error occurred");
@@ -186,7 +185,7 @@ void PushInstance::onPushNotify(push_service_notification_h noti) {
}
picojson::value::object dict;
picojson::value::object pushMessage;
- PushManager::getInstance().notificationToJson(noti, &pushMessage);
+ impl->notificationToJson(noti, &pushMessage);
dict["listenerId"] = picojson::value("Push_Notification_Listener");
dict["pushMessage"] = picojson::value(pushMessage);
@@ -208,7 +207,7 @@ void PushInstance::onDeregister(double callbackId,
PushInstance::~PushInstance() {
LoggerD("Enter");
- PushManager::getInstance().setListener(nullptr);
+ impl->setListener(nullptr);
}
} // namespace push
diff --git a/src/push/push_instance.h b/src/push/push_instance.h
index 90fb8296..b2d03adc 100644
--- a/src/push/push_instance.h
+++ b/src/push/push_instance.h
@@ -43,6 +43,7 @@ class PushInstance: public common::ParsedInstance, public EventListener {
void getPushMessage(const picojson::value& args, picojson::object& out);
bool m_ignoreNotificationEvents;
+ IPushManager* impl;
};
} // namespace push
diff --git a/src/push/push_manager.cc b/src/push/push_manager.cc
index 9b6711c4..f3e0aab7 100644
--- a/src/push/push_manager.cc
+++ b/src/push/push_manager.cc
@@ -21,7 +21,9 @@
#include
#include
#include
+#include
+#include "push/push_manager_common.h"
#include "common/extension.h"
#include "common/logger.h"
@@ -31,33 +33,6 @@ namespace push {
using common::PlatformResult;
using common::ErrorCode;
-namespace {
-
-ErrorCode ConvertPushError(int e) {
- ErrorCode error;
-
- switch(e) {
- case PUSH_SERVICE_ERROR_NONE:
- error = ErrorCode::NO_ERROR;
- break;
-
- case PUSH_SERVICE_ERROR_INVALID_PARAMETER:
- error = ErrorCode::INVALID_VALUES_ERR;
- break;
-
- case PUSH_SERVICE_ERROR_OUT_OF_MEMORY:
- case PUSH_SERVICE_ERROR_NOT_CONNECTED:
- case PUSH_SERVICE_ERROR_OPERATION_FAILED:
- default:
- error = ErrorCode::ABORT_ERR;
- break;
- }
-
- return error;
-}
-
-} // namespace
-
PushManager::PushManager() :
m_handle(NULL),
m_listener(NULL),
@@ -69,7 +44,7 @@ PushManager::PushManager() :
initAppId();
InitAppControl();
- int ret = push_service_connect(m_pkgId.c_str(), onPushState, onPushNotify, NULL,
+ int ret = push_service_connect(m_pkgId.c_str(), onPushState, onPushNotify, this,
&m_handle);
if (ret != PUSH_SERVICE_ERROR_NONE) {
LoggerE("Failed to connect to push (%d)", ret);
@@ -156,40 +131,36 @@ void PushManager::InitAppControl() {
}
}
-PushManager& PushManager::getInstance() {
- static PushManager instance;
- return instance;
-}
-
PlatformResult PushManager::registerApplication(double callbackId) {
LoggerD("Enter");
- double* pcallback = new double(callbackId);
+ PushManagerHolder* holder = new PushManagerHolder{this, callbackId};
- int ret = push_service_register(m_handle, onApplicationRegister, static_cast(pcallback));
+ int ret = push_service_register(m_handle, onApplicationRegister, static_cast(holder));
if (ret != PUSH_SERVICE_ERROR_NONE) {
- delete pcallback;
+ delete holder;
- return LogAndCreateResult(
- ConvertPushError(ret), "Failed to register", ("push_service_register failed (%d)", ret));
+ return LogAndCreateResult(PushManagerCommon::ConvertPushError(ret),
+ "Failed to register", ("push_service_register failed (%d)", ret));
}
return common::PlatformResult(ErrorCode::NO_ERROR);
}
common::PlatformResult PushManager::unregisterApplication(double callbackId) {
LoggerD("Enter");
- double* pcallbackId = new double(callbackId);
+ PushManagerHolder* holder = new PushManagerHolder{this, callbackId};
+
if (m_state == PUSH_SERVICE_STATE_UNREGISTERED) {
LoggerD("Already unregister, call unregister callback");
- if (!g_idle_add(onFakeDeregister, pcallbackId)) {
- delete pcallbackId;
+ if (!g_idle_add(onFakeDeregister, holder)) {
+ delete holder;
return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
"Unknown error", ("g_idle_add failed"));
}
} else {
- int ret = push_service_deregister(m_handle, onDeregister, pcallbackId);
+ int ret = push_service_deregister(m_handle, onDeregister, holder);
if (ret != PUSH_SERVICE_ERROR_NONE) {
- delete pcallbackId;
+ delete holder;
if (ret == PUSH_SERVICE_ERROR_INVALID_PARAMETER) {
LoggerE("[push_service_deregister] PUSH_SERVICE_ERROR_INVALID_PARAMETER");
} else if (ret == PUSH_SERVICE_ERROR_OUT_OF_MEMORY) {
@@ -253,7 +224,7 @@ PlatformResult PushManager::getPushMessage(picojson::value* out) {
return common::PlatformResult(ErrorCode::NO_ERROR);
} else {
return LogAndCreateResult(
- ConvertPushError(ret), "Failed to get message",
+ PushManagerCommon::ConvertPushError(ret), "Failed to get message",
("push_service_app_control_to_notification failed: (%d)", ret));
}
}
@@ -267,113 +238,47 @@ PlatformResult PushManager::getPushMessage(picojson::value* out) {
return common::PlatformResult(ErrorCode::NO_ERROR);
}
-void PushManager::notificationToJson(push_service_notification_h noti, picojson::object* obj) {
- LoggerD("Enter");
-
- char* temp = nullptr;
- int ret = push_service_get_notification_data(noti, &temp);
- if (ret != PUSH_SERVICE_ERROR_NONE) {
- LoggerE("Failed to get appData");
- return;
- }
- (*obj)["appData"] = picojson::value(temp);
- free(temp);
-
- char* fullMessage = nullptr;
- ret = push_service_get_notification_message(noti, &fullMessage);
- if (ret != PUSH_SERVICE_ERROR_NONE) {
- LoggerE("Failed to get message");
- return;
- }
- (*obj)["message"] = picojson::value(fullMessage);
-
- // parse query string and find value for alertMessage
- pcrecpp::StringPiece input(fullMessage);
- pcrecpp::RE re("([^=]+)=([^&]*)&?");
- string key;
- string value;
- while (re.Consume(&input, &key, &value)) {
- if (key == "alertMessage") {
- (*obj)["alertMessage"] = picojson::value(key);
- break;
- }
- }
- free(fullMessage);
-
- long long int date = -1;
- ret = push_service_get_notification_time(noti, &date);
- if (ret != PUSH_SERVICE_ERROR_NONE) {
- LoggerE("Failed to get date");
- return;
- }
- (*obj)["date"] = picojson::value(static_cast(date));
-
- ret = push_service_get_notification_sender(noti, &temp);
- if (ret != PUSH_SERVICE_ERROR_NONE) {
- LoggerE("Failed to get sender");
- return;
- }
- (*obj)["sender"] = picojson::value(temp);
- free(temp);
-
- ret = push_service_get_notification_session_info(noti, &temp);
- if (ret != PUSH_SERVICE_ERROR_NONE) {
- LoggerE("Failed to get session info");
- return;
- }
- std::string session_info = temp;
- (*obj)["sesionInfo"] = picojson::value(temp);
- free(temp);
-
- ret = push_service_get_notification_request_id(noti, &temp);
- if (ret != PUSH_SERVICE_ERROR_NONE) {
- LoggerE("Failed to get request id");
- return;
- }
- (*obj)["requestId"] = picojson::value(temp);
- free(temp);
-
- int type;
- ret = push_service_get_notification_type(noti, &type);
- if (ret != PUSH_SERVICE_ERROR_NONE) {
- LoggerE("Failed to get type");
- return;
- }
- (*obj)["type"] = picojson::value(static_cast(type));
-}
-
void PushManager::onPushState(push_service_state_e state, const char* err,
void* user_data) {
LoggerD("Enter %d, err: %s", state, err);
- getInstance().m_state = state;
+ PushManager* impl = static_cast(user_data);
+ if (nullptr != impl) {
+ impl->m_state = state;
+ }
}
void PushManager::onPushNotify(push_service_notification_h noti, void* user_data) {
LoggerD("Enter");
- if (!getInstance().m_listener) {
+ PushManager* impl = static_cast(user_data);
+ if (nullptr == impl || !impl->m_listener) {
LoggerW("Listener not set, ignoring");
return;
}
- getInstance().m_listener->onPushNotify(noti);
+ impl->m_listener->onPushNotify(noti);
}
void PushManager::onApplicationRegister(push_service_result_e result, const char* msg, void* user_data) {
LoggerD("Enter");
- if (!getInstance().m_listener) {
+ PushManagerHolder* holder = static_cast(user_data);
+ // automatically releases memory
+ std::unique_ptr holder_ptr(holder);
+ PushManager* impl = dynamic_cast(holder->impl);
+ double callbackId = holder->callbackId;
+
+ if (nullptr == impl || !impl->m_listener) {
LoggerW("Listener not set, ignoring");
return;
}
- double* callbackId = static_cast(user_data);
std::string id;
PlatformResult res(ErrorCode::NO_ERROR);
if (PUSH_SERVICE_RESULT_SUCCESS == result) {
LoggerD("Success");
char *temp = nullptr;
- int ret = push_service_get_registration_id(getInstance().m_handle, &temp);
+ int ret = push_service_get_registration_id(impl->m_handle, &temp);
if (PUSH_SERVICE_ERROR_NONE == ret) {
LoggerD("Registration id retrieved");
id = temp;
@@ -395,41 +300,45 @@ void PushManager::onApplicationRegister(push_service_result_e result, const char
}
// onPushState is not always called when onPushRegister is successful
- getInstance().m_state = PUSH_SERVICE_STATE_REGISTERED;
- getInstance().m_listener->onPushRegister(*callbackId, res, id);
- delete callbackId;
+ impl->m_state = PUSH_SERVICE_STATE_REGISTERED;
+ impl->m_listener->onPushRegister(callbackId, res, id);
}
gboolean PushManager::onFakeDeregister(gpointer user_data) {
LoggerD("Enter");
- if (!getInstance().m_listener) {
+ PushManagerHolder* holder = static_cast(user_data);
+ // automatically releases memory
+ std::unique_ptr holder_ptr(holder);
+ PushManager* impl = dynamic_cast(holder->impl);
+ double callbackId = holder->callbackId;
+
+ if (nullptr == impl || !impl->m_listener) {
LoggerW("Listener not set, ignoring");
return G_SOURCE_REMOVE;
}
- double* callbackId = static_cast(user_data);
- getInstance().m_listener->onDeregister(*callbackId,
- PlatformResult(ErrorCode::NO_ERROR));
- delete callbackId;
+ impl->m_listener->onDeregister(callbackId, PlatformResult(ErrorCode::NO_ERROR));
return G_SOURCE_REMOVE;
}
void PushManager::onDeregister(push_service_result_e result, const char* msg,
void* user_data) {
LoggerD("Enter");
- if (!getInstance().m_listener) {
+ PushManagerHolder* holder = static_cast(user_data);
+ // automatically releases memory
+ PushManager* impl = dynamic_cast(holder->impl);
+ std::unique_ptr holder_ptr(holder);
+ double callbackId = holder->callbackId;
+
+ if (nullptr == impl || !impl->m_listener) {
LoggerW("Listener not set, ignoring");
return;
}
- double* callbackId = static_cast(user_data);
if (result == PUSH_SERVICE_RESULT_SUCCESS) {
- getInstance().m_listener->onDeregister(*callbackId,
- PlatformResult(ErrorCode::NO_ERROR));
+ impl->m_listener->onDeregister(callbackId, PlatformResult(ErrorCode::NO_ERROR));
} else {
- getInstance().m_listener->onDeregister(*callbackId,
- LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
+ impl->m_listener->onDeregister(callbackId, LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
msg == NULL ? "Unknown error" : msg));
}
- delete callbackId;
}
diff --git a/src/push/push_manager.h b/src/push/push_manager.h
index 6f4906d2..ba4dbb8a 100644
--- a/src/push/push_manager.h
+++ b/src/push/push_manager.h
@@ -23,23 +23,14 @@
#include
#include