merge with master
[platform/framework/web/wrt-plugins-common.git] / src / CommonsJavaScript / JSCallbackManager.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        JSCalendar.cpp
18  * @author      Lukasz Marek (l.marek@samsung.com)
19  * @version     0.1
20  */
21
22 #include "JSCallbackManager.h"
23 #include <dpl/log/log.h>
24
25 namespace WrtDeviceApis {
26 namespace CommonsJavaScript {
27 JSCallbackManagerPtr JSCallbackManager::createObject(
28     JSContextRef context,
29     JSValueRef onSuccess,
30     JSValueRef onError,
31     bool
32     acceptJSNullAsOnSuccess,
33     bool acceptJSNullAsOnError)
34 {
35     LogDebug("entered");
36     JSObjectRef l_onSuccess = NULL;
37     JSObjectRef l_onError = NULL;
38
39     LogDebug("onSuccessCallback ptr=" << onSuccess);
40     LogDebug("onErrorCallback   ptr=" << onError);
41     if (NULL != onSuccess &&
42         (!acceptJSNullAsOnSuccess || !JSValueIsNull(context, onSuccess)))
43     {
44         l_onSuccess = JSValueToObject(context, onSuccess, NULL);
45         if (!l_onSuccess ||
46             !JSObjectIsFunction(context, l_onSuccess))
47         {
48             ThrowMsg(Commons::InvalidArgumentException,
49                      "success callback is not a function");
50         }
51     } else {
52         LogWarning("onSuccessCallback is NULL and is not registred");
53     }
54     if (NULL != onError &&
55         (!acceptJSNullAsOnError || !JSValueIsNull(context, onError)))
56     {
57         l_onError = JSValueToObject(context, onError, NULL);
58         if (!l_onError ||
59             !JSObjectIsFunction(context, l_onError))
60         {
61             ThrowMsg(Commons::InvalidArgumentException,
62                      "error callback is not a function");
63         }
64     } else {
65         LogWarning("onErrorCallback is NULL and is not registred");
66     }
67     return JSCallbackManagerPtr(new JSCallbackManager(context, l_onSuccess,
68                                                       l_onError));
69 }
70
71 JSCallbackManager::JSCallbackManager(JSContextRef context,
72                                      JSObjectRef onSuccess,
73                                      JSObjectRef onError) :
74     m_onSuccess(NULL),
75     m_onError(NULL),
76     m_context(context),
77     m_object(NULL)
78 {
79     LogDebug("entered");
80
81     setOnSuccess(onSuccess);
82     setOnError(onError);
83 }
84
85 JSCallbackManager::~JSCallbackManager()
86 {
87     LogDebug("entered");
88
89     if (m_onSuccess) {
90         JSValueUnprotect(m_context, m_onSuccess);
91     }
92
93     if (m_onError) {
94         JSValueUnprotect(m_context, m_onError);
95     }
96
97     if (m_object) {
98         JSValueUnprotect(m_context, m_object);
99     }
100 }
101
102 void JSCallbackManager::setOnSuccess(JSValueRef onSuccess)
103 {
104     LogDebug("entered");
105     if (m_onSuccess != NULL) {
106         JSValueUnprotect(m_context, m_onSuccess);
107     }
108
109     if (onSuccess) {
110         m_onSuccess = JSValueToObject(m_context, onSuccess, NULL);
111     } else {
112         m_onSuccess = NULL;
113     }
114
115     if (m_onSuccess != NULL) {
116         JSValueProtect(m_context, m_onSuccess);
117     }
118 }
119
120 JSValueRef JSCallbackManager::getOnSuccess() const
121 {
122     LogDebug("entered");
123     return m_onSuccess;
124 }
125
126 void JSCallbackManager::setOnError(JSValueRef onError)
127 {
128     LogDebug("entered");
129     if (m_onError != NULL) {
130         JSValueUnprotect(m_context, m_onError);
131     }
132
133     if (onError) {
134         m_onError = JSValueToObject(m_context, onError, NULL);
135     } else {
136         m_onError = NULL;
137     }
138
139     if (m_onError != NULL) {
140         JSValueProtect(m_context, m_onError);
141     }
142 }
143
144 JSValueRef JSCallbackManager::getOnError() const
145 {
146     LogDebug("entered");
147     return m_onError;
148 }
149
150 void JSCallbackManager::setObject(JSObjectRef object)
151 {
152     LogDebug("entered");
153     if (m_object != NULL) {
154         JSValueUnprotect(m_context, m_object);
155     }
156
157     m_object = object;
158
159     if (m_object != NULL) {
160         JSValueProtect(m_context, m_object);
161     }
162 }
163
164 JSObjectRef JSCallbackManager::getObject() const
165 {
166     LogDebug("entered");
167     return m_object;
168 }
169
170 void JSCallbackManager::setContext(JSContextRef context)
171 {
172     LogDebug("entered");
173     m_context = context;
174 }
175
176 void JSCallbackManager::callOnSuccess()
177 {
178     LogDebug("entered");
179     if (m_onSuccess == NULL) {
180         LogDebug("Success callback is not set");
181         return;
182     }
183     makeCallback(m_context, NULL, m_onSuccess, NULL, 0);
184 }
185
186 void JSCallbackManager::callOnSuccess(JSValueRef obj)
187 {
188     LogDebug("entered");
189     if (m_onSuccess == NULL) {
190         LogDebug("Success callback is not set");
191         return;
192     }
193     JSValueRef objParam[1] = { obj };
194     makeCallback(m_context, NULL, m_onSuccess, objParam, 1);
195 }
196
197 void JSCallbackManager::callOnSuccess(JSValueRef obj[],
198                                       int paramCount)
199 {
200     LogDebug("entered");
201     if (m_onSuccess == NULL) {
202         LogDebug("Success callback is not set");
203         return;
204     }
205     makeCallback(m_context, NULL, m_onSuccess, obj, paramCount);
206 }
207
208 void JSCallbackManager::callOnError()
209 {
210     LogDebug("entered");
211     if (m_onError == NULL) {
212         LogDebug("Error callback is not set");
213         return;
214     }
215     makeCallback(m_context, NULL, m_onError, NULL, 0);
216 }
217
218 void JSCallbackManager::callOnError(JSValueRef obj)
219 {
220     LogDebug("entered");
221     if (m_onError == NULL) {
222         LogDebug("Error callback is not set");
223         return;
224     }
225     JSValueRef objParam[1] = { obj };
226     makeCallback(m_context, NULL, m_onError, objParam, 1);
227 }
228
229 void JSCallbackManager::callOnError(JSValueRef obj[],
230                                     int paramCount)
231 {
232     LogDebug("entered");
233     if (m_onError == NULL) {
234         LogDebug("Error callback is not set");
235         return;
236     }
237     makeCallback(m_context, NULL, m_onError, obj, paramCount);
238 }
239
240 void JSCallbackManager::makeCallback(JSContextRef context,
241                                      JSObjectRef object,
242                                      JSObjectRef callback,
243                                      JSValueRef argv[],
244                                      unsigned argc)
245 {
246     LogDebug("entered");
247
248     if (callback == NULL) {
249         LogError("callback is NULL");
250         return;
251     }
252
253     if (JSObjectIsFunction(context, callback)) {
254         if (argc == 0) {
255             LogDebug("Calling object directly, no arguments");
256             JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
257         } else {
258             LogDebug("Calling object directly, one argument");
259             JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);
260         }
261         return;
262     }
263 }
264 }
265 }