From: Jerzy Pabich Date: Mon, 15 Dec 2014 14:45:05 +0000 (+0100) Subject: [Messaging] Added callbacks (sync() part 2) X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~818^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=15eb78543a8788e2e3820f70a8d5b6dee656eb22;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Messaging] Added callbacks (sync() part 2) Change-Id: I28a1a3b5c26382094d343bf76504928772a13fc2 Signed-off-by: Jerzy Pabich --- diff --git a/src/common/callback_user_data.cc b/src/common/callback_user_data.cc new file mode 100644 index 00000000..35c15b90 --- /dev/null +++ b/src/common/callback_user_data.cc @@ -0,0 +1,114 @@ +// +// Tizen Web Device API +// Copyright (c) 2013 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 "common/callback_user_data.h" +//#include "common/multi_callback_user_data.h" +//#include + +//namespace DeviceAPI { +namespace common { + +namespace { +const char* CALLBACK_SUCCESS = "success"; +const char* CALLBACK_ERROR = "error"; +} //anonymous namespace + +CallbackUserData::CallbackUserData(): +// m_context(global_ctx), +// m_impl(new MultiCallbackUserData()), + m_is_act(true) +{ +} + +CallbackUserData::~CallbackUserData() +{ +// delete m_impl; +} + +//void CallbackUserData::setSuccessCallback(JSValueRef on_success) +//{ +// if(on_success && JSValueIsObject(m_context, on_success)) { +// JSObjectRef success = JSValueToObject(m_context, on_success, NULL); +// m_impl->setCallback(CALLBACK_SUCCESS, success); +// } +//} +// +//void CallbackUserData::setErrorCallback(JSValueRef on_error) +//{ +// if(on_error && JSValueIsObject(m_context, on_error)) { +// JSObjectRef error = JSValueToObject(m_context, on_error, NULL); +// m_impl->setCallback(CALLBACK_ERROR, error); +// } +//} +// +//void CallbackUserData::callSuccessCallback(int count, JSValueRef obj[]) +//{ +// if(!m_is_act) { +// return; +// } +// +// m_impl->invokeCallback(CALLBACK_SUCCESS, count, obj); +//} +// +//void CallbackUserData::callSuccessCallback(JSValueRef obj) +//{ +// JSValueRef args[1] = {obj}; +// callSuccessCallback(1, args); +//} +// +//void CallbackUserData::callSuccessCallback() +//{ +// callSuccessCallback(0, NULL); +//} +// +//void CallbackUserData::callErrorCallback(int count, JSValueRef obj[]) +//{ +// if(!m_is_act) { +// return; +// } +// +// m_impl->invokeCallback(CALLBACK_ERROR, count, obj); +//} +// +//void CallbackUserData::callErrorCallback(JSValueRef obj) +//{ +// JSValueRef args[1] = {obj}; +// callErrorCallback(1, args); +//} +// +//void CallbackUserData::callErrorCallback() +//{ +// callErrorCallback(0, NULL); +//} +// +//JSContextRef CallbackUserData::getContext() +//{ +// return m_context; +//} + +void CallbackUserData::setActive(bool act) +{ + m_is_act = act; +} + +bool CallbackUserData::isActive() const +{ + return m_is_act; +} + +} // Common +//} // DeviceAPI diff --git a/src/common/callback_user_data.h b/src/common/callback_user_data.h new file mode 100644 index 00000000..f9f9dc3e --- /dev/null +++ b/src/common/callback_user_data.h @@ -0,0 +1,60 @@ +// +// Tizen Web Device API +// Copyright (c) 2013 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. +// + +// Class added for backward compatibility with WRT plugins. +// To be cleaned/replaced in the future (TODO). + +#ifndef __TIZEN_COMMON_CALLBACK_USER_DATA_H__ +#define __TIZEN_COMMON_CALLBACK_USER_DATA_H__ + +//#include + +//namespace DeviceAPI { +namespace common { + +//class MultiCallbackUserData; + +class CallbackUserData { +public: + CallbackUserData(); + virtual ~CallbackUserData(); + +// JSContextRef getContext(); +// void setSuccessCallback(JSValueRef on_success); +// void setErrorCallback(JSValueRef on_error); +// +// void callSuccessCallback(); +// void callSuccessCallback(JSValueRef obj); +// void callSuccessCallback(int count, JSValueRef obj[]); +// +// void callErrorCallback(); +// void callErrorCallback(JSValueRef obj); +// void callErrorCallback(int count, JSValueRef obj[]); + + void setActive(bool act); + bool isActive() const; +private: +// JSContextRef m_context; +// MultiCallbackUserData* m_impl; + bool m_is_act; +}; + +} // Common +//} // DeviceAPI + +#endif //__TIZEN_COMMON_CALLBACK_USER_DATA_H__ + diff --git a/src/common/common.gypi b/src/common/common.gypi index f3aba324..1c6a4fd7 100644 --- a/src/common/common.gypi +++ b/src/common/common.gypi @@ -88,6 +88,10 @@ 'scope_exit.h', 'task-queue.cpp', 'task-queue.h', + 'callback_user_data.cc', + 'callback_user_data.h', + #'multi_callback_user_data.cc', + #'multi_callback_user_data.h', ], 'cflags': [ '-std=c++0x', diff --git a/src/common/multi_callback_user_data.cc b/src/common/multi_callback_user_data.cc new file mode 100755 index 00000000..fd41f441 --- /dev/null +++ b/src/common/multi_callback_user_data.cc @@ -0,0 +1,169 @@ +// +// Tizen Web Device API +// Copyright (c) 2013 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 "common/multi_callback_user_data.h" +//#include "GlobalContextManager.h" +//#include "PlatformException.h" +//#include "WebKitProxy.h" +//#include "JSUtil.h" +#include "common/logger.h" + +//using namespace std; + +//namespace DeviceAPI { +namespace common { + +MultiCallbackUserData::MultiCallbackUserData() //: +// m_context(global_ctx), +// m_object(NULL) +{ +} + +//MultiCallbackUserData::MultiCallbackUserData(JSContextRef global_ctx, +// JSObjectRef object) : +// m_context(global_ctx), +// m_object(object) +//{ +// if(m_object) { +// JSValueProtect(m_context, m_object); +// } +//} +// +//MultiCallbackUserData::~MultiCallbackUserData() +//{ +// if(!GlobalContextManager::getInstance()->isAliveGlobalContext(m_context)) { +// //Remove Callback functions in Native Map +// CallbackMapT::iterator itr; +// for(itr = m_callbacks.begin(); itr != m_callbacks.end(); ++itr) { +// if(itr->second) { +// JSValueUnprotect(m_context, itr->second); +// } +// } +// +// //Remove Callback Object +// if(m_object) { +// JSValueUnprotect(m_context, m_object); +// } +// } +//} +// +//void MultiCallbackUserData::setCallback(const string &key, JSObjectRef callback) +//{ +// // Callback Object Case +// if(m_object) { +// JSUtil::setProperty(m_context, m_object, key.c_str(), callback, +// kJSPropertyAttributeNone); +// return; +// } +// +// // Callback function Case +// CallbackMapT::iterator itr = m_callbacks.find(key); +// if(itr != m_callbacks.end() && itr->second) { +// JSValueUnprotect(m_context, itr->second); +// } +// +// if(callback) { +// JSValueProtect(m_context, callback); +// } +// +// m_callbacks[key] = callback; +//} +// +// +//void MultiCallbackUserData::invokeCallback(const std::string &key, int count, +// JSValueRef obj[]) +//{ +// if(!GlobalContextManager::getInstance()->isAliveGlobalContext(m_context)) { +// LOGE("context was closed"); +// return; +// } +// +// // Callback Object case +// if(m_object) { +// try { +// // Getting callback value +// JSValueRef callbackValue = JSUtil::getProperty(m_context, m_object, +// key.c_str()); +// +// // Testing existing +// if(JSValueIsUndefined(m_context, callbackValue)) { +// LOGE("There is no such callback: [%s]", key.c_str()); +// return; +// } +// +// JSObjectRef callbackObject = JSUtil::JSValueToObject(m_context, callbackValue); +// +// // Testing type validation +// if(!JSObjectIsFunction(m_context, callbackObject)) { +// LOGE("[%s] is not function", key.c_str()); +// return; +// } +// +// JSValueRef exception = NULL; +// JSObjectCallAsFunction(m_context, callbackObject, NULL, count, obj, +// &exception); +// +// if(exception){ +// WebKitProxy::reportException(m_context, exception); +// } +// // check Exception in function call +// if(exception != NULL) { +// throw UnknownException(m_context, exception); +// } +// } catch(const BasePlatformException& err) { +// LOGE("Error in Callback invoke - %s:%s", err.getName().c_str(), +// err.getMessage().c_str()); +// } +// return; +// } +// +// // Callback function case +// CallbackMapT::iterator itr = m_callbacks.find(key); +// if(itr == m_callbacks.end()) { +// LOGE("There is no such callback: [%s]", key.c_str()); +// return; +// } +// +// if(itr->second) { +// JSValueRef exception = NULL; +// JSObjectCallAsFunction(m_context, itr->second , NULL, count, obj, &exception); +// if(exception) +// WebKitProxy::reportException(m_context, exception); +// } +// else { +// LOGE("The callback: [%s] is NULL", key.c_str()); +// } +//} +// +//void MultiCallbackUserData::invokeCallback(const std::string &key, JSValueRef obj) +//{ +// JSValueRef args[1] = {obj}; +// invokeCallback(key, 1, args); +//} +// +//void MultiCallbackUserData::invokeCallback(const std::string &key) +//{ +// invokeCallback(key, 0, NULL); +//} +// +//JSContextRef MultiCallbackUserData::getContext() const +//{ +// return m_context; +//} + +} // Common +//} // DeviceAPI diff --git a/src/common/multi_callback_user_data.h b/src/common/multi_callback_user_data.h new file mode 100755 index 00000000..b75f164e --- /dev/null +++ b/src/common/multi_callback_user_data.h @@ -0,0 +1,58 @@ +// +// Tizen Web Device API +// Copyright (c) 2013 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. +// + +// Class added for backward compatibility with WRT plugins. +// To be cleaned/replaced in the future (TODO). + +#ifndef __TIZEN_COMMON_MULTI_CALLBACK_USER_DATA_H__ +#define __TIZEN_COMMON_MULTI_CALLBACK_USER_DATA_H__ + +//#include +#include +#include +#include + +//namespace DeviceAPI { +namespace common { + +class MultiCallbackUserData; +typedef std::shared_ptr MultiCallbackUserDataPtr; + +class MultiCallbackUserData { +public: + MultiCallbackUserData(); +// MultiCallbackUserData(JSContextRef global_ctx, JSObjectRef object); + virtual ~MultiCallbackUserData(); + +// JSContextRef getContext() const; +// void setCallback(const std::string &key, JSObjectRef callback); +// +// void invokeCallback(const std::string &key); +// void invokeCallback(const std::string &key, JSValueRef obj); +// void invokeCallback(const std::string &key, int count, JSValueRef obj[]); + +//private: +// JSContextRef m_context; +// JSObjectRef m_object; +// typedef std::map CallbackMapT; +// CallbackMapT m_callbacks; +}; + +} // Common +//} // DeviceAPI + +#endif //__TIZEN_COMMON_MULTI_CALLBACK_USER_DATA_H__ diff --git a/src/messaging/message_service.cc b/src/messaging/message_service.cc index 78bf5ef7..daa2ebde 100644 --- a/src/messaging/message_service.cc +++ b/src/messaging/message_service.cc @@ -22,6 +22,121 @@ const char* JSON_SERVICE_NAME = "name"; const char* JSON_SERVICE_STORAGE = "messageStorage"; } +//#################### BaseMessageServiceCallbackData #################### + +BaseMessageServiceCallbackData::BaseMessageServiceCallbackData(): +// CallbackUserData(globalCtx), + m_is_error(false), + m_op_handle(-1) +{ + LOGD("Entered"); +} + +BaseMessageServiceCallbackData::~BaseMessageServiceCallbackData() +{ + LOGD("Entered"); +} + +void BaseMessageServiceCallbackData::setError(const std::string& err_name, + const std::string& err_message) +{ + // keep only first error in chain + if (!m_is_error) { + m_is_error = true; + m_err_name = err_name; + m_err_message = err_message; + } +} + +bool BaseMessageServiceCallbackData::isError() const +{ + return m_is_error; +} + +std::string BaseMessageServiceCallbackData::getErrorName() const +{ + return m_err_name; +} + +std::string BaseMessageServiceCallbackData::getErrorMessage() const +{ + return m_err_message; +} + +void BaseMessageServiceCallbackData::setOperationHandle(const int op_handle) +{ + m_op_handle = op_handle; +} + +int BaseMessageServiceCallbackData::getOperationHandle() const +{ + return m_op_handle; +} + +void BaseMessageServiceCallbackData::setCallbackId(const double callback_id) +{ + m_callback_id = callback_id; +} + +double BaseMessageServiceCallbackData::getCallbackId() const +{ + return m_callback_id; +} + +//#################### SyncCallbackData #################### + +SyncCallbackData::SyncCallbackData(): +// BaseMessageServiceCallbackData(globalCtx), + m_is_limit(false), + m_limit(0), + m_account_id(-1) +{ + LOGD("Entered"); +} + +SyncCallbackData::~SyncCallbackData() +{ + LOGD("Entered"); +} + +void SyncCallbackData::setLimit(const unsigned long limit) +{ + m_is_limit = true; + m_limit = limit; +} + +bool SyncCallbackData::isLimit() const +{ + return m_is_limit; +} + +unsigned long SyncCallbackData::getLimit() const +{ + return m_limit; +} + +void SyncCallbackData::setOpId(long op_id) +{ + m_op_id = op_id; +} + +long SyncCallbackData::getOpId() +{ + return m_op_id; +} + +void SyncCallbackData::setAccountId(int account_id) +{ + m_account_id = account_id; +} + +int SyncCallbackData::getAccountId() const +{ + return m_account_id; +} + + + MessageService::MessageService(int id, MessageType msgType, std::string name): diff --git a/src/messaging/message_service.h b/src/messaging/message_service.h index c51af772..80705bb0 100755 --- a/src/messaging/message_service.h +++ b/src/messaging/message_service.h @@ -10,6 +10,7 @@ #include #include "common/picojson.h" +#include "common/callback_user_data.h" #include "messaging_util.h" #include "message_storage.h" @@ -24,6 +25,61 @@ enum MessageServiceAccountId MMS_ACCOUNT_ID = 102 }; +class BaseMessageServiceCallbackData : public common::CallbackUserData { +public: + BaseMessageServiceCallbackData(); + virtual ~BaseMessageServiceCallbackData(); + + void setError(const std::string& err_name, + const std::string& err_message); + bool isError() const; + std::string getErrorName() const; + std::string getErrorMessage() const; + + /** + * This handle is returned from various native API functions: + * int email_sync_header(..., int *handle); + * int email_download_body(..., int *handle); + * int email_download_attachment(..., int *handle); + * + * It is used to stop and identify request. + */ + void setOperationHandle(const int op_handle); + int getOperationHandle() const; + void setCallbackId(const double callback_id); + double getCallbackId() const; + +protected: + bool m_is_error; + std::string m_err_name; + std::string m_err_message; + + int m_op_handle; + double m_callback_id; +}; + +class SyncCallbackData : public BaseMessageServiceCallbackData { +public: + SyncCallbackData(); + virtual ~SyncCallbackData(); + + void setLimit(const unsigned long limit); + bool isLimit() const; + unsigned long getLimit() const; + + void setOpId(long op_id); + long getOpId(); + void setAccountId(int account_id); + int getAccountId() const; + +protected: + bool m_is_limit; + unsigned long m_limit; + + long m_op_id; + int m_account_id; +}; + class MessageService { public: