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 "JSSyncProfileInfo.h"
20 #include "JSSyncInfo.h"
21 #include "JSSyncServiceInfo.h"
22 #include "DataSyncConverter.h"
24 #include <JSTizenException.h>
25 #include <JSTizenExceptionFactory.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <CommonsJavaScript/JSUtils.h>
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
37 JSClassDefinition JSSyncProfileInfo::m_classInfo = {
39 kJSClassAttributeNone,
40 TIZEN_SYNC_PROFILE_INFO_INTERFACE,
49 NULL, //deleteProperty,
50 NULL, //getPropertyNames,
51 NULL, //callAsFunction,
54 NULL, //convertToType,
57 JSStaticValue JSSyncProfileInfo::m_property[] = {
58 { TIZEN_SYNC_PROFILE_INFO_PROFILE_ID, getProperty, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
59 { TIZEN_SYNC_PROFILE_INFO_PROFILE_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
60 { TIZEN_SYNC_PROFILE_INFO_SYNC_INFO, getProperty, setProperty, kJSPropertyAttributeNone },
61 { TIZEN_SYNC_PROFILE_INFO_SERVICE_INFO, getProperty, setProperty, kJSPropertyAttributeNone },
66 JSClassRef JSSyncProfileInfo::m_jsClassRef = JSClassCreate(
67 JSSyncProfileInfo::getClassInfo());
69 const JSClassDefinition* JSSyncProfileInfo::getClassInfo()
71 return &(m_classInfo);
74 JSClassRef JSSyncProfileInfo::getClassRef()
77 m_jsClassRef = JSClassCreate(&m_classInfo);
82 JSObjectRef JSSyncProfileInfo::createJSSyncProfileInfo(JSContextRef context, SyncProfileInfoPtr syncProfileInfo)
84 SyncProfileInfoPrivateObject *priv = new SyncProfileInfoPrivateObject(context, syncProfileInfo);
85 return JSObjectMake(context, getClassRef(), priv);
88 void JSSyncProfileInfo::initialize(JSContextRef context,
91 if (!JSObjectGetPrivate(object)) {
92 LoggerD("Create JSSyncProfileInfo private object.");
93 SyncProfileInfoPtr syncProfileInfo( new SyncProfileInfo() );
94 SyncProfileInfoPrivateObject *priv = new SyncProfileInfoPrivateObject(context, syncProfileInfo);
95 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
99 LoggerD("Private object already set.");
103 void JSSyncProfileInfo::finalize(JSObjectRef object)
105 SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
107 LoggerD("Deleting JSSyncProfileInfo private object.");
109 JSObjectSetPrivate(object, NULL);
113 SyncProfileInfoPtr JSSyncProfileInfo::getPrivateObject(JSObjectRef object)
115 SyncProfileInfoPrivateObject *priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
117 ThrowMsg(NullPointerException, "Private object is null.");
120 SyncProfileInfoPtr syncProfileInfo = priv->getObject();
121 if (!syncProfileInfo) {
122 ThrowMsg(NullPointerException, "JSSyncProfileInfo object is null.");
124 return syncProfileInfo;
127 void JSSyncProfileInfo::setPrivateObject(const SyncProfileInfoPtr &syncProfileInfo,
129 const JSObjectRef object)
133 SyncProfileInfoPrivateObject *priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
135 priv = new SyncProfileInfoPrivateObject(ctx, syncProfileInfo);
136 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
142 LoggerE("Error during setting JSSyncProfileInfo object.");
146 JSObjectRef JSSyncProfileInfo::constructor(JSContextRef context,
147 JSObjectRef constructor,
148 size_t argumentCount,
149 const JSValueRef arguments[],
150 JSValueRef* exception)
154 DataSyncConverter converter(context);
156 SyncProfileInfoPtr syncProfileInfo( new SyncProfileInfo() );
158 if (argumentCount>=1) {
159 syncProfileInfo->setProfileName(converter.toString(arguments[0]));
161 if (argumentCount>=2) {
162 if (!JSValueIsObjectOfClass(context, arguments[1], JSSyncInfo::getClassRef())) {
163 ThrowMsg(ConversionException, "Wrong second parameter type.");
165 syncProfileInfo->setSyncInfo(JSSyncInfo::getPrivateObject(JSValueToObject(context, arguments[1], exception)));
167 if (argumentCount>=3) {
168 if(!JSIsArrayValue(context, arguments[2])) {
169 ThrowMsg(ConversionException, "Wrong third parameter type.");
171 syncProfileInfo->setServiceInfo(converter.toServiceInfoList(arguments[2]));
174 LoggerD("profileName: "<<syncProfileInfo->getProfileName()<<", syncInfo url: "<<syncProfileInfo->getSyncInfo()->getUrl()<<", serviceInfo.length: "<<syncProfileInfo->getServiceInfo()->size());
176 return createJSSyncProfileInfo(context, syncProfileInfo);
178 Catch(UnsupportedException)
180 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
181 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
183 Catch(InvalidArgumentException)
185 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
186 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
188 Catch(ConversionException)
190 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
191 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
195 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
196 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
202 JSValueRef JSSyncProfileInfo::getProperty(JSContextRef context,
204 JSStringRef propertyName,
205 JSValueRef* exception)
209 SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
211 ThrowMsg(NullPointerException, "Private object is NULL.");
214 SyncProfileInfoPtr syncProfileInfo = priv->getObject();
216 DataSyncConverter converter(context);
218 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_ID)) {
219 return converter.toJSValueRef(syncProfileInfo->getProfileId());
220 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_NAME)) {
221 return converter.toJSValueRef(syncProfileInfo->getProfileName());
222 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SYNC_INFO)) {
223 return JSUtils::makeObject(context, JSSyncInfo::getClassRef(), syncProfileInfo->getSyncInfo());
224 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SERVICE_INFO)) {
225 return converter.toJSValueRefServiceInfoList(syncProfileInfo->getServiceInfo());
227 LoggerD("Wrong property name.");
232 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
235 return JSValueMakeUndefined(context);
238 bool JSSyncProfileInfo::setProperty(JSContextRef context,
240 JSStringRef propertyName,
242 JSValueRef* exception)
246 SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
248 ThrowMsg(NullPointerException, "Private object is NULL.");
251 SyncProfileInfoPtr syncProfileInfo = priv->getObject();
253 DataSyncConverter converter(context);
255 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_NAME)) {
256 syncProfileInfo->setProfileName(converter.toString(value));
258 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SYNC_INFO)) {
259 syncProfileInfo->setSyncInfo(JSSyncInfo::getPrivateObject(JSValueToObject(context, value, exception)));
261 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SERVICE_INFO)) {
262 syncProfileInfo->setServiceInfo(converter.toServiceInfoList(value));
265 LoggerD("Wrong property name.");
270 LoggerW("Exception: "<<_rethrown_exception.GetMessage());