0f6be883cb794d6092bbba1ef0a53ff147458d2c
[framework/web/wrt-plugins-tizen.git] / src / Push / JSPushMessage.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 <JSWebAPIError.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
26
27 #include "plugin_config.h"
28
29 #include "JSPushMessage.h"
30
31 using namespace WrtDeviceApis::Commons;
32 using namespace DeviceAPI::Common;
33
34 namespace DeviceAPI {
35 namespace Push {
36
37 JSClassDefinition JSPushMessage::m_classInfo = {
38     0,
39     kJSClassAttributeNone,
40     "PushMessage",
41     NULL, //ParentClass
42     NULL, //StaticValues
43     NULL, //StaticFunctions
44     initialize, //Initialize
45     finalize, //Finalize
46     NULL, //HasProperty,
47     NULL, //GetProperty,
48     NULL, //SetProperty,
49     NULL, //DeleteProperty,
50     NULL, //GetPropertyNames,
51     NULL, //CallAsFunction,
52     NULL, //CallAsConstructor,
53     NULL, //HasInstance,
54     NULL //ConvertToType
55 };
56
57
58 JSClassRef JSPushMessage::m_jsClassRef = JSClassCreate(JSPushMessage::getClassInfo());
59
60 const JSClassRef JSPushMessage::getClassRef()
61 {
62     if (!m_jsClassRef) {
63         m_jsClassRef = JSClassCreate(&m_classInfo);
64     }
65     return m_jsClassRef;
66 }
67
68 const JSClassDefinition* JSPushMessage::getClassInfo()
69 {
70     return &m_classInfo;
71 }
72
73 void JSPushMessage::initialize(JSContextRef context, JSObjectRef object)
74 {
75     if (!JSObjectGetPrivate(object)) {
76         PushMessage *priv = new PushMessage();
77         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
78             delete priv;
79         }
80     }
81 }
82
83 void JSPushMessage::finalize(JSObjectRef object)
84 {
85     PushMessage *priv = static_cast<PushMessage *>(JSObjectGetPrivate(object));
86     if (priv) {
87         JSObjectSetPrivate(object, NULL);
88         delete priv;
89     }
90 }
91
92 PushMessage* JSPushMessage::getPrivateObject(JSContextRef context, JSObjectRef object)
93 {
94     PushMessage *priv = static_cast<PushMessage *>(JSObjectGetPrivate(object));
95     if (!priv) {
96         throw TypeMismatchException("PushMessage's private object is NULL.");
97     }
98
99     // appData
100     JSValueRef appData = JSUtil::getProperty(context, object, PUSH_MESSAGE_APP_DATA);
101     priv->setAppData(JSUtil::JSValueToString(context, appData));
102
103     // alertMessage
104     JSValueRef alertMessage = JSUtil::getProperty(context, object, PUSH_MESSAGE_ALERT_MESSAGE);
105     priv->setAlertMessage(JSUtil::JSValueToString(context, alertMessage));
106
107     // date
108     JSValueRef date = JSUtil::getProperty(context, object, PUSH_MESSAGE_DATE);
109     priv->setDate(JSUtil::JSValueToTimeT(context, date));
110
111     return priv;
112 }
113
114 void JSPushMessage::setPrivateObject(JSContextRef context, JSObjectRef object, PushMessage *priv)
115 {
116     if (!priv) {
117         throw TypeMismatchException("PushMessage's private object is NULL.");
118     }
119
120     JSObjectSetPrivate(object, static_cast<void*>(priv));
121
122     // appData
123     JSUtil::setProperty(context, object, PUSH_MESSAGE_APP_DATA,
124             JSUtil::toJSValueRef(context, priv->getAppData()), kJSPropertyAttributeNone);
125
126     // alertMessage
127     JSUtil::setProperty(context, object, PUSH_MESSAGE_ALERT_MESSAGE,
128             JSUtil::toJSValueRef(context, priv->getAlertMessage()), kJSPropertyAttributeNone);
129
130     // date
131     JSUtil::setProperty(context, object, PUSH_MESSAGE_DATE,
132             JSUtil::makeDateObject(context, priv->getDate()), kJSPropertyAttributeNone);
133 }
134
135
136 } // Push
137 } // DeviceAPI