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 * @file JSContactWebSite.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactWebSite class
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <JSWebAPIErrorFactory.h>
28 #include "ContactConverter.h"
29 #include "JSContactWebSite.h"
33 #define CONTACT_CLASS_NAME "ContactWebSite"
35 #define CONTACT_ATTR_URL "url"
36 #define CONTACT_ATTR_TYPE "type"
37 #define CONTACT_ATTR_LABEL "label"
42 using namespace DeviceAPI::Common;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
46 JSClassDefinition JSContactWebSite::m_classInfo =
49 kJSClassAttributeNone,
59 NULL, //DeleteProperty,
60 NULL, //getPropertyNames,
61 NULL, //CallAsFunction,
62 constructor, //CallAsConstructor,
63 hasInstance, //HasInstance,
64 NULL, //ConvertToType,
67 JSStaticValue JSContactWebSite::m_property[] = {
68 { CONTACT_ATTR_URL, getUrl, setUrl, kJSPropertyAttributeNone },
69 { CONTACT_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
70 { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
74 JSStaticFunction JSContactWebSite::m_functions[] =
79 JSClassRef JSContactWebSite::m_classRef = JSClassCreate(&m_classInfo);
81 JSClassRef DLL_EXPORT JSContactWebSite::getClassRef() {
83 m_classRef = JSClassCreate(&m_classInfo);
88 bool JSContactWebSite::isObjectOfClass(JSContextRef context, JSValueRef value)
90 return JSValueIsObjectOfClass(context, value, getClassRef());
93 ContactWebSitePtr JSContactWebSite::getContactWebSite(JSContextRef context, JSValueRef value)
95 if (!isObjectOfClass(context, value)) {
96 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
98 JSObjectRef object = JSValueToObject(context, value, NULL);
100 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
104 Throw(WrtDeviceApis::Commons::NullPointerException);
106 return priv->getObject();
109 void JSContactWebSite::Initialize(JSContextRef context, JSObjectRef object)
111 if (!JSObjectGetPrivate(object))
113 ContactWebSitePtr webSite(new ContactWebSite());
114 JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, ContactWebSitePtr(webSite));
115 if (!JSObjectSetPrivate(object, priv)) {
121 void JSContactWebSite::Finalize(JSObjectRef object)
123 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
129 JSObjectRef JSContactWebSite::createJSObject(JSContextRef context, ContactWebSitePtr contactWebSite)
131 JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, contactWebSite);
132 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
133 if (NULL == jsObjectRef) {
134 LoggerE("object creation error");
140 ContactWebSitePtr JSContactWebSite::getPrivData(JSObjectRef object)
142 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
144 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
146 ContactWebSitePtr result = priv->getObject();
148 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153 JSObjectRef JSContactWebSite::constructor(JSContextRef context,
154 JSObjectRef constructor,
155 size_t argumentCount,
156 const JSValueRef arguments[],
157 JSValueRef* exception)
159 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(constructor));
161 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
163 JSContextRef gContext = priv->getContext();
166 std::string stringType;
167 ContactWebSiteType type = WEBSITE_TYPE_HOMEPAGE;
168 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
172 ArgumentValidator Argvalidator(context, argumentCount, arguments);
173 url = Argvalidator.toString(0, false);
175 if (argumentCount >= 2){
176 stringType = Argvalidator.toString(1, false);
177 type = converter->toContactWebSiteType(stringType);
180 if (argumentCount >= 3){
181 label = Argvalidator.toString(2, false);
184 } catch (const TypeMismatchException& err ) {
185 JSWebAPIErrorFactory::postException(context, exception, err);
187 } catch(const BasePlatformException& err) {
188 JSWebAPIErrorFactory::postException(context, exception, err);
190 } catch(const ConversionException& err) {
191 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
193 } catch(const NullPointerException& err) {
194 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
198 ContactWebSitePtr contactWebSite(new ContactWebSite());
199 contactWebSite->setUrl(url);
200 contactWebSite->setType(type);
201 contactWebSite->setLabel(label);
203 JSObjectRef jsobject;
206 jsobject = createJSObject(gContext, contactWebSite);
208 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
209 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
216 bool JSContactWebSite::hasInstance(JSContextRef context,
217 JSObjectRef constructor,
218 JSValueRef possibleInstance,
219 JSValueRef* exception)
221 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
224 JSValueRef JSContactWebSite::getUrl(JSContextRef context,
226 JSStringRef propertyName,
227 JSValueRef* exception)
231 ContactConverterFactory::ConverterType converter =
232 ContactConverterFactory::getConverter(context);
233 ContactWebSitePtr webSite = getPrivData(object);
234 return converter->toJSValueRef(webSite->getUrl());
236 Catch(WrtDeviceApis::Commons::Exception)
238 LoggerW("trying to get incorrect value");
240 return JSValueMakeUndefined(context);
243 bool JSContactWebSite::setUrl(JSContextRef context,
245 JSStringRef propertyName,
247 JSValueRef* exception)
251 ContactWebSitePtr webSite = getPrivData(object);
252 ContactConverterFactory::ConverterType converter =
253 ContactConverterFactory::getConverter(context);
254 webSite->setUrl(converter->toString(value));
256 Catch(WrtDeviceApis::Commons::Exception)
258 LoggerW("trying to set incorrect value");
259 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
265 JSValueRef JSContactWebSite::getType(JSContextRef context,
267 JSStringRef propertyName,
268 JSValueRef* exception)
272 ContactConverterFactory::ConverterType converter =
273 ContactConverterFactory::getConverter(context);
274 ContactWebSitePtr webSite = getPrivData(object);
275 std::string ret = converter->toContactWebSiteTypeStr(webSite->getType());
276 if(!webSite->getTypeIsSet())
277 return JSValueMakeNull(context);
279 return converter->toJSValueRef(ret);
281 Catch(WrtDeviceApis::Commons::Exception)
283 LoggerW("trying to get incorrect value");
285 return JSValueMakeUndefined(context);
288 bool JSContactWebSite::setType(JSContextRef context,
290 JSStringRef propertyName,
292 JSValueRef* exception)
296 ContactWebSitePtr webSite = getPrivData(object);
297 ContactConverterFactory::ConverterType converter =
298 ContactConverterFactory::getConverter(context);
299 BasicValidator validator =
300 BasicValidatorFactory::getValidator(context, exception);
301 if(validator->isNullOrUndefined(value))
302 webSite->unsetType();
305 ContactWebSiteType type = converter->toContactWebSiteType(converter->toString(value));
306 webSite->setType(type);
309 Catch(WrtDeviceApis::Commons::Exception)
311 LoggerW("trying to set incorrect value");
312 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
317 JSValueRef JSContactWebSite::getLabel(JSContextRef context,
319 JSStringRef propertyName,
320 JSValueRef* exception)
324 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
325 ContactWebSitePtr webSite = getPrivData(object);
326 return converter->toJSValueRef(webSite->getLabel());
328 Catch(WrtDeviceApis::Commons::Exception)
330 LoggerW("trying to get incorrect value");
332 return JSValueMakeUndefined(context);
335 bool JSContactWebSite::setLabel(JSContextRef context,
337 JSStringRef propertyName,
339 JSValueRef* exception)
343 ContactWebSitePtr webSite = getPrivData(object);
344 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
345 webSite->setLabel(converter->toString(value));
347 Catch(WrtDeviceApis::Commons::Exception)
349 LoggerW("trying to set incorrect value");
350 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");