tizen beta release
[platform/framework/web/wrt-plugins-common.git] / src / CommonsJavaScript / JSDOMException.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include "JSDOMException.h"
17
18 #include <dpl/log/log.h>
19
20 #include "Converter.h"
21
22 namespace {
23 const char* PLUGIN_NAME = "DOMException";
24 const char* PROPERTY_CODE = "code";
25 const char* PROPERTY_MESSAGE = "message";
26
27 struct Error
28 {
29     const char* name;
30     const unsigned short value;
31 };
32 // This array CAN'T be left empty!
33 const Error PROPERTY_ERROR[] = {
34     { "UNKNOWN_ERR",
35       WrtDeviceApis::CommonsJavaScript::JSDOMException::UNKNOWN_ERR },
36     { "INDEX_SIZE_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 },
54     { "NOT_FOUND_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 },
63     { "SYNTAX_ERR",
64       WrtDeviceApis::CommonsJavaScript::JSDOMException::SYNTAX_ERR },
65     { "INVALID_MODIFICATION_ERR",
66       WrtDeviceApis::CommonsJavaScript::JSDOMException::
67           INVALID_MODIFICATION_ERR },
68     { "NAMESPACE_ERR",
69       WrtDeviceApis::CommonsJavaScript::JSDOMException::NAMESPACE_ERR },
70     { "INVALID_ACCESS_ERR",
71       WrtDeviceApis::CommonsJavaScript::JSDOMException::INVALID_ACCESS_ERR },
72     { "VALIDATION_ERR",
73       WrtDeviceApis::CommonsJavaScript::JSDOMException::VALIDATION_ERR },
74     { "TYPE_MISMATCH_ERR",
75       WrtDeviceApis::CommonsJavaScript::JSDOMException::TYPE_MISMATCH_ERR },
76     { "SECURITY_ERR",
77       WrtDeviceApis::CommonsJavaScript::JSDOMException::SECURITY_ERR },
78     { "NETWORK_ERR",
79       WrtDeviceApis::CommonsJavaScript::JSDOMException::NETWORK_ERR },
80     { "ABORT_ERR", 
81       WrtDeviceApis::CommonsJavaScript::JSDOMException::ABORT_ERR },
82     { "TIMEOUT_ERR",
83       WrtDeviceApis::CommonsJavaScript::JSDOMException::TIMEOUT_ERR },
84     { "INVALID_VALUES_ERR",
85       WrtDeviceApis::CommonsJavaScript::JSDOMException::INVALID_VALUES_ERR },
86     { "IO_ERR", 
87        WrtDeviceApis::CommonsJavaScript::JSDOMException::IO_ERR },
88     { "QUOTA_EXCEEDED_ERR",
89       WrtDeviceApis::CommonsJavaScript::JSDOMException::QUOTA_EXCEEDED_ERR }
90 };
91 } // namespace
92
93 namespace WrtDeviceApis {
94 namespace CommonsJavaScript {
95
96 JSClassRef JSDOMException::m_classRef = NULL;
97
98 JSClassDefinition JSDOMException::m_classInfo = {
99     0,
100     kJSClassAttributeNone,
101     PLUGIN_NAME,
102     0,
103     m_properties,
104     NULL, //__function,
105     initialize,
106     finalize,
107     hasProperty,
108     getProperty,
109     NULL, //SetProperty,
110     NULL, //DeleteProperty,
111     getPropertyNames,
112     NULL, //CallAsFunction,
113     NULL, //CallAsConstructor,
114     hasInstance,
115     NULL, //ConvertToType,
116 };
117
118 JSStaticValue JSDOMException::m_properties[] = {
119     { PROPERTY_CODE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
120     { PROPERTY_MESSAGE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
121     { 0, 0, 0, 0 }
122 };
123
124 const JSClassDefinition* JSDOMException::getClassInfo()
125 {
126     return &m_classInfo;
127 }
128
129 JSClassRef JSDOMException::getClassRef()
130 {
131     if (!m_classRef) {
132         m_classRef = JSClassCreate(&m_classInfo);
133     }
134     return m_classRef;
135 }
136
137 void JSDOMException::initialize(JSContextRef /*context*/,
138                                 JSObjectRef /*object*/)
139 {
140 }
141
142 void JSDOMException::finalize(JSObjectRef object)
143 {
144     PrivateObject* privateObject =
145         static_cast<PrivateObject*>(JSObjectGetPrivate(object));
146     if (privateObject) {
147         JSObjectSetPrivate(object, NULL);
148         delete privateObject;
149     }
150 }
151
152 bool JSDOMException::hasProperty(JSContextRef /*context*/,
153                                  JSObjectRef /*object*/,
154                                  JSStringRef propertyName)
155 {
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)) {
160             return true;
161         }
162     }
163     return false;
164 }
165
166 JSValueRef JSDOMException::getStaticProperty(JSContextRef context,
167                                              JSObjectRef object,
168                                              JSStringRef propertyName,
169                                              JSValueRef* /*exception*/)
170 {
171     PrivateObject* privateObject =
172         static_cast<PrivateObject*>(JSObjectGetPrivate(object));
173     if (!privateObject) {
174         LogError("Private object is not set.");
175         return JSValueMakeUndefined(context);
176     }
177
178     Converter converter(context);
179     try {
180         if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_CODE)) {
181             return converter.toJSValueRef(privateObject->getObject()->getCode());
182         } else if (JSStringIsEqualToUTF8CString(propertyName,
183                                                 PROPERTY_MESSAGE)) {
184             return converter.toJSValueRef(
185                        privateObject->getObject()->getMessage());
186         }
187     }
188     catch (const Commons::ConversionException& ex) {
189         LogError("Exception: " << ex.GetMessage());
190     }
191
192     return JSValueMakeUndefined(context);
193 }
194
195 JSValueRef JSDOMException::getProperty(JSContextRef context,
196                                        JSObjectRef /*object*/,
197                                        JSStringRef propertyName,
198                                        JSValueRef* /*exception*/)
199 {
200     Converter converter(context);
201     try {
202         std::string prop = converter.toString(propertyName);
203
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);
208             }
209         }
210     }
211     catch (const Commons::ConversionException& ex) {
212         LogError("Exception: " << ex.GetMessage());
213     }
214
215     return JSValueMakeUndefined(context);
216 }
217
218 void JSDOMException::getPropertyNames(
219         JSContextRef /*context*/,
220         JSObjectRef /*object*/,
221         JSPropertyNameAccumulatorRef accumulator)
222 {
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));
228     }
229 }
230
231 bool JSDOMException::hasInstance(JSContextRef context,
232                                  JSObjectRef /*constructor*/,
233                                  JSValueRef possibleInstance,
234                                  JSValueRef* /*exception*/)
235 {
236     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
237 }
238
239 } // CommonsJavaScript
240 } // WrtDeviceApis
241