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 convertedArg[3];
199 JSObjectRef convertedArgObjects[3];
200 for( unsigned int i = 0 ; i < 3; i++ )
204 convertedArg[i] = arguments[i];
205 convertedArgObjects[i] = JSValueToObject(ctx, arguments[i] , NULL );
209 convertedArg[i] = JSValueMakeUndefined(ctx);
210 convertedArgObjects[i] = NULL;
214 //Check the first argument
215 if(JSValueIsNull(ctx, convertedArg[0]) || JSValueIsUndefined(ctx, convertedArg[0]) || !JSValueIsObject(ctx, convertedArg[0]))
217 return Commons::JSTizenExceptionFactory::postException(ctx,exception, Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The first argument is invalid (null, undefined or not an object)");
220 JSObjectRef onsuccess;
223 // The second argument is null or undefined or function object
224 bool isFunctionOnSuccess = false;
225 if(JSValueIsNull(ctx, convertedArg[1]) || JSValueIsUndefined(ctx, convertedArg[1]))
227 isFunctionOnSuccess = false;
228 LOGD("%s - 2rd argument is OK (Either null or undefined) ", __func__);
230 else if (JSObjectIsFunction(ctx, convertedArgObjects[1]))
232 isFunctionOnSuccess = true;
233 onsuccess = convertedArgObjects[1];
234 LOGD("%s - 2rd argument is OK (function) ", __func__);
238 return Commons::JSTizenExceptionFactory::postException(ctx,exception, Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The second argument must be a function");
241 // The third argument is null or undefined or function object
242 bool isFunctionOnFail = false;
243 if(JSValueIsNull(ctx, convertedArg[2]) || JSValueIsUndefined(ctx, convertedArg[2]))
245 isFunctionOnFail = false;
246 LOGD("%s - 3rd argument is OK (Either null or undefined) ", __func__);
248 else if (JSObjectIsFunction(ctx, convertedArgObjects[2]))
250 isFunctionOnFail = true;
251 onfail = convertedArgObjects[2];
252 LOGD("%s - 3rd argument is OK (function) ", __func__);
256 return Commons::JSTizenExceptionFactory::postException(ctx,exception, Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The third argument must be a function");
259 // Initialize a CallbackData
260 CallbackData * userdata = NULL;
261 userdata = new CallbackData();
263 //set the success callback
264 if(isFunctionOnSuccess)
266 userdata->onSuccess = onsuccess;
267 JSValueProtect(ctx, onsuccess);
271 userdata->onSuccess = NULL;
274 //set the error callback
277 userdata->onFail = onfail;
278 JSValueProtect(ctx, onfail);
282 userdata->onFail = NULL;
285 // The first argument is option object
286 JSObjectRef jsOptions = convertedArgObjects[0];
287 if( jsOptions == NULL ){
288 return Commons::JSTizenExceptionFactory::postException(ctx, exception, Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The option arguments is not Object");
291 // Convert Object to vector
292 std::vector<std::pair<std::string, std::string>> options = LBSUtil::convertObject(ctx, jsOptions);
294 // Call native setOption function
295 int ret = priv->setOption(options , _setOptionCb , userdata, NULL);
296 LOGD("ret : %d ", ret);
298 // Invalid options values
300 JSObjectRef error = Commons::JSTizenExceptionFactory::makeErrorObject(JSLBS::getGlobalContext(), Commons::JSTizenException::INVALID_VALUES_ERROR , "invalid option value");
301 JSValueRef args[1] = {error};
302 if( userdata->onFail )
303 JSObjectCallAsFunction(JSLBS::getGlobalContext(), userdata->onFail , NULL, 1, args, NULL);
305 //remove callback values
306 if( userdata != NULL ){
307 if( userdata->onSuccess )
308 JSValueUnprotect(ctx, userdata->onSuccess);
309 if( userdata->onFail )
310 JSValueUnprotect(ctx, userdata->onFail);
315 return JSValueMakeUndefined(ctx);