2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
28 #include "ApplicationConverter.h"
29 #include "JSApplicationService.h"
34 namespace Application {
35 using namespace WrtDeviceApis;
36 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace Api::Application;
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";
49 JSClassRef JSApplicationService::m_jsClassRef = NULL;
51 JSClassDefinition JSApplicationService::m_classInfo = {
53 kJSClassAttributeNone,
63 NULL, //DeleteProperty,
64 NULL, //GetPropertyNames,
65 NULL, //CallAsFunction,
66 constructor, //CallAsConstructor,
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 },
80 const JSClassDefinition* JSApplicationService::getClassInfo()
85 const JSClassRef JSApplicationService::getClassRef()
88 m_jsClassRef = JSClassCreate(&m_classInfo);
93 //JSClassRef JSApplicationService::m_jsClassRef = JSClassCreate(
94 // JSApplicationService::getClassInfo());
96 JSObjectRef JSApplicationService::createJSObject(JSContextRef context,
97 const ApplicationServicePtr &appsvc)
99 JSApplicationServicePriv *priv = new JSApplicationServicePriv(context, appsvc);
102 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
104 return JSObjectMake(context, getClassRef(), priv);
108 ApplicationServicePtr JSApplicationService::getApplicationService(JSContextRef context, JSValueRef value)
110 if (!isObjectOfClass(context, value)) {
111 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
113 JSObjectRef object = JSValueToObject(context, value, NULL);
115 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
117 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
119 Throw(WrtDeviceApis::Commons::NullPointerException);
121 return priv->getObject();
124 ApplicationServicePtr JSApplicationService::getPrivateData(JSObjectRef object)
126 JSApplicationServicePriv* priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
128 Throw(WrtDeviceApis::Commons::NullPointerException);
130 return priv->getObject();
134 void JSApplicationService::initialize(JSContextRef context,JSObjectRef object)
136 LogDebug("Entered. Nothing to do.");
139 void JSApplicationService::finalize(JSObjectRef object)
142 JSApplicationServicePriv* priv =
143 static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
144 JSObjectSetPrivate(object, NULL);
145 LogDebug("Deleting ApplicationService object");
149 JSObjectRef JSApplicationService::constructor(JSContextRef context, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
151 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
153 // TODO: exception handling
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");
175 std::string operation = "";
176 std::string uri = "";
177 std::string mime = "";
178 std::vector<ApplicationServiceDataPtr> serviceData;
180 if ((argumentCount > 4) && (argumentCount < 1)) {
181 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Wrong ApplicationService argumentCount");
183 if (!JSValueIsNull(context, arguments[0]) && JSValueIsString(context, arguments[0])) {
184 operation = converter->toString(arguments[0]);
186 if ((argumentCount >= 2) && !JSValueIsUndefined(context, arguments[1])) {
187 if (!JSValueIsNull(context, arguments[1]) && JSValueIsString(context, arguments[1])) {
188 uri = converter->toString(arguments[1]);
191 if ((argumentCount >= 3) && !JSValueIsUndefined(context, arguments[2])) {
192 if (!JSValueIsNull(context, arguments[2]) && JSValueIsString(context, arguments[2])) {
193 mime = converter->toString(arguments[2]);
196 if ((argumentCount == 4) && !JSValueIsUndefined(context, arguments[3]) && !JSValueIsNull(context, arguments[3])) {
197 serviceData = converter->toApplicationServiceDataArray(arguments[3]);
200 ApplicationServicePtr appsvc = ApplicationServicePtr(new ApplicationService(operation, uri, mime, serviceData));
202 Throw(WrtDeviceApis::Commons::NullPointerException);
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");
213 bool JSApplicationService::isObjectOfClass(JSContextRef context, JSValueRef value)
215 return JSValueIsObjectOfClass(context, value, getClassRef());
218 JSValueRef JSApplicationService::getProperty(JSContextRef context,
220 JSStringRef propertyName,
221 JSValueRef* exception)
224 JSApplicationServicePriv *priv =
225 static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
226 assert(priv && "Private object not set.");
229 ApplicationServicePtr appsvc = priv->getObject();
230 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
232 if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_OPERATION)) {
234 "JSApplicationService::getProperty::operation " <<
235 appsvc->getOperation());
236 return converter->toJSValueRef(appsvc->getOperation());
237 }else if (JSStringIsEqualToUTF8CString(propertyName,
238 APPLICATION_SERVICE_URI)) {
240 "JSApplicationService::getProperty ::uri " <<
242 return converter->toJSValueRef(appsvc->getUri());
243 }else if (JSStringIsEqualToUTF8CString(propertyName,
244 APPLICATION_SERVICE_MIME)) {
246 "JSApplicationService::getProperty ::mime " <<
248 return converter->toJSValueRef(appsvc->getMime());
249 }else if (JSStringIsEqualToUTF8CString(propertyName,
250 APPLICATION_SERVICE_DATA)) {
252 "JSApplicationService::getProperty ::extraData ");
253 return converter->toJSValueRef(appsvc->getServiceDataArray());
257 Catch(WrtDeviceApis::Commons::Exception) {
258 LogError("Exception: " << _rethrown_exception.GetMessage());
259 return JSDOMExceptionFactory::UnknownException.make(context, exception);
261 return JSValueMakeUndefined(context);
264 bool JSApplicationService::hasInstance(JSContextRef context,
265 JSObjectRef constructor,
266 JSValueRef possibleInstance,
267 JSValueRef* exception)
269 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());