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