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