tizen beta release
[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_context(context)
72 {
73     LogDebug("entered");
74
75     setOnSuccess(onSuccess);
76     setOnError(onError);
77 }
78
79 JSCallbackManager::~JSCallbackManager()
80 {
81     LogDebug("entered");
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
92 void JSCallbackManager::setOnSuccess(JSValueRef onSuccess)
93 {
94     LogDebug("entered");
95     if (onSuccess) {
96         if (m_onSuccess != NULL) {
97             JSValueUnprotect(m_context, m_onSuccess);
98         }
99
100         m_onSuccess = JSValueToObject(m_context, onSuccess, NULL);
101
102         if (m_onSuccess != NULL) {
103             JSValueProtect(m_context, m_onSuccess);
104         }
105     }
106 }
107
108 JSValueRef JSCallbackManager::getOnSuccess() const
109 {
110     LogDebug("entered");
111     return m_onSuccess;
112 }
113
114 void JSCallbackManager::setOnError(JSValueRef onError)
115 {
116     LogDebug("entered");
117     if (onError) {
118         if (m_onError != NULL) {
119             JSValueUnprotect(m_context, m_onError);
120         }
121
122         m_onError = JSValueToObject(m_context, onError, NULL);
123
124         if (m_onError != NULL) {
125             JSValueProtect(m_context, m_onError);
126         }
127     }
128 }
129
130 JSValueRef JSCallbackManager::getOnError() const
131 {
132     LogDebug("entered");
133     return m_onError;
134 }
135
136 void JSCallbackManager::setContext(JSContextRef context)
137 {
138     LogDebug("entered");
139     m_context = context;
140 }
141
142 void JSCallbackManager::callOnSuccess()
143 {
144     LogDebug("entered");
145     if (m_onSuccess == NULL) {
146         LogDebug("Success callback is not set");
147         return;
148     }
149     makeCallback(m_context, NULL, m_onSuccess, NULL, 0);
150 }
151
152 void JSCallbackManager::callOnSuccess(JSValueRef obj)
153 {
154     LogDebug("entered");
155     if (m_onSuccess == NULL) {
156         LogDebug("Success callback is not set");
157         return;
158     }
159     JSValueRef objParam[1] = { obj };
160     makeCallback(m_context, NULL, m_onSuccess, objParam, 1);
161 }
162
163 void JSCallbackManager::callOnSuccess(JSValueRef obj[],
164         int paramCount)
165 {
166     LogDebug("entered");
167     if (m_onSuccess == NULL) {
168         LogDebug("Success callback is not set");
169         return;
170     }
171     makeCallback(m_context, NULL, m_onSuccess, obj, paramCount);
172 }
173
174 void JSCallbackManager::callOnError()
175 {
176     LogDebug("entered");
177     if (m_onError == NULL) {
178         LogDebug("Error callback is not set");
179         return;
180     }
181     makeCallback(m_context, NULL, m_onError, NULL, 0);
182 }
183
184 void JSCallbackManager::callOnError(JSValueRef obj)
185 {
186     LogDebug("entered");
187     if (m_onError == NULL) {
188         LogDebug("Error callback is not set");
189         return;
190     }
191     JSValueRef objParam[1] = { obj };
192     makeCallback(m_context, NULL, m_onError, objParam, 1);
193 }
194
195 void JSCallbackManager::callOnError(JSValueRef obj[],
196         int paramCount)
197 {
198     LogDebug("entered");
199     if (m_onError == NULL) {
200         LogDebug("Error callback is not set");
201         return;
202     }
203     makeCallback(m_context, NULL, m_onError, obj, paramCount);
204 }
205
206 void JSCallbackManager::makeCallback(JSContextRef context,
207                                      JSObjectRef object,
208                                      JSObjectRef callback,
209                                      JSValueRef argv[],
210                                      unsigned argc)
211 {
212     LogDebug("entered");
213
214     if (callback == NULL) {
215         LogError("callback is NULL");
216         return;
217     }
218
219     if (JSObjectIsFunction(context, callback)) {
220         if (argc == 0) {
221             LogDebug("Calling object directly, no arguments");
222             JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
223         } else {
224             LogDebug("Calling object directly, one argument");
225             JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);
226         }
227         return;
228     }
229 }
230
231 }
232 }