1ac5cfa41c85e771f9f9b313ea918834a940a6a5
[framework/web/wrt-plugins-tizen.git] / src / Common / MultiCallbackUserData.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include "MultiCallbackUserData.h"
19 #include "GlobalContextManager.h"
20
21 #include <dlog.h>
22
23 #undef LOG_TAG
24 #define LOG_TAG "TIZEN_DEVICEAPI"
25
26 using namespace std;
27
28 namespace DeviceAPI {
29 namespace Common {
30
31 MultiCallbackUserData::MultiCallbackUserData(JSContextRef globalCtx): mContext(globalCtx){
32 }
33
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;
39             if( t != NULL )
40                 JSValueUnprotect(mContext, t);
41         }
42     }
43 }
44
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);
50     }
51     
52     if( callback != NULL ){
53         JSValueProtect(mContext, callback);
54     }
55     
56     mCallbacks[key] = callback;
57 }
58
59
60 void MultiCallbackUserData::invokeCallback(const std::string &key, int count, JSValueRef obj [ ]){
61     if( !GlobalContextManager::getInstance()->isAliveGlobalContext(mContext)){
62         LOGE("context was closed");
63         return;
64     }
65
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());
70         return;
71     }
72     
73     if( itr->second ){
74         JSObjectCallAsFunction(mContext, itr->second , NULL, count, obj, NULL);
75     }else{
76         LOGE("The callback[%s] is NULL", key.c_str());
77     }
78 }
79
80 void MultiCallbackUserData::invokeCallback(const std::string &key, JSValueRef obj){
81     JSValueRef args[1] = {obj};
82     invokeCallback(key, 1, args);
83 }
84
85 void MultiCallbackUserData::invokeCallback(const std::string &key){
86     invokeCallback(key, 0, NULL);
87 }
88
89 JSContextRef MultiCallbackUserData::getContext(){
90     return mContext;
91 }
92
93 }
94 }
95