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