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"
41 using namespace DeviceAPI::Common;
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
45 JSClassDefinition JSContactWebSite::m_classInfo =
48 kJSClassAttributeNone,
58 NULL, //DeleteProperty,
59 NULL, //getPropertyNames,
60 NULL, //CallAsFunction,
61 constructor, //CallAsConstructor,
62 hasInstance, //HasInstance,
63 NULL, //ConvertToType,
66 JSStaticValue JSContactWebSite::m_property[] = {
67 { CONTACT_ATTR_URL, getUrl, setUrl, kJSPropertyAttributeNone },
68 { CONTACT_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
72 JSStaticFunction JSContactWebSite::m_functions[] =
77 JSClassRef JSContactWebSite::m_classRef = JSClassCreate(&m_classInfo);
79 JSClassRef DLL_EXPORT JSContactWebSite::getClassRef() {
81 m_classRef = JSClassCreate(&m_classInfo);
86 bool JSContactWebSite::isObjectOfClass(JSContextRef context, JSValueRef value)
88 return JSValueIsObjectOfClass(context, value, getClassRef());
91 ContactWebSitePtr JSContactWebSite::getContactWebSite(JSContextRef context, JSValueRef value)
93 if (!isObjectOfClass(context, value)) {
94 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
96 JSObjectRef object = JSValueToObject(context, value, NULL);
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
102 Throw(WrtDeviceApis::Commons::NullPointerException);
104 return priv->getObject();
107 void JSContactWebSite::Initialize(JSContextRef context, JSObjectRef object)
109 if (!JSObjectGetPrivate(object))
111 ContactWebSitePtr webSite(new ContactWebSite());
112 JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, ContactWebSitePtr(webSite));
113 if (!JSObjectSetPrivate(object, priv)) {
119 void JSContactWebSite::Finalize(JSObjectRef object)
121 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
127 JSObjectRef JSContactWebSite::createJSObject(JSContextRef context, ContactWebSitePtr contactWebSite)
129 JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, contactWebSite);
130 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
131 if (NULL == jsObjectRef) {
132 LoggerE("object creation error");
138 ContactWebSitePtr JSContactWebSite::getPrivData(JSObjectRef object)
140 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
142 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
144 ContactWebSitePtr result = priv->getObject();
146 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
151 JSObjectRef JSContactWebSite::constructor(JSContextRef context,
152 JSObjectRef constructor,
153 size_t argumentCount,
154 const JSValueRef arguments[],
155 JSValueRef* exception)
157 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(constructor));
159 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
161 JSContextRef gContext = priv->getContext();
164 std::string stringType;
165 ContactWebSiteType type = WEBSITE_TYPE_HOMEPAGE;
166 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
169 ArgumentValidator Argvalidator(context, argumentCount, arguments);
170 url = Argvalidator.toString(0, false);
172 if (argumentCount >= 2){
173 stringType = Argvalidator.toString(1, true);
174 type = converter->toContactWebSiteType(stringType);
177 } catch (const TypeMismatchException& err ) {
178 JSWebAPIErrorFactory::postException(context, exception, err);
180 } catch(const BasePlatformException& err) {
181 JSWebAPIErrorFactory::postException(context, exception, err);
183 } catch(const ConversionException& err) {
184 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
186 } catch(const NullPointerException& err) {
187 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
191 ContactWebSitePtr contactWebSite(new ContactWebSite());
192 contactWebSite->setUrl(url);
193 contactWebSite->setType(type);
195 JSObjectRef jsobject;
198 jsobject = createJSObject(gContext, contactWebSite);
200 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
201 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
208 bool JSContactWebSite::hasInstance(JSContextRef context,
209 JSObjectRef constructor,
210 JSValueRef possibleInstance,
211 JSValueRef* exception)
213 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
216 JSValueRef JSContactWebSite::getUrl(JSContextRef context,
218 JSStringRef propertyName,
219 JSValueRef* exception)
223 ContactConverterFactory::ConverterType converter =
224 ContactConverterFactory::getConverter(context);
225 ContactWebSitePtr webSite = getPrivData(object);
226 return converter->toJSValueRef(webSite->getUrl());
228 Catch(WrtDeviceApis::Commons::Exception)
230 LoggerW("trying to get incorrect value");
232 return JSValueMakeUndefined(context);
235 bool JSContactWebSite::setUrl(JSContextRef context,
237 JSStringRef propertyName,
239 JSValueRef* exception)
243 ContactWebSitePtr webSite = getPrivData(object);
244 ContactConverterFactory::ConverterType converter =
245 ContactConverterFactory::getConverter(context);
246 webSite->setUrl(converter->toString(value));
248 Catch(WrtDeviceApis::Commons::Exception)
250 LoggerW("trying to set incorrect value");
251 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
257 JSValueRef JSContactWebSite::getType(JSContextRef context,
259 JSStringRef propertyName,
260 JSValueRef* exception)
264 ContactConverterFactory::ConverterType converter =
265 ContactConverterFactory::getConverter(context);
266 ContactWebSitePtr webSite = getPrivData(object);
267 std::string ret = converter->toContactWebSiteTypeStr(webSite->getType());
268 if(!webSite->getTypeIsSet())
269 return JSValueMakeNull(context);
271 return converter->toJSValueRef(ret);
273 Catch(WrtDeviceApis::Commons::Exception)
275 LoggerW("trying to get incorrect value");
277 return JSValueMakeUndefined(context);
280 bool JSContactWebSite::setType(JSContextRef context,
282 JSStringRef propertyName,
284 JSValueRef* exception)
288 ContactWebSitePtr webSite = getPrivData(object);
289 ContactConverterFactory::ConverterType converter =
290 ContactConverterFactory::getConverter(context);
291 BasicValidator validator =
292 BasicValidatorFactory::getValidator(context, exception);
293 if(validator->isNullOrUndefined(value))
294 webSite->unsetType();
297 ContactWebSiteType type = converter->toContactWebSiteType(converter->toString(value));
298 webSite->setType(type);
301 Catch(WrtDeviceApis::Commons::Exception)
303 LoggerW("trying to set incorrect value");
304 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");