wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Push / JSPushManager.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 <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
26
27 #include <JSApplicationControl.h>
28 #include <TimeTracer.h>
29
30 #include "plugin_config.h"
31
32 #include "JSPushManager.h"
33
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
36
37 namespace DeviceAPI {
38 namespace Push {
39
40 JSClassDefinition JSPushManager::m_classInfo = {
41     0,
42     kJSClassAttributeNone,
43     "PushManager",
44     NULL, //ParentClass
45     NULL, //StaticValues
46     m_function, //StaticFunctions
47     initialize, //Initialize
48     finalize, //Finalize
49     NULL, //HasProperty,
50     NULL, //GetProperty,
51     NULL, //SetProperty,
52     NULL, //DeleteProperty,
53     NULL, //GetPropertyNames,
54     NULL, //CallAsFunction,
55     NULL, //CallAsConstructor,
56     NULL, //HasInstance,
57     NULL //ConvertToType
58 };
59
60 JSStaticFunction JSPushManager::m_function[] = {
61     { PUSH_MANAGER_API_REGISTER_SERVICE, registerService, kJSPropertyAttributeNone },
62     { PUSH_MANAGER_API_UNREGISTER_SERVICE, unregisterService, kJSPropertyAttributeNone },
63     { PUSH_MANAGER_API_CONNECT_SERVICE, connectService, kJSPropertyAttributeNone },
64     { PUSH_MANAGER_API_DISCONNECT_SERVICE, disconnectService, kJSPropertyAttributeNone },
65     { PUSH_MANAGER_API_GET_REGISTRATION_ID, getRegistrationId, kJSPropertyAttributeNone },
66     { 0, 0, 0 }
67 };
68
69 JSClassRef JSPushManager::m_jsClassRef = JSClassCreate(JSPushManager::getClassInfo());
70
71 const JSClassRef JSPushManager::getClassRef()
72 {
73     if (!m_jsClassRef) {
74         m_jsClassRef = JSClassCreate(&m_classInfo);
75     }
76     return m_jsClassRef;
77 }
78
79 const JSClassDefinition* JSPushManager::getClassInfo()
80 {
81     return &m_classInfo;
82 }
83
84 void JSPushManager::initialize(JSContextRef context, JSObjectRef object)
85 {
86     if (!JSObjectGetPrivate(object)) {
87         PushManager *priv = PushManager::getInstance();
88         JSObjectSetPrivate(object, static_cast<void*>(priv));
89     }
90 }
91
92 void JSPushManager::finalize(JSObjectRef object)
93 {
94 }
95
96 JSValueRef JSPushManager::registerService(JSContextRef context,
97         JSObjectRef object,
98         JSObjectRef thisObject,
99         size_t argumentCount,
100         const JSValueRef arguments[],
101         JSValueRef* exception)
102 {
103         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
104     AceSecurityStatus status = PUSH_CHECK_ACCESS(PUSH_MANAGER_API_REGISTER_SERVICE);
105     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
106
107     try {
108         // Private Object
109         PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
110         if (!priv) {
111             throw TypeMismatchException("Private object is NULL.");
112         }
113
114         ArgumentValidator validator(context, argumentCount, arguments);
115
116         // appControl
117         DeviceAPI::Application::ApplicationControlPtr appControl(NULL);
118         JSObjectRef appControlObj = validator.toObject(0, DeviceAPI::Application::JSApplicationControl::getClassRef());
119         if (appControlObj) {
120             JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv *>(JSObjectGetPrivate(appControlObj));
121             if (!priv) {
122                 throw TypeMismatchException("ApplicationControl's private object is NULL.");
123             }
124             appControl = priv->getObject();
125         }
126
127         MultiCallbackUserDataPtr callback(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
128
129         // successCallback
130         JSObjectRef successCallbackObj = validator.toFunction(1);
131         if (successCallbackObj) {
132             callback->setCallback("onsuccess", successCallbackObj);
133         }
134
135         // errorCallback
136         JSObjectRef errorCallbackObj = validator.toFunction(2, true);
137         if (errorCallbackObj) {
138             callback->setCallback("onerror", errorCallbackObj);
139         }
140
141         // perform
142         priv->registerService(appControl, callback);
143
144                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
145         return JSValueMakeUndefined(context);
146     } catch (const BasePlatformException &err) {
147         LoggerE(err.getName() << ": " << err.getMessage());
148         return JSWebAPIException::throwException(context, exception, err);
149     } catch (...) {
150         DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.registerService().");
151         LoggerE(err.getName() << ": " << err.getMessage());
152         return JSWebAPIException::throwException(context, exception, err);
153     }
154 }
155
156 JSValueRef JSPushManager::unregisterService(JSContextRef context,
157         JSObjectRef object,
158         JSObjectRef thisObject,
159         size_t argumentCount,
160         const JSValueRef arguments[],
161         JSValueRef* exception)
162 {
163         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
164     AceSecurityStatus status = PUSH_CHECK_ACCESS(PUSH_MANAGER_API_UNREGISTER_SERVICE);
165     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
166
167     try {
168         // Private Object
169         PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
170         if (!priv) {
171             throw TypeMismatchException("Private object is NULL.");
172         }
173
174         ArgumentValidator validator(context, argumentCount, arguments);
175
176         MultiCallbackUserDataPtr callback(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
177
178         // successCallback
179         JSObjectRef successCallbackObj = validator.toFunction(0, true);
180         if (successCallbackObj) {
181             callback->setCallback("onsuccess", successCallbackObj);
182         }
183
184         // errorCallback
185         JSObjectRef errorCallbackObj = validator.toFunction(1, true);
186         if (errorCallbackObj) {
187             callback->setCallback("onerror", errorCallbackObj);
188         }
189
190         // perform
191         priv->unregisterService(callback);
192
193                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
194         return JSValueMakeUndefined(context);
195     } catch (const BasePlatformException &err) {
196         LoggerE(err.getName() << ": " << err.getMessage());
197         return JSWebAPIException::throwException(context, exception, err);
198     } catch (...) {
199         DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.unregisterService().");
200         LoggerE(err.getName() << ": " << err.getMessage());
201         return JSWebAPIException::throwException(context, exception, err);
202     }
203 }
204
205 JSValueRef JSPushManager::connectService(JSContextRef context,
206         JSObjectRef object,
207         JSObjectRef thisObject,
208         size_t argumentCount,
209         const JSValueRef arguments[],
210         JSValueRef* exception)
211 {
212         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
213     AceSecurityStatus status = PUSH_CHECK_ACCESS(PUSH_MANAGER_API_CONNECT_SERVICE);
214     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
215
216     try {
217         // Private Object
218         PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
219         if (!priv) {
220             throw TypeMismatchException("Private object is NULL.");
221         }
222
223         ArgumentValidator validator(context, argumentCount, arguments);
224
225         // notificationCallback
226         MultiCallbackUserDataPtr callback;
227         JSObjectRef notificationCallbackObj = validator.toFunction(0);
228         if (notificationCallbackObj) {
229             callback.reset(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
230             callback->setCallback("onsuccess", notificationCallbackObj);
231         }
232
233         // perform
234         priv->connectService(callback);
235
236                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
237         return JSValueMakeUndefined(context);
238     } catch (const BasePlatformException &err) {
239         LoggerE(err.getName() << ": " << err.getMessage());
240         return JSWebAPIException::throwException(context, exception, err);
241     } catch (...) {
242         DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.connectService().");
243         LoggerE(err.getName() << ": " << err.getMessage());
244         return JSWebAPIException::throwException(context, exception, err);
245     }
246 }
247
248 JSValueRef JSPushManager::disconnectService(JSContextRef context,
249         JSObjectRef object,
250         JSObjectRef thisObject,
251         size_t argumentCount,
252         const JSValueRef arguments[],
253         JSValueRef* exception)
254 {
255         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
256     AceSecurityStatus status = PUSH_CHECK_ACCESS(PUSH_MANAGER_API_DISCONNECT_SERVICE);
257     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
258
259     try {
260         // Private Object
261         PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
262         if (!priv) {
263             throw TypeMismatchException("Private object is NULL.");
264         }
265
266         // perform
267         priv->disconnectService();
268
269                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
270         return JSValueMakeUndefined(context);
271     } catch (const BasePlatformException &err) {
272         LoggerE(err.getName() << ": " << err.getMessage());
273         return JSWebAPIException::throwException(context, exception, err);
274     } catch (...) {
275         DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.disconnectService().");
276         LoggerE(err.getName() << ": " << err.getMessage());
277         return JSWebAPIException::throwException(context, exception, err);
278     }
279 }
280
281 JSValueRef JSPushManager::getRegistrationId(JSContextRef context,
282         JSObjectRef object,
283         JSObjectRef thisObject,
284         size_t argumentCount,
285         const JSValueRef arguments[],
286         JSValueRef* exception)
287 {
288         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
289     AceSecurityStatus status = PUSH_CHECK_ACCESS(PUSH_MANAGER_API_GET_REGISTRATION_ID);
290     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
291
292     try {
293         // Private Object
294         PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
295         if (!priv) {
296             throw TypeMismatchException("Private object is NULL.");
297         }
298
299         // perform
300         std::string ret = priv->getRegistrationId();
301         if (ret.empty()) {
302                         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
303             return JSValueMakeNull(context);
304         } else {
305                         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
306                         return JSUtil::toJSValueRef(context, ret);
307         }
308     } catch (const BasePlatformException &err) {
309         LoggerE(err.getName() << ": " << err.getMessage());
310         return JSWebAPIException::throwException(context, exception, err);
311     } catch (...) {
312         DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.getRegistrationId().");
313         LoggerE(err.getName() << ": " << err.getMessage());
314         return JSWebAPIException::throwException(context, exception, err);
315     }
316 }
317
318
319 } // Push
320 } // DeviceAPI