Update change log and spec for wrt-plugins-tizen_0.4.52
[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 JSStaticFunction JSApplication::m_function[] = {
72                 { APPLICATION_FUNCTION_API_EXIT, JSApplication::exit, kJSPropertyAttributeNone },
73                 { APPLICATION_FUNCTION_API_HIDE, JSApplication::hide, kJSPropertyAttributeNone },
74                 { APPLICATION_FUNCTION_API_GET_REQUESTED_APP_CONTROL, JSApplication::getRequestedAppControl, kJSPropertyAttributeNone },                        
75                 { 0, 0, 0 }
76 };
77
78
79 JSClassRef DLL_EXPORT JSApplication::getClassRef() {
80         if (!m_classRef) {
81                 m_classRef = JSClassCreate(&m_classInfo);
82         }
83         return m_classRef;
84 }
85
86 void JSApplication::initialize(JSContextRef context, JSObjectRef object)
87 {
88 }
89
90 void JSApplication::finalize(JSObjectRef object)
91 {
92     JSApplicationPriv* priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
93     JSObjectSetPrivate(object, NULL);
94     LoggerI("Deleting JSApplication object");
95     delete priv;                
96 }
97
98 JSValueRef JSApplication::makeObject(JSContextRef ctx, const ApplicationPtr value)
99 {
100         if(value == NULL)       {
101                 throw TypeMismatchException("Private object is NULL.");
102         }
103
104         JSApplicationPriv* priv = new JSApplicationPriv(ctx, value);
105         JSObjectRef target = JSObjectMake(ctx, getClassRef(), priv);
106
107         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(ctx);
108         JSUtil::setProperty(ctx, target, TIZEN_APPLICATION_APP_INFO, converter->toJSValueRefFromApplicationInformation(value->getAppInfo()),
109                                                                 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete );
110         JSUtil::setProperty(ctx, target, TIZEN_APPLICATION_APP_CONTEXT_ID, converter->toJSValueRef(value->getContextId()), 
111                                                                 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete );
112         return target;
113 }
114
115 JSValueRef JSApplication::exit(JSContextRef context, 
116         JSObjectRef object, 
117         JSObjectRef thisObject, 
118         size_t argumentCount,
119         const JSValueRef arguments[], 
120         JSValueRef* exception) 
121 {
122         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
123
124         try {
125                 JSApplicationManager::setTitleProperty(context, "tizen://exit");
126                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
127                 return JSValueMakeUndefined(context);
128
129         } catch (const BasePlatformException &err) {
130             return JSWebAPIErrorFactory::postException(context, exception, err);
131         } catch (...) {
132                 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
133                 return JSWebAPIErrorFactory::postException(context, exception, err);
134         }
135 }
136
137 JSValueRef JSApplication::hide(JSContextRef context, 
138         JSObjectRef object, 
139         JSObjectRef thisObject, 
140         size_t argumentCount,
141         const JSValueRef arguments[], 
142         JSValueRef* exception) 
143 {
144         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
145
146         try {
147                 JSApplicationManager::setTitleProperty(context, "tizen://hide");
148                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
149                 return JSValueMakeUndefined(context);
150
151         } catch (const BasePlatformException &err) {
152                 return JSWebAPIErrorFactory::postException(context, exception, err);
153         } catch (...) {
154                 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
155                 return JSWebAPIErrorFactory::postException(context, exception, err);
156         }
157 }
158
159
160 JSValueRef JSApplication::getRequestedAppControl(JSContextRef context,
161         JSObjectRef object, 
162         JSObjectRef thisObject, 
163         size_t argumentCount,
164         const JSValueRef arguments[], 
165         JSValueRef* exception) 
166 {
167         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
168
169         JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(thisObject));
170
171
172         try {
173                 if (!priv) {
174                         throw TypeMismatchException("No private object.");
175                 }
176                 
177         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
178         EventApplicationGetRequestedAppControlPtr event(new EventApplicationGetRequestedAppControl());
179                 ApplicationPtr app = priv->getObject();
180
181                 JSObjectRef windowObject = JSContextGetGlobalObject(context);
182                 JSValueRef encodedBundle = JSObjectGetProperty(context, 
183                                                                                                         windowObject, 
184                                                                                                         ScopedJSStringRef(JSStringCreateWithUTF8CString("__bundle")).get(), 
185                                                                                                         exception);
186                 if (JSValueIsUndefined(context, encodedBundle) || JSValueIsNull(context, encodedBundle)) {
187                         return JSValueMakeNull(context);
188                 }
189
190                 event->setEncodedBundle(converter->toString(encodedBundle));
191                 app->getRequestedAppControl(event);
192                 
193                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException) {
194                         throw NotFoundException("No application control request found.");
195                 } else if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::UnknownException) {
196                         return JSValueMakeNull(context);
197                 }
198                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
199                 return converter->toJSValueRef(event->getRequestedAppControl());
200
201          } catch (const BasePlatformException &err) {
202                  return JSWebAPIErrorFactory::postException(context, exception, err);
203          } catch (...) {
204                  DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
205                  return JSWebAPIErrorFactory::postException(context, exception, err);
206          }
207 }
208
209 }
210 }