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) :
76 setOnSuccess(onSuccess);
80 JSCallbackManager::~JSCallbackManager()
85 JSValueUnprotect(m_context, m_onSuccess);
89 JSValueUnprotect(m_context, m_onError);
93 JSValueUnprotect(m_context, m_object);
97 void JSCallbackManager::setOnSuccess(JSValueRef onSuccess)
100 if (m_onSuccess != NULL) {
101 JSValueUnprotect(m_context, m_onSuccess);
105 m_onSuccess = JSValueToObject(m_context, onSuccess, NULL);
110 if (m_onSuccess != NULL) {
111 JSValueProtect(m_context, m_onSuccess);
115 JSValueRef JSCallbackManager::getOnSuccess() const
121 void JSCallbackManager::setOnError(JSValueRef onError)
124 if (m_onError != NULL) {
125 JSValueUnprotect(m_context, m_onError);
129 m_onError = JSValueToObject(m_context, onError, NULL);
134 if (m_onError != NULL) {
135 JSValueProtect(m_context, m_onError);
139 JSValueRef JSCallbackManager::getOnError() const
145 void JSCallbackManager::setObject(JSObjectRef object)
148 if (m_object != NULL) {
149 JSValueUnprotect(m_context, m_object);
154 if (m_object != NULL) {
155 JSValueProtect(m_context, m_object);
159 JSObjectRef JSCallbackManager::getObject() const
165 void JSCallbackManager::setContext(JSContextRef context)
171 void JSCallbackManager::callOnSuccess()
174 if (m_onSuccess == NULL) {
175 LogDebug("Success callback is not set");
178 makeCallback(m_context, NULL, m_onSuccess, NULL, 0);
181 void JSCallbackManager::callOnSuccess(JSValueRef obj)
184 if (m_onSuccess == NULL) {
185 LogDebug("Success callback is not set");
188 JSValueRef objParam[1] = { obj };
189 makeCallback(m_context, NULL, m_onSuccess, objParam, 1);
192 void JSCallbackManager::callOnSuccess(JSValueRef obj[],
196 if (m_onSuccess == NULL) {
197 LogDebug("Success callback is not set");
200 makeCallback(m_context, NULL, m_onSuccess, obj, paramCount);
203 void JSCallbackManager::callOnError()
206 if (m_onError == NULL) {
207 LogDebug("Error callback is not set");
210 makeCallback(m_context, NULL, m_onError, NULL, 0);
213 void JSCallbackManager::callOnError(JSValueRef obj)
216 if (m_onError == NULL) {
217 LogDebug("Error callback is not set");
220 JSValueRef objParam[1] = { obj };
221 makeCallback(m_context, NULL, m_onError, objParam, 1);
224 void JSCallbackManager::callOnError(JSValueRef obj[],
228 if (m_onError == NULL) {
229 LogDebug("Error callback is not set");
232 makeCallback(m_context, NULL, m_onError, obj, paramCount);
235 void JSCallbackManager::makeCallback(JSContextRef context,
237 JSObjectRef callback,
243 if (callback == NULL) {
244 LogError("callback is NULL");
248 if (JSObjectIsFunction(context, callback)) {
250 LogDebug("Calling object directly, no arguments");
251 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
253 LogDebug("Calling object directly, one argument");
254 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);