2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "JSDOMException.h"
18 #include <dpl/log/log.h>
20 #include "Converter.h"
23 const char* PLUGIN_NAME = "DOMException";
24 const char* PROPERTY_CODE = "code";
25 const char* PROPERTY_MESSAGE = "message";
30 const unsigned short value;
32 // This array CAN'T be left empty!
33 const Error PROPERTY_ERROR[] = {
35 WrtDeviceApis::CommonsJavaScript::JSDOMException::UNKNOWN_ERR },
37 WrtDeviceApis::CommonsJavaScript::JSDOMException::INDEX_SIZE_ERR },
38 { "DOMSTRING_SIZE_ERR",
39 WrtDeviceApis::CommonsJavaScript::JSDOMException::DOMSTRING_SIZE_ERR },
40 { "HIERARCHY_REQUEST_ERR",
41 WrtDeviceApis::CommonsJavaScript::JSDOMException::
42 HIERARCHY_REQUEST_ERR },
43 { "WRONG_DOCUMENT_ERR",
44 WrtDeviceApis::CommonsJavaScript::JSDOMException::WRONG_DOCUMENT_ERR },
45 { "INVALID_CHARACTER_ERR",
46 WrtDeviceApis::CommonsJavaScript::JSDOMException::
47 INVALID_CHARACTER_ERR },
48 { "NO_DATA_ALLOWED_ERR",
49 WrtDeviceApis::CommonsJavaScript::JSDOMException::
50 NO_DATA_ALLOWED_ERR },
51 { "NO_MODIFICATION_ALLOWED_ERR",
52 WrtDeviceApis::CommonsJavaScript::JSDOMException::
53 NO_MODIFICATION_ALLOWED_ERR },
55 WrtDeviceApis::CommonsJavaScript::JSDOMException::NOT_FOUND_ERR },
56 { "NOT_SUPPORTED_ERR",
57 WrtDeviceApis::CommonsJavaScript::JSDOMException::NOT_SUPPORTED_ERR },
58 { "INUSE_ATTRIBUTE_ERR",
59 WrtDeviceApis::CommonsJavaScript::JSDOMException::
60 INUSE_ATTRIBUTE_ERR },
61 { "INVALID_STATE_ERR",
62 WrtDeviceApis::CommonsJavaScript::JSDOMException::INVALID_STATE_ERR },
64 WrtDeviceApis::CommonsJavaScript::JSDOMException::SYNTAX_ERR },
65 { "INVALID_MODIFICATION_ERR",
66 WrtDeviceApis::CommonsJavaScript::JSDOMException::
67 INVALID_MODIFICATION_ERR },
69 WrtDeviceApis::CommonsJavaScript::JSDOMException::NAMESPACE_ERR },
70 { "INVALID_ACCESS_ERR",
71 WrtDeviceApis::CommonsJavaScript::JSDOMException::INVALID_ACCESS_ERR },
73 WrtDeviceApis::CommonsJavaScript::JSDOMException::VALIDATION_ERR },
74 { "TYPE_MISMATCH_ERR",
75 WrtDeviceApis::CommonsJavaScript::JSDOMException::TYPE_MISMATCH_ERR },
77 WrtDeviceApis::CommonsJavaScript::JSDOMException::SECURITY_ERR },
79 WrtDeviceApis::CommonsJavaScript::JSDOMException::NETWORK_ERR },
81 WrtDeviceApis::CommonsJavaScript::JSDOMException::ABORT_ERR },
83 WrtDeviceApis::CommonsJavaScript::JSDOMException::TIMEOUT_ERR },
84 { "INVALID_VALUES_ERR",
85 WrtDeviceApis::CommonsJavaScript::JSDOMException::INVALID_VALUES_ERR },
87 WrtDeviceApis::CommonsJavaScript::JSDOMException::IO_ERR },
88 { "QUOTA_EXCEEDED_ERR",
89 WrtDeviceApis::CommonsJavaScript::JSDOMException::QUOTA_EXCEEDED_ERR }
93 namespace WrtDeviceApis {
94 namespace CommonsJavaScript {
95 JSClassRef JSDOMException::m_classRef = NULL;
97 JSClassDefinition JSDOMException::m_classInfo = {
99 kJSClassAttributeNone,
109 NULL, //DeleteProperty,
111 NULL, //CallAsFunction,
112 NULL, //CallAsConstructor,
114 NULL, //ConvertToType,
117 JSStaticValue JSDOMException::m_properties[] = {
118 { PROPERTY_CODE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
119 { PROPERTY_MESSAGE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
123 const JSClassDefinition* JSDOMException::getClassInfo()
128 JSClassRef JSDOMException::getClassRef()
131 m_classRef = JSClassCreate(&m_classInfo);
136 void JSDOMException::initialize(JSContextRef /*context*/,
137 JSObjectRef /*object*/)
140 void JSDOMException::finalize(JSObjectRef object)
142 PrivateObject* privateObject =
143 static_cast<PrivateObject*>(JSObjectGetPrivate(object));
145 JSObjectSetPrivate(object, NULL);
146 delete privateObject;
150 bool JSDOMException::hasProperty(JSContextRef /*context*/,
151 JSObjectRef /*object*/,
152 JSStringRef propertyName)
154 const size_t size = sizeof(PROPERTY_ERROR) / sizeof(PROPERTY_ERROR[0]);
155 for (size_t i = 0; i < size; ++i) {
156 if (JSStringIsEqualToUTF8CString(propertyName,
157 PROPERTY_ERROR[i].name))
165 JSValueRef JSDOMException::getStaticProperty(JSContextRef context,
167 JSStringRef propertyName,
168 JSValueRef* /*exception*/)
170 PrivateObject* privateObject =
171 static_cast<PrivateObject*>(JSObjectGetPrivate(object));
172 if (!privateObject) {
173 LogError("Private object is not set.");
174 return JSValueMakeUndefined(context);
177 Converter converter(context);
179 if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_CODE)) {
180 return converter.toJSValueRef(privateObject->getObject()->getCode());
181 } else if (JSStringIsEqualToUTF8CString(propertyName,
184 return converter.toJSValueRef(
185 privateObject->getObject()->getMessage());
187 } catch (const Commons::ConversionException& ex) {
188 LogError("Exception: " << ex.GetMessage());
191 return JSValueMakeUndefined(context);
194 JSValueRef JSDOMException::getProperty(JSContextRef context,
195 JSObjectRef /*object*/,
196 JSStringRef propertyName,
197 JSValueRef* /*exception*/)
199 Converter converter(context);
201 std::string prop = converter.toString(propertyName);
203 const size_t size = sizeof(PROPERTY_ERROR) / sizeof(PROPERTY_ERROR[0]);
204 for (size_t i = 0; i < size; ++i) {
205 if (prop == PROPERTY_ERROR[i].name) {
206 return converter.toJSValueRef(PROPERTY_ERROR[i].value);
209 } catch (const Commons::ConversionException& ex) {
210 LogError("Exception: " << ex.GetMessage());
213 return JSValueMakeUndefined(context);
216 void JSDOMException::getPropertyNames(
217 JSContextRef /*context*/,
218 JSObjectRef /*object*/,
219 JSPropertyNameAccumulatorRef accumulator)
221 const size_t size = sizeof(PROPERTY_ERROR) / sizeof(PROPERTY_ERROR[0]);
222 for (size_t i = 0; i < size; ++i) {
223 JSPropertyNameAccumulatorAddName(accumulator,
224 JSStringCreateWithUTF8CString(
225 PROPERTY_ERROR[i].name));
229 bool JSDOMException::hasInstance(JSContextRef context,
230 JSObjectRef /*constructor*/,
231 JSValueRef possibleInstance,
232 JSValueRef* /*exception*/)
234 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
236 } // CommonsJavaScript