2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
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>
30 #include "JSDataControlManager.h"
31 #include "JSMappedDataControlConsumer.h"
32 #include "JSSQLDataControlConsumer.h"
33 #include "DataControlListener.h"
34 #include "plugin_config.h"
36 #include <ArgumentValidator.h>
37 #include <JSWebAPIError.h>
38 #include <JSWebAPIException.h>
44 using namespace WrtDeviceApis;
45 using namespace WrtDeviceApis::CommonsJavaScript;
46 using namespace DeviceAPI::Common;
50 namespace DataControl {
52 JSClassRef JSDataControlManager::m_jsClassRef = NULL;
54 JSClassDefinition JSDataControlManager::m_classInfo =
57 kJSClassAttributeNone,
75 JSStaticFunction JSDataControlManager::m_function[] =
77 { "getDataControlConsumer", getDataControlConsumer, kJSPropertyAttributeNone },
81 const JSClassRef JSDataControlManager::getClassRef()
85 m_jsClassRef = JSClassCreate(&m_classInfo);
90 const JSClassDefinition* JSDataControlManager::getClassInfo()
96 void JSDataControlManager::initialize(JSContextRef context, JSObjectRef object)
98 JSDataControlManagerPriv *priv = static_cast<JSDataControlManagerPriv*>(JSObjectGetPrivate(object));
99 LoggerD("JSDataControlManager::initialize");
103 LoggerE("already exist");
107 priv = new JSDataControlManagerPriv(context);
108 LoggerD("OK creation");
109 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
111 LoggerE("Object can't store private data.");
118 void JSDataControlManager::finalize(JSObjectRef object)
120 LoggerD("JSDataControlManager::Finalrize");
123 JSDataControlManagerPriv *priv = static_cast<JSDataControlManagerPriv*>(JSObjectGetPrivate(object));
131 bool JSDataControlManager::hasInstance(JSContextRef context, JSObjectRef constructor,
132 JSValueRef possibleInstance, JSValueRef* exception)
134 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
138 JSValueRef JSDataControlManager::getDataControlConsumer (
139 JSContextRef context, JSObjectRef object,
140 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
141 JSValueRef* exception)
145 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
148 // argument validation with new validator
151 ArgumentValidator validator(context, argumentCount, arguments);
152 validator.toString(0, false);
153 validator.toString(1, false);
154 validator.toString(2, false);
156 catch (const BasePlatformException &err)
158 return JSWebAPIException::throwException(context, exception, err);
162 DeviceAPI::Common::UnknownException err("");
163 return JSWebAPIException::throwException(context, exception, err);
166 JSValueRef reserveArguments[3];
167 JSDataControlManagerPriv *priv = static_cast<JSDataControlManagerPriv*>(JSObjectGetPrivate(thisObject));
171 for (size_t index = 0; index < 3; index++)
173 if (index < argumentCount)
174 reserveArguments[index] = arguments[index];
176 reserveArguments[index] = JSValueMakeUndefined(context);
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]);
186 if (type == SQL_DATA_CONTROL)
188 return JSSQLDataControlConsumer::createJSObject(
189 priv->getContext(), providerId, dataId, type);
191 else if (type == MAP_DATA_CONTROL)
193 return JSMappedDataControlConsumer::createJSObject(
194 priv->getContext(), providerId, dataId, type);
198 Throw(WrtDeviceApis::Commons::ConversionException);
200 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
202 Catch (WrtDeviceApis::Commons::ConversionException)
204 LoggerE("ConversionException");
205 return JSTizenExceptionFactory::postException(context, exception,
206 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
209 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
211 LoggerE("InvalidArgumentException");
212 return JSTizenExceptionFactory::postException(context, exception,
213 JSTizenException::INVALID_VALUES_ERROR, "type mismatch error");
215 Catch(WrtDeviceApis::Commons::UnsupportedException)
217 LoggerE("UnsupportException");
218 return JSTizenExceptionFactory::postException(context, exception,
219 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
221 Catch(WrtDeviceApis::Commons::Exception)
223 LoggerE("UnkownException");
224 return JSTizenExceptionFactory::postException(context, exception,
225 JSTizenException::UNKNOWN_ERROR, "Unkown error");