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
LoggerD("Enter");
CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
- common::PlatformResult result = PushManager::getInstance().registerApplication(
+ common::PlatformResult result = impl->registerApplication(
args.get("callbackId").get<double>());
if (result.IsError()) {
LogAndReportError(result, &out, ("Error occured"));
CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
- common::PlatformResult result = PushManager::getInstance()
- .unregisterApplication(args.get("callbackId").get<double>());
+ common::PlatformResult result = impl->unregisterApplication(
+ args.get("callbackId").get<double>());
if (result.IsError()) {
LogAndReportError(result, &out, ("Error occured"));
} else {
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();
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 {
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");
}
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);
PushInstance::~PushInstance() {
LoggerD("Enter");
- PushManager::getInstance().setListener(nullptr);
+ impl->setListener(nullptr);
}
} // namespace push
#include <app_control_internal.h>
#include <app_manager.h>
#include <bundle.h>
+#include <memory>
+#include "push/push_manager_common.h"
#include "common/extension.h"
#include "common/logger.h"
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),
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);
}
}
-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<void*>(pcallback));
+ int ret = push_service_register(m_handle, onApplicationRegister, static_cast<void*>(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) {
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));
}
}
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<double>(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<double>(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<PushManager*>(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<PushManager*>(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<PushManagerHolder*>(user_data);
+ // automatically releases memory
+ std::unique_ptr<PushManagerHolder> holder_ptr(holder);
+ PushManager* impl = dynamic_cast<PushManager*>(holder->impl);
+ double callbackId = holder->callbackId;
+
+ if (nullptr == impl || !impl->m_listener) {
LoggerW("Listener not set, ignoring");
return;
}
- double* callbackId = static_cast<double*>(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;
}
// 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<PushManagerHolder*>(user_data);
+ // automatically releases memory
+ std::unique_ptr<PushManagerHolder> holder_ptr(holder);
+ PushManager* impl = dynamic_cast<PushManager*>(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<double*>(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<PushManagerHolder*>(user_data);
+ // automatically releases memory
+ PushManager* impl = dynamic_cast<PushManager*>(holder->impl);
+ std::unique_ptr<PushManagerHolder> holder_ptr(holder);
+ double callbackId = holder->callbackId;
+
+ if (nullptr == impl || !impl->m_listener) {
LoggerW("Listener not set, ignoring");
return;
}
- double* callbackId = static_cast<double*>(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;
}
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 "push/push_manager_common.h"
+#include <unistd.h>
+#include <pcrecpp.h>
+#include <app_control.h>
+#include <app_control_internal.h>
+#include <app_manager.h>
+#include <bundle.h>
+#include <memory>
+
+#include "common/extension.h"
+#include "common/logger.h"
+
+namespace extension {
+namespace push {
+
+using common::PlatformResult;
+using common::ErrorCode;
+
+ErrorCode PushManagerCommon::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;
+}
+
+IPushManager::IPushManager() {
+ LoggerD("Enter");
+}
+
+IPushManager::~IPushManager() {
+ LoggerD("Enter");
+}
+
+void IPushManager::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(value);
+ 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<double>(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<double>(type));
+}
+
+} // namespace push
+} // namespace extension
+
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 SRC_PUSH_PUSH_MANAGER_COMMON_H_
+#define SRC_PUSH_PUSH_MANAGER_COMMON_H_
+
+#include <push-service.h>
+#include <glib.h>
+#include <string>
+#include <vector>
+#include <map>
+#include "common/platform_result.h"
+
+namespace extension {
+namespace push {
+
+class EventListener {
+ public:
+ virtual void onPushRegister(double callbackId,
+ common::PlatformResult result, const std::string& id) = 0;
+ virtual void onPushNotify(push_service_notification_h noti) = 0;
+ virtual void onDeregister(double callbackId,
+ common::PlatformResult result) = 0;
+ virtual ~EventListener() {}
+};
+
+class IPushManager {
+ public:
+ IPushManager();
+ virtual ~IPushManager();
+
+ virtual void setListener(EventListener* listener) = 0;
+
+ virtual common::PlatformResult registerApplication(double callbackId) = 0;
+ virtual common::PlatformResult unregisterApplication(double callbackId) = 0;
+ virtual common::PlatformResult getRegistrationId(std::string &id) = 0;
+ virtual common::PlatformResult getUnreadNotifications() = 0;
+ virtual common::PlatformResult getPushMessage(picojson::value* out) = 0;
+
+ void notificationToJson(push_service_notification_h noti, picojson::object* obj);
+};
+
+struct PushManagerHolder {
+ IPushManager* impl;
+ double callbackId;
+};
+
+class PushManagerCommon {
+ public :
+ static common::ErrorCode ConvertPushError(int e);
+};
+
+} // namespace push
+} // namespace extension
+
+#endif // SRC_PUSH_PUSH_MANAGER_COMMON_H_
+