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.
16 #include "JSLocationServiceProvider.h"
17 #include "LocationServiceProvider.h"
18 #include <Tizen/Common/JSTizenExceptionFactory.h>
19 #include <Tizen/Common/JSTizenException.h>
20 #include <Tizen/Common/SecurityExceptions.h>
23 #include "JSLBSPending.h"
29 #define LOG_TAG "TIZEN_LBS"
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace WrtDeviceApis::Commons;
40 JSObjectRef onSuccess;
44 JSContextRef JSLocationServiceProvider::m_globalContextRef = NULL;
45 JSClassRef JSLocationServiceProvider::m_jsClassRef = NULL;
47 JSClassDefinition JSLocationServiceProvider::m_jsClassInfo = {
49 kJSClassAttributeNone,
50 "LocationServiceProvider",
57 JSLocationServiceProvider::getProperty, //getProperty
59 NULL, //deleteProperty
60 NULL, //getPropertyNames
68 JSStaticFunction JSLocationServiceProvider::m_function[] = {
69 { "setOptions",JSLocationServiceProvider::setOptions,kJSPropertyAttributeNone },
73 const JSClassRef JSLocationServiceProvider::getClassRef()
76 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
81 const JSClassDefinition* JSLocationServiceProvider::getClassInfo()
83 return &m_jsClassInfo;
86 void JSLocationServiceProvider::initialize(JSContextRef ctx, JSObjectRef object)
88 if( JSLocationServiceProvider::m_globalContextRef == NULL ){
89 JSLocationServiceProvider::m_globalContextRef = ctx;
93 void JSLocationServiceProvider::finalize(JSObjectRef object)
95 LocationServiceProvider * priv = reinterpret_cast<LocationServiceProvider*>(JSObjectGetPrivate(object));
98 JSObjectSetPrivate(object, NULL);
102 bool JSLocationServiceProvider::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
103 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
106 JSValueRef JSLocationServiceProvider::getProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception){
108 LocationServiceProvider * priv = reinterpret_cast<LocationServiceProvider*>(JSObjectGetPrivate(object));
112 if(JSStringIsEqualToUTF8CString(propertyName, "name")) {
113 JSStringRef name = JSStringCreateWithUTF8CString(priv->getName().c_str());
114 JSValueRef jsv = JSValueMakeString(ctx, name);
115 JSStringRelease(name);
118 else if(JSStringIsEqualToUTF8CString(propertyName, "metaData")) {
119 std::vector<std::pair<std::string, std::string>> options = priv->getMetadata();
120 JSObjectRef metaobject = JSObjectMake(ctx, NULL, NULL);
121 std::vector<std::pair<std::string, std::string>>::iterator pos;
122 for( pos = options.begin() ; pos != options.end(); ++pos){
123 JSStringRef key = JSStringCreateWithUTF8CString(pos->first.c_str());
124 JSStringRef value = JSStringCreateWithUTF8CString(pos->second.c_str());
125 JSValueRef valueRef = JSValueMakeString(ctx, value);
126 JSObjectSetProperty(ctx, metaobject , key , valueRef ,kJSPropertyAttributeReadOnly, NULL );
127 JSStringRelease(key);
128 JSStringRelease(value);
132 else if(JSStringIsEqualToUTF8CString(propertyName, "attribution")) {
133 JSStringRef attr = JSStringCreateWithUTF8CString(priv->getAttribution().c_str());
134 JSValueRef jsv = JSValueMakeString(ctx, attr);
135 JSStringRelease(attr);
138 else if(JSStringIsEqualToUTF8CString(propertyName, "supportedOptions")) {
139 std::vector<std::string> supportedOptionVector = priv->getSupportedOptions();
140 JSValueRef* supportedOptionValues = NULL;
141 supportedOptionValues = new JSValueRef[supportedOptionVector.size()];
142 for( unsigned int i=0 ; i < supportedOptionVector.size(); i++){
143 JSStringRef optionString = JSStringCreateWithUTF8CString( supportedOptionVector[i].c_str());
144 supportedOptionValues[i] = JSValueMakeString(ctx, optionString);
145 JSStringRelease(optionString);
147 JSObjectRef supportedOptionArray = JSObjectMakeArray(ctx, supportedOptionVector.size(), supportedOptionValues, NULL);
148 delete []supportedOptionValues;
149 return supportedOptionArray;
151 else if(JSStringIsEqualToUTF8CString(propertyName, "connectivity")) {
152 JSStringRef conn = JSStringCreateWithUTF8CString(priv->getConnectivity().c_str());
153 JSValueRef jsv = JSValueMakeString(ctx, conn);
154 JSStringRelease(conn);
161 void _setOptionCb(int result , void * user_data){
163 CallbackData * userdata = static_cast<CallbackData *>(user_data);
164 if ( userdata == NULL)
167 if( result == 0 && userdata->onSuccess ){
168 JSValueRef args[1] = {NULL};
169 JSObjectCallAsFunction(JSLBS::getGlobalContext() , userdata->onSuccess , NULL, 1, args, NULL);
171 else if( result != 0 && userdata->onFail ){
172 JSObjectRef error = Commons::JSTizenExceptionFactory::makeErrorObject(JSLBS::getGlobalContext(), Commons::JSTizenException::INVALID_VALUES_ERROR , "Fail setOption");
173 JSValueRef args[1] = {error};
174 JSObjectCallAsFunction(JSLBS::getGlobalContext() , userdata->onFail , NULL, 1, args, NULL);
177 if( userdata->onSuccess )
178 JSValueUnprotect(JSLBS::getGlobalContext(), userdata->onSuccess);
179 if( userdata->onFail )
180 JSValueUnprotect(JSLBS::getGlobalContext(), userdata->onFail);
185 JSValueRef JSLocationServiceProvider::setOptions(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
187 // Check the access control
188 AceSecurityStatus status = LBS_CHECK_ACCESS(LBS_FUNCTION_CATEGORY_SERVICE_PROVIDER);
189 if( status != AceSecurityStatus::AccessGranted )
190 return Commons::JSTizenExceptionFactory::postException(ctx,exception, Commons::JSTizenException::PERMISSION_DENIED_ERROR );
192 // Check the object type
193 LocationServiceProvider * priv = reinterpret_cast<LocationServiceProvider*>(JSObjectGetPrivate(thisObject));
195 return Commons::JSTizenExceptionFactory::postException(ctx,exception, Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The object is not LocationServiceProvider instance");
198 JSValueRef convertedArguments[3];
199 JSObjectRef convertedArgumentObjects[3];
200 for( unsigned int i = 0 ; i < 3; i++ ){
201 if( i < argumentCount ){
202 convertedArguments[i] = arguments[i];
203 if( JSValueIsObject(ctx, arguments[i]) )
204 convertedArgumentObjects[i] = JSValueToObject(ctx, arguments[i] , NULL );
206 convertedArgumentObjects[i] = NULL;
208 convertedArguments[i] = JSValueMakeUndefined(ctx);
209 convertedArgumentObjects[i] = NULL;
213 // The second argument is null or undefined or function object
214 if( !JSValueIsNull(ctx, convertedArguments[1]) &&
215 !JSValueIsUndefined(ctx, convertedArguments[1]) &&
216 ( convertedArgumentObjects[1] == NULL || !JSObjectIsFunction(ctx, convertedArgumentObjects[1]) )
218 return Commons::JSTizenExceptionFactory::postException(ctx,exception, Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The first argument must be function or null");
220 // The third argument is null or undefined or function object
221 if( !JSValueIsNull(ctx, convertedArguments[2]) &&
222 !JSValueIsUndefined(ctx, convertedArguments[2]) &&
223 ( convertedArgumentObjects[2] == NULL || !JSObjectIsFunction(ctx, convertedArgumentObjects[2]))
225 return Commons::JSTizenExceptionFactory::postException(ctx,exception, Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The first argument must be function or null");
227 // The first argument is option object
228 JSObjectRef jsOptions = convertedArgumentObjects[0];
229 if( jsOptions == NULL ){
230 return Commons::JSTizenExceptionFactory::postException(ctx, exception, Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The option arguments is not Object");
233 // Initialize a CallbackData
234 CallbackData * userdata = NULL;
236 JSObjectRef onsuccess = convertedArgumentObjects[1];
237 JSObjectRef onfail = convertedArgumentObjects[2];
238 if( onsuccess != NULL || onfail != NULL ){
239 userdata = new CallbackData();
240 userdata->onSuccess = NULL;
241 userdata->onFail = NULL;
244 userdata->onSuccess = onsuccess;
245 JSValueProtect(ctx, onsuccess);
248 userdata->onFail = onfail;
249 JSValueProtect(ctx, onfail);
253 // Convert Object to vector
254 std::vector<std::pair<std::string, std::string>> options = LBSUtil::convertObject(ctx, jsOptions);
256 // Call native setOption function
257 int ret = priv->setOption(options , _setOptionCb , userdata, NULL);
259 // Invalid options values
261 //remove callback values
262 if( userdata != NULL ){
263 if( userdata->onSuccess )
264 JSValueUnprotect(ctx, userdata->onSuccess);
265 if( userdata->onFail )
266 JSValueUnprotect(ctx, userdata->onFail);
270 JSObjectRef error = Commons::JSTizenExceptionFactory::makeErrorObject(JSLBS::getGlobalContext(), Commons::JSTizenException::INVALID_VALUES_ERROR , "invalid option value");
271 JSValueRef args[1] = {error};
272 JSObjectCallAsFunction(JSLBS::getGlobalContext(), onfail , NULL, 1, args, NULL);
275 return JSValueMakeUndefined(ctx);