Update change log and spec for wrt-plugins-tizen_0.4.9
[platform/framework/web/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
19 #include <cassert>
20 #include <memory>
21 #include <dpl/log/log.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24 #include <CommonsJavaScript/PrivateObject.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include <SecurityExceptions.h>
27 #include <Commons/Exception.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <JSTizenException.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     getProperty,
57     setProperty,     //SetProperty,
58     NULL,     //DeleteProperty,
59     NULL,     //GetPropertyNames,
60     NULL,     //CallAsFunction,
61     constructor,     //CallAsConstructor,
62     hasInstance,
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     return &m_classInfo;
78 }
79
80 const JSClassRef JSApplicationControl::getClassRef()
81 {
82     if (!m_jsClassRef) {
83         m_jsClassRef = JSClassCreate(&m_classInfo);
84     }
85         
86     return m_jsClassRef;
87 }
88
89 JSObjectRef JSApplicationControl::createJSObject(JSContextRef context, const ApplicationControlPtr &appsvc)
90 {
91          LogInfo(">> createJSObject");
92     JSApplicationControlPriv *priv = new JSApplicationControlPriv(context, appsvc);
93
94     if (!priv) {
95         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
96     }
97         
98     return JSObjectMake(context, getClassRef(), priv);
99 }
100
101
102 ApplicationControlPtr JSApplicationControl::getApplicationControl(JSContextRef context, JSValueRef value)
103 {
104         if (!isObjectOfClass(context, value)) {
105                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
106         }
107         
108         JSObjectRef object = JSValueToObject(context, value, NULL);
109         if (!object) {
110                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
111         }
112         
113         JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
114         if (!priv) {
115                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
116         }
117         return priv->getObject();
118 }
119
120 ApplicationControlPtr JSApplicationControl::getPrivateData(JSObjectRef object)
121 {
122         JSApplicationControlPriv* priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
123         if (!priv) {
124                 Throw(WrtDeviceApis::Commons::NullPointerException);
125         }
126         
127         return priv->getObject();
128 }
129
130
131 void JSApplicationControl::initialize(JSContextRef context,JSObjectRef object)
132 {
133     LogInfo(">> initialize");
134 }
135
136 void JSApplicationControl::finalize(JSObjectRef object)
137 {
138     LogInfo(">> finalize");
139     JSApplicationControlPriv* priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
140     JSObjectSetPrivate(object, NULL);
141     LogDebug("Deleting ApplicationControl object");
142     delete priv;
143 }
144
145 JSObjectRef JSApplicationControl::constructor(JSContextRef context, 
146         JSObjectRef constructor, 
147         size_t argumentCount, 
148         const JSValueRef arguments[], 
149         JSValueRef* exception)
150 {
151         if (argumentCount == 0) {
152                 LogError("Wrong parameters");
153                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
154                 return NULL;
155         }
156
157         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
158
159         Try {
160                 std::string operation = "";
161                 std::string uri = "";
162                 std::string mime = "";
163                 std::string category = "";
164                 std::vector<ApplicationControlDataPtr> appControlDataArray;
165
166                 operation = converter->toString(arguments[0]);
167                 ApplicationUtil util(context, exception);
168                 if ((argumentCount > 1) && !util.isNullOrUndefined(arguments[1])) {
169                         uri = converter->toString(arguments[1]);
170                 }
171                 if ((argumentCount > 2) && !util.isNullOrUndefined(arguments[2])) {
172                         mime = converter->toString(arguments[2]);
173                 }
174                 if ((argumentCount > 3) && !util.isNullOrUndefined(arguments[3])) {
175                         category = converter->toString(arguments[3]);
176                 }
177                 if (argumentCount > 4) {
178                         appControlDataArray = converter->toApplicationControlDataArray(arguments[4]);
179                 }               
180
181                 ApplicationControlPtr appsvc = ApplicationControlPtr(new ApplicationControl(operation, uri, mime, category, appControlDataArray));
182                 if (!appsvc) {
183                         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "unknow error on constructor");
184                         return NULL;
185                 }
186         
187                 return createJSObject(context, appsvc);
188                 
189         } Catch (WrtDeviceApis::Commons::Exception) {
190                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
191                 return NULL;
192                 
193         }
194 }
195         
196
197 bool JSApplicationControl::isObjectOfClass(JSContextRef context, JSValueRef value)
198 {
199         return JSValueIsObjectOfClass(context, value, getClassRef());
200 }
201
202 ApplicationControlPtr JSApplicationControl::getPrivData(JSObjectRef object)
203 {
204         LogDebug("entered");
205         JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
206         if (!priv) {
207                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
208         }
209         ApplicationControlPtr result = priv->getObject();
210         if (!result) {
211                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
212         }
213         return result;
214 }
215
216
217 JSValueRef JSApplicationControl::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
218 {
219     JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
220     if (!priv) {
221                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
222         }
223
224     Try {
225         ApplicationControlPtr appsvc = priv->getObject();
226                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
227
228         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_OPERATION)) {
229             LogDebug("JSApplicationControl::getProperty::operation " << appsvc->getOperation());
230             return converter->toJSValueRef(appsvc->getOperation());
231         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_URI)) {
232             LogDebug("JSApplicationControl::getProperty::uri " << appsvc->getUri());
233             return converter->toJSValueRef(appsvc->getUri());
234         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_MIME)) {
235             LogDebug("JSApplicationControl::getProperty::mime " << appsvc->getMime());
236             return converter->toJSValueRef(appsvc->getMime());
237         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_CATEGORY)) {
238             LogDebug("JSApplicationControl::getProperty::category " << appsvc->getCategory());
239             return converter->toJSValueRef(appsvc->getCategory());
240                 }else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA)) {
241                         LogDebug("JSApplicationControl::getProperty::extraData ");
242                         return converter->toJSValueRef(appsvc->getAppControlDataArray());
243                 }
244
245     } Catch(WrtDeviceApis::Commons::Exception) {
246         LogError("Exception: " << _rethrown_exception.GetMessage());
247                 return JSDOMExceptionFactory::UnknownException.make(context, exception);
248     }
249
250         /* do not return undefined object to find method */
251     return NULL;
252 }
253
254 bool JSApplicationControl::setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
255 {
256         Try     {
257                 ApplicationControlPtr privateData = getPrivData(object);
258                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
259         
260                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_OPERATION)) {
261                         privateData->setOperation(converter->toString(value));
262                         return true;
263                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_URI)) {
264                         privateData->setUri(converter->toString(value));
265                         return true;
266                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_MIME)) {
267                         privateData->setMime(converter->toString(value));
268                         return true;
269                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_CATEGORY)) {
270                         privateData->setCategory(converter->toString(value));
271                         return true;
272                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA)) {
273                         privateData->setAppControlDataArray(converter->toApplicationControlDataArray(value));
274                         return true;
275                 } 
276
277         } Catch(WrtDeviceApis::Commons::Exception) {
278                 LogError("Exception: " << _rethrown_exception.GetMessage());
279         JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
280     }
281         
282         return false;
283 }
284
285
286 bool JSApplicationControl::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
287 {
288     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
289 }
290
291
292 }
293 }