wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Application / JSRequestedApplicationControl.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 <cassert>
19 #include <memory>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
22 #include <CommonsJavaScript/PrivateObject.h>
23 #include <CommonsJavaScript/JSUtils.h>
24 #include <SecurityExceptions.h>
25 #include <Commons/Exception.h>
26 #include <JSTizenExceptionFactory.h>
27 #include <JSTizenException.h>
28
29 #include "plugin_config.h"
30 #include "ApplicationConverter.h"
31 #include "ApplicationUtil.h"
32 #include "JSRequestedApplicationControl.h"
33 #include <Logger.h>
34
35 using namespace WrtDeviceApis::Commons;
36 using namespace WrtDeviceApis::CommonsJavaScript;
37 using namespace DeviceAPI::Common;
38
39 namespace DeviceAPI {
40 namespace Application {
41
42 JSClassRef JSRequestedApplicationControl::m_jsClassRef = NULL;
43
44 JSClassDefinition JSRequestedApplicationControl::m_classInfo = {
45     0,
46     kJSClassAttributeNone,
47     TIZEN_INTERFACE_REQUESTED_APPLICATION_CONTROL,
48     0,
49     m_property,
50     m_function,
51     initialize,
52     finalize,
53     NULL,     //HasProperty,
54     getProperty,
55     NULL,     //SetProperty,
56     NULL,     //DeleteProperty,
57     NULL,     //GetPropertyNames,
58     NULL,     //CallAsFunction,
59     NULL,     //CallAsConstructor,
60     hasInstance,
61     NULL,     //ConvertToType
62 };
63
64 JSStaticValue JSRequestedApplicationControl::m_property[] = {
65     { TIZEN_APPLICATION_CONTROL, getProperty, NULL, kJSPropertyAttributeNone },
66     { TIZEN_CALLER_APP_ID, getProperty, NULL, kJSPropertyAttributeNone },
67     { 0, 0, 0, 0 }
68 };
69
70 JSStaticFunction JSRequestedApplicationControl::m_function[] = {
71                 { APPLICATION_FUNCTION_API_REPLY_RESULT, JSRequestedApplicationControl::replyResult, kJSPropertyAttributeNone },
72                 { APPLICATION_FUNCTION_API_REPLY_FAILURE, JSRequestedApplicationControl::replyFailure,kJSPropertyAttributeNone },
73                 { 0, 0, 0 }
74 };
75
76 const JSClassDefinition* JSRequestedApplicationControl::getClassInfo()
77 {
78     return &m_classInfo;
79 }
80
81 JSClassRef JSRequestedApplicationControl::getClassRef()
82 {
83     if (!m_jsClassRef) {
84         m_jsClassRef = JSClassCreate(&m_classInfo);
85     }
86         
87     return m_jsClassRef;
88 }
89
90 JSObjectRef JSRequestedApplicationControl::createJSObject(JSContextRef context, const RequestedApplicationControlPtr &appsvc)
91 {
92         LoggerI(">> createJSObject");
93     JSRequestedApplicationControlPriv *priv = new JSRequestedApplicationControlPriv(context, appsvc);
94
95     if (!priv) {
96         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
97     }
98         
99     return JSObjectMake(context, getClassRef(), priv);
100 }
101
102
103
104 void JSRequestedApplicationControl::initialize(JSContextRef context,JSObjectRef object)
105 {
106     LoggerI(">> initialize");
107 }
108
109 void JSRequestedApplicationControl::finalize(JSObjectRef object)
110 {
111     LoggerI(">> finalize");
112     JSRequestedApplicationControlPriv* priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
113     JSObjectSetPrivate(object, NULL);
114     LoggerD("Deleting ApplicationControl object");
115     delete priv;
116 }
117
118
119 bool JSRequestedApplicationControl::isObjectOfClass(JSContextRef context, JSValueRef value)
120 {
121         return JSValueIsObjectOfClass(context, value, getClassRef());
122 }
123
124 RequestedApplicationControlPtr JSRequestedApplicationControl::getRequestedApplicationControl(JSContextRef context, JSValueRef value)
125 {
126         if (!isObjectOfClass(context, value)) {
127                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
128         }
129         
130         JSObjectRef object = JSValueToObject(context, value, NULL);
131         if (!object) {
132                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
133         }
134         
135         JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
136         if (!priv) {
137                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
138         }
139         return priv->getObject();
140 }
141
142
143 RequestedApplicationControlPtr JSRequestedApplicationControl::getPrivData(JSObjectRef object)
144 {
145         LoggerD("entered");
146         JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
147         if (!priv) {
148                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
149         }
150         RequestedApplicationControlPtr result = priv->getObject();
151         if (!result) {
152                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153         }
154         return result;
155 }
156
157 JSValueRef JSRequestedApplicationControl::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
158 {
159     JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
160     if (!priv) {
161                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
162         }
163
164     Try {
165         RequestedApplicationControlPtr providerMgr = priv->getObject();
166                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
167
168         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL)) {
169             LoggerD("JSRequestedApplicationControl::getProperty::appControl " << providerMgr->getAppControl());
170             return converter->toJSValueRef(providerMgr->getAppControl());
171         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALLER_APP_ID)) {
172             LoggerD("JSRequestedApplicationControl::getProperty::callerAppId " << providerMgr->getCallerAppId());
173             return converter->toJSValueRef(providerMgr->getCallerAppId());
174         }
175     } Catch(WrtDeviceApis::Commons::Exception) {
176         LoggerE("Exception: " << _rethrown_exception.GetMessage());
177                 return JSDOMExceptionFactory::UnknownException.make(context, exception);
178     }
179
180         /* do not return undefined object to find method */
181     return NULL;
182 }
183
184
185 bool JSRequestedApplicationControl::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
186 {
187     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
188 }
189
190 JSValueRef JSRequestedApplicationControl::replyResult(JSContextRef context, 
191         JSObjectRef object, 
192         JSObjectRef thisObject, 
193         size_t argumentCount,
194         const JSValueRef arguments[], 
195         JSValueRef* exception) 
196 {
197         LoggerD("entered");
198         JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(thisObject));
199
200         Try {
201         if (!priv) {
202             ThrowMsg(ConversionException, "Object is null.");
203         }
204
205         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
206
207                 RequestedApplicationControlPtr providerMgr = priv->getObject();
208                 std::vector<ApplicationControlDataPtr> resultArray;
209                 if (argumentCount > 0) {
210                         resultArray = converter->toApplicationControlDataArray(arguments[0]);
211                 }
212                 providerMgr->replyResult(resultArray);
213         } Catch (ConversionException) {
214         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
215                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
216         } Catch (NotFoundException) {
217             LoggerW("Exception: "<<_rethrown_exception.GetMessage());
218                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage()); 
219         } Catch (NullPointerException) {
220             LoggerW("Exception: "<<_rethrown_exception.GetMessage());
221                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
222         } Catch (Exception) {
223         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
224                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
225         }
226
227         return JSValueMakeUndefined(context);
228 }
229
230 JSValueRef JSRequestedApplicationControl::replyFailure(JSContextRef context, 
231         JSObjectRef object, 
232         JSObjectRef thisObject, 
233         size_t argumentCount,
234         const JSValueRef arguments[], 
235         JSValueRef* exception) 
236 {
237         LoggerD("entered");
238         JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(thisObject));
239
240         Try {
241         if (!priv) {
242             ThrowMsg(ConversionException, "Object is null.");
243         }
244
245                 RequestedApplicationControlPtr providerMgr = priv->getObject();
246                 providerMgr->replyFailure();
247         } Catch (NotFoundException) {
248             LoggerW("Exception: "<<_rethrown_exception.GetMessage());
249                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage()); 
250         } Catch (NullPointerException) {
251             LoggerW("Exception: "<<_rethrown_exception.GetMessage());
252                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
253         } Catch (Exception) {
254         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
255                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
256         }
257         
258         return JSValueMakeUndefined(context);
259 }
260
261 }
262 }