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 <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <Commons/Exception.h>
24 #include <CommonsJavaScript/ScopedJSStringRef.h>
26 #include <ArgumentValidator.h>
29 #include <JSWebAPIErrorFactory.h>
31 #include "ApplicationController.h"
32 #include "ApplicationConverter.h"
33 #include "plugin_config.h"
34 #include "JSApplicationManager.h"
35 #include "JSApplication.h"
37 #include <TimeTracer.h>
42 namespace Application {
44 using namespace DeviceAPI::Common;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
49 JSClassRef JSApplication::m_classRef = NULL;
51 JSClassDefinition JSApplication::m_classInfo = {
53 kJSClassAttributeNone,
54 TIZEN_INTERFACE_APPLICATION,
63 NULL, //DeleteProperty,
64 NULL, //GetPropertyNames,
65 NULL, //CallAsFunction,
66 NULL, //CallAsConstructor,
72 JSStaticValue JSApplication::m_property[] = {
73 { TIZEN_APPLICATION_APP_INFO, getProperty, NULL, kJSPropertyAttributeReadOnly },
74 { TIZEN_APPLICATION_APP_CONTEXT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
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 },
87 JSClassRef DLL_EXPORT JSApplication::getClassRef() {
89 m_classRef = JSClassCreate(&m_classInfo);
94 void JSApplication::initialize(JSContextRef context, JSObjectRef object)
98 void JSApplication::finalize(JSObjectRef object)
100 JSApplicationPriv* priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
101 JSObjectSetPrivate(object, NULL);
102 LoggerI("Deleting JSApplication object");
106 JSValueRef JSApplication::makeObject(JSContextRef ctx, const ApplicationPtr value)
109 throw TypeMismatchException("Private object is NULL.");
112 JSApplicationPriv* priv = new JSApplicationPriv(ctx, value);
113 JSObjectRef target = JSObjectMake(ctx, getClassRef(), priv);
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 );
125 bool JSApplication::isObjectOfClass(JSContextRef context, JSValueRef value)
127 return JSValueIsObjectOfClass(context, value, getClassRef());
130 ApplicationPtr JSApplication::getPrivData(JSObjectRef object)
132 JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
134 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
136 ApplicationPtr result = priv->getObject();
138 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
144 JSValueRef JSApplication::getProperty(JSContextRef context,
146 JSStringRef propertyName,
147 JSValueRef* exception)
150 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
151 ApplicationPtr privateData = getPrivData(object);
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());
158 } Catch(WrtDeviceApis::Commons::Exception) {
159 LoggerE("Exception: " << _rethrown_exception.GetMessage());
160 JSWebAPIErrorFactory::postException(context, exception,JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid value error");
167 JSValueRef JSApplication::exit(JSContextRef context,
169 JSObjectRef thisObject,
170 size_t argumentCount,
171 const JSValueRef arguments[],
172 JSValueRef* exception)
174 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
177 JSApplicationManager::setTitleProperty(context, "tizen://exit");
178 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
179 return JSValueMakeUndefined(context);
181 } catch (const BasePlatformException &err) {
182 return JSWebAPIErrorFactory::postException(context, exception, err);
184 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
185 return JSWebAPIErrorFactory::postException(context, exception, err);
189 JSValueRef JSApplication::hide(JSContextRef context,
191 JSObjectRef thisObject,
192 size_t argumentCount,
193 const JSValueRef arguments[],
194 JSValueRef* exception)
196 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
199 JSApplicationManager::setTitleProperty(context, "tizen://hide");
200 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
201 return JSValueMakeUndefined(context);
203 } catch (const BasePlatformException &err) {
204 return JSWebAPIErrorFactory::postException(context, exception, err);
206 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
207 return JSWebAPIErrorFactory::postException(context, exception, err);
212 JSValueRef JSApplication::getRequestedAppControl(JSContextRef context,
214 JSObjectRef thisObject,
215 size_t argumentCount,
216 const JSValueRef arguments[],
217 JSValueRef* exception)
219 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
221 JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(thisObject));
226 throw TypeMismatchException("No private object.");
229 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
230 EventApplicationGetRequestedAppControlPtr event(new EventApplicationGetRequestedAppControl());
231 ApplicationPtr app = priv->getObject();
233 JSObjectRef windowObject = JSContextGetGlobalObject(context);
234 JSValueRef encodedBundle = JSObjectGetProperty(context,
236 ScopedJSStringRef(JSStringCreateWithUTF8CString("__bundle")).get(),
238 if (JSValueIsUndefined(context, encodedBundle) || JSValueIsNull(context, encodedBundle)) {
239 return JSValueMakeNull(context);
242 event->setEncodedBundle(converter->toString(encodedBundle));
243 app->getRequestedAppControl(event);
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);
250 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
251 return converter->toJSValueRef(event->getRequestedAppControl());
253 } catch (const BasePlatformException &err) {
254 return JSWebAPIErrorFactory::postException(context, exception, err);
256 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
257 return JSWebAPIErrorFactory::postException(context, exception, err);