Update change log and spec for wrt-plugins-tizen_0.4.29
[framework/web/wrt-plugins-tizen.git] / src / Common / JSWebAPIException.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 "JSWebAPIException.h"
19 #include <string>
20 #include <map>
21 #include "JSUtil.h"
22 #include "JSWebAPIError.h"
23 #include "Logger.h"
24
25 namespace DeviceAPI {
26 namespace Common {
27
28 #define CLASS_NAME  "WebAPIException"
29
30 JSClassRef JSWebAPIException::m_classRef = NULL;
31
32 JSClassDefinition JSWebAPIException::m_classInfo =
33 {
34         0,
35         kJSClassAttributeNone,
36         CLASS_NAME,
37         JSWebAPIError::getClassRef(),
38         m_properties,
39         m_function,
40         initialize,
41         finalize,
42         NULL, // hasProperty,
43         NULL, // getProperty,
44         NULL, // setProperty,
45         NULL, // deleteProperty,
46         NULL, // getPropertyNames,
47         NULL, // callAsFunction,
48         NULL, // callAsConstructor,
49         hasInstance,
50         NULL, // convertToType,
51 };
52
53 JSStaticFunction JSWebAPIException::m_function[] =
54 {
55         { "toString", toString, kJSPropertyAttributeNone },
56         { 0, 0, 0 }
57 };
58
59 JSStaticValue JSWebAPIException::m_properties[] = {
60         { 0, 0, 0, 0 }
61 };
62
63 const JSClassDefinition* JSWebAPIException::getClassInfo()
64 {
65         return &m_classInfo;
66 }
67
68 JSClassRef JSWebAPIException::getClassRef()
69 {
70         if (!m_classRef)
71         {
72                 m_classRef = JSClassCreate(&m_classInfo);
73         }
74         return m_classRef;
75 }
76
77 bool JSWebAPIException::isObjectOfClass(JSContextRef context, JSValueRef value)
78 {
79         return JSValueIsObjectOfClass(context, value, getClassRef());
80 }
81
82 JSObjectRef JSWebAPIException::createJSObject(JSContextRef context, WebAPIError* webapiError)
83 {
84         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(webapiError));
85         if (NULL == jsObjectRef) {
86                 LoggerE("object creation error");
87                 return NULL;
88         }
89         return jsObjectRef;
90 }
91
92 WebAPIError* JSWebAPIException::getPriv(JSContextRef context, JSObjectRef object)
93 {
94         if(!JSValueIsObjectOfClass(context, object, getClassRef()))
95                 return NULL;
96
97         return static_cast<WebAPIError*>(JSObjectGetPrivate(object));
98 }
99
100 void JSWebAPIException::initialize(JSContextRef /*context*/, JSObjectRef /*object*/)
101 {
102 }
103
104 void JSWebAPIException::finalize(JSObjectRef /*object*/)
105 {
106 }
107
108 JSValueRef JSWebAPIException::toString(JSContextRef context,
109                 JSObjectRef object,
110                 JSObjectRef thisObject,
111                 size_t argumentCount,
112                 const JSValueRef arguments[],
113                 JSValueRef* exception)
114 {
115         WebAPIError* webapiErrObj = getPriv(context, thisObject);
116         if(!webapiErrObj)
117         {
118                 LoggerE("Private object is not set.");
119                 return NULL;
120         }
121
122         std::string name = webapiErrObj->getName();
123         std::string message = webapiErrObj->getMessage();
124
125         std::string result = name + ": " + message;
126
127         return JSUtil::toJSValueRef(context, result);
128 }
129
130 bool JSWebAPIException::hasInstance(JSContextRef context,
131                 JSObjectRef /*constructor*/,
132                 JSValueRef possibleInstance,
133                 JSValueRef* /*exception*/)
134 {
135         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
136 }
137
138 } // Common
139 } // DeviceAPI