2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
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>
30 #include "ApplicationController.h"
31 #include "ApplicationConverter.h"
32 #include "plugin_config.h"
33 #include "JSApplicationManager.h"
34 #include "JSApplication.h"
37 namespace Application {
39 using namespace DeviceAPI::Common;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
44 JSClassRef JSApplication::m_classRef = NULL;
46 JSClassDefinition JSApplication::m_classInfo = {
48 kJSClassAttributeNone,
49 TIZEN_INTERFACE_APPLICATION,
56 getProperty, //GetProperty,
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 NULL, //CallAsConstructor,
67 JSStaticValue JSApplication::m_property[] = {
68 { TIZEN_APPLICATION_APP_INFO, getProperty, NULL, kJSPropertyAttributeReadOnly },
69 { TIZEN_APPLICATION_APP_CONTEXT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
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 },
81 JSClassRef JSApplication::getClassRef() {
83 m_classRef = JSClassCreate(&m_classInfo);
88 void JSApplication::initialize(JSContextRef context, JSObjectRef object)
90 LogInfo(">> initialize");
93 void JSApplication::finalize(JSObjectRef object)
95 LogInfo(">> finalize");
96 JSApplicationPriv* priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
97 JSObjectSetPrivate(object, NULL);
98 LogDebug("Deleting JSApplication object");
102 bool JSApplication::isObjectOfClass(JSContextRef context, JSValueRef value)
104 return JSValueIsObjectOfClass(context, value, getClassRef());
107 ApplicationPtr JSApplication::getPrivData(JSObjectRef object)
109 JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
111 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
113 ApplicationPtr result = priv->getObject();
115 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
120 JSValueRef JSApplication::getProperty(JSContextRef context,
122 JSStringRef propertyName,
123 JSValueRef* exception)
126 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
127 ApplicationPtr privateData = getPrivData(object);
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());
134 } Catch(WrtDeviceApis::Commons::Exception) {
135 LogError("Exception: " << _rethrown_exception.GetMessage());
136 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
143 JSValueRef JSApplication::exit(JSContextRef context,
145 JSObjectRef thisObject,
146 size_t argumentCount,
147 const JSValueRef arguments[],
148 JSValueRef* exception)
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());
162 return JSValueMakeUndefined(context);
165 JSValueRef JSApplication::hide(JSContextRef context,
167 JSObjectRef thisObject,
168 size_t argumentCount,
169 const JSValueRef arguments[],
170 JSValueRef* exception)
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());
184 return JSValueMakeUndefined(context);
188 JSValueRef JSApplication::getRequestedAppControl(JSContextRef context,
190 JSObjectRef thisObject,
191 size_t argumentCount,
192 const JSValueRef arguments[],
193 JSValueRef* exception)
197 JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(thisObject));
202 ThrowMsg(ConversionException, "Object is null.");
205 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
206 EventApplicationGetRequestedAppControlPtr event(new EventApplicationGetRequestedAppControl());
207 ApplicationPtr app = priv->getObject();
209 JSObjectRef windowObject = JSContextGetGlobalObject(context);
210 JSValueRef encodedBundle = JSObjectGetProperty(context,
212 ScopedJSStringRef(JSStringCreateWithUTF8CString("__bundle")).get(),
214 if (JSValueIsUndefined(context, encodedBundle) || JSValueIsNull(context, encodedBundle)) {
215 LogError("encodedBundle"+converter->toString(encodedBundle));
216 return JSValueMakeNull(context);
219 event->setEncodedBundle(converter->toString(encodedBundle));
220 app->getRequestedAppControl(event);
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);
228 return converter->toJSValueRef(event->getRequestedAppControl());
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());