Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Application / JSApplicationServiceReply.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <cassert>
18 #include <memory>
19 #include <dpl/log.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include "JSApplicationServiceReply.h"
24
25 namespace TizenApis {
26 namespace Tizen1_0 {
27 namespace Application {
28 using namespace WrtDeviceApis;
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31 using namespace Api::Application;
32
33 namespace {
34 const char* APPLICATION_SERVICE_REPLY_SUCCESS = "isSucceed";
35 const char* APPLICATION_SERVICE_EXTRADATA = "extraData";
36
37
38 } //private namespace
39
40 JSClassRef JSApplicationServiceReply::m_jsClassRef = NULL;
41
42 JSClassDefinition JSApplicationServiceReply::m_classInfo = {
43     0,
44     kJSClassAttributeNone,
45     "ApplicationServiceReply",
46     0,
47     m_property,
48     0,
49     initialize,
50     finalize,
51     NULL,     //HasProperty,
52     NULL,
53     NULL,     //SetProperty,
54     NULL,     //DeleteProperty,
55     NULL,     //GetPropertyNames,
56     NULL,     //CallAsFunction,
57     NULL,     //CallAsConstructor,
58     hasInstance,
59     NULL,     //ConvertToType
60 };
61
62 JSStaticValue JSApplicationServiceReply::m_property[] = {
63     { APPLICATION_SERVICE_REPLY_SUCCESS, getIsSucceed, NULL, kJSPropertyAttributeReadOnly },
64         { APPLICATION_SERVICE_EXTRADATA, getExtraData, NULL, kJSPropertyAttributeReadOnly },
65     { 0, 0, 0, 0 }
66 };
67
68 const JSClassDefinition* JSApplicationServiceReply::getClassInfo()
69 {
70     return &m_classInfo;
71 }
72
73 const JSClassRef JSApplicationServiceReply::getClassRef()
74 {
75     if (!m_jsClassRef) {
76         m_jsClassRef = JSClassCreate(&m_classInfo);
77     }
78     return m_jsClassRef;
79 }
80
81 JSObjectRef JSApplicationServiceReply::createJSObject(JSContextRef context,
82         const ApplicationServiceReplyPtr &appsvc)
83 {
84     ApplicationServiceReplyPtr privateData(new ApplicationServiceReply());
85     JSApplicationServiceReplyPriv *priv = new JSApplicationServiceReplyPriv(context, privateData);
86
87     if (!priv) {
88         ThrowMsg(Commons::NullPointerException, "Can not new an object");
89     }
90     return JSObjectMake(context, getClassRef(), priv);
91 }
92
93 ApplicationServiceReplyPtr JSApplicationServiceReply::getApplicationServiceReply(JSContextRef context, JSValueRef value)
94 {
95         if (!isObjectOfClass(context, value)) {
96                 Throw(Commons::InvalidArgumentException);
97         }
98         JSObjectRef object = JSValueToObject(context, value, NULL);
99         if (!object) {
100                 Throw(Commons::InvalidArgumentException);
101         }
102         JSApplicationServiceReplyPriv *priv = static_cast<JSApplicationServiceReplyPriv*>(JSObjectGetPrivate(object));
103         if (!priv) {
104                 Throw(Commons::NullPointerException);
105         }
106         return priv->getObject();
107 }
108
109 ApplicationServiceReplyPtr JSApplicationServiceReply::getPrivateData(JSObjectRef object)
110 {
111         JSApplicationServiceReplyPriv* priv = static_cast<JSApplicationServiceReplyPriv*>(JSObjectGetPrivate(object));
112         if (!priv) {
113                 Throw(Commons::NullPointerException);
114         }
115         return priv->getObject();
116 }
117
118
119
120 void JSApplicationServiceReply::initialize(JSContextRef context,JSObjectRef object)
121 {
122     LogDebug("Entered. Nothing to do.");
123 }
124
125 void JSApplicationServiceReply::finalize(JSObjectRef object)
126 {
127     LogDebug("Entered");
128     JSApplicationServiceReplyPriv* priv =
129         static_cast<JSApplicationServiceReplyPriv*>(JSObjectGetPrivate(object));
130     JSObjectSetPrivate(object, NULL);
131     LogDebug("Deleting ApplicationServiceReply object");
132     delete priv;
133 }
134
135 bool JSApplicationServiceReply::isObjectOfClass(JSContextRef context, JSValueRef value)
136 {
137         return JSValueIsObjectOfClass(context, value, getClassRef());
138 }
139
140 JSValueRef JSApplicationServiceReply::getIsSucceed(JSContextRef context,
141         JSObjectRef object,
142         JSStringRef propertyName,
143         JSValueRef* exception)
144 {
145     LogDebug("Enter");
146     JSApplicationServiceReplyPriv *priv =
147         static_cast<JSApplicationServiceReplyPriv*>(JSObjectGetPrivate(object));
148     assert(priv && "Private object not set.");
149
150     Try     {
151         ApplicationServiceReplyPtr appsvc = priv->getObject();
152         CommonsJavaScript::Converter converter(context);
153
154                 if (JSStringIsEqualToUTF8CString(propertyName,APPLICATION_SERVICE_REPLY_SUCCESS)) {
155                         LogDebug(
156                                 "JSApplicationServiceReply::getProperty ::success ");
157
158                         bool isSucceed = appsvc->getIsSucceed();
159                         return converter.toJSValueRef(isSucceed);
160                 }
161
162     }
163     Catch(Commons::Exception) {
164         LogError("Exception: " << _rethrown_exception.GetMessage());
165                 return JSDOMExceptionFactory::UnknownException.make(context, exception);
166     }
167     return JSValueMakeUndefined(context);
168 }
169
170 JSValueRef JSApplicationServiceReply::getExtraData(JSContextRef context,
171         JSObjectRef object,
172         JSStringRef propertyName,
173         JSValueRef* exception)
174 {
175     LogDebug("Enter");
176     JSApplicationServiceReplyPriv *priv =
177         static_cast<JSApplicationServiceReplyPriv*>(JSObjectGetPrivate(object));
178     assert(priv && "Private object not set.");
179
180     Try     {
181         ApplicationServiceReplyPtr appsvc = priv->getObject();
182         CommonsJavaScript::Converter convert(context);
183
184                 if (JSStringIsEqualToUTF8CString(propertyName,APPLICATION_SERVICE_EXTRADATA)) {
185                         LogDebug(
186                                 "JSApplicationServiceReply::getProperty ::extraData ");
187
188                         JSObjectRef result = JSApplicationServiceExtraDataArray::createArray(context, appsvc->getExtraDataArray());
189
190                         return result;
191                 }
192     }
193     Catch(Commons::Exception) {
194         LogError("Exception: " << _rethrown_exception.GetMessage());
195                 return JSDOMExceptionFactory::UnknownException.make(context, exception);
196     }
197     return JSValueMakeUndefined(context);
198 }
199
200 bool JSApplicationServiceReply::hasInstance(JSContextRef context,
201         JSObjectRef constructor,
202         JSValueRef possibleInstance,
203         JSValueRef* exception)
204 {
205     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
206 }
207 } //
208 } //TizenApis
209 }