upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / LBS / JSLocationServiceProvider.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 #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>
21 #include "LBSAce.h"
22 #include "LBSUtil.h"
23 #include "JSLBSPending.h"
24 #include "JSLBS.h"
25
26 #include <dlog.h>
27
28 #undef LOG_TAG
29 #define LOG_TAG "TIZEN_LBS"
30
31 using namespace std;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace WrtDeviceApis::Commons;
34
35 namespace TizenApis {
36 namespace Tizen1_0 {
37 namespace LBS {
38
39 struct CallbackData {
40         JSObjectRef onSuccess;
41         JSObjectRef onFail;
42 };
43
44 JSContextRef JSLocationServiceProvider::m_globalContextRef = NULL;
45 JSClassRef JSLocationServiceProvider::m_jsClassRef = NULL;
46
47 JSClassDefinition JSLocationServiceProvider::m_jsClassInfo = {
48         0,
49         kJSClassAttributeNone,
50         "LocationServiceProvider",
51         NULL,
52         NULL,
53         m_function,
54         initialize,
55         finalize,
56         NULL, //hasProperty
57         JSLocationServiceProvider::getProperty, //getProperty
58         NULL, //setProperty
59         NULL, //deleteProperty
60         NULL, //getPropertyNames
61         NULL,
62         NULL, // constructor
63         hasInstance,
64         NULL
65 };
66
67
68 JSStaticFunction JSLocationServiceProvider::m_function[] = {
69         { "setOptions",JSLocationServiceProvider::setOptions,kJSPropertyAttributeNone },
70                 { 0, 0, 0 }
71 };
72
73 const JSClassRef JSLocationServiceProvider::getClassRef()
74 {
75         if (!m_jsClassRef) {
76                 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
77         }
78         return m_jsClassRef;
79 }
80
81 const JSClassDefinition* JSLocationServiceProvider::getClassInfo()
82 {
83         return &m_jsClassInfo;
84 }
85
86 void JSLocationServiceProvider::initialize(JSContextRef ctx, JSObjectRef object)
87 {
88         if( JSLocationServiceProvider::m_globalContextRef == NULL ){
89                 JSLocationServiceProvider::m_globalContextRef = ctx;
90         }
91 }
92
93 void JSLocationServiceProvider::finalize(JSObjectRef object)
94 {
95         LocationServiceProvider * priv = reinterpret_cast<LocationServiceProvider*>(JSObjectGetPrivate(object));
96         if( priv ){
97                 delete priv;
98                 JSObjectSetPrivate(object, NULL);
99         }
100 }
101
102 bool JSLocationServiceProvider::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
103         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
104 }
105
106 JSValueRef JSLocationServiceProvider::getProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception){
107
108         LocationServiceProvider * priv = reinterpret_cast<LocationServiceProvider*>(JSObjectGetPrivate(object));
109         if( priv == NULL)
110                 return NULL;
111
112         if(JSStringIsEqualToUTF8CString(propertyName, "name")) {
113                 JSStringRef name = JSStringCreateWithUTF8CString(priv->getName().c_str());
114                 JSValueRef jsv = JSValueMakeString(ctx, name);
115                 JSStringRelease(name);
116                 return jsv;
117         }
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);
129                 }               
130                 return metaobject;
131         }
132         else if(JSStringIsEqualToUTF8CString(propertyName, "attribution")) {
133                 JSStringRef attr = JSStringCreateWithUTF8CString(priv->getAttribution().c_str());
134                 JSValueRef jsv = JSValueMakeString(ctx, attr);
135                 JSStringRelease(attr);
136                 return jsv;
137         }
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);
146                 }
147                 JSObjectRef supportedOptionArray = JSObjectMakeArray(ctx, supportedOptionVector.size(), supportedOptionValues, NULL);
148                 delete []supportedOptionValues;
149                 return supportedOptionArray;
150         }
151         else if(JSStringIsEqualToUTF8CString(propertyName, "connectivity")) {           
152                 JSStringRef conn = JSStringCreateWithUTF8CString(priv->getConnectivity().c_str());
153                 JSValueRef jsv = JSValueMakeString(ctx, conn);
154                 JSStringRelease(conn);
155                 return jsv;
156         }
157
158         return NULL;
159 }
160
161 void _setOptionCb(int result , void * user_data){
162
163         CallbackData * userdata =  static_cast<CallbackData *>(user_data);
164         if ( userdata == NULL)
165                 return;
166
167         if( result == 0 && userdata->onSuccess ){
168                 JSValueRef args[1] = {NULL};
169                 JSObjectCallAsFunction(JSLBS::getGlobalContext() , userdata->onSuccess , NULL, 1, args, NULL);
170         }
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);
175         }
176
177         if( userdata->onSuccess )
178                 JSValueUnprotect(JSLBS::getGlobalContext(), userdata->onSuccess);
179         if( userdata->onFail )
180                 JSValueUnprotect(JSLBS::getGlobalContext(), userdata->onFail);
181
182         delete userdata;
183 }
184
185 JSValueRef JSLocationServiceProvider::setOptions(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
186
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 );
191
192         // Check the object type
193         LocationServiceProvider * priv = reinterpret_cast<LocationServiceProvider*>(JSObjectGetPrivate(thisObject));
194         if( priv == NULL){
195                 return Commons::JSTizenExceptionFactory::postException(ctx,exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The object is not LocationServiceProvider instance");
196         }
197
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 );
205                         else
206                                 convertedArgumentObjects[i] = NULL;
207                 }else{
208                         convertedArguments[i] = JSValueMakeUndefined(ctx);
209                         convertedArgumentObjects[i] = NULL;
210                 }
211         }
212
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]) )
217           )
218                 return Commons::JSTizenExceptionFactory::postException(ctx,exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The first argument must be function or null");
219
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]))
224            )
225                 return Commons::JSTizenExceptionFactory::postException(ctx,exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The first argument must be function or null");
226
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");
231         }
232
233         // Initialize a CallbackData
234         CallbackData * userdata = NULL;
235
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;
242
243                 if( onsuccess ){
244                         userdata->onSuccess = onsuccess;
245                         JSValueProtect(ctx, onsuccess);
246                 }
247                 if( onfail ){
248                         userdata->onFail = onfail;
249                         JSValueProtect(ctx, onfail);
250                 }
251         }
252
253         // Convert  Object to vector
254         std::vector<std::pair<std::string, std::string>> options = LBSUtil::convertObject(ctx, jsOptions);      
255
256         // Call native setOption function
257         int ret = priv->setOption(options , _setOptionCb , userdata, NULL);
258
259         // Invalid options values
260         if( ret != 0 ){
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);
267                         delete userdata;
268                 }
269
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);
273         }
274
275         return JSValueMakeUndefined(ctx);
276 }
277
278 } //LBS
279 } // Tizen1_0
280 } // TizenApis
281