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 <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
23 #include <MultiCallbackUserData.h>
24 #include <PlatformException.h>
26 #include <JSApplicationControl.h>
27 #include <TimeTracer.h>
29 #include "plugin_config.h"
31 #include "JSPushManager.h"
33 using namespace WrtDeviceApis::Commons;
34 using namespace DeviceAPI::Common;
39 JSClassDefinition JSPushManager::m_classInfo = {
41 kJSClassAttributeNone,
45 m_function, //StaticFunctions
46 initialize, //Initialize
51 NULL, //DeleteProperty,
52 NULL, //GetPropertyNames,
53 NULL, //CallAsFunction,
54 NULL, //CallAsConstructor,
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 },
68 JSClassRef JSPushManager::m_jsClassRef = JSClassCreate(JSPushManager::getClassInfo());
70 const JSClassRef JSPushManager::getClassRef()
73 m_jsClassRef = JSClassCreate(&m_classInfo);
78 const JSClassDefinition* JSPushManager::getClassInfo()
83 void JSPushManager::initialize(JSContextRef context, JSObjectRef object)
85 if (!JSObjectGetPrivate(object)) {
86 PushManager *priv = PushManager::getInstance();
87 JSObjectSetPrivate(object, static_cast<void*>(priv));
91 void JSPushManager::finalize(JSObjectRef object)
95 JSValueRef JSPushManager::registerService(JSContextRef context,
97 JSObjectRef thisObject,
99 const JSValueRef arguments[],
100 JSValueRef* exception)
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);
108 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
110 throw TypeMismatchException("Private object is NULL.");
113 ArgumentValidator validator(context, argumentCount, arguments);
116 DeviceAPI::Application::ApplicationControlPtr appControl(NULL);
117 JSObjectRef appControlObj = validator.toObject(0, DeviceAPI::Application::JSApplicationControl::getClassRef());
119 JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv *>(JSObjectGetPrivate(appControlObj));
121 throw TypeMismatchException("ApplicationControl's private object is NULL.");
123 appControl = priv->getObject();
126 MultiCallbackUserDataPtr callback(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
129 JSObjectRef successCallbackObj = validator.toFunction(1);
130 if (successCallbackObj) {
131 callback->setCallback("onsuccess", successCallbackObj);
135 JSObjectRef errorCallbackObj = validator.toFunction(2, true);
136 if (errorCallbackObj) {
137 callback->setCallback("onerror", errorCallbackObj);
141 priv->registerService(appControl, callback);
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);
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);
155 JSValueRef JSPushManager::unregisterService(JSContextRef context,
157 JSObjectRef thisObject,
158 size_t argumentCount,
159 const JSValueRef arguments[],
160 JSValueRef* exception)
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);
168 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
170 throw TypeMismatchException("Private object is NULL.");
173 ArgumentValidator validator(context, argumentCount, arguments);
175 MultiCallbackUserDataPtr callback(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
178 JSObjectRef successCallbackObj = validator.toFunction(0, true);
179 if (successCallbackObj) {
180 callback->setCallback("onsuccess", successCallbackObj);
184 JSObjectRef errorCallbackObj = validator.toFunction(1, true);
185 if (errorCallbackObj) {
186 callback->setCallback("onerror", errorCallbackObj);
190 priv->unregisterService(callback);
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);
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);
204 JSValueRef JSPushManager::connectService(JSContextRef context,
206 JSObjectRef thisObject,
207 size_t argumentCount,
208 const JSValueRef arguments[],
209 JSValueRef* exception)
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);
217 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
219 throw TypeMismatchException("Private object is NULL.");
222 ArgumentValidator validator(context, argumentCount, arguments);
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);
233 priv->connectService(callback);
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);
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);
247 JSValueRef JSPushManager::disconnectService(JSContextRef context,
249 JSObjectRef thisObject,
250 size_t argumentCount,
251 const JSValueRef arguments[],
252 JSValueRef* exception)
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);
260 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
262 throw TypeMismatchException("Private object is NULL.");
266 priv->disconnectService();
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);
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);
280 JSValueRef JSPushManager::getRegistrationId(JSContextRef context,
282 JSObjectRef thisObject,
283 size_t argumentCount,
284 const JSValueRef arguments[],
285 JSValueRef* exception)
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);
293 PushManager *priv = static_cast<PushManager *>(JSObjectGetPrivate(thisObject));
295 throw TypeMismatchException("Private object is NULL.");
299 std::string ret = priv->getRegistrationId();
301 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
302 return JSValueMakeNull(context);
304 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
305 return JSUtil::toJSValueRef(context, ret);
307 } catch (const BasePlatformException &err) {
308 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
309 return JSWebAPIErrorFactory::postException(context, exception, err);
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);