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 {
28 JSCallbackManagerPtr JSCallbackManager::createObject(JSContextRef context,
31 bool acceptJSNullAsOnSuccess,
32 bool acceptJSNullAsOnError)
35 JSObjectRef l_onSuccess = NULL;
36 JSObjectRef l_onError = NULL;
38 LogDebug("onSuccessCallback ptr=" << onSuccess);
39 LogDebug("onErrorCallback ptr=" << onError);
40 if (NULL != onSuccess &&
41 (!acceptJSNullAsOnSuccess || !JSValueIsNull(context, onSuccess))) {
42 l_onSuccess = JSValueToObject(context, onSuccess, NULL);
44 !JSObjectIsFunction(context, l_onSuccess)) {
45 ThrowMsg(Commons::InvalidArgumentException,
46 "success callback is not a function");
49 LogWarning("onSuccessCallback is NULL and is not registred");
51 if (NULL != onError &&
52 (!acceptJSNullAsOnError || !JSValueIsNull(context, onError))) {
53 l_onError = JSValueToObject(context, onError, NULL);
55 !JSObjectIsFunction(context, l_onError)) {
56 ThrowMsg(Commons::InvalidArgumentException,
57 "error callback is not a function");
60 LogWarning("onErrorCallback is NULL and is not registred");
62 return JSCallbackManagerPtr(new JSCallbackManager(context, l_onSuccess,
66 JSCallbackManager::JSCallbackManager(JSContextRef context,
67 JSObjectRef onSuccess,
68 JSObjectRef onError) :
75 setOnSuccess(onSuccess);
79 JSCallbackManager::~JSCallbackManager()
84 JSValueUnprotect(m_context, m_onSuccess);
88 JSValueUnprotect(m_context, m_onError);
92 void JSCallbackManager::setOnSuccess(JSValueRef onSuccess)
96 if (m_onSuccess != NULL) {
97 JSValueUnprotect(m_context, m_onSuccess);
100 m_onSuccess = JSValueToObject(m_context, onSuccess, NULL);
102 if (m_onSuccess != NULL) {
103 JSValueProtect(m_context, m_onSuccess);
108 JSValueRef JSCallbackManager::getOnSuccess() const
114 void JSCallbackManager::setOnError(JSValueRef onError)
118 if (m_onError != NULL) {
119 JSValueUnprotect(m_context, m_onError);
122 m_onError = JSValueToObject(m_context, onError, NULL);
124 if (m_onError != NULL) {
125 JSValueProtect(m_context, m_onError);
130 JSValueRef JSCallbackManager::getOnError() const
136 void JSCallbackManager::setContext(JSContextRef context)
142 void JSCallbackManager::callOnSuccess()
145 if (m_onSuccess == NULL) {
146 LogDebug("Success callback is not set");
149 makeCallback(m_context, NULL, m_onSuccess, NULL, 0);
152 void JSCallbackManager::callOnSuccess(JSValueRef obj)
155 if (m_onSuccess == NULL) {
156 LogDebug("Success callback is not set");
159 JSValueRef objParam[1] = { obj };
160 makeCallback(m_context, NULL, m_onSuccess, objParam, 1);
163 void JSCallbackManager::callOnSuccess(JSValueRef obj[],
167 if (m_onSuccess == NULL) {
168 LogDebug("Success callback is not set");
171 makeCallback(m_context, NULL, m_onSuccess, obj, paramCount);
174 void JSCallbackManager::callOnError()
177 if (m_onError == NULL) {
178 LogDebug("Error callback is not set");
181 makeCallback(m_context, NULL, m_onError, NULL, 0);
184 void JSCallbackManager::callOnError(JSValueRef obj)
187 if (m_onError == NULL) {
188 LogDebug("Error callback is not set");
191 JSValueRef objParam[1] = { obj };
192 makeCallback(m_context, NULL, m_onError, objParam, 1);
195 void JSCallbackManager::callOnError(JSValueRef obj[],
199 if (m_onError == NULL) {
200 LogDebug("Error callback is not set");
203 makeCallback(m_context, NULL, m_onError, obj, paramCount);
206 void JSCallbackManager::makeCallback(JSContextRef context,
208 JSObjectRef callback,
214 if (callback == NULL) {
215 LogError("callback is NULL");
219 if (JSObjectIsFunction(context, callback)) {
221 LogDebug("Calling object directly, no arguments");
222 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
224 LogDebug("Calling object directly, one argument");
225 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);