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 {
96 JSClassRef JSDOMException::m_classRef = NULL;
98 JSClassDefinition JSDOMException::m_classInfo = {
100 kJSClassAttributeNone,
110 NULL, //DeleteProperty,
112 NULL, //CallAsFunction,
113 NULL, //CallAsConstructor,
115 NULL, //ConvertToType,
118 JSStaticValue JSDOMException::m_properties[] = {
119 { PROPERTY_CODE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
120 { PROPERTY_MESSAGE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
124 const JSClassDefinition* JSDOMException::getClassInfo()
129 JSClassRef JSDOMException::getClassRef()
132 m_classRef = JSClassCreate(&m_classInfo);
137 void JSDOMException::initialize(JSContextRef /*context*/,
138 JSObjectRef /*object*/)
142 void JSDOMException::finalize(JSObjectRef object)
144 PrivateObject* privateObject =
145 static_cast<PrivateObject*>(JSObjectGetPrivate(object));
147 JSObjectSetPrivate(object, NULL);
148 delete privateObject;
152 bool JSDOMException::hasProperty(JSContextRef /*context*/,
153 JSObjectRef /*object*/,
154 JSStringRef propertyName)
156 const size_t size = sizeof(PROPERTY_ERROR) / sizeof(PROPERTY_ERROR[0]);
157 for (size_t i = 0; i < size; ++i) {
158 if (JSStringIsEqualToUTF8CString(propertyName,
159 PROPERTY_ERROR[i].name)) {
166 JSValueRef JSDOMException::getStaticProperty(JSContextRef context,
168 JSStringRef propertyName,
169 JSValueRef* /*exception*/)
171 PrivateObject* privateObject =
172 static_cast<PrivateObject*>(JSObjectGetPrivate(object));
173 if (!privateObject) {
174 LogError("Private object is not set.");
175 return JSValueMakeUndefined(context);
178 Converter converter(context);
180 if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_CODE)) {
181 return converter.toJSValueRef(privateObject->getObject()->getCode());
182 } else if (JSStringIsEqualToUTF8CString(propertyName,
184 return converter.toJSValueRef(
185 privateObject->getObject()->getMessage());
188 catch (const Commons::ConversionException& ex) {
189 LogError("Exception: " << ex.GetMessage());
192 return JSValueMakeUndefined(context);
195 JSValueRef JSDOMException::getProperty(JSContextRef context,
196 JSObjectRef /*object*/,
197 JSStringRef propertyName,
198 JSValueRef* /*exception*/)
200 Converter converter(context);
202 std::string prop = converter.toString(propertyName);
204 const size_t size = sizeof(PROPERTY_ERROR) / sizeof(PROPERTY_ERROR[0]);
205 for (size_t i = 0; i < size; ++i) {
206 if (prop == PROPERTY_ERROR[i].name) {
207 return converter.toJSValueRef(PROPERTY_ERROR[i].value);
211 catch (const Commons::ConversionException& ex) {
212 LogError("Exception: " << ex.GetMessage());
215 return JSValueMakeUndefined(context);
218 void JSDOMException::getPropertyNames(
219 JSContextRef /*context*/,
220 JSObjectRef /*object*/,
221 JSPropertyNameAccumulatorRef accumulator)
223 const size_t size = sizeof(PROPERTY_ERROR) / sizeof(PROPERTY_ERROR[0]);
224 for (size_t i = 0; i < size; ++i) {
225 JSPropertyNameAccumulatorAddName(accumulator,
226 JSStringCreateWithUTF8CString(
227 PROPERTY_ERROR[i].name));
231 bool JSDOMException::hasInstance(JSContextRef context,
232 JSObjectRef /*constructor*/,
233 JSValueRef possibleInstance,
234 JSValueRef* /*exception*/)
236 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
239 } // CommonsJavaScript