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