2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <SecurityExceptions.h>
21 #include <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
27 #include <JSApplicationControl.h>
28 #include <TimeTracer.h>
30 #include "plugin_config.h"
32 #include "JSPushManager.h"
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
40 JSClassDefinition JSPushManager::m_classInfo = {
42 kJSClassAttributeNone,
46 m_function, //StaticFunctions
47 initialize, //Initialize
52 NULL, //DeleteProperty,
53 NULL, //GetPropertyNames,
54 NULL, //CallAsFunction,
55 NULL, //CallAsConstructor,
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 },
69 JSClassRef JSPushManager::m_jsClassRef = JSClassCreate(JSPushManager::getClassInfo());
71 const JSClassRef JSPushManager::getClassRef()
74 m_jsClassRef = JSClassCreate(&m_classInfo);
79 const JSClassDefinition* JSPushManager::getClassInfo()
84 void JSPushManager::initialize(JSContextRef context, JSObjectRef object)
86 if (!JSObjectGetPrivate(object)) {
87 PushManager *priv = PushManager::getInstance();
88 JSObjectSetPrivate(object, static_cast<void*>(priv));
92 void JSPushManager::finalize(JSObjectRef object)
96 JSValueRef JSPushManager::registerService(JSContextRef context,
98 JSObjectRef thisObject,
100 const JSValueRef arguments[],
101 JSValueRef* exception)
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);
109 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
111 throw TypeMismatchException("Private object is NULL.");
114 ArgumentValidator validator(context, argumentCount, arguments);
117 DeviceAPI::Application::ApplicationControlPtr appControl(NULL);
118 JSObjectRef appControlObj = validator.toObject(0, DeviceAPI::Application::JSApplicationControl::getClassRef());
120 JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv *>(JSObjectGetPrivate(appControlObj));
122 throw TypeMismatchException("ApplicationControl's private object is NULL.");
124 appControl = priv->getObject();
127 MultiCallbackUserDataPtr callback(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
130 JSObjectRef successCallbackObj = validator.toFunction(1);
131 if (successCallbackObj) {
132 callback->setCallback("onsuccess", successCallbackObj);
136 JSObjectRef errorCallbackObj = validator.toFunction(2, true);
137 if (errorCallbackObj) {
138 callback->setCallback("onerror", errorCallbackObj);
142 priv->registerService(appControl, callback);
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);
150 DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.registerService().");
151 LoggerE(err.getName() << ": " << err.getMessage());
152 return JSWebAPIException::throwException(context, exception, err);
156 JSValueRef JSPushManager::unregisterService(JSContextRef context,
158 JSObjectRef thisObject,
159 size_t argumentCount,
160 const JSValueRef arguments[],
161 JSValueRef* exception)
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);
169 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
171 throw TypeMismatchException("Private object is NULL.");
174 ArgumentValidator validator(context, argumentCount, arguments);
176 MultiCallbackUserDataPtr callback(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
179 JSObjectRef successCallbackObj = validator.toFunction(0, true);
180 if (successCallbackObj) {
181 callback->setCallback("onsuccess", successCallbackObj);
185 JSObjectRef errorCallbackObj = validator.toFunction(1, true);
186 if (errorCallbackObj) {
187 callback->setCallback("onerror", errorCallbackObj);
191 priv->unregisterService(callback);
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);
199 DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.unregisterService().");
200 LoggerE(err.getName() << ": " << err.getMessage());
201 return JSWebAPIException::throwException(context, exception, err);
205 JSValueRef JSPushManager::connectService(JSContextRef context,
207 JSObjectRef thisObject,
208 size_t argumentCount,
209 const JSValueRef arguments[],
210 JSValueRef* exception)
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);
218 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
220 throw TypeMismatchException("Private object is NULL.");
223 ArgumentValidator validator(context, argumentCount, arguments);
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);
234 priv->connectService(callback);
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);
242 DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.connectService().");
243 LoggerE(err.getName() << ": " << err.getMessage());
244 return JSWebAPIException::throwException(context, exception, err);
248 JSValueRef JSPushManager::disconnectService(JSContextRef context,
250 JSObjectRef thisObject,
251 size_t argumentCount,
252 const JSValueRef arguments[],
253 JSValueRef* exception)
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);
261 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
263 throw TypeMismatchException("Private object is NULL.");
267 priv->disconnectService();
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);
275 DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.disconnectService().");
276 LoggerE(err.getName() << ": " << err.getMessage());
277 return JSWebAPIException::throwException(context, exception, err);
281 JSValueRef JSPushManager::getRegistrationId(JSContextRef context,
283 JSObjectRef thisObject,
284 size_t argumentCount,
285 const JSValueRef arguments[],
286 JSValueRef* exception)
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);
294 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
296 throw TypeMismatchException("Private object is NULL.");
300 std::string ret = priv->getRegistrationId();
302 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
303 return JSValueMakeNull(context);
305 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
306 return JSUtil::toJSValueRef(context, ret);
308 } catch (const BasePlatformException &err) {
309 LoggerE(err.getName() << ": " << err.getMessage());
310 return JSWebAPIException::throwException(context, exception, err);
312 DeviceAPI::Common::UnknownException err("Unknown Error in PushManager.getRegistrationId().");
313 LoggerE(err.getName() << ": " << err.getMessage());
314 return JSWebAPIException::throwException(context, exception, err);