2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include "MultiCallbackUserData.h"
19 #include "GlobalContextManager.h"
24 #define LOG_TAG "TIZEN_DEVICEAPI"
31 MultiCallbackUserData::MultiCallbackUserData(JSContextRef globalCtx): mContext(globalCtx){
34 MultiCallbackUserData::~MultiCallbackUserData(){
35 if( !GlobalContextManager::getInstance()->isAliveGlobalContext(mContext)){
36 CallbackMapT::iterator itr;
37 for( itr = mCallbacks.begin() ; itr != mCallbacks.end() ; ++itr){
38 JSObjectRef t = itr->second;
40 JSValueUnprotect(mContext, t);
45 void MultiCallbackUserData::setCallback(const string &key, JSObjectRef callback){
46 CallbackMapT::iterator itr;
47 itr = mCallbacks.find(key);
48 if( itr != mCallbacks.end() && itr->second != NULL){
49 JSValueUnprotect(mContext, itr->second);
52 if( callback != NULL ){
53 JSValueProtect(mContext, callback);
56 mCallbacks[key] = callback;
60 void MultiCallbackUserData::invokeCallback(const std::string &key, int count, JSValueRef obj [ ]){
61 if( !GlobalContextManager::getInstance()->isAliveGlobalContext(mContext)){
62 LOGE("context was closed");
66 CallbackMapT::iterator itr;
67 itr = mCallbacks.find(key);
68 if( itr == mCallbacks.end()){
69 LOGE("There is no such callback[%s]", key.c_str());
74 JSObjectCallAsFunction(mContext, itr->second , NULL, count, obj, NULL);
76 LOGE("The callback[%s] is NULL", key.c_str());
80 void MultiCallbackUserData::invokeCallback(const std::string &key, JSValueRef obj){
81 JSValueRef args[1] = {obj};
82 invokeCallback(key, 1, args);
85 void MultiCallbackUserData::invokeCallback(const std::string &key){
86 invokeCallback(key, 0, NULL);
89 JSContextRef MultiCallbackUserData::getContext(){