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