Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / 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 JSClassRef JSDOMException::m_classRef = NULL;
96
97 JSClassDefinition JSDOMException::m_classInfo = {
98     0,
99     kJSClassAttributeNone,
100     PLUGIN_NAME,
101     0,
102     m_properties,
103     NULL, //__function,
104     initialize,
105     finalize,
106     hasProperty,
107     getProperty,
108     NULL, //SetProperty,
109     NULL, //DeleteProperty,
110     getPropertyNames,
111     NULL, //CallAsFunction,
112     NULL, //CallAsConstructor,
113     hasInstance,
114     NULL, //ConvertToType,
115 };
116
117 JSStaticValue JSDOMException::m_properties[] = {
118     { PROPERTY_CODE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
119     { PROPERTY_MESSAGE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
120     { 0, 0, 0, 0 }
121 };
122
123 const JSClassDefinition* JSDOMException::getClassInfo()
124 {
125     return &m_classInfo;
126 }
127
128 JSClassRef JSDOMException::getClassRef()
129 {
130     if (!m_classRef) {
131         m_classRef = JSClassCreate(&m_classInfo);
132     }
133     return m_classRef;
134 }
135
136 void JSDOMException::initialize(JSContextRef /*context*/,
137                                 JSObjectRef /*object*/)
138 {}
139
140 void JSDOMException::finalize(JSObjectRef object)
141 {
142     PrivateObject* privateObject =
143         static_cast<PrivateObject*>(JSObjectGetPrivate(object));
144     if (privateObject) {
145         JSObjectSetPrivate(object, NULL);
146         delete privateObject;
147     }
148 }
149
150 bool JSDOMException::hasProperty(JSContextRef /*context*/,
151                                  JSObjectRef /*object*/,
152                                  JSStringRef propertyName)
153 {
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))
158         {
159             return true;
160         }
161     }
162     return false;
163 }
164
165 JSValueRef JSDOMException::getStaticProperty(JSContextRef context,
166                                              JSObjectRef object,
167                                              JSStringRef propertyName,
168                                              JSValueRef* /*exception*/)
169 {
170     PrivateObject* privateObject =
171         static_cast<PrivateObject*>(JSObjectGetPrivate(object));
172     if (!privateObject) {
173         LogError("Private object is not set.");
174         return JSValueMakeUndefined(context);
175     }
176
177     Converter converter(context);
178     try {
179         if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_CODE)) {
180             return converter.toJSValueRef(privateObject->getObject()->getCode());
181         } else if (JSStringIsEqualToUTF8CString(propertyName,
182                                                 PROPERTY_MESSAGE))
183         {
184             return converter.toJSValueRef(
185                        privateObject->getObject()->getMessage());
186         }
187     } catch (const Commons::ConversionException& ex) {
188         LogError("Exception: " << ex.GetMessage());
189     }
190
191     return JSValueMakeUndefined(context);
192 }
193
194 JSValueRef JSDOMException::getProperty(JSContextRef context,
195                                        JSObjectRef /*object*/,
196                                        JSStringRef propertyName,
197                                        JSValueRef* /*exception*/)
198 {
199     Converter converter(context);
200     try {
201         std::string prop = converter.toString(propertyName);
202
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);
207             }
208         }
209     } catch (const Commons::ConversionException& ex) {
210         LogError("Exception: " << ex.GetMessage());
211     }
212
213     return JSValueMakeUndefined(context);
214 }
215
216 void JSDOMException::getPropertyNames(
217     JSContextRef /*context*/,
218     JSObjectRef /*object*/,
219     JSPropertyNameAccumulatorRef accumulator)
220 {
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));
226     }
227 }
228
229 bool JSDOMException::hasInstance(JSContextRef context,
230                                  JSObjectRef /*constructor*/,
231                                  JSValueRef possibleInstance,
232                                  JSValueRef* /*exception*/)
233 {
234     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
235 }
236 } // CommonsJavaScript
237 } // WrtDeviceApis
238