06891efae477d5c275c8757c6d83c0450cb41308
[framework/web/wrt-plugins-tizen.git] / src / DataControl / JSDataControlManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <JSTizenExceptionFactory.h>
25 #include <JSTizenException.h>
26 #include <SecurityExceptions.h>
27 #include <Commons/Exception.h>
28 #include <TimeTracer.h>
29 #include "DataType.h"
30 #include "JSDataControlManager.h"
31 #include "JSMappedDataControlConsumer.h"
32 #include "JSSQLDataControlConsumer.h"
33 #include "DataControlListener.h"
34 #include "plugin_config.h"
35
36 #include <ArgumentValidator.h> 
37 #include <JSWebAPIError.h>
38 #include <JSWebAPIException.h> 
39 #include <JSUtil.h>  
40
41
42 using namespace std;
43 using namespace DPL;
44 using namespace WrtDeviceApis;
45 using namespace WrtDeviceApis::CommonsJavaScript;
46 using namespace DeviceAPI::Common;
47
48
49 namespace DeviceAPI {
50 namespace DataControl {
51         
52 JSClassRef JSDataControlManager::m_jsClassRef = NULL;
53
54 JSClassDefinition JSDataControlManager::m_classInfo =
55 {
56         0,
57         kJSClassAttributeNone,
58         "DataControlManager",
59         NULL,
60         NULL,
61         m_function,
62         initialize,
63         finalize,
64         NULL,
65         NULL,
66         NULL,
67         NULL,
68         NULL,
69         NULL,
70         NULL,
71         hasInstance,
72         NULL
73 };
74
75 JSStaticFunction JSDataControlManager::m_function[] =
76 {
77         { "getDataControlConsumer", getDataControlConsumer, kJSPropertyAttributeNone },
78         { 0, 0, 0 }
79 };
80
81 const JSClassRef JSDataControlManager::getClassRef() 
82 {
83         if (!m_jsClassRef) 
84         {
85                 m_jsClassRef = JSClassCreate(&m_classInfo);
86         }
87         return m_jsClassRef;
88 }
89
90 const JSClassDefinition* JSDataControlManager::getClassInfo() 
91 {
92         return &m_classInfo;
93 }
94
95
96 void JSDataControlManager::initialize(JSContextRef context, JSObjectRef object) 
97 {
98         JSDataControlManagerPriv *priv = static_cast<JSDataControlManagerPriv*>(JSObjectGetPrivate(object));
99         LoggerD("JSDataControlManager::initialize");
100
101         if (priv != NULL)
102         {
103                 LoggerE("already exist");
104         }
105         else
106         {
107                 priv = new JSDataControlManagerPriv(context);
108                 LoggerD("OK creation");
109                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) 
110                 {
111                         LoggerE("Object can't store private data.");
112                         delete priv;
113                 }
114         }
115
116 }
117
118 void JSDataControlManager::finalize(JSObjectRef object) 
119 {
120         LoggerD("JSDataControlManager::Finalrize");
121
122         
123         JSDataControlManagerPriv *priv = static_cast<JSDataControlManagerPriv*>(JSObjectGetPrivate(object));
124
125         if (priv != NULL)
126         {
127                 delete priv;
128         }
129 }
130
131 bool JSDataControlManager::hasInstance(JSContextRef context, JSObjectRef constructor,
132                 JSValueRef possibleInstance, JSValueRef* exception) 
133 {
134         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
135 }
136
137
138 JSValueRef JSDataControlManager::getDataControlConsumer (
139         JSContextRef context, JSObjectRef object,
140         JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
141         JSValueRef* exception)
142
143 {
144         LoggerD("Enter");
145         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
146
147
148         // argument validation with new validator  
149         try 
150         {
151                 ArgumentValidator validator(context, argumentCount, arguments); 
152                 validator.toString(0, false); 
153                 validator.toString(1, false);  
154                 validator.toString(2, false);
155         }
156         catch (const BasePlatformException &err) 
157         {
158                 return JSWebAPIException::throwException(context, exception, err);
159         } 
160         catch (...) 
161         {
162                 DeviceAPI::Common::UnknownException err("");
163                 return JSWebAPIException::throwException(context, exception, err);
164         }  
165
166         JSValueRef reserveArguments[3];
167         JSDataControlManagerPriv *priv = static_cast<JSDataControlManagerPriv*>(JSObjectGetPrivate(thisObject));
168
169         Try 
170         {
171                 for (size_t index = 0; index < 3; index++)
172                 {
173                         if (index < argumentCount)
174                                 reserveArguments[index] = arguments[index];
175                         else 
176                                 reserveArguments[index] = JSValueMakeUndefined(context);
177                 }
178
179
180                 Converter converter(context);
181                 std::string providerId = converter.toString(reserveArguments[0]);
182                 std::string dataId = converter.toString(reserveArguments[1]);
183                 std::string type = converter.toString(reserveArguments[2]);
184
185
186                 if (type == SQL_DATA_CONTROL)
187                 {
188                         return JSSQLDataControlConsumer::createJSObject(
189                                 priv->getContext(), providerId, dataId, type);
190                 }
191                 else if (type == MAP_DATA_CONTROL)
192                 {
193                         return JSMappedDataControlConsumer::createJSObject(
194                                 priv->getContext(), providerId, dataId, type);
195                 }
196                 else
197                 {
198                         Throw(WrtDeviceApis::Commons::ConversionException);             
199                 }
200                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
201         }
202         Catch (WrtDeviceApis::Commons::ConversionException)
203         {
204                 LoggerE("ConversionException");
205                 return JSTizenExceptionFactory::postException(context, exception, 
206                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
207
208         }
209         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
210         {
211                 LoggerE("InvalidArgumentException");
212                 return JSTizenExceptionFactory::postException(context, exception, 
213                                 JSTizenException::INVALID_VALUES_ERROR, "type mismatch error");
214         }
215         Catch(WrtDeviceApis::Commons::UnsupportedException)
216         {
217                 LoggerE("UnsupportException");
218                 return JSTizenExceptionFactory::postException(context, exception,
219                                 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
220         }
221         Catch(WrtDeviceApis::Commons::Exception) 
222         {
223                 LoggerE("UnkownException");
224                 return JSTizenExceptionFactory::postException(context, exception,
225                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
226         }
227 }
228
229
230 }
231 }