2 // Tizen Web Device API
3 // Copyright (c) 2012 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 "JSApplicationEventCallbackManager.h"
22 namespace Application {
24 JSApplicationEventCallbackManagerPtr JSApplicationEventCallbackManager::createObject(JSContextRef context)
26 return JSApplicationEventCallbackManagerPtr( new JSApplicationEventCallbackManager(context) );
29 JSApplicationEventCallbackManager::JSApplicationEventCallbackManager(JSContextRef context,
30 JSObjectRef onInstalled,
31 JSObjectRef onUpdated,
32 JSObjectRef onUninstalled,
33 JSObjectRef onError ) :
36 m_onUninstalled(NULL),
41 setOnInstalled(onInstalled);
42 setOnUpdated(onUpdated);
43 setOnUninstalled(onUninstalled);
47 JSApplicationEventCallbackManager::~JSApplicationEventCallbackManager()
51 JSValueUnprotect(m_context, m_onInstalled);
56 JSValueUnprotect(m_context, m_onUpdated);
61 JSValueUnprotect(m_context, m_onUninstalled);
66 JSValueUnprotect(m_context, m_onError);
70 void JSApplicationEventCallbackManager::setOnInstalled( JSValueRef onInstalled )
74 if (m_onInstalled != NULL)
76 JSValueUnprotect(m_context, m_onInstalled);
79 m_onInstalled = JSValueToObject( m_context, onInstalled, NULL );
81 if (m_onInstalled != NULL)
83 JSValueProtect(m_context, m_onInstalled);
88 JSValueRef JSApplicationEventCallbackManager::getOnInstalled() const
93 void JSApplicationEventCallbackManager::setOnUpdated( JSValueRef onUpdated )
97 if (m_onUpdated != NULL)
99 JSValueUnprotect(m_context, m_onUpdated);
102 m_onUpdated = JSValueToObject( m_context, onUpdated, NULL );
104 if (m_onUpdated != NULL)
106 JSValueProtect(m_context, m_onUpdated);
111 JSValueRef JSApplicationEventCallbackManager::getOnUpdated() const
116 void JSApplicationEventCallbackManager::setOnUninstalled( JSValueRef onUninstalled )
120 if (m_onUninstalled != NULL)
122 JSValueUnprotect(m_context, m_onUninstalled);
125 m_onUninstalled = JSValueToObject( m_context, onUninstalled, NULL );
127 if (m_onUninstalled != NULL)
129 JSValueProtect(m_context, m_onUninstalled);
134 JSValueRef JSApplicationEventCallbackManager::getOnUninstalled() const
136 return m_onUninstalled;
139 void JSApplicationEventCallbackManager::setOnError( JSValueRef onError )
143 if (m_onError != NULL)
145 JSValueUnprotect(m_context, m_onError);
148 m_onError = JSValueToObject( m_context, onError, NULL );
150 if (m_onError != NULL)
152 JSValueProtect(m_context, m_onError);
157 JSValueRef JSApplicationEventCallbackManager::getOnError() const
162 void JSApplicationEventCallbackManager::setContext( JSContextRef context )
167 void JSApplicationEventCallbackManager::setObject( JSObjectRef object )
172 JSObjectRef JSApplicationEventCallbackManager::getObject() const
177 void JSApplicationEventCallbackManager::callOnInstalled( JSValueRef appInfo )
179 if ( m_onInstalled == NULL )
181 //LoggerD("oninstalled callback is not set");
184 JSValueRef objParam[1] = { appInfo };
185 makeCallback( m_context, NULL, m_onInstalled, "oninstalled", objParam, 1 );
188 void JSApplicationEventCallbackManager::callOnUpdated( JSValueRef appInfo )
190 if ( m_onUpdated == NULL )
192 //LoggerD("onupdated callback is not set");
195 JSValueRef objParam[1] = { appInfo };
196 makeCallback( m_context, NULL, m_onUpdated, "onupdated", objParam, 1 );
199 void JSApplicationEventCallbackManager::callOnUninstalled( JSValueRef appId )
201 if ( m_onUninstalled == NULL )
203 //LoggerD("onuninstalled callback is not set");
206 JSValueRef objParam[1] = { appId };
207 makeCallback( m_context, NULL, m_onUninstalled, "onuninstalled", objParam, 1 );
210 void JSApplicationEventCallbackManager::callOnError( JSValueRef error )
212 if ( m_onError == NULL )
214 //LoggerD("Error callback is not set");
217 JSValueRef objParam[1] = { error };
218 makeCallback( m_context, NULL, m_onError, "onerror", objParam, 1 );
221 void JSApplicationEventCallbackManager::makeCallback(JSContextRef context, JSObjectRef object, JSObjectRef callback, const char *szName, JSValueRef argv[], unsigned argc)
224 if (callback == NULL)
226 LoggerE("callback is NULL");
230 if (JSObjectIsFunction(context, callback))
234 //LoggerD("Calling object directly, no arguments");
235 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
239 //LoggerD("Calling object directly, one argument");
240 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);