wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Application / JSApplicationControlData.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 <dpl/shared_ptr.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
21
22 #include <ArgumentValidator.h>
23 #include <JSWebAPIException.h>
24 //#include <JSTizenExceptionFactory.h>
25 //#include <JSTizenException.h>
26 #include <JSUtil.h>
27 #include "JSApplicationControlData.h"
28 #include "ApplicationConverter.h"
29 #include "ApplicationUtil.h"
30 #include <Logger.h>
31
32 using namespace WrtDeviceApis::Commons;
33 using namespace WrtDeviceApis::CommonsJavaScript;
34 using namespace DeviceAPI::Common;
35
36 namespace DeviceAPI {
37 namespace Application {
38
39 JSClassRef JSApplicationControlData::m_classRef = NULL;
40
41 JSClassDefinition JSApplicationControlData::m_classInfo =
42 {
43         0,
44         kJSClassAttributeNone,
45         TIZEN_INTERFACE_APPLICATION_CONTROL_DATA,
46         NULL,
47         m_property,
48         m_functions,
49         initialize,
50         finalize,
51         NULL, //hasProperty,
52         NULL, //GetProperty,
53         NULL, //SetProperty,
54         NULL, //DeleteProperty,
55         NULL, //getPropertyNames,
56         NULL,
57         NULL,
58         NULL,
59         NULL, //ConvertToType,
60 };
61
62 JSStaticValue JSApplicationControlData::m_property[] = {
63         { TIZEN_APPLICATION_CONTROL_DATA_KEY, getProperty, setProperty, kJSPropertyAttributeNone },
64         { TIZEN_APPLICATION_CONTROL_DATA_VALUE, getProperty, setProperty, kJSPropertyAttributeNone },
65         { 0, 0, 0, 0 }
66 };
67
68 JSStaticFunction JSApplicationControlData::m_functions[] =
69 {
70         { 0, 0, 0 }
71 };
72
73 JSClassRef JSApplicationControlData::getClassRef() {
74         if (!m_classRef) {
75                 m_classRef = JSClassCreate(&m_classInfo);
76         }
77         return m_classRef;
78 }
79
80 JSObjectRef JSApplicationControlData::createJSObject(JSContextRef context, const ApplicationControlDataPtr &appdata)
81 {
82         LoggerI(">> createJSObject");
83         JSApplicationControlDataPriv *priv = new JSApplicationControlDataPriv(context, appdata);
84
85         if (!priv) {
86                 throw TypeMismatchException("Private object is null");
87         }
88
89         return JSObjectMake(context, getClassRef(), priv);
90 }
91
92 JSObjectRef JSApplicationControlData::constructor(JSContextRef context, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
93 {
94         try {
95                 ArgumentValidator validator(context, argumentCount, arguments);
96                 ApplicationControlDataPtr appdata(new ApplicationControlData());
97
98             appdata->setKey(validator.toString(0));
99                 appdata->setValue(JSUtil::JSArrayToStringVector(context, validator.toArrayObject(1)));
100
101                 JSApplicationControlDataPriv *priv = new JSApplicationControlDataPriv(context, appdata);
102                 JSObjectRef obj = JSObjectMake(context, getClassRef(),priv);
103
104             JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
105             JSObjectSetProperty(context, obj, ctorName, constructor,
106                 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
107             JSStringRelease(ctorName);
108
109             return obj;
110         } catch (BasePlatformException &err) {
111                 JSObjectRef exceptionObj = JSWebAPIException::makeJSWebAPIException(context, err);
112                 *exception = exceptionObj;
113                 return exceptionObj;
114     }
115 }
116
117 void JSApplicationControlData::initialize(JSContextRef context, JSObjectRef object)
118 {
119         LoggerI(">> initialize");
120 }
121
122 void JSApplicationControlData::finalize(JSObjectRef object)
123 {
124     LoggerI(">> finalize");
125     JSApplicationControlDataPriv* priv = static_cast<JSApplicationControlDataPriv*>(JSObjectGetPrivate(object));
126     JSObjectSetPrivate(object, NULL);
127     LoggerD("Deleting ApplicationControl object");
128     delete priv;        
129 }
130
131 bool JSApplicationControlData::isObjectOfClass(JSContextRef context, JSValueRef value)
132 {
133         return JSValueIsObjectOfClass(context, value, getClassRef());
134 }
135
136 ApplicationControlDataPtr JSApplicationControlData::getPrivData(JSObjectRef object)
137 {
138         LoggerD("entered");
139         JSApplicationControlDataPriv *priv = static_cast<JSApplicationControlDataPriv*>(JSObjectGetPrivate(object));
140         if (!priv) {
141                 throw TypeMismatchException("Private object is null");
142         }
143         ApplicationControlDataPtr result = priv->getObject();
144         if (!result) {
145                 throw TypeMismatchException("Private object is null");
146         }
147         return result;
148 }
149
150 ApplicationControlDataPtr JSApplicationControlData::getApplicationControlData(JSContextRef context, JSValueRef value)
151 {
152         if (!isObjectOfClass(context, value)) {
153                 throw TypeMismatchException("is not a object class");
154         }
155         JSObjectRef object = JSValueToObject(context, value, NULL);
156         if (!object) {
157                 throw TypeMismatchException("Fail to get object");
158         }
159         JSApplicationControlDataPriv *priv = static_cast<JSApplicationControlDataPriv*>(JSObjectGetPrivate(object));
160         if (!priv) {
161                 throw TypeMismatchException("Private object is null");
162         }
163         return priv->getObject();
164 }
165
166
167 JSValueRef JSApplicationControlData::getProperty(JSContextRef context,
168                 JSObjectRef object,
169                 JSStringRef propertyName,
170                 JSValueRef* exception)
171 {
172         try {
173                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
174                 ApplicationControlDataPtr privateData = getPrivData(object);
175                 
176                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_KEY)) {
177                         return converter.toJSValueRef(privateData->getKey());
178                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_VALUE)) {
179                         return converter.toJSValueRef(privateData->getValue());
180                 } 
181         } catch (const BasePlatformException &err) {
182         return JSWebAPIException::throwException(context, exception, err);
183     } catch (...) {
184         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
185         return JSWebAPIException::throwException(context, exception, err);
186     }
187         
188         return JSValueMakeUndefined(context);
189 }
190
191 bool JSApplicationControlData::setProperty(JSContextRef context,
192                         JSObjectRef object,
193                         JSStringRef propertyName,
194                         JSValueRef value,
195                         JSValueRef* exception)
196 {
197         try {
198                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
199                 ApplicationControlDataPtr privateData = getPrivData(object);
200                 
201                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_KEY)) {
202                         privateData->setKey(converter.toString(value));
203                         return true;
204                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_VALUE)) {
205                         privateData->setValue(converter.toVectorOfStrings(value));
206                         return true;                    
207                 } 
208         } catch (const BasePlatformException &err) {
209         return JSWebAPIException::throwException(context, exception, err);
210     } catch (...) {
211         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
212         return JSWebAPIException::throwException(context, exception, err);
213     }
214         
215         return false;
216 }
217
218 }
219 }