tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Application / JSApplicationControl.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 <memory>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
21 #include <CommonsJavaScript/PrivateObject.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/ScopedJSStringRef.h>
24 #include <SecurityExceptions.h>
25 #include <ArgumentValidator.h>
26 #include <Export.h>
27
28 //#include <Commons/Exception.h>
29 #include <JSWebAPIErrorFactory.h>
30
31 #include "plugin_config.h"
32
33 #include "ApplicationConverter.h"
34 #include "ApplicationUtil.h"
35 #include "JSApplicationControl.h"
36
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace DeviceAPI::Common;
40
41 namespace DeviceAPI {
42 namespace Application {
43
44 JSClassRef JSApplicationControl::m_jsClassRef = NULL;
45
46 JSClassDefinition JSApplicationControl::m_classInfo = {
47     0,
48     kJSClassAttributeNone,
49     TIZEN_INTERFACE_APPLICATION_CONTROL,
50     0,
51     m_property,
52     NULL,
53     initialize,
54     finalize,
55     NULL,     //HasProperty,
56     NULL,
57     NULL,     //SetProperty,
58     NULL,     //DeleteProperty,
59     NULL,     //GetPropertyNames,
60     NULL,     //CallAsFunction,
61     NULL,     //CallAsConstructor,
62     NULL,
63     NULL,     //ConvertToType
64 };
65
66 JSStaticValue JSApplicationControl::m_property[] = {
67     { TIZEN_APPLICATION_CONTROL_OPERATION, getProperty, setProperty, kJSPropertyAttributeNone },
68     { TIZEN_APPLICATION_CONTROL_URI, getProperty, setProperty, kJSPropertyAttributeNone },
69     { TIZEN_APPLICATION_CONTROL_MIME, getProperty, setProperty, kJSPropertyAttributeNone },
70     { TIZEN_APPLICATION_CONTROL_CATEGORY, getProperty, setProperty, kJSPropertyAttributeNone },
71     { TIZEN_APPLICATION_CONTROL_DATA, getProperty, setProperty, kJSPropertyAttributeNone },
72     { 0, 0, 0, 0 }
73 };
74
75 const JSClassDefinition* JSApplicationControl::getClassInfo()
76 {
77     LOGD("Entered");
78     return &m_classInfo;
79 }
80
81 const JSClassRef DLL_EXPORT JSApplicationControl::getClassRef()
82 {
83     LOGD("Entered");
84     if (!m_jsClassRef) {
85         m_jsClassRef = JSClassCreate(&m_classInfo);
86     }
87
88     return m_jsClassRef;
89 }
90
91 JSObjectRef JSApplicationControl::createJSObject(JSContextRef context,
92         const ApplicationControlPtr &appsvc,
93         JSValueRef jsValueData)
94 {
95     LOGD("Entered");
96     JSApplicationControlPriv *priv = new JSApplicationControlPriv(context, appsvc);
97     if (!priv) {
98         LOGE("Failed to alloc memory");
99         return NULL;
100     }
101
102     JSObjectRef jsObjectRef = JSObjectMake(context,
103             getClassRef(),
104             static_cast<void*>(priv));
105     if (NULL == jsObjectRef)
106     {
107         LOGE("object creation error");
108         return NULL;
109     }
110
111     const ScopedJSStringRef jsStrData(JSStringCreateWithUTF8CString(
112             TIZEN_APPLICATION_CONTROL_DATA_INTERNAL));
113
114     JSObjectSetProperty(context, jsObjectRef, jsStrData.get(), jsValueData,
115             kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
116
117     return jsObjectRef;
118 }
119
120
121 ApplicationControlPtr DLL_EXPORT JSApplicationControl::getApplicationControl(
122         JSContextRef context, JSValueRef value)
123 {
124     LOGD("Entered");
125     if (!isObjectOfClass(context, value)) {
126         throw TypeMismatchException("is not a object class");
127     }
128
129     JSObjectRef object = JSValueToObject(context, value, NULL);
130     if (!object) {
131         throw TypeMismatchException("Private object is null");
132     }
133
134     JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(
135             JSObjectGetPrivate(object));
136     if (!priv) {
137         throw TypeMismatchException("Private object is null");
138     }
139     return priv->getObject();
140 }
141
142 void JSApplicationControl::initialize(JSContextRef context,JSObjectRef object)
143 {
144     LOGD("Entered");
145 }
146
147 void JSApplicationControl::finalize(JSObjectRef object)
148 {
149     LOGD("Entered");
150     JSApplicationControlPriv* priv = static_cast<JSApplicationControlPriv*>(
151             JSObjectGetPrivate(object));
152     JSObjectSetPrivate(object, NULL);
153     delete priv;
154 }
155
156 JSObjectRef DLL_EXPORT JSApplicationControl::constructor(JSContextRef context,
157     JSObjectRef constructor,
158     size_t argumentCount,
159     const JSValueRef arguments[],
160     JSValueRef* exception)
161 {
162     LOGD("Entered");
163     ArgumentValidator validator(context, argumentCount, arguments);
164
165     ApplicationControlPtr appsvc = ApplicationControlPtr(new ApplicationControl());
166
167     JSValueRef jsValueData = NULL;
168
169     try {
170         appsvc->setOperation(validator.toString(0));
171         appsvc->setUri(validator.toString(1, true, ""));
172         appsvc->setMime(validator.toString(2, true, ""));
173         appsvc->setCategory(validator.toString(3, true, ""));
174
175         JSObjectRef dataArray = validator.toArrayObject(4, true);
176         if (dataArray) {
177             ApplicationConverterFactory::ConverterType converter =
178                     ApplicationConverterFactory::getConverter(context);
179             std::vector<ApplicationControlDataPtr> appControlDataArray =
180                     converter->toApplicationControlDataArray(dataArray);
181             appsvc->setAppControlDataArray(appControlDataArray);
182             jsValueData = dataArray;
183         }
184     } catch (const BasePlatformException& err) {
185         LOGD("Exception occured while creating constructor : %s", err.getMessage().c_str());
186     } catch (const ConversionException& err) {
187         LOGD("Exception occured while creating constructor : %s", err.GetMessage().c_str());
188     }
189
190     if (jsValueData == NULL)
191     {
192         jsValueData = JSCreateArrayObject(context, 0, NULL);
193     }
194
195     JSObjectRef obj = createJSObject(context, appsvc, jsValueData);
196     if (obj == NULL)
197         return NULL;
198
199     JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
200     JSObjectSetProperty(context, obj, ctorName, constructor,
201         kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
202     JSStringRelease(ctorName);
203
204     return obj;
205 }
206
207
208 bool JSApplicationControl::isObjectOfClass(JSContextRef context, JSValueRef value)
209 {
210     LOGD("Entered");
211     return JSValueIsObjectOfClass(context, value, getClassRef());
212 }
213
214 ApplicationControlPtr JSApplicationControl::getPrivData(JSContextRef context,
215         JSObjectRef object)
216 {
217     LOGD("Entered");
218     JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(
219             JSObjectGetPrivate(object));
220     if (!priv) {
221         throw TypeMismatchException("Private object is null");
222     }
223     ApplicationControlPtr result = priv->getObject();
224     if (!result) {
225         throw TypeMismatchException("Private object is null");
226     }
227     return result;
228 }
229
230
231 JSValueRef JSApplicationControl::getProperty(JSContextRef context, JSObjectRef object,
232         JSStringRef propertyName, JSValueRef* exception)
233 {
234     LOGD("Entered");
235     JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(
236             JSObjectGetPrivate(object));
237     if (!priv) {
238         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
239         return JSWebAPIErrorFactory::postException(context, exception, err);
240     }
241
242     try {
243         ApplicationControlPtr appsvc = priv->getObject();
244         ApplicationConverterFactory::ConverterType converter =
245                 ApplicationConverterFactory::getConverter(context);
246
247         if (JSStringIsEqualToUTF8CString(propertyName,
248                 TIZEN_APPLICATION_CONTROL_OPERATION)) {
249             return converter->toJSValueRef(appsvc->getOperation());
250         } else if (JSStringIsEqualToUTF8CString(propertyName,
251                 TIZEN_APPLICATION_CONTROL_URI)) {
252             std::string uri = appsvc->getUri();
253             if (uri.empty()) {
254                 return JSValueMakeNull(context);
255             } else {
256                 return converter->toJSValueRef(uri);
257             }
258         } else if (JSStringIsEqualToUTF8CString(propertyName,
259                 TIZEN_APPLICATION_CONTROL_MIME)) {
260             std::string mime = appsvc->getMime();
261             if (mime.empty()) {
262                 return JSValueMakeNull(context);
263             } else {
264                 return converter->toJSValueRef(mime);
265             }
266         } else if (JSStringIsEqualToUTF8CString(propertyName,
267                 TIZEN_APPLICATION_CONTROL_CATEGORY)) {
268             std::string category = appsvc->getCategory();
269             if (category.empty()) {
270                 return JSValueMakeNull(context);
271             } else {
272                 return converter->toJSValueRef(category);
273             }
274         } else if (JSStringIsEqualToUTF8CString(propertyName,
275                 TIZEN_APPLICATION_CONTROL_DATA)) {
276             const ScopedJSStringRef jsStrData(JSStringCreateWithUTF8CString(
277                     TIZEN_APPLICATION_CONTROL_DATA_INTERNAL));
278             return JSObjectGetProperty(context, object, jsStrData.get(), NULL);
279         }
280
281     } catch (...) {
282         LOGE("Exception occured while get property");
283         return JSValueMakeUndefined(context);
284     }
285
286     /* do not return undefined object to find method */
287     return NULL;
288 }
289
290 bool JSApplicationControl::setProperty(JSContextRef context,
291         JSObjectRef object,
292         JSStringRef propertyName,
293         JSValueRef value,
294         JSValueRef* exception)
295 {
296     LOGD("Entered");
297     try {
298         ApplicationControlPtr privateData = getPrivData(context, object);
299         ApplicationConverterFactory::ConverterType converter =
300                 ApplicationConverterFactory::getConverter(context);
301
302         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_OPERATION)) {
303             privateData->setOperation(converter->toString(value));
304             return true;
305         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_URI)) {
306             privateData->setUri(converter->toString(value));
307             return true;
308         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_MIME)) {
309             privateData->setMime(converter->toString(value));
310             return true;
311         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_CATEGORY)) {
312             privateData->setCategory(converter->toString(value));
313             return true;
314         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA)) {
315             if (!JSIsArrayValue(context, value))
316                 return true;
317
318             const ScopedJSStringRef jsStrData(
319                     JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTROL_DATA_INTERNAL));
320             JSObjectSetProperty(context, object, jsStrData.get(), value,
321                     kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
322
323             return true;
324         }
325
326     } catch (...) {
327         LOGE("Exception occured while set property");
328     }
329
330     return false;
331 }
332
333 }
334 }