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,
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 },
79 JSClassRef DLL_EXPORT JSApplication::getClassRef() {
81 m_classRef = JSClassCreate(&m_classInfo);
86 void JSApplication::initialize(JSContextRef context, JSObjectRef object)
90 void JSApplication::finalize(JSObjectRef object)
92 JSApplicationPriv* priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
93 JSObjectSetPrivate(object, NULL);
94 LoggerI("Deleting JSApplication object");
98 JSValueRef JSApplication::makeObject(JSContextRef ctx, const ApplicationPtr value)
101 throw TypeMismatchException("Private object is NULL.");
104 JSApplicationPriv* priv = new JSApplicationPriv(ctx, value);
105 JSObjectRef target = JSObjectMake(ctx, getClassRef(), priv);
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 );
115 JSValueRef JSApplication::exit(JSContextRef context,
117 JSObjectRef thisObject,
118 size_t argumentCount,
119 const JSValueRef arguments[],
120 JSValueRef* exception)
122 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
125 JSApplicationManager::setTitleProperty(context, "tizen://exit");
126 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
127 return JSValueMakeUndefined(context);
129 } catch (const BasePlatformException &err) {
130 return JSWebAPIErrorFactory::postException(context, exception, err);
132 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
133 return JSWebAPIErrorFactory::postException(context, exception, err);
137 JSValueRef JSApplication::hide(JSContextRef context,
139 JSObjectRef thisObject,
140 size_t argumentCount,
141 const JSValueRef arguments[],
142 JSValueRef* exception)
144 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
147 JSApplicationManager::setTitleProperty(context, "tizen://hide");
148 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
149 return JSValueMakeUndefined(context);
151 } catch (const BasePlatformException &err) {
152 return JSWebAPIErrorFactory::postException(context, exception, err);
154 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
155 return JSWebAPIErrorFactory::postException(context, exception, err);
160 JSValueRef JSApplication::getRequestedAppControl(JSContextRef context,
162 JSObjectRef thisObject,
163 size_t argumentCount,
164 const JSValueRef arguments[],
165 JSValueRef* exception)
167 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
169 JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(thisObject));
174 throw TypeMismatchException("No private object.");
177 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
178 EventApplicationGetRequestedAppControlPtr event(new EventApplicationGetRequestedAppControl());
179 ApplicationPtr app = priv->getObject();
181 JSObjectRef windowObject = JSContextGetGlobalObject(context);
182 JSValueRef encodedBundle = JSObjectGetProperty(context,
184 ScopedJSStringRef(JSStringCreateWithUTF8CString("__bundle")).get(),
186 if (JSValueIsUndefined(context, encodedBundle) || JSValueIsNull(context, encodedBundle)) {
187 return JSValueMakeNull(context);
190 event->setEncodedBundle(converter->toString(encodedBundle));
191 app->getRequestedAppControl(event);
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);
198 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
199 return converter->toJSValueRef(event->getRequestedAppControl());
201 } catch (const BasePlatformException &err) {
202 return JSWebAPIErrorFactory::postException(context, exception, err);
204 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
205 return JSWebAPIErrorFactory::postException(context, exception, err);