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