wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Application / JSApplicationEventCallbackManager.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 "JSApplicationEventCallbackManager.h"
19 #include <Logger.h>
20
21 namespace DeviceAPI {
22 namespace Application {
23
24 JSApplicationEventCallbackManagerPtr JSApplicationEventCallbackManager::createObject(JSContextRef context)
25 {
26         return JSApplicationEventCallbackManagerPtr( new JSApplicationEventCallbackManager(context) );
27 }
28
29 JSApplicationEventCallbackManager::JSApplicationEventCallbackManager(JSContextRef context,
30                 JSObjectRef onInstalled,
31                 JSObjectRef onUpdated,
32                 JSObjectRef onUninstalled,
33                 JSObjectRef onError ) :
34                 m_onInstalled(NULL),
35                 m_onUpdated(NULL),
36                 m_onUninstalled(NULL),
37                 m_onError(NULL),
38                 m_context(context),
39                 m_object(NULL)
40 {
41         setOnInstalled(onInstalled);
42         setOnUpdated(onUpdated);
43         setOnUninstalled(onUninstalled);
44     setOnError(onError);
45 }
46
47 JSApplicationEventCallbackManager::~JSApplicationEventCallbackManager()
48 {
49         if(m_onInstalled)
50         {
51                 JSValueUnprotect(m_context, m_onInstalled);
52         }
53
54         if(m_onUpdated)
55         {
56                 JSValueUnprotect(m_context, m_onUpdated);
57         }
58
59         if(m_onUninstalled)
60         {
61                 JSValueUnprotect(m_context, m_onUninstalled);
62         }
63
64     if(m_onError)
65     {
66         JSValueUnprotect(m_context, m_onError);
67     }
68 }
69
70 void JSApplicationEventCallbackManager::setOnInstalled( JSValueRef onInstalled )
71 {
72         if (onInstalled)
73         {
74                 if (m_onInstalled != NULL)
75                 {
76                         JSValueUnprotect(m_context, m_onInstalled);
77                 }
78
79                 m_onInstalled = JSValueToObject( m_context, onInstalled, NULL );
80
81                 if (m_onInstalled != NULL)
82                 {
83                         JSValueProtect(m_context, m_onInstalled);
84                 }
85         }
86 }
87
88 JSValueRef JSApplicationEventCallbackManager::getOnInstalled() const
89 {
90         return m_onInstalled;
91 }
92
93 void JSApplicationEventCallbackManager::setOnUpdated( JSValueRef onUpdated )
94 {
95         if (onUpdated)
96         {
97                 if (m_onUpdated != NULL)
98                 {
99                         JSValueUnprotect(m_context, m_onUpdated);
100                 }
101
102                 m_onUpdated = JSValueToObject( m_context, onUpdated, NULL );
103
104                 if (m_onUpdated != NULL)
105                 {
106                         JSValueProtect(m_context, m_onUpdated);
107                 }
108         }
109 }
110
111 JSValueRef JSApplicationEventCallbackManager::getOnUpdated() const
112 {
113         return m_onUpdated;
114 }
115
116 void JSApplicationEventCallbackManager::setOnUninstalled( JSValueRef onUninstalled )
117 {
118         if (onUninstalled)
119         {
120                 if (m_onUninstalled != NULL)
121                 {
122                         JSValueUnprotect(m_context, m_onUninstalled);
123                 }
124
125                 m_onUninstalled = JSValueToObject( m_context, onUninstalled, NULL );
126
127                 if (m_onUninstalled != NULL)
128                 {
129                         JSValueProtect(m_context, m_onUninstalled);
130                 }
131         }
132 }
133
134 JSValueRef JSApplicationEventCallbackManager::getOnUninstalled() const
135 {
136         return m_onUninstalled;
137 }
138
139 void JSApplicationEventCallbackManager::setOnError( JSValueRef onError )
140 {
141     if (onError)
142     {
143         if (m_onError != NULL)
144         {
145             JSValueUnprotect(m_context, m_onError);
146         }
147
148         m_onError = JSValueToObject( m_context, onError, NULL );
149
150         if (m_onError != NULL)
151         {
152             JSValueProtect(m_context, m_onError);
153         }
154     }
155 }
156
157 JSValueRef JSApplicationEventCallbackManager::getOnError() const
158 {
159     return m_onError;
160 }
161
162 void JSApplicationEventCallbackManager::setContext( JSContextRef context )
163 {
164     m_context = context;
165 }
166
167 void JSApplicationEventCallbackManager::setObject( JSObjectRef object )
168 {
169     m_object = object;
170 }
171
172 JSObjectRef JSApplicationEventCallbackManager::getObject() const
173 {
174     return m_object;
175 }
176
177 void JSApplicationEventCallbackManager::callOnInstalled( JSValueRef appInfo )
178 {
179     if ( m_onInstalled == NULL )
180     {
181         //LoggerD("oninstalled callback is not set");
182         return;
183     }
184     JSValueRef objParam[1] = { appInfo };
185     makeCallback( m_context, NULL, m_onInstalled, "oninstalled", objParam, 1 );
186 }
187
188 void JSApplicationEventCallbackManager::callOnUpdated( JSValueRef appInfo )
189 {
190     if ( m_onUpdated == NULL )
191     {
192         //LoggerD("onupdated callback is not set");
193         return;
194     }
195     JSValueRef objParam[1] = { appInfo };
196     makeCallback( m_context, NULL, m_onUpdated, "onupdated", objParam, 1 );
197 }
198
199 void JSApplicationEventCallbackManager::callOnUninstalled( JSValueRef appId )
200 {
201     if ( m_onUninstalled == NULL )
202     {
203         //LoggerD("onuninstalled callback is not set");
204         return;
205     }
206     JSValueRef objParam[1] = { appId };
207     makeCallback( m_context, NULL, m_onUninstalled, "onuninstalled", objParam, 1 );
208 }
209
210 void JSApplicationEventCallbackManager::callOnError( JSValueRef error )
211 {
212     if ( m_onError == NULL )
213     {
214         //LoggerD("Error callback is not set");
215         return;
216     }
217     JSValueRef objParam[1] = { error };
218     makeCallback( m_context, NULL, m_onError, "onerror", objParam, 1 );
219 }
220
221 void JSApplicationEventCallbackManager::makeCallback(JSContextRef context, JSObjectRef object, JSObjectRef callback, const char *szName, JSValueRef argv[], unsigned argc)
222 {
223
224     if (callback == NULL)
225     {
226         LoggerE("callback is NULL");
227         return;
228     }
229
230     if (JSObjectIsFunction(context, callback))
231     {
232         if (argc == 0)
233         {
234                 //LoggerD("Calling object directly, no arguments");
235                 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
236         }
237         else
238         {
239                 //LoggerD("Calling object directly, one argument");
240                 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);
241         }
242         return;
243     }
244 }
245
246 } // Application
247 } // DeviceAPI