Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Application / JSApplicationServiceData.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
17 #include <dpl/log/log.h>
18 #include <dpl/shared_ptr.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
21 #include "JSApplicationServiceData.h"
22 #include "ApplicationConverter.h"
23
24 namespace TizenApis {
25 namespace Tizen1_0 {
26 namespace Application {
27
28 using namespace Api::Application;
29 using namespace WrtDeviceApis;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31
32
33 //namespace {
34 //      const char* APPLICATION_SERVICE_DATA_KEY = "key";
35 //      const char* APPLICATION_SERVICE_DATA_VALUE = "value";
36 //}
37
38 JSClassRef JSApplicationServiceData::m_classRef = NULL;
39
40 JSClassDefinition JSApplicationServiceData::m_classInfo =
41 {
42         0,
43         kJSClassAttributeNone,
44         "ApplicationServiceData",
45         NULL,
46         m_property,
47         m_functions,
48         initialize,
49         finalize,
50         NULL, //hasProperty,
51         NULL, //GetProperty,
52         NULL, //SetProperty,
53         NULL, //DeleteProperty,
54         NULL, //getPropertyNames,
55         NULL,
56         constructor,
57         NULL,
58         NULL, //ConvertToType,
59 };
60
61 JSStaticValue JSApplicationServiceData::m_property[] = {
62         { "key", getDataKey, setDataKey, kJSPropertyAttributeNone },
63         { "value", getDataValue, setDataValue, kJSPropertyAttributeNone },
64         { 0, 0, 0, 0 }
65 };
66
67 JSStaticFunction JSApplicationServiceData::m_functions[] =
68 {
69         { 0, 0, 0 }
70 };
71
72 JSClassRef JSApplicationServiceData::getClassRef() {
73         if (!m_classRef) {
74                 m_classRef = JSClassCreate(&m_classInfo);
75         }
76         return m_classRef;
77 }
78
79 JSValueRef JSApplicationServiceData::createJSObject(JSContextRef context,
80                 const std::string &key,
81                 const std::vector<std::string> &value)
82 {
83         ApplicationServiceDataPtr privateData = ApplicationServiceDataPtr(new ApplicationServiceData());
84
85         privateData->setKey(key);
86         privateData->setValue(value);
87         JSApplicationServiceDataPriv *priv = new JSApplicationServiceDataPriv(context, privateData);
88         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
89         if (NULL == jsValueRef) {
90                 LogError("object creation error");
91                 return JSValueMakeUndefined(context);
92         }
93         return jsValueRef;
94 }
95
96 JSObjectRef JSApplicationServiceData::constructor(JSContextRef context, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
97 {
98         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
99
100         if ((argumentCount == 2) && JSValueIsString(context, arguments[0]) && JSIsArrayValue(context, arguments[1])) {
101                 return JSValueToObject(context, createJSObject(context, converter->toString(arguments[0]), converter->toVectorOfStrings(arguments[1])), exception);
102         } else {
103                 // throw exception
104                 return NULL;
105         }
106 }
107
108 void JSApplicationServiceData::initialize(JSContextRef context, JSObjectRef object)
109 {
110         //assert(NULL != JSObjectGetPrivate(object));
111 }
112
113 void JSApplicationServiceData::finalize(JSObjectRef object)
114 {
115         // TODO : check
116         //delete (JSObjectGetPrivate(object));
117 }
118
119 bool JSApplicationServiceData::isObjectOfClass(JSContextRef context, JSValueRef value)
120 {
121         return JSValueIsObjectOfClass(context, value, getClassRef());
122 }
123
124 ApplicationServiceDataPtr JSApplicationServiceData::getPrivData(JSObjectRef object)
125 {
126         LogDebug("entered");
127         JSApplicationServiceDataPriv *priv = static_cast<JSApplicationServiceDataPriv*>(JSObjectGetPrivate(object));
128         if (!priv) {
129                 ThrowMsg(Commons::NullPointerException, "Private object is null");
130         }
131         ApplicationServiceDataPtr result = priv->getObject();
132         if (!result) {
133                 ThrowMsg(Commons::NullPointerException, "Private object is null");
134         }
135         return result;
136 }
137
138 ApplicationServiceDataPtr JSApplicationServiceData::getApplicationServiceData(JSContextRef context, JSValueRef value)
139 {
140         if (!isObjectOfClass(context, value)) {
141                 Throw(Commons::InvalidArgumentException);
142         }
143         JSObjectRef object = JSValueToObject(context, value, NULL);
144         if (!object) {
145                 Throw(Commons::InvalidArgumentException);
146         }
147         JSApplicationServiceDataPriv *priv = static_cast<JSApplicationServiceDataPriv*>(JSObjectGetPrivate(object));
148         if (!priv) {
149                 Throw(Commons::NullPointerException);
150         }
151         return priv->getObject();
152 }
153
154 JSValueRef JSApplicationServiceData::getDataKey(JSContextRef context,
155                 JSObjectRef object,
156                 JSStringRef propertyName,
157                 JSValueRef* exception)
158 {
159         Try
160         {
161                 CommonsJavaScript::Converter converter(context);
162                 ApplicationServiceDataPtr privateData = getPrivData(object);
163                 return converter.toJSValueRef(privateData->getKey());
164         }
165         Catch(Commons::Exception)
166         {
167                 LogWarning("trying to get incorrect value");
168         }
169         return JSValueMakeUndefined(context);
170 }
171
172 bool JSApplicationServiceData::setDataKey(JSContextRef context,
173                 JSObjectRef object,
174                 JSStringRef propertyName,
175                 JSValueRef key,
176                 JSValueRef* exception)
177 {
178         Try
179         {
180                 ApplicationServiceDataPtr privateData = getPrivData(object);
181                 CommonsJavaScript::Converter converter(context);
182                 privateData->setKey(converter.toString(key));
183                 return true;
184         }
185         Catch(Commons::Exception)
186         {
187                 LogWarning("trying to set incorrect value");
188         }
189         JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
190         return false;
191 }
192
193 JSValueRef JSApplicationServiceData::getDataValue(JSContextRef context,
194                 JSObjectRef object,
195                 JSStringRef propertyName,
196                 JSValueRef* exception)
197 {
198         Try
199         {
200                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
201                 ApplicationServiceDataPtr privateData = getPrivData(object);
202
203                 return converter.toJSValueRef(privateData->getValue());
204         }
205         Catch(WrtDeviceApis::Commons::Exception)
206         {
207                 LogWarning("trying to get incorrect value");
208         }
209         return JSValueMakeUndefined(context);
210 }
211
212 bool JSApplicationServiceData::setDataValue(JSContextRef context,
213                 JSObjectRef object,
214                 JSStringRef propertyName,
215                 JSValueRef value,
216                 JSValueRef* exception)
217 {
218         Try
219         {
220                 ApplicationServiceDataPtr privateData = getPrivData(object);
221                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
222                 privateData->setValue(converter.toVectorOfStrings(value));
223                 return true;
224         }
225         Catch(WrtDeviceApis::Commons::Exception)
226         {
227                 LogWarning("trying to set incorrect value");
228         }
229         JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
230         return false;
231 }
232
233 }
234 }
235 }