2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file JSCalendar.cpp
18 * @author Lukasz Marek (l.marek@samsung.com)
22 #include "JSCallbackManager.h"
23 #include <dpl/log/log.h>
25 namespace WrtDeviceApis {
26 namespace CommonsJavaScript {
27 JSCallbackManagerPtr JSCallbackManager::createObject(
32 acceptJSNullAsOnSuccess,
33 bool acceptJSNullAsOnError)
36 JSObjectRef l_onSuccess = NULL;
37 JSObjectRef l_onError = NULL;
39 LogDebug("onSuccessCallback ptr=" << onSuccess);
40 LogDebug("onErrorCallback ptr=" << onError);
41 if (NULL != onSuccess &&
42 (!acceptJSNullAsOnSuccess || !JSValueIsNull(context, onSuccess)))
44 l_onSuccess = JSValueToObject(context, onSuccess, NULL);
46 !JSObjectIsFunction(context, l_onSuccess))
48 ThrowMsg(Commons::InvalidArgumentException,
49 "success callback is not a function");
52 LogWarning("onSuccessCallback is NULL and is not registred");
54 if (NULL != onError &&
55 (!acceptJSNullAsOnError || !JSValueIsNull(context, onError)))
57 l_onError = JSValueToObject(context, onError, NULL);
59 !JSObjectIsFunction(context, l_onError))
61 ThrowMsg(Commons::InvalidArgumentException,
62 "error callback is not a function");
65 LogWarning("onErrorCallback is NULL and is not registred");
67 return JSCallbackManagerPtr(new JSCallbackManager(context, l_onSuccess,
71 JSCallbackManager::JSCallbackManager(JSContextRef context,
72 JSObjectRef onSuccess,
73 JSObjectRef onError) :
81 setOnSuccess(onSuccess);
85 JSCallbackManager::~JSCallbackManager()
90 JSValueUnprotect(m_context, m_onSuccess);
94 JSValueUnprotect(m_context, m_onError);
98 JSValueUnprotect(m_context, m_object);
102 void JSCallbackManager::setOnSuccess(JSValueRef onSuccess)
105 if (m_onSuccess != NULL) {
106 JSValueUnprotect(m_context, m_onSuccess);
110 m_onSuccess = JSValueToObject(m_context, onSuccess, NULL);
115 if (m_onSuccess != NULL) {
116 JSValueProtect(m_context, m_onSuccess);
120 JSValueRef JSCallbackManager::getOnSuccess() const
126 void JSCallbackManager::setOnError(JSValueRef onError)
129 if (m_onError != NULL) {
130 JSValueUnprotect(m_context, m_onError);
134 m_onError = JSValueToObject(m_context, onError, NULL);
139 if (m_onError != NULL) {
140 JSValueProtect(m_context, m_onError);
144 JSValueRef JSCallbackManager::getOnError() const
150 void JSCallbackManager::setObject(JSObjectRef object)
153 if (m_object != NULL) {
154 JSValueUnprotect(m_context, m_object);
159 if (m_object != NULL) {
160 JSValueProtect(m_context, m_object);
164 JSObjectRef JSCallbackManager::getObject() const
170 void JSCallbackManager::setContext(JSContextRef context)
176 void JSCallbackManager::callOnSuccess()
179 if (m_onSuccess == NULL) {
180 LogDebug("Success callback is not set");
183 makeCallback(m_context, NULL, m_onSuccess, NULL, 0);
186 void JSCallbackManager::callOnSuccess(JSValueRef obj)
189 if (m_onSuccess == NULL) {
190 LogDebug("Success callback is not set");
193 JSValueRef objParam[1] = { obj };
194 makeCallback(m_context, NULL, m_onSuccess, objParam, 1);
197 void JSCallbackManager::callOnSuccess(JSValueRef obj[],
201 if (m_onSuccess == NULL) {
202 LogDebug("Success callback is not set");
205 makeCallback(m_context, NULL, m_onSuccess, obj, paramCount);
208 void JSCallbackManager::callOnError()
211 if (m_onError == NULL) {
212 LogDebug("Error callback is not set");
215 makeCallback(m_context, NULL, m_onError, NULL, 0);
218 void JSCallbackManager::callOnError(JSValueRef obj)
221 if (m_onError == NULL) {
222 LogDebug("Error callback is not set");
225 JSValueRef objParam[1] = { obj };
226 makeCallback(m_context, NULL, m_onError, objParam, 1);
229 void JSCallbackManager::callOnError(JSValueRef obj[],
233 if (m_onError == NULL) {
234 LogDebug("Error callback is not set");
237 makeCallback(m_context, NULL, m_onError, obj, paramCount);
240 void JSCallbackManager::makeCallback(JSContextRef context,
242 JSObjectRef callback,
248 if (callback == NULL) {
249 LogError("callback is NULL");
253 if (JSObjectIsFunction(context, callback)) {
255 LogDebug("Calling object directly, no arguments");
256 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
258 LogDebug("Calling object directly, one argument");
259 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);