update for beta universally
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Sensors / JSSensorError.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 <vector>
17 #include <CommonsJavaScript/Converter.h>
18 #include <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
21 #include "JSSensorError.h"
22
23 #include <dlog.h>
24
25 #undef LOG_TAG
26 #define LOG_TAG "TIZEN_SENSOR"
27
28 using namespace std;
29 using namespace DPL;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31
32 namespace TizenApis {
33 namespace Tizen1_0 {
34
35 struct MsgCode {
36     string msg;
37     int code;
38 };
39
40 JSClassRef JSSensorError::m_jsClassRef = NULL;
41
42 JSClassDefinition JSSensorError::m_jsClassInfo = {
43                 0,
44                 kJSClassAttributeNone,
45                 "SensorError",
46                 NULL,
47                 m_jsStaticProperties,
48         NULL,
49                 initialize,
50                 finalize,
51                 NULL, //hasProperty,
52                 JSSensorError::getProperty, //getProperty,
53                 JSSensorError::setProperty, //setProperty,
54                 NULL, //deleteProperty,Geolocation
55                 NULL, //getPropertyNames,
56                 NULL,
57                 NULL, // constructor
58                 hasInstance,
59                 NULL
60 };
61
62 JSStaticValue JSSensorError::m_jsStaticProperties[] = {
63     {"PERMISSION_DENIED", JSSensorError::getStaticProperty, NULL, kJSPropertyAttributeReadOnly},
64     {"CONNECTION_ERROR", JSSensorError::getStaticProperty, NULL, kJSPropertyAttributeReadOnly},
65     {0, 0, 0, 0}
66 };
67
68
69 const JSClassRef JSSensorError::getClassRef() 
70 {
71         if (!m_jsClassRef) {
72                 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
73         }
74         return m_jsClassRef;
75 }
76
77 const JSClassDefinition* JSSensorError::getClassInfo() 
78 {
79         return &m_jsClassInfo;
80 }
81
82 JSObjectRef JSSensorError::makeSensorError(JSContextRef ctx, const char* message, unsigned short code)
83 {
84     struct MsgCode* msgcode = new MsgCode;
85     msgcode->msg = message;
86     msgcode->code = code;
87
88     JSObjectRef errorObj = JSObjectMake(ctx, getClassRef(), (void*)msgcode);
89
90     return errorObj;
91 }
92
93 void JSSensorError::initialize(JSContextRef ctx, JSObjectRef object) 
94 {
95 }
96
97 void JSSensorError::finalize(JSObjectRef object) 
98 {
99 }
100
101 bool JSSensorError::hasInstance(JSContextRef ctx, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
102         return JSValueIsObjectOfClass(ctx, possibleInstance, getClassRef());
103 }
104
105 JSValueRef JSSensorError::getProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
106 {
107     struct MsgCode* msgcode = (struct MsgCode*) JSObjectGetPrivate(object);
108     if(msgcode == NULL){
109         return JSValueMakeNull(ctx);
110     }
111
112     if(JSStringIsEqualToUTF8CString(propertyName, "message")) {
113         JSStringRef msg = JSStringCreateWithUTF8CString(msgcode->msg.c_str());
114         JSValueRef msgVal = JSValueMakeString(ctx, msg);
115         JSStringRelease(msg);
116         return msgVal;
117     }
118     else if(JSStringIsEqualToUTF8CString(propertyName, "code")) {
119         JSValueRef msgVal = JSValueMakeNumber(ctx, msgcode->code);
120         return msgVal;
121     }
122
123     return NULL;
124 }
125
126 bool JSSensorError::setProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
127 {
128     // return true for read only
129     if(JSStringIsEqualToUTF8CString(propertyName, "message")) {
130         return true;
131     }
132     else if(JSStringIsEqualToUTF8CString(propertyName, "code")) {
133         return true;
134     }
135
136     return false; 
137 }
138
139 JSValueRef JSSensorError::getStaticProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
140 {
141     if(JSStringIsEqualToUTF8CString(propertyName, "PERMISSION_DENIED")) {
142         return JSValueMakeNumber(ctx, PERMISSION_DENIED);
143     }
144     else if(JSStringIsEqualToUTF8CString(propertyName, "CONNECTION_ERROR")) {
145         return JSValueMakeNumber(ctx, PERMISSION_DENIED);
146     }
147     return JSValueMakeNull(ctx);
148 }
149
150 } // Tizen1_0
151 } // TizenApis