wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Common / JSTizenException.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include "JSTizenException.h"
19 #include <CommonsJavaScript/Converter.h>
20 #include "Logger.h"
21
22 namespace DeviceAPI {
23 namespace Common {
24
25                 const std::string JSTizenException::UNKNOWN_ERROR           = "UnknownError";
26                 const std::string JSTizenException::TYPE_MISMATCH_ERROR     = "TypeMismatchError";
27                 const std::string JSTizenException::INVALID_VALUES_ERROR    = "InvalidValuesError";
28                 const std::string JSTizenException::TIMEOUT_ERROR           = "TimeoutError";
29                 const std::string JSTizenException::IO_ERROR                = "IOError";
30                 const std::string JSTizenException::NOT_SUPPORTED_ERROR     = "NotSupportedError";
31                 const std::string JSTizenException::PERMISSION_DENIED_ERROR = "SecurityError";
32                 const std::string JSTizenException::NOT_FOUND_ERROR         = "NotFoundError";
33                 const std::string JSTizenException::SERVICE_NOT_AVAILABLE   = "ServiceNotAvailableError";
34                 const std::string JSTizenException::NETWORK_ERROR           = "NetworkError";
35                 const std::string JSTizenException::INVALID_ACCESS_ERROR    = "InvalidAccessError";
36
37                 namespace{
38                         #define PLUGIN_NAME       "TizenException"
39                         #define PROPERTY_NAME     "name"
40                         #define PROPERTY_MESSAGE  "message"
41
42                         const std::string PROPERTY_ERROR[] = {
43                                         DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR,
44                                         DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR,
45                                         DeviceAPI::Common::JSTizenException::INVALID_VALUES_ERROR,
46                                         DeviceAPI::Common::JSTizenException::TIMEOUT_ERROR,
47                                         DeviceAPI::Common::JSTizenException::IO_ERROR,
48                                         DeviceAPI::Common::JSTizenException::NOT_SUPPORTED_ERROR,
49                                         DeviceAPI::Common::JSTizenException::PERMISSION_DENIED_ERROR,
50                                         DeviceAPI::Common::JSTizenException::NOT_FOUND_ERROR,
51                                         DeviceAPI::Common::JSTizenException::NETWORK_ERROR,
52                                         DeviceAPI::Common::JSTizenException::INVALID_ACCESS_ERROR,
53                         };
54                 }
55
56                 JSClassRef JSTizenException::m_classRef = NULL;
57
58                 JSClassDefinition JSTizenException::m_classInfo = {
59                         0,
60                         kJSClassAttributeNone,
61                         PLUGIN_NAME,
62                         0,
63                         m_properties,
64                         NULL, //__function,
65                         initialize,
66                         finalize,
67                         hasProperty,
68                         NULL,
69                         NULL, //SetProperty,
70                         NULL, //DeleteProperty,
71                         NULL,
72                         NULL, //CallAsFunction,
73                         NULL, //CallAsConstructor,
74                         hasInstance,
75                         NULL, //ConvertToType,
76                 };
77
78                 JSStaticValue JSTizenException::m_properties[] = {
79                         { PROPERTY_NAME,    getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
80                         { PROPERTY_MESSAGE, getStaticProperty, NULL, kJSPropertyAttributeReadOnly },
81                         { 0, 0, 0, 0 }
82                 };
83
84                 const JSClassDefinition* JSTizenException::getClassInfo() {
85                         return &m_classInfo;
86                 }
87
88                 JSClassRef JSTizenException::getClassRef() {
89                         if (!m_classRef) {
90                                 m_classRef = JSClassCreate(&m_classInfo);
91                         }
92                         return m_classRef;
93                 }
94
95                 void JSTizenException::initialize(JSContextRef /*context*/, JSObjectRef /*object*/) {
96                 }
97
98                 void JSTizenException::finalize(JSObjectRef object) {
99                         PrivateObject* privateObject = static_cast<PrivateObject*> (JSObjectGetPrivate(object));
100                         if (privateObject) {
101                                 JSObjectSetPrivate(object, NULL);
102                                 delete privateObject;
103                         }
104                 }
105
106                 bool JSTizenException::hasProperty(JSContextRef /*context*/, JSObjectRef /*object*/, JSStringRef propertyName) {
107                         const size_t size = sizeof(PROPERTY_ERROR) / sizeof(PROPERTY_ERROR[0]);
108                         for (size_t i = 0; i < size; ++i) {
109                                 if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_ERROR[i].c_str())) {
110                                         return true;
111                                 }
112                         }
113                         return false;
114                 }
115
116                 JSValueRef JSTizenException::getStaticProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* /*exception*/) {
117                         PrivateObject* privateObject = static_cast<PrivateObject*> (JSObjectGetPrivate(object));
118                         if (!privateObject) {
119                                 LoggerE("Private object is not set.");
120                                 return JSValueMakeUndefined(context);
121                         }
122
123 //                      WrtDeviceApis::CommonsJavaScript::Converter converter(context);
124 //                      try {
125 //                              if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NAME)) {
126 //                                      return converter.toJSValueRef(privateObject->getObject()->getName());
127 //                              } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_MESSAGE)) {
128 //                                      return converter.toJSValueRef(privateObject->getObject()->getMessage());
129 //                              }
130 //                      } catch (const WrtDeviceApis::Commons::ConversionException& ex) {
131 //                              LoggerE("Exception: " << ex.GetMessage());
132 //                      }
133
134                         return JSValueMakeUndefined(context);
135                 }
136
137                 bool JSTizenException::hasInstance(JSContextRef context, JSObjectRef /*constructor*/, JSValueRef possibleInstance, JSValueRef* /*exception*/) {
138                         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
139                 }
140                 
141 } // Common
142 }// DeviceAPI