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>
26 namespace WrtDeviceApis {
27 namespace CommonsJavaScript {
28 JSCallbackManagerPtr JSCallbackManager::createObject(
33 acceptJSNullAsOnSuccess,
34 bool acceptJSNullAsOnError)
36 JSObjectRef l_onSuccess = NULL;
37 JSObjectRef l_onError = NULL;
39 if (NULL != onSuccess &&
40 (!acceptJSNullAsOnSuccess || !JSValueIsNull(context, onSuccess)))
42 l_onSuccess = JSValueToObject(context, onSuccess, NULL);
44 !JSObjectIsFunction(context, l_onSuccess))
46 ThrowMsg(Commons::InvalidArgumentException,
47 "success callback is not a function");
50 //LogWarning("onSuccessCallback is NULL and is not registred");
52 if (NULL != onError &&
53 (!acceptJSNullAsOnError || !JSValueIsNull(context, onError)))
55 l_onError = JSValueToObject(context, onError, NULL);
57 !JSObjectIsFunction(context, l_onError))
59 ThrowMsg(Commons::InvalidArgumentException,
60 "error callback is not a function");
63 //LogWarning("onErrorCallback is NULL and is not registred");
65 return JSCallbackManagerPtr(new JSCallbackManager(context, l_onSuccess,
69 JSCallbackManager::JSCallbackManager(JSContextRef context,
70 JSObjectRef onSuccess,
71 JSObjectRef onError) :
77 setOnSuccess(onSuccess);
81 JSCallbackManager::~JSCallbackManager()
84 JSValueUnprotect(m_context, m_onSuccess);
88 JSValueUnprotect(m_context, m_onError);
92 JSValueUnprotect(m_context, m_object);
96 void JSCallbackManager::setOnSuccess(JSValueRef onSuccess)
98 if (m_onSuccess != NULL) {
99 JSValueUnprotect(m_context, m_onSuccess);
103 m_onSuccess = JSValueToObject(m_context, onSuccess, NULL);
108 if (m_onSuccess != NULL) {
109 JSValueProtect(m_context, m_onSuccess);
113 JSValueRef JSCallbackManager::getOnSuccess() const
118 void JSCallbackManager::setOnError(JSValueRef onError)
120 if (m_onError != NULL) {
121 JSValueUnprotect(m_context, m_onError);
125 m_onError = JSValueToObject(m_context, onError, NULL);
130 if (m_onError != NULL) {
131 JSValueProtect(m_context, m_onError);
135 JSValueRef JSCallbackManager::getOnError() const
140 void JSCallbackManager::setObject(JSObjectRef object)
142 if (m_object != NULL) {
143 JSValueUnprotect(m_context, m_object);
148 if (m_object != NULL) {
149 JSValueProtect(m_context, m_object);
153 JSObjectRef JSCallbackManager::getObject() const
158 void JSCallbackManager::setContext(JSContextRef context)
163 void JSCallbackManager::callOnSuccess()
165 if (m_onSuccess == NULL) {
166 //LogDebug("Success callback is not set");
169 makeCallback(m_context, NULL, m_onSuccess, NULL, 0);
172 void JSCallbackManager::callOnSuccess(JSValueRef obj)
174 if (m_onSuccess == NULL) {
175 //LogDebug("Success callback is not set");
178 JSValueRef objParam[1] = { obj };
179 makeCallback(m_context, NULL, m_onSuccess, objParam, 1);
182 void JSCallbackManager::callOnSuccess(JSValueRef obj[],
185 if (m_onSuccess == NULL) {
186 //LogDebug("Success callback is not set");
189 makeCallback(m_context, NULL, m_onSuccess, obj, paramCount);
192 void JSCallbackManager::callOnError()
194 if (m_onError == NULL) {
195 //LogDebug("Error callback is not set");
198 makeCallback(m_context, NULL, m_onError, NULL, 0);
201 void JSCallbackManager::callOnError(JSValueRef obj)
203 if (m_onError == NULL) {
204 //LogDebug("Error callback is not set");
207 JSValueRef objParam[1] = { obj };
208 makeCallback(m_context, NULL, m_onError, objParam, 1);
211 void JSCallbackManager::callOnError(JSValueRef obj[],
214 if (m_onError == NULL) {
215 //LogDebug("Error callback is not set");
218 makeCallback(m_context, NULL, m_onError, obj, paramCount);
221 void JSCallbackManager::makeCallback(JSContextRef context,
223 JSObjectRef callback,
227 if (callback == NULL) {
228 //LogError("callback is NULL");
232 if (JSObjectIsFunction(context, callback)) {
233 JSValueRef exception = NULL;
236 JSObjectCallAsFunction(context, callback, object, 0, NULL, &exception);
238 JSObjectCallAsFunction(context, callback, object, argc, argv, &exception);
242 WKBundleReportException(context, exception);