1759b33ffe0939ef2f518fc118714885a89cddef
[framework/web/wrt-plugins-tizen.git] / src / NetworkBearerSelection / JSNetworkBearerSelectionCallbackManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "JSNetworkBearerSelectionCallbackManager.h"
19 #include <Logger.h>
20
21 namespace DeviceAPI {
22 namespace NetworkBearerSelection {
23
24
25 JSNetworkBearerSelectionCallbackManagerPtr JSNetworkBearerSelectionCallbackManager::createObject(JSContextRef context)
26 {
27         return JSNetworkBearerSelectionCallbackManagerPtr(new JSNetworkBearerSelectionCallbackManager(context));
28 }
29
30 JSNetworkBearerSelectionCallbackManager::JSNetworkBearerSelectionCallbackManager(JSContextRef context,
31                 JSObjectRef onSuccess,
32                 JSObjectRef onPaused,
33                 JSObjectRef onResumed,
34                 JSObjectRef onDisconnected,             
35                 JSObjectRef onError ) :
36                 m_onSuccess(NULL),
37                 m_onPaused(NULL),
38                 m_onResumed(NULL),
39                 m_onDisconnected(NULL),
40                 m_onError(NULL),
41                 m_context(context),
42                 m_object(NULL)
43 {
44         setOnSuccess(onSuccess);
45         setOnPaused(onPaused);
46         setOnResumed(onResumed);
47         setOnDisconneced(onDisconnected);
48     setOnError(onError);
49 }
50
51 JSNetworkBearerSelectionCallbackManager::~JSNetworkBearerSelectionCallbackManager()
52 {
53         if(m_onSuccess)
54         {
55                 JSValueUnprotect(m_context, m_onSuccess);
56         }
57
58         if(m_onPaused)
59         {
60                 JSValueUnprotect(m_context, m_onPaused);
61         }
62
63         if(m_onResumed)
64         {
65                 JSValueUnprotect(m_context, m_onResumed);
66         }
67
68         if(m_onDisconnected)
69         {
70                 JSValueUnprotect(m_context, m_onDisconnected);
71         }
72
73     if(m_onError)
74     {
75         JSValueUnprotect(m_context, m_onError);
76     }
77 }
78
79 void JSNetworkBearerSelectionCallbackManager::setOnSuccess(JSValueRef onSuccess)
80 {
81         if (onSuccess)
82         {
83                 if (m_onSuccess != NULL)
84                 {
85                         JSValueUnprotect(m_context, m_onSuccess);
86                 }
87
88                 m_onSuccess = JSValueToObject( m_context, onSuccess, NULL );
89
90                 if (m_onSuccess != NULL)
91                 {
92                         JSValueProtect(m_context, m_onSuccess);
93                 }
94         }
95 }
96
97 JSValueRef JSNetworkBearerSelectionCallbackManager::getOnSuccess() const
98 {
99         return m_onSuccess;
100 }
101
102 void JSNetworkBearerSelectionCallbackManager::setOnPaused(JSValueRef onPaused)
103 {
104         if (onPaused)
105         {
106                 if (m_onPaused != NULL)
107                 {
108                         JSValueUnprotect(m_context, m_onPaused);
109                 }
110
111                 m_onPaused = JSValueToObject( m_context, onPaused, NULL );
112
113                 if (m_onPaused != NULL)
114                 {
115                         JSValueProtect(m_context, m_onPaused);
116                 }
117         }
118 }
119
120 JSValueRef JSNetworkBearerSelectionCallbackManager::getOnPaused() const
121 {
122         return m_onPaused;
123 }
124
125 void JSNetworkBearerSelectionCallbackManager::setOnResumed(JSValueRef onResumed)
126 {
127         if (onResumed)
128         {
129                 if (m_onResumed != NULL)
130                 {
131                         JSValueUnprotect(m_context, m_onResumed);
132                 }
133
134                 m_onResumed = JSValueToObject( m_context, onResumed, NULL );
135
136                 if (m_onResumed != NULL)
137                 {
138                         JSValueProtect(m_context, m_onResumed);
139                 }
140         }
141 }
142
143 JSValueRef JSNetworkBearerSelectionCallbackManager::getOnResumed() const
144 {
145         return m_onResumed;
146 }
147
148 void JSNetworkBearerSelectionCallbackManager::setOnDisconneced(JSValueRef onDisconnected)
149 {
150         if (onDisconnected)
151         {
152                 if (m_onDisconnected != NULL)
153                 {
154                         JSValueUnprotect(m_context, m_onDisconnected);
155                 }
156
157                 m_onDisconnected = JSValueToObject( m_context, onDisconnected, NULL );
158
159                 if (m_onDisconnected != NULL)
160                 {
161                         JSValueProtect(m_context, m_onDisconnected);
162                 }
163         }
164 }
165
166 JSValueRef JSNetworkBearerSelectionCallbackManager::getOnDisconneced() const
167 {
168         return m_onDisconnected;
169 }
170
171 void JSNetworkBearerSelectionCallbackManager::setOnError(JSValueRef onError)
172 {
173     if (onError)
174     {
175         if (m_onError != NULL)
176         {
177             JSValueUnprotect(m_context, m_onError);
178         }
179
180         m_onError = JSValueToObject(m_context, onError, NULL);
181
182         if (m_onError != NULL)
183         {
184             JSValueProtect(m_context, m_onError);
185         }
186     }
187 }
188
189 JSValueRef JSNetworkBearerSelectionCallbackManager::getOnError() const
190 {
191     return m_onError;
192 }
193
194 void JSNetworkBearerSelectionCallbackManager::setContext(JSContextRef context)
195 {
196     m_context = context;
197 }
198
199 void JSNetworkBearerSelectionCallbackManager::setObject(JSObjectRef object)
200 {
201     m_object = object;
202 }
203
204 JSObjectRef JSNetworkBearerSelectionCallbackManager::getObject() const
205 {
206     return m_object;
207 }
208
209 void JSNetworkBearerSelectionCallbackManager::callOnSuccess()
210 {
211     if (m_onSuccess == NULL)
212     {
213         //LoggerD("oncontactsadded callback is not set");
214         return;
215     }
216
217     makeCallback(m_context, NULL, m_onSuccess, "onsuccess", NULL, 0);
218 }
219
220 void JSNetworkBearerSelectionCallbackManager::callOnPaused()
221 {
222     if (m_onPaused == NULL)
223     {
224         //LoggerD("oncontactsadded callback is not set");
225         return;
226     }
227
228     makeCallback(m_context, NULL, m_onPaused, "onpaused", NULL, 0);
229 }
230
231 void JSNetworkBearerSelectionCallbackManager::callOnResumed()
232 {
233     if (m_onResumed == NULL)
234     {
235         //LoggerD("oncontactsadded callback is not set");
236         return;
237     }
238
239     makeCallback(m_context, NULL, m_onResumed, "onresumed", NULL, 0);
240 }
241
242 void JSNetworkBearerSelectionCallbackManager::callOnDisconnected()
243 {
244     if (m_onDisconnected == NULL)
245     {
246         //LoggerD("oncontactsadded callback is not set");
247         return;
248     }
249
250     makeCallback(m_context, NULL, m_onDisconnected, "ondisconnected", NULL, 0);
251 }
252
253 void JSNetworkBearerSelectionCallbackManager::callOnError(JSValueRef error)
254 {
255     if (m_onError == NULL)
256     {
257         //LoggerD("Error callback is not set");
258         return;
259     }
260     JSValueRef objParam[1] = { error };
261     makeCallback(m_context, NULL, m_onError, "onError", objParam, 1);
262 }
263
264 void JSNetworkBearerSelectionCallbackManager::makeCallback(JSContextRef context, JSObjectRef object, JSObjectRef callback, const char *szName, JSValueRef argv[], unsigned argc)
265 {
266     if (callback == NULL)
267     {
268         LoggerE("callback is NULL");
269         return;
270     }
271
272     if (JSObjectIsFunction(context, callback))
273     {
274         if (argc == 0)
275         {
276                 //LoggerD("Calling object directly, no arguments");
277                 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
278         }
279         else
280         {
281                 //LoggerD("Calling object directly, one argument");
282                 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);
283         }
284         return;
285     }
286 }
287
288 }
289 }