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.
21 #include <CommonsJavaScript/JSUtils.h>
23 #include <JSTizenExceptionFactory.h>
24 #include <JSTizenException.h>
25 #include <SecurityExceptions.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <ArgumentValidator.h>
28 #include <GlobalContextManager.h>
29 #include <JSWebAPIException.h>
30 #include <PlatformException.h>
32 #include "NFCFactory.h"
33 #include "JSNdefRecordURI.h"
39 using namespace DeviceAPI::Common;
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
43 #define TIZEN_NDEFRECORD_URI_URI "uri"
45 JSClassDefinition JSNdefRecordURI::m_classInfo =
48 kJSClassAttributeNone,
50 JSNdefRecord::getClassRef(),
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 NULL, //CallAsConstructor,
66 JSStaticValue JSNdefRecordURI::m_property[] =
68 {TIZEN_NDEFRECORD_URI_URI, getProperty, NULL, kJSPropertyAttributeReadOnly},
72 const JSClassRef JSNdefRecordURI::getClassRef() {
74 m_jsClassRef = JSClassCreate(&m_classInfo);
79 const JSClassDefinition* JSNdefRecordURI::getClassInfo() {
83 JSClassRef JSNdefRecordURI::m_jsClassRef = JSClassCreate(JSNdefRecordURI::getClassInfo());
85 void JSNdefRecordURI::initialize(JSContextRef context, JSObjectRef object) {
89 void JSNdefRecordURI::finalize(JSObjectRef object) {
93 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
94 return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload));
97 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const std::string &uri) {
98 return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(uri));
101 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, INdefRecordPtr ndefRecord) {
102 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, ndefRecord);
105 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
108 JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
112 JSObjectRef JSNdefRecordURI::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
117 ArgumentValidator validator(ctx, argumentCount, arguments);
118 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx);
120 JSObjectRef result = createJSObject(global_context, validator.toString(0));
122 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
123 JSObjectSetProperty(global_context, result, ctorName, constructor,
124 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
125 JSStringRelease(ctorName);
127 } Catch (BasePlatformException) {
128 JSObjectRef error = JSWebAPIException::makeJSWebAPIException(ctx, _rethrown_exception);
131 } Catch(ConversionException) {
132 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
133 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
136 } Catch (InvalidArgumentException) {
137 LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage());
138 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
141 } Catch (PlatformException) {
142 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
143 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
146 } Catch (WrtDeviceApis::Commons::UnknownException) {
147 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
148 } Catch (WrtDeviceApis::Commons::Exception) {
149 LoggerE("Exception: " << _rethrown_exception.GetMessage());
151 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
155 JSValueRef JSNdefRecordURI::getProperty(JSContextRef context, JSObjectRef object,
156 JSStringRef propertyName, JSValueRef* exception) {
160 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_URI_URI)) {
161 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
163 ThrowMsg(NullPointerException, "Private object not set.");
165 INdefRecordPtr ndefRecord = priv->getObject();
167 if (ndefRecord->getUri(&uri)) {
168 Converter convert(context);
169 std::string result(uri);
171 LoggerD("uri : " << result);
172 return convert.toJSValueRef(result);
174 LoggerD("This record is not URI Type");
176 } Catch (ConversionException) {
177 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
178 } Catch (WrtDeviceApis::Commons::UnknownException) {
179 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
180 } Catch (PlatformException) {
181 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
182 } Catch (NullPointerException) {
183 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
184 } Catch (WrtDeviceApis::Commons::Exception) {
185 LoggerE("Exception: " << _rethrown_exception.GetMessage());
187 return JSValueMakeUndefined(context);