Update change log and spec for wrt-plugins-tizen_0.4.9
[platform/framework/web/wrt-plugins-tizen.git] / src / Application / JSApplication.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/JSUtils.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include <Commons/Exception.h>
25 #include <CommonsJavaScript/ScopedJSStringRef.h>
26 #include <JSTizenExceptionFactory.h>
27 #include <JSTizenException.h>
28 #include <SecurityExceptions.h>
29
30 #include "ApplicationController.h"
31 #include "ApplicationConverter.h"
32 #include "plugin_config.h"
33 #include "JSApplicationManager.h"
34 #include "JSApplication.h"
35
36 namespace DeviceAPI {
37 namespace Application { 
38
39 using namespace DeviceAPI::Common;
40
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43         
44 JSClassRef JSApplication::m_classRef = NULL;
45
46 JSClassDefinition JSApplication::m_classInfo = {
47     0,
48     kJSClassAttributeNone,
49     TIZEN_INTERFACE_APPLICATION,
50     0,
51     m_property,
52     m_function,
53     initialize,
54     finalize,
55     NULL,     //HasProperty,
56     getProperty,        //GetProperty,
57     NULL,     //SetProperty,
58     NULL,     //DeleteProperty,
59     NULL,     //GetPropertyNames,
60     NULL,     //CallAsFunction,
61     NULL,     //CallAsConstructor,
62     NULL,
63     NULL,     //ConvertToType
64 };
65
66
67 JSStaticValue JSApplication::m_property[] = {
68     { TIZEN_APPLICATION_APP_INFO, getProperty, NULL, kJSPropertyAttributeReadOnly },
69     { TIZEN_APPLICATION_APP_CONTEXT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
70     { 0, 0, 0, 0 }
71 };
72
73 JSStaticFunction JSApplication::m_function[] = {
74                 { APPLICATION_FUNCTION_API_EXIT, JSApplication::exit, kJSPropertyAttributeNone },
75                 { APPLICATION_FUNCTION_API_HIDE, JSApplication::hide, kJSPropertyAttributeNone },
76                 { APPLICATION_FUNCTION_API_GET_REQUESTED_APP_CONTROL, JSApplication::getRequestedAppControl, kJSPropertyAttributeNone },                        
77                 { 0, 0, 0 }
78 };
79
80
81 JSClassRef JSApplication::getClassRef() {
82         if (!m_classRef) {
83                 m_classRef = JSClassCreate(&m_classInfo);
84         }
85         return m_classRef;
86 }
87
88 void JSApplication::initialize(JSContextRef context, JSObjectRef object)
89 {
90         LogInfo(">> initialize");
91 }
92
93 void JSApplication::finalize(JSObjectRef object)
94 {
95     LogInfo(">> finalize");
96     JSApplicationPriv* priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
97     JSObjectSetPrivate(object, NULL);
98     LogDebug("Deleting JSApplication object");
99     delete priv;                
100 }
101
102 bool JSApplication::isObjectOfClass(JSContextRef context, JSValueRef value)
103 {
104         return JSValueIsObjectOfClass(context, value, getClassRef());
105 }
106
107 ApplicationPtr JSApplication::getPrivData(JSObjectRef object)
108 {
109         JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
110         if (!priv) {
111                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
112         }
113         ApplicationPtr result = priv->getObject();
114         if (!result) {
115                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
116         }
117         return result;
118 }
119
120 JSValueRef JSApplication::getProperty(JSContextRef context,
121         JSObjectRef object,
122         JSStringRef propertyName,
123         JSValueRef* exception)
124 {
125         Try     {
126                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
127                 ApplicationPtr privateData = getPrivData(object);
128         
129                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_APP_INFO)) {
130                         return converter->toJSValueRefFromApplicationInformation(privateData->getAppInfo());
131                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_APP_CONTEXT_ID)) {
132                         return converter->toJSValueRef(privateData->getContextId());
133                 }
134         } Catch(WrtDeviceApis::Commons::Exception) {
135         LogError("Exception: " << _rethrown_exception.GetMessage());
136                 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
137     }
138         
139         return NULL;
140 }
141
142
143 JSValueRef JSApplication::exit(JSContextRef context, 
144         JSObjectRef object, 
145         JSObjectRef thisObject, 
146         size_t argumentCount,
147         const JSValueRef arguments[], 
148         JSValueRef* exception) 
149 {
150         LogDebug("entered");
151
152         Try {
153                 JSApplicationManager::setTitleProperty(context, "tizen://exit");
154         } Catch (UnsupportedException) {
155         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
156                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
157         } Catch (Exception) {
158         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
159                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
160         }
161
162         return JSValueMakeUndefined(context);
163 }
164
165 JSValueRef JSApplication::hide(JSContextRef context, 
166         JSObjectRef object, 
167         JSObjectRef thisObject, 
168         size_t argumentCount,
169         const JSValueRef arguments[], 
170         JSValueRef* exception) 
171 {
172         LogDebug("entered");
173
174         Try {
175                 JSApplicationManager::setTitleProperty(context, "tizen://hide");
176         } Catch (UnsupportedException) {
177         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
178                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
179         } Catch (Exception) {
180         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
181                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
182         }
183
184         return JSValueMakeUndefined(context);
185 }
186
187
188 JSValueRef JSApplication::getRequestedAppControl(JSContextRef context,
189         JSObjectRef object, 
190         JSObjectRef thisObject, 
191         size_t argumentCount,
192         const JSValueRef arguments[], 
193         JSValueRef* exception) 
194 {
195         LogDebug("entered");
196
197         JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(thisObject));
198
199
200         Try {
201                 if (!priv) {
202                         ThrowMsg(ConversionException, "Object is null.");
203                 }
204                 
205         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
206         EventApplicationGetRequestedAppControlPtr event(new EventApplicationGetRequestedAppControl());
207                 ApplicationPtr app = priv->getObject();
208
209                 JSObjectRef windowObject = JSContextGetGlobalObject(context);
210                 JSValueRef encodedBundle = JSObjectGetProperty(context, 
211                                                                                                         windowObject, 
212                                                                                                         ScopedJSStringRef(JSStringCreateWithUTF8CString("__bundle")).get(), 
213                                                                                                         exception);
214                 if (JSValueIsUndefined(context, encodedBundle) || JSValueIsNull(context, encodedBundle)) {
215                         LogError("encodedBundle"+converter->toString(encodedBundle));
216                         return JSValueMakeNull(context);
217                 }
218
219                 event->setEncodedBundle(converter->toString(encodedBundle));
220                 app->getRequestedAppControl(event);
221                 
222                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException) {
223                         ThrowMsg(NotFoundException, "No application control request found.");
224                 } else if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::UnknownException) {
225                         return JSValueMakeNull(context);
226                 }
227
228                 return converter->toJSValueRef(event->getRequestedAppControl());
229
230     } Catch (NotFoundException) {
231         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
232         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
233         } Catch (ConversionException) {
234         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
235                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
236         } Catch (UnsupportedException) {
237         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
238                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
239         } Catch (InvalidArgumentException) {
240         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
241                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
242         } Catch (Exception) {
243         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
244                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
245         }
246 }
247
248 }
249 }