Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Application / JSApplicationService.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/log.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
22 #include <CommonsJavaScript/PrivateObject.h>
23 #include <CommonsJavaScript/JSUtils.h>
24 #include <Commons/Exception.h>
25 #include <Tizen/Common/JSTizenExceptionFactory.h>
26 #include <Tizen/Common/JSTizenException.h>
27
28 #include "ApplicationConverter.h"
29 #include "JSApplicationService.h"
30
31
32 namespace TizenApis {
33 namespace Tizen1_0 {
34 namespace Application {
35 using namespace WrtDeviceApis;
36 using namespace WrtDeviceApis::CommonsJavaScript;
37
38 using namespace Api::Application;
39
40 namespace {
41 const char* APPLICATION_SERVICE_OPERATION = "operation";
42 const char* APPLICATION_SERVICE_URI = "uri";
43 const char* APPLICATION_SERVICE_MIME = "mime";
44 const char* APPLICATION_SERVICE_PACKAGE = "package";
45 const char* APPLICATION_SERVICE_DATA = "data";
46
47 } //private namespace
48
49 JSClassRef JSApplicationService::m_jsClassRef = NULL;
50
51 JSClassDefinition JSApplicationService::m_classInfo = {
52     0,
53     kJSClassAttributeNone,
54     "ApplicationService",
55     0,
56     m_property,
57     0,
58     initialize,
59     finalize,
60     NULL,     //HasProperty,
61     getProperty,
62     NULL,     //SetProperty,
63     NULL,     //DeleteProperty,
64     NULL,     //GetPropertyNames,
65     NULL,     //CallAsFunction,
66     constructor,     //CallAsConstructor,
67     hasInstance,
68     NULL,     //ConvertToType
69 };
70
71 JSStaticValue JSApplicationService::m_property[] = {
72     { APPLICATION_SERVICE_OPERATION, getProperty, NULL, kJSPropertyAttributeReadOnly },
73     { APPLICATION_SERVICE_URI, getProperty, NULL, kJSPropertyAttributeReadOnly },
74     { APPLICATION_SERVICE_MIME, getProperty, NULL, kJSPropertyAttributeReadOnly },
75     { APPLICATION_SERVICE_PACKAGE, getProperty, NULL, kJSPropertyAttributeReadOnly },
76     { APPLICATION_SERVICE_DATA, getProperty, NULL, kJSPropertyAttributeReadOnly },
77     { 0, 0, 0, 0 }
78 };
79
80 const JSClassDefinition* JSApplicationService::getClassInfo()
81 {
82     return &m_classInfo;
83 }
84
85 const JSClassRef JSApplicationService::getClassRef()
86 {
87     if (!m_jsClassRef) {
88         m_jsClassRef = JSClassCreate(&m_classInfo);
89     }
90     return m_jsClassRef;
91 }
92
93 //JSClassRef JSApplicationService::m_jsClassRef = JSClassCreate(
94 //        JSApplicationService::getClassInfo());
95
96 JSObjectRef JSApplicationService::createJSObject(JSContextRef context,
97         const ApplicationServicePtr &appsvc)
98 {
99     JSApplicationServicePriv *priv = new JSApplicationServicePriv(context, appsvc);
100
101     if (!priv) {
102         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
103     }
104     return JSObjectMake(context, getClassRef(), priv);
105 }
106
107
108 ApplicationServicePtr JSApplicationService::getApplicationService(JSContextRef context, JSValueRef value)
109 {
110         if (!isObjectOfClass(context, value)) {
111                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
112         }
113         JSObjectRef object = JSValueToObject(context, value, NULL);
114         if (!object) {
115                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
116         }
117         JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
118         if (!priv) {
119                 Throw(WrtDeviceApis::Commons::NullPointerException);
120         }
121         return priv->getObject();
122 }
123
124 ApplicationServicePtr JSApplicationService::getPrivateData(JSObjectRef object)
125 {
126         JSApplicationServicePriv* priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
127         if (!priv) {
128                 Throw(WrtDeviceApis::Commons::NullPointerException);
129         }
130         return priv->getObject();
131 }
132
133
134 void JSApplicationService::initialize(JSContextRef context,JSObjectRef object)
135 {
136     LogDebug("Entered. Nothing to do.");
137 }
138
139 void JSApplicationService::finalize(JSObjectRef object)
140 {
141     LogDebug("Entered");
142     JSApplicationServicePriv* priv =
143         static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
144     JSObjectSetPrivate(object, NULL);
145     LogDebug("Deleting ApplicationService object");
146     delete priv;
147 }
148
149 JSObjectRef JSApplicationService::constructor(JSContextRef context, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
150 {
151         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
152
153         // TODO: exception handling
154 #if 0
155     if ((argumentCount < 1 && argumentCount > 4) ||
156        (JSValueIsNull(context, arguments[0]) ||
157          JSValueIsUndefined(context, arguments[0]) ||
158          JSObjectIsFunction(context, converter->toJSObjectRef(arguments[0]))) ||
159        ((argumentCount >= 2) &&
160          JSValueIsUndefined(context, arguments[1]) ||
161          JSObjectIsFunction(context, converter->toJSObjectRef(arguments[1]))) ||
162        ((argumentCount >= 3) &&
163          JSValueIsUndefined(context, arguments[2]) ||
164          JSObjectIsFunction(context, converter->toJSObjectRef(arguments[2]))) ||
165        ((argumentCount = 4) && !JSValueIsNull(context, arguments[3]) &&
166          JSIsArrayValue(context, arguments[3]))) {
167          LogError("Wrong callbacks parameters");
168                  LogError("===[WS] 2");
169                 *exception = TizenApis::Commons::JSTizenExceptionFactory::makeErrorObject(context,TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
170                 return NULL;
171      }  
172 #endif
173
174         Try {
175                 std::string operation = "";
176                 std::string uri = "";
177                 std::string mime = "";
178                 std::vector<ApplicationServiceDataPtr> serviceData;
179
180                 if ((argumentCount > 4) && (argumentCount < 1)) {
181                         ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Wrong ApplicationService argumentCount");
182                 }
183                 if (!JSValueIsNull(context, arguments[0]) && JSValueIsString(context, arguments[0])) {
184                         operation = converter->toString(arguments[0]);
185                 }
186                 if ((argumentCount >= 2) && !JSValueIsUndefined(context, arguments[1])) {
187                         if (!JSValueIsNull(context, arguments[1]) && JSValueIsString(context, arguments[1])) {
188                                 uri = converter->toString(arguments[1]);
189                         }                       
190                 }               
191                 if ((argumentCount >= 3) && !JSValueIsUndefined(context, arguments[2])) {
192                         if (!JSValueIsNull(context, arguments[2]) && JSValueIsString(context, arguments[2])) {
193                                 mime = converter->toString(arguments[2]);
194                         }
195                 }       
196                 if ((argumentCount == 4) && !JSValueIsUndefined(context, arguments[3]) && !JSValueIsNull(context, arguments[3])) {
197                         serviceData = converter->toApplicationServiceDataArray(arguments[3]);
198                 }
199
200                 ApplicationServicePtr appsvc = ApplicationServicePtr(new ApplicationService(operation, uri, mime, serviceData));
201                 if (!appsvc) {
202                         Throw(WrtDeviceApis::Commons::NullPointerException);
203                 }
204                 return createJSObject(context, appsvc);
205         } Catch (WrtDeviceApis::Commons::InvalidArgumentException) {
206                 LogError("Exception: " << _rethrown_exception.GetMessage());
207                 *exception = TizenApis::Commons::JSTizenExceptionFactory::makeErrorObject(context,TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
208                 return NULL;
209         }
210 }
211         
212
213 bool JSApplicationService::isObjectOfClass(JSContextRef context, JSValueRef value)
214 {
215         return JSValueIsObjectOfClass(context, value, getClassRef());
216 }
217
218 JSValueRef JSApplicationService::getProperty(JSContextRef context,
219         JSObjectRef object,
220         JSStringRef propertyName,
221         JSValueRef* exception)
222 {
223     LogDebug("Enter");
224     JSApplicationServicePriv *priv =
225         static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
226     assert(priv && "Private object not set.");
227
228     Try     {
229         ApplicationServicePtr appsvc = priv->getObject();
230                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
231
232         if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_OPERATION)) {
233             LogDebug(
234                 "JSApplicationService::getProperty::operation " <<
235                 appsvc->getOperation());
236             return converter->toJSValueRef(appsvc->getOperation());
237         }else if (JSStringIsEqualToUTF8CString(propertyName,
238                                                 APPLICATION_SERVICE_URI)) {
239             LogDebug(
240                 "JSApplicationService::getProperty ::uri " <<
241                 appsvc->getUri());
242             return converter->toJSValueRef(appsvc->getUri());
243         }else if (JSStringIsEqualToUTF8CString(propertyName,
244                                                 APPLICATION_SERVICE_MIME)) {
245             LogDebug(
246                 "JSApplicationService::getProperty ::mime " <<
247                 appsvc->getMime());
248             return converter->toJSValueRef(appsvc->getMime());
249                 }else if (JSStringIsEqualToUTF8CString(propertyName,
250                                                                                                 APPLICATION_SERVICE_DATA)) {
251                         LogDebug(
252                                 "JSApplicationService::getProperty ::extraData ");
253                         return converter->toJSValueRef(appsvc->getServiceDataArray());
254                 }
255
256     }
257     Catch(WrtDeviceApis::Commons::Exception) {
258         LogError("Exception: " << _rethrown_exception.GetMessage());
259                 return JSDOMExceptionFactory::UnknownException.make(context, exception);
260     }
261     return JSValueMakeUndefined(context);
262 }
263
264 bool JSApplicationService::hasInstance(JSContextRef context,
265         JSObjectRef constructor,
266         JSValueRef possibleInstance,
267         JSValueRef* exception)
268 {
269     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
270 }
271 } //
272 } //TizenApis
273 }