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