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/secure_log.h>
25 namespace WrtDeviceApis {
26 namespace CommonsJavaScript {
27 JSCallbackManagerPtr JSCallbackManager::createObject(
32 acceptJSNullAsOnSuccess,
33 bool acceptJSNullAsOnError)
35 JSObjectRef l_onSuccess = NULL;
36 JSObjectRef l_onError = NULL;
38 if (NULL != onSuccess &&
39 (!acceptJSNullAsOnSuccess || !JSValueIsNull(context, onSuccess)))
41 l_onSuccess = JSValueToObject(context, onSuccess, NULL);
43 !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)))
54 l_onError = JSValueToObject(context, onError, NULL);
56 !JSObjectIsFunction(context, l_onError))
58 ThrowMsg(Commons::InvalidArgumentException,
59 "error callback is not a function");
62 //LogWarning("onErrorCallback is NULL and is not registred");
64 return JSCallbackManagerPtr(new JSCallbackManager(context, l_onSuccess,
68 JSCallbackManager::JSCallbackManager(JSContextRef context,
69 JSObjectRef onSuccess,
70 JSObjectRef onError) :
76 setOnSuccess(onSuccess);
80 JSCallbackManager::~JSCallbackManager()
83 JSValueUnprotect(m_context, m_onSuccess);
87 JSValueUnprotect(m_context, m_onError);
91 JSValueUnprotect(m_context, m_object);
95 void JSCallbackManager::setOnSuccess(JSValueRef onSuccess)
97 if (m_onSuccess != NULL) {
98 JSValueUnprotect(m_context, m_onSuccess);
102 m_onSuccess = JSValueToObject(m_context, onSuccess, NULL);
107 if (m_onSuccess != NULL) {
108 JSValueProtect(m_context, m_onSuccess);
112 JSValueRef JSCallbackManager::getOnSuccess() const
117 void JSCallbackManager::setOnError(JSValueRef onError)
119 if (m_onError != NULL) {
120 JSValueUnprotect(m_context, m_onError);
124 m_onError = JSValueToObject(m_context, onError, NULL);
129 if (m_onError != NULL) {
130 JSValueProtect(m_context, m_onError);
134 JSValueRef JSCallbackManager::getOnError() const
139 void JSCallbackManager::setObject(JSObjectRef object)
141 if (m_object != NULL) {
142 JSValueUnprotect(m_context, m_object);
147 if (m_object != NULL) {
148 JSValueProtect(m_context, m_object);
152 JSObjectRef JSCallbackManager::getObject() const
157 void JSCallbackManager::setContext(JSContextRef context)
162 void JSCallbackManager::callOnSuccess()
164 if (m_onSuccess == NULL) {
165 //LogDebug("Success callback is not set");
168 makeCallback(m_context, NULL, m_onSuccess, NULL, 0);
171 void JSCallbackManager::callOnSuccess(JSValueRef obj)
173 if (m_onSuccess == NULL) {
174 //LogDebug("Success callback is not set");
177 JSValueRef objParam[1] = { obj };
178 makeCallback(m_context, NULL, m_onSuccess, objParam, 1);
181 void JSCallbackManager::callOnSuccess(JSValueRef obj[],
184 if (m_onSuccess == NULL) {
185 //LogDebug("Success callback is not set");
188 makeCallback(m_context, NULL, m_onSuccess, obj, paramCount);
191 void JSCallbackManager::callOnError()
193 if (m_onError == NULL) {
194 //LogDebug("Error callback is not set");
197 makeCallback(m_context, NULL, m_onError, NULL, 0);
200 void JSCallbackManager::callOnError(JSValueRef obj)
202 if (m_onError == NULL) {
203 //LogDebug("Error callback is not set");
206 JSValueRef objParam[1] = { obj };
207 makeCallback(m_context, NULL, m_onError, objParam, 1);
210 void JSCallbackManager::callOnError(JSValueRef obj[],
213 if (m_onError == NULL) {
214 //LogDebug("Error callback is not set");
217 makeCallback(m_context, NULL, m_onError, obj, paramCount);
220 void JSCallbackManager::makeCallback(JSContextRef context,
222 JSObjectRef callback,
226 if (callback == NULL) {
227 //LogError("callback is NULL");
231 if (JSObjectIsFunction(context, callback)) {
233 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
235 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);