c77f8e6efe05795204f2e9c1a62b8b70c8721e71
[framework/web/wrt-plugins-tizen.git] / src / standards / Tizen / Geocoder / JSGeocoderServiceManager.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 <CommonsJavaScript/Converter.h>
18 #include <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
21 #include <Tizen/Common/JSTizenExceptionFactory.h>
22 #include <Tizen/Common/JSTizenException.h>
23 #include <Tizen/Common/SecurityExceptions.h>
24 #include "JSGeocoderServiceManager.h"
25 #include "JSGeocoderProvider.h"
26 #include "DefaultGeocoderProvider.h"
27 #include "GeocoderACE.h"
28
29 #include <dlog.h>
30
31 #undef LOG_TAG
32 #define LOG_TAG "TIZEN_W_GEOCODER"
33
34 using namespace std;
35 using namespace WrtDeviceApis::CommonsJavaScript;
36 using namespace WrtDeviceApis::Commons;
37
38 namespace TizenApis {
39 namespace Tizen1_0 {
40 namespace LBS {
41
42 JSContextRef JSGeocoderServiceManager::m_globalContextRef = NULL;
43
44 JSValueRef JSGeocoderServiceManager::m_defaultObj = NULL;
45
46 JSClassRef JSGeocoderServiceManager::m_jsClassRef = NULL;
47
48 JSClassDefinition JSGeocoderServiceManager::m_jsClassInfo = {
49         0,                                                                                                                              // current (and only) version is 0
50         kJSClassAttributeNone,                                                  //attributes
51         "Geocoder",                                                                                                             // class name
52         NULL,                                                                                                           // parent class
53         NULL,                                                                                                           // static values
54         JSGeocoderServiceManager::m_function,           // static functions
55         JSGeocoderServiceManager::initialize,                   // initialize
56         JSGeocoderServiceManager::finalize,
57         NULL, //hasProperty
58         NULL, //getProperty
59         NULL, //setProperty
60         NULL, //deleteProperty
61         NULL, //getPropertyNames
62         NULL, //call as function
63         NULL, //call as constructor
64         JSGeocoderServiceManager::hasInstance,
65         NULL
66 };
67
68 JSStaticFunction JSGeocoderServiceManager::m_function[] = {
69         { "getDefaultProvider",JSGeocoderServiceManager::getDefaultProvider,kJSPropertyAttributeNone },
70         { "getProviders",JSGeocoderServiceManager::getProviders,kJSPropertyAttributeNone },
71                 { 0, 0, 0 }
72 };
73
74 const JSClassRef JSGeocoderServiceManager::getClassRef() 
75 {
76         if (!m_jsClassRef) {
77                 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
78         }
79         return m_jsClassRef;
80 }
81
82 const JSClassDefinition* JSGeocoderServiceManager::getClassInfo() 
83 {
84         return &m_jsClassInfo;
85 }
86
87 void JSGeocoderServiceManager::initialize(JSContextRef ctx, JSObjectRef object) 
88 {
89         LOGD("%s - JSGeocoderServiceManager", __func__);
90         if( JSGeocoderServiceManager::m_globalContextRef == NULL )
91         {
92                 JSGeocoderServiceManager::m_globalContextRef = ctx;
93         }
94 }
95
96 void JSGeocoderServiceManager::finalize(JSObjectRef object) 
97 {
98         LOGD("%s - JSGeocoderServiceManager", __func__);
99         if( JSGeocoderServiceManager::m_defaultObj != NULL )
100         {
101                 JSValueUnprotect(JSGeocoderServiceManager::m_globalContextRef, JSGeocoderServiceManager::m_defaultObj);
102        JSGeocoderServiceManager::m_defaultObj = NULL;
103         }
104 }
105
106 bool JSGeocoderServiceManager::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
107 {
108         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
109 }
110
111 JSValueRef JSGeocoderServiceManager::getDefaultProvider(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
112 {
113         AceSecurityStatus status = GEOCODER_CHECK_ACCESS(GEOCODER_FUNCTION_CATEGORY_BASIC);
114         
115         if( status != AceSecurityStatus::AccessGranted )
116                 return Commons::JSTizenExceptionFactory::postException(context,exception, Commons::JSTizenException::PERMISSION_DENIED_ERROR );
117
118         if( JSGeocoderServiceManager::m_defaultObj == NULL){
119                 GeocoderProvider *geocoder = new DefaultGeocoderProvider();             
120                 JSGeocoderServiceManager::m_defaultObj  = JSObjectMake(context , JSGeocoderProvider::getClassRef() , geocoder);
121                 JSValueProtect(context, JSGeocoderServiceManager::m_defaultObj);
122         }
123         return JSGeocoderServiceManager::m_defaultObj;
124 }
125
126 JSValueRef JSGeocoderServiceManager::getProviders(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
127 {
128         AceSecurityStatus status = GEOCODER_CHECK_ACCESS(GEOCODER_FUNCTION_CATEGORY_BASIC);
129         
130         if( status != AceSecurityStatus::AccessGranted )
131                 return Commons::JSTizenExceptionFactory::postException(context,exception, Commons::JSTizenException::PERMISSION_DENIED_ERROR );
132         
133         JSValueRef providers[1];
134         JSObjectRef array;
135         providers[0] = getDefaultProvider(context ,object, thisObject, argumentCount, arguments, exception);
136         array = JSObjectMakeArray( context, 1, providers, NULL);
137         return array;   
138 }
139
140 } //LBS
141 } // Tizen1_0
142 } // TizenApis
143