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.
18 #include "JSWebAPIException.h"
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSUtils.h>
29 #define PLUGIN_NAME "WebAPIException"
30 #define PROPERTY_CODE "code"
31 #define PROPERTY_NAME "name"
32 #define PROPERTY_TYPE "type"
33 #define PROPERTY_MESSAGE "message"
35 #define PROPERTY_UNKNOWN_ERR "UNKNOWN_ERR"
36 #define PROPERTY_INDEX_SIZE_ERR "INDEX_SIZE_ERR"
37 #define PROPERTY_DOMSTRING_SIZE_ERR "DOMSTRING_SIZE_ERR"
38 #define PROPERTY_HIERARCHY_REQUEST_ERR "HIERARCHY_REQUEST_ERR"
39 #define PROPERTY_WRONG_DOCUMENT_ERR "WRONG_DOCUMENT_ERR"
40 #define PROPERTY_INVALID_CHARACTER_ERR "INVALID_CHARACTER_ERR"
41 #define PROPERTY_NO_DATA_ALLOWED_ERR "NO_DATA_ALLOWED_ERR"
42 #define PROPERTY_NO_MODIFICATION_ALLOWED_ERR "NO_MODIFICATION_ALLOWED_ERR"
43 #define PROPERTY_NOT_FOUND_ERR "NOT_FOUND_ERR"
44 #define PROPERTY_NOT_SUPPORTED_ERR "NOT_SUPPORTED_ERR"
45 #define PROPERTY_INUSE_ATTRIBUTE_ERR "INUSE_ATTRIBUTE_ERR"
46 #define PROPERTY_INVALID_STATE_ERR "INVALID_STATE_ERR"
47 #define PROPERTY_SYNTAX_ERR "SYNTAX_ERR"
48 #define PROPERTY_INVALID_MODIFICATION_ERR "INVALID_MODIFICATION_ERR"
49 #define PROPERTY_NAMESPACE_ERR "NAMESPACE_ERR"
50 #define PROPERTY_INVALID_ACCESS_ERR "INVALID_ACCESS_ERR"
51 #define PROPERTY_VALIDATION_ERR "VALIDATION_ERR"
52 #define PROPERTY_TYPE_MISMATCH_ERR "TYPE_MISMATCH_ERR"
53 #define PROPERTY_SECURITY_ERR "SECURITY_ERR"
54 #define PROPERTY_NETWORK_ERR "NETWORK_ERR"
55 #define PROPERTY_ABORT_ERR "ABORT_ERR"
56 #define PROPERTY_URL_MISMATCH_ERR "URL_MISMATCH_ERR"
57 #define PROPERTY_QUOTA_EXCEEDED_ERR "QUOTA_EXCEEDED_ERR"
58 #define PROPERTY_TIMEOUT_ERR "TIMEOUT_ERR"
59 #define PROPERTY_INVALID_NODE_TYPE_ERR "INVALID_NODE_TYPE_ERR"
60 #define PROPERTY_DATA_CLONE_ERR "DATA_CLONE_ERR"
61 #define PROPERTY_INVALID_VALUES_ERR "INVALID_VALUES_ERR"
62 #define PROPERTY_IO_ERR "IO_ERR"
63 #define PROPERTY_OUT_OF_MEMORY_ERR "OUT_OF_MEMORY_ERR"
64 #define PROPERTY_NOT_CONNECTED_SERVICE_ERR "NOT_CONNECTED_SERVICE_ERR"
65 #define PROPERTY_FAIL_TO_REQUEST_ERR "FAIL_TO_REQUEST_ERR"
66 #define PROPERTY_INVALID_RESPONSE_ERR "INVALID_RESPONSE_ERR"
67 #define PROPERTY_NO_AVAILABLE_NETWORK_ERR "NO_AVAILABLE_NETWORK_ERR"
68 #define PROPERTY_INVALID_DEVICE_ERR "INVALID_DEVICE_ERR"
69 #define PROPERTY_NOT_SUPPORTED_FUNCTION_ERR "NOT_SUPPORTED_FUNCTION_ERR"
70 #define PROPERTY_NO_PERMISSION_ERR "NO_PERMISSION_ERR"
71 #define PROPERTY_INVALID_ITEM_ERR "INVALID_ITEM_ERR"
72 #define PROPERTY_FAIL_TO_PLAY_ERR "FAIL_TO_PLAY_ERR"
73 #define PROPERTY_SERVICE_NOT_AVAILABLE "SERVICE_NOT_AVAILABLE"
74 #define PROPERTY_AVPLAY_NETWORK_DISCONNECTED_ERR "AVPLAY_NETWORK_DISCONNECTED_ERR"
75 #define PROPERTY_AVPLAY_UNSUPPORTED_VIDEO_FORMAT_ERR "AVPLAY_UNSUPPORTED_VIDEO_FORMAT_ERR"
76 #define PROPERTY_AVPLAY_UNSUPPORTED_AUDIO_FORMAT_ERR "AVPLAY_UNSUPPORTED_AUDIO_FORMAT_ERR"
77 #define PROPERTY_AVPLAY_UNSUPPORTED_VIDEO_RESOLUTION_ERR "AVPLAY_UNSUPPORTED_VIDEO_RESOLUTION_ERR"
78 #define PROPERTY_AVPLAY_UNSUPPORTED_VIDEO_FRAMERATE_ERR "AVPLAY_UNSUPPORTED_VIDEO_FRAMERATE_ERR"
79 #define PROPERTY_AVPLAY_CURRUPTED_STREAM_ERR "AVPLAY_CURRUPTED_STREAM_ERR"
82 JSClassRef JSWebAPIException::m_classRef = NULL;
84 JSClassDefinition JSWebAPIException::m_classInfo = {
86 kJSClassAttributeNone,
90 m_function, //__function,
96 NULL, //DeleteProperty,
98 NULL, //CallAsFunction,
99 NULL, //CallAsConstructor,
101 NULL, //ConvertToType,
104 JSStaticFunction JSWebAPIException::m_function[] = {
105 { "toString", toString, kJSPropertyAttributeNone },
110 JSStaticValue JSWebAPIException::m_properties[] = {
111 { PROPERTY_CODE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
112 { PROPERTY_NAME, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
113 { PROPERTY_TYPE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
114 { PROPERTY_MESSAGE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
116 { PROPERTY_UNKNOWN_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
117 { PROPERTY_INDEX_SIZE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
118 { PROPERTY_DOMSTRING_SIZE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
119 { PROPERTY_HIERARCHY_REQUEST_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
120 { PROPERTY_WRONG_DOCUMENT_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
121 { PROPERTY_INVALID_CHARACTER_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
122 { PROPERTY_NO_DATA_ALLOWED_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
123 { PROPERTY_NO_MODIFICATION_ALLOWED_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
124 { PROPERTY_NOT_FOUND_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
125 { PROPERTY_NOT_SUPPORTED_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
126 { PROPERTY_INUSE_ATTRIBUTE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
127 { PROPERTY_INVALID_STATE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
128 { PROPERTY_SYNTAX_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
129 { PROPERTY_INVALID_MODIFICATION_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
130 { PROPERTY_NAMESPACE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
131 { PROPERTY_INVALID_ACCESS_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
132 { PROPERTY_VALIDATION_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
133 { PROPERTY_TYPE_MISMATCH_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
134 { PROPERTY_SECURITY_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
135 { PROPERTY_NETWORK_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
136 { PROPERTY_ABORT_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
137 { PROPERTY_URL_MISMATCH_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
138 { PROPERTY_QUOTA_EXCEEDED_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
139 { PROPERTY_TIMEOUT_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
140 { PROPERTY_INVALID_NODE_TYPE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
141 { PROPERTY_DATA_CLONE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
142 { PROPERTY_INVALID_VALUES_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
143 { PROPERTY_IO_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
144 { PROPERTY_OUT_OF_MEMORY_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
145 { PROPERTY_NOT_CONNECTED_SERVICE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
146 { PROPERTY_FAIL_TO_REQUEST_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
147 { PROPERTY_INVALID_RESPONSE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
148 { PROPERTY_NO_AVAILABLE_NETWORK_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
149 { PROPERTY_INVALID_DEVICE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
150 { PROPERTY_NOT_SUPPORTED_FUNCTION_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
151 { PROPERTY_NO_PERMISSION_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
152 { PROPERTY_INVALID_ITEM_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
153 { PROPERTY_FAIL_TO_PLAY_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
154 { PROPERTY_SERVICE_NOT_AVAILABLE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
155 { PROPERTY_AVPLAY_NETWORK_DISCONNECTED_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
156 { PROPERTY_AVPLAY_UNSUPPORTED_VIDEO_FORMAT_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
157 { PROPERTY_AVPLAY_UNSUPPORTED_AUDIO_FORMAT_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
158 { PROPERTY_AVPLAY_UNSUPPORTED_VIDEO_RESOLUTION_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
159 { PROPERTY_AVPLAY_UNSUPPORTED_VIDEO_FRAMERATE_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
160 { PROPERTY_AVPLAY_CURRUPTED_STREAM_ERR, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
164 const JSClassDefinition* JSWebAPIException::getClassInfo() {
168 JSClassRef JSWebAPIException::getClassRef() {
170 m_classRef = JSClassCreate(&m_classInfo);
175 void JSWebAPIException::initialize(JSContextRef /*context*/, JSObjectRef /*object*/) {
178 void JSWebAPIException::finalize(JSObjectRef object) {
179 PrivateObject* privateObject = static_cast<PrivateObject*> (JSObjectGetPrivate(object));
181 JSObjectSetPrivate(object, NULL);
182 delete privateObject;
186 JSValueRef JSWebAPIException::getStaticProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* /*exception*/) {
187 PrivateObject* privateObject = static_cast<PrivateObject*> (JSObjectGetPrivate(object));
188 if (!privateObject) {
189 LoggerE("Private object is not set.");
190 return JSValueMakeUndefined(context);
193 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
195 if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_CODE)) {
196 LoggerD("propertyName PROPERTY_CODE");
197 return converter.toJSValueRef(privateObject->getObject()->getCode());
198 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NAME)) {
199 return converter.toJSValueRef(privateObject->getObject()->getName());
200 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_TYPE)) {
201 return converter.toJSValueRef(privateObject->getObject()->getName());
202 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_MESSAGE)) {
203 return converter.toJSValueRef(privateObject->getObject()->getMessage());
204 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_UNKNOWN_ERR)) {
205 return converter.toJSValueRef(UNKNOWN_ERR);
206 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_INDEX_SIZE_ERR)) {
207 return converter.toJSValueRef(INDEX_SIZE_ERR);
208 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_DOMSTRING_SIZE_ERR)) {
209 return converter.toJSValueRef(DOMSTRING_SIZE_ERR);
210 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_HIERARCHY_REQUEST_ERR)) {
211 return converter.toJSValueRef(HIERARCHY_REQUEST_ERR);
212 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_WRONG_DOCUMENT_ERR)) {
213 return converter.toJSValueRef(WRONG_DOCUMENT_ERR);
214 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_INVALID_CHARACTER_ERR)) {
215 return converter.toJSValueRef(INVALID_CHARACTER_ERR);
216 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NO_DATA_ALLOWED_ERR)) {
217 return converter.toJSValueRef(NO_DATA_ALLOWED_ERR);
218 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NO_MODIFICATION_ALLOWED_ERR)) {
219 return converter.toJSValueRef(NO_MODIFICATION_ALLOWED_ERR);
220 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NOT_FOUND_ERR)) {
221 return converter.toJSValueRef(NOT_FOUND_ERR);
222 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NOT_SUPPORTED_ERR)) {
223 return converter.toJSValueRef(NOT_SUPPORTED_ERR);
224 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_INUSE_ATTRIBUTE_ERR)) {
225 return converter.toJSValueRef(INUSE_ATTRIBUTE_ERR);
226 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_INVALID_STATE_ERR)) {
227 return converter.toJSValueRef(INVALID_STATE_ERR);
228 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_SYNTAX_ERR)) {
229 return converter.toJSValueRef(SYNTAX_ERR);
230 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_INVALID_MODIFICATION_ERR)) {
231 return converter.toJSValueRef(INVALID_MODIFICATION_ERR);
232 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NAMESPACE_ERR)) {
233 return converter.toJSValueRef(NAMESPACE_ERR);
234 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_INVALID_ACCESS_ERR)) {
235 return converter.toJSValueRef(INVALID_ACCESS_ERR);
236 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_VALIDATION_ERR)) {
237 return converter.toJSValueRef(VALIDATION_ERR);
238 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_TYPE_MISMATCH_ERR)) {
239 return converter.toJSValueRef(TYPE_MISMATCH_ERR);
240 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_SECURITY_ERR)) {
241 return converter.toJSValueRef(SECURITY_ERR);
242 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NETWORK_ERR)) {
243 return converter.toJSValueRef(NETWORK_ERR);
244 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_ABORT_ERR)) {
245 return converter.toJSValueRef(ABORT_ERR);
246 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_URL_MISMATCH_ERR)) {
247 return converter.toJSValueRef(URL_MISMATCH_ERR);
248 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_QUOTA_EXCEEDED_ERR)) {
249 return converter.toJSValueRef(QUOTA_EXCEEDED_ERR);
250 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_TIMEOUT_ERR)) {
251 return converter.toJSValueRef(TIMEOUT_ERR);
252 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_INVALID_NODE_TYPE_ERR)) {
253 return converter.toJSValueRef(INVALID_NODE_TYPE_ERR);
254 }else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_DATA_CLONE_ERR)) {
255 return converter.toJSValueRef(DATA_CLONE_ERR);
257 LoggerD("propertyName not supported");
258 return converter.toJSValueRef(UNKNOWN_ERR);
260 } catch (const WrtDeviceApis::Commons::ConversionException& ex) {
261 LoggerE("Exception: " << ex.GetMessage());
264 return JSValueMakeUndefined(context);
267 JSValueRef JSWebAPIException::toString(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
268 PrivateObject* privateObject = static_cast<PrivateObject*> (JSObjectGetPrivate(thisObject));
269 if (!privateObject) {
273 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
274 std::string name = privateObject->getObject()->getName();
275 std::string message = privateObject->getObject()->getMessage();
276 std::string result = name + ": "+message;
277 return converter.toJSValueRef(result);
280 bool JSWebAPIException::hasInstance(JSContextRef context, JSObjectRef /*constructor*/, JSValueRef possibleInstance, JSValueRef* /*exception*/) {
281 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
284 int JSWebAPIException::convertToWebAPIExceptionCode(const std::string& errorName){
285 LoggerD("<<< errorName:[" << errorName << "]");
287 int retErrorCode = -1;
289 if(errorName.compare(JSTizenException::UNKNOWN_ERROR) == 0){
290 retErrorCode = UNKNOWN_ERR;
291 }else if(errorName.compare(JSTizenException::NOT_FOUND_ERROR) == 0){
292 retErrorCode = NOT_FOUND_ERR;
293 }else if(errorName.compare(JSTizenException::NOT_SUPPORTED_ERROR) == 0){
294 retErrorCode = NOT_SUPPORTED_ERR;
295 }else if(errorName.compare(JSTizenException::TYPE_MISMATCH_ERROR) == 0){
296 retErrorCode = TYPE_MISMATCH_ERR;
297 }else if(errorName.compare(JSTizenException::PERMISSION_DENIED_ERROR) == 0){
298 retErrorCode = SECURITY_ERR;
299 }else if(errorName.compare(JSTizenException::TIMEOUT_ERROR) == 0){
300 retErrorCode = TIMEOUT_ERR;
301 }else if(errorName.compare(JSTizenException::INVALID_VALUES_ERROR) == 0){
303 }else if(errorName.compare(JSTizenException::IO_ERROR) == 0){
305 }else if(errorName.compare(JSTizenException::SERVICE_NOT_AVAILABLE) == 0){
307 }else if(errorName.compare(JSTizenException::NETWORK_ERROR) == 0){
308 retErrorCode = NETWORK_ERR;
310 retErrorCode = UNKNOWN_ERR;
313 LoggerD(">>> retERrorCode:" << retErrorCode );
317 JSObjectRef JSWebAPIException::makeJSWebAPIException(JSContextRef context, const BasePlatformException& error){
318 JSWebAPIException::PrivateObject::ObjectType data(new TizenExceptionData(error.getCode(), error.getName(), error.getMessage()));
319 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(context, JSWebAPIException::getClassRef(), data);
321 JSValueRef JSWebAPIException::throwException(JSContextRef context, JSValueRef* exception, const BasePlatformException& error){
322 Assert(exception && "Exception object can't be NULL.");
324 *exception = makeJSWebAPIException(context, error);
325 return JSValueMakeUndefined(context);