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