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.
20 #include <CommonsJavaScript/JSUtils.h>
22 #include <JSTizenExceptionFactory.h>
23 #include <JSTizenException.h>
24 #include <SecurityExceptions.h>
25 #include <ArgumentValidator.h>
26 #include <GlobalContextManager.h>
27 #include <JSWebAPIException.h>
28 #include <PlatformException.h>
30 #include "NFCFactory.h"
31 #include "JSNdefRecordMedia.h"
32 #include "NFCConverter.h"
38 using namespace DeviceAPI::Common;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
42 #define TIZEN_NDEFRECORD_MEDIA_MIMETYPE "mimeType"
44 JSClassDefinition JSNdefRecordMedia::m_classInfo =
47 kJSClassAttributeNone,
49 JSNdefRecord::getClassRef(),
57 NULL, //DeleteProperty,
58 NULL, //GetPropertyNames,
59 NULL, //CallAsFunction,
60 NULL, //CallAsConstructor,
65 JSStaticValue JSNdefRecordMedia::m_property[] =
67 {TIZEN_NDEFRECORD_MEDIA_MIMETYPE, getProperty, NULL, kJSPropertyAttributeReadOnly},
71 const JSClassRef JSNdefRecordMedia::getClassRef() {
73 m_jsClassRef = JSClassCreate(&m_classInfo);
78 const JSClassDefinition* JSNdefRecordMedia::getClassInfo() {
82 JSClassRef JSNdefRecordMedia::m_jsClassRef = JSClassCreate(JSNdefRecordMedia::getClassInfo());
84 JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
85 return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload));
88 JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, const std::string &mimeType, std::vector<unsigned char> data) {
89 return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(mimeType, data));
92 JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, INdefRecordPtr ndefRecord) {
93 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, ndefRecord);
96 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
99 JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
103 void JSNdefRecordMedia::initialize(JSContextRef context, JSObjectRef object) {
107 void JSNdefRecordMedia::finalize(JSObjectRef object) {
111 JSObjectRef JSNdefRecordMedia::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
116 ArgumentValidator validator(ctx, argumentCount, arguments);
117 std::string mimeType = validator.toString(0);
118 std::vector<unsigned char> data;
119 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx);
121 Converter convert(ctx);
122 if (validator.toArrayObject(1))
123 data = convert.toVectorOfUChars(arguments[1]);
125 JSObjectRef result = createJSObject(global_context, mimeType, data);
127 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
128 JSObjectSetProperty(global_context, result, ctorName, constructor,
129 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
130 JSStringRelease(ctorName);
132 } Catch (BasePlatformException) {
133 JSObjectRef error = JSWebAPIException::makeJSWebAPIException(ctx, _rethrown_exception);
136 } Catch(ConversionException) {
137 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
138 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
141 } Catch (InvalidArgumentException) {
142 LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage());
143 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
146 } Catch (PlatformException) {
147 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
148 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
151 } Catch (WrtDeviceApis::Commons::UnknownException) {
152 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
153 } Catch (WrtDeviceApis::Commons::Exception) {
154 LoggerE("Exception: " << _rethrown_exception.GetMessage());
156 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
161 JSValueRef JSNdefRecordMedia::getProperty(JSContextRef context, JSObjectRef object,
162 JSStringRef propertyName, JSValueRef* exception) {
166 NFCConverter convert(context);
167 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_MEDIA_MIMETYPE)) {
168 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
170 ThrowMsg(NullPointerException, "Private object not set.");
172 INdefRecordPtr ndefRecord = priv->getObject();
173 char * mimeType = NULL;
174 if (ndefRecord->getMimeType(&mimeType)) {
175 std::string result(mimeType);
177 LoggerD("mimeType : " << result);
178 return convert.toJSValueRef(result);
180 LoggerD("This record is not MEDIA Type");
183 } Catch (ConversionException) {
184 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
185 } Catch (WrtDeviceApis::Commons::UnknownException) {
186 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
187 } Catch (PlatformException) {
188 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
189 } Catch (NullPointerException) {
190 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
191 } Catch (WrtDeviceApis::Commons::Exception) {
192 LoggerE("Exception: " << _rethrown_exception.GetMessage());
194 return JSValueMakeUndefined(context);