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 JSCallbackManager.cpp
18 * @author Lukasz Marek (l.marek@samsung.com)
22 #include "JSCallbackManager.h"
24 #include <dpl/log/secure_log.h>
25 #include <CommonsJavaScript/JSLifeManager.h>
27 namespace WrtDeviceApis {
28 namespace CommonsJavaScript {
29 JSCallbackManagerPtr JSCallbackManager::createObject(
34 acceptJSNullAsOnSuccess,
35 bool acceptJSNullAsOnError)
37 JSObjectRef l_onSuccess = NULL;
38 JSObjectRef l_onError = NULL;
40 if (NULL != onSuccess &&
41 (!acceptJSNullAsOnSuccess || !JSValueIsNull(context, onSuccess)))
43 l_onSuccess = JSValueToObject(context, onSuccess, NULL);
45 !JSObjectIsFunction(context, l_onSuccess))
47 ThrowMsg(Commons::InvalidArgumentException,
48 "success callback is not a function");
51 //LogWarning("onSuccessCallback is NULL and is not registred");
53 if (NULL != onError &&
54 (!acceptJSNullAsOnError || !JSValueIsNull(context, onError)))
56 l_onError = JSValueToObject(context, onError, NULL);
58 !JSObjectIsFunction(context, l_onError))
60 ThrowMsg(Commons::InvalidArgumentException,
61 "error callback is not a function");
64 //LogWarning("onErrorCallback is NULL and is not registred");
66 return JSCallbackManagerPtr(new JSCallbackManager(context, l_onSuccess,
70 JSCallbackManager::JSCallbackManager(JSContextRef context,
71 JSObjectRef onSuccess,
72 JSObjectRef onError) :
78 setOnSuccess(onSuccess);
82 JSCallbackManager::~JSCallbackManager()
85 JSValueSafeUnprotect(m_context, m_onSuccess);
89 JSValueSafeUnprotect(m_context, m_onError);
93 JSValueSafeUnprotect(m_context, m_object);
97 void JSCallbackManager::setOnSuccess(JSValueRef onSuccess)
99 if (m_onSuccess != NULL) {
100 JSValueSafeUnprotect(m_context, m_onSuccess);
104 m_onSuccess = JSValueToObject(m_context, onSuccess, NULL);
109 if (m_onSuccess != NULL) {
110 JSValueSafeProtect(m_context, m_onSuccess);
114 JSValueRef JSCallbackManager::getOnSuccess() const
119 void JSCallbackManager::setOnError(JSValueRef onError)
121 if (m_onError != NULL) {
122 JSValueSafeUnprotect(m_context, m_onError);
126 m_onError = JSValueToObject(m_context, onError, NULL);
131 if (m_onError != NULL) {
132 JSValueSafeProtect(m_context, m_onError);
136 JSValueRef JSCallbackManager::getOnError() const
141 void JSCallbackManager::setObject(JSObjectRef object)
143 if (m_object != NULL) {
144 JSValueSafeUnprotect(m_context, m_object);
149 if (m_object != NULL) {
150 JSValueSafeProtect(m_context, m_object);
154 JSObjectRef JSCallbackManager::getObject() const
159 void JSCallbackManager::setContext(JSContextRef context)
164 void JSCallbackManager::callOnSuccess()
166 if (m_onSuccess == NULL) {
167 //LogDebug("Success callback is not set");
170 makeCallback(m_context, NULL, m_onSuccess, NULL, 0);
173 void JSCallbackManager::callOnSuccess(JSValueRef obj)
175 if (m_onSuccess == NULL) {
176 //LogDebug("Success callback is not set");
179 JSValueRef objParam[1] = { obj };
180 makeCallback(m_context, NULL, m_onSuccess, objParam, 1);
183 void JSCallbackManager::callOnSuccess(JSValueRef obj[],
186 if (m_onSuccess == NULL) {
187 //LogDebug("Success callback is not set");
190 makeCallback(m_context, NULL, m_onSuccess, obj, paramCount);
193 void JSCallbackManager::callOnError()
195 if (m_onError == NULL) {
196 //LogDebug("Error callback is not set");
199 makeCallback(m_context, NULL, m_onError, NULL, 0);
202 void JSCallbackManager::callOnError(JSValueRef obj)
204 if (m_onError == NULL) {
205 //LogDebug("Error callback is not set");
208 JSValueRef objParam[1] = { obj };
209 makeCallback(m_context, NULL, m_onError, objParam, 1);
212 void JSCallbackManager::callOnError(JSValueRef obj[],
215 if (m_onError == NULL) {
216 //LogDebug("Error callback is not set");
219 makeCallback(m_context, NULL, m_onError, obj, paramCount);
222 void JSCallbackManager::makeCallback(JSContextRef context,
224 JSObjectRef callback,
228 if (callback == NULL) {
229 //LogError("callback is NULL");
233 if (JSObjectIsFunction(context, callback)) {
234 JSValueRef exception = NULL;
237 JSObjectCallAsFunction(context, callback, object, 0, NULL, &exception);
239 JSObjectCallAsFunction(context, callback, object, argc, argv, &exception);
243 WKBundleReportException(context, exception);