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