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 <JSWebAPIException.h>
27 #include <ArgumentValidator.h>
30 //#include <JSTizenExceptionFactory.h>
31 //#include <JSTizenException.h>
32 //#include <SecurityExceptions.h>
34 #include "ApplicationController.h"
35 #include "ApplicationConverter.h"
36 #include "plugin_config.h"
37 #include "JSApplicationManager.h"
38 #include "JSApplication.h"
40 #include <TimeTracer.h>
44 namespace Application {
46 using namespace DeviceAPI::Common;
48 using namespace WrtDeviceApis::Commons;
49 using namespace WrtDeviceApis::CommonsJavaScript;
51 JSClassRef JSApplication::m_classRef = NULL;
53 JSClassDefinition JSApplication::m_classInfo = {
55 kJSClassAttributeNone,
56 TIZEN_INTERFACE_APPLICATION,
65 NULL, //DeleteProperty,
66 NULL, //GetPropertyNames,
67 NULL, //CallAsFunction,
68 NULL, //CallAsConstructor,
74 JSStaticValue JSApplication::m_property[] = {
75 { TIZEN_APPLICATION_APP_INFO, getProperty, NULL, kJSPropertyAttributeReadOnly },
76 { TIZEN_APPLICATION_APP_CONTEXT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
81 JSStaticFunction JSApplication::m_function[] = {
82 { APPLICATION_FUNCTION_API_EXIT, JSApplication::exit, kJSPropertyAttributeNone },
83 { APPLICATION_FUNCTION_API_HIDE, JSApplication::hide, kJSPropertyAttributeNone },
84 { APPLICATION_FUNCTION_API_GET_REQUESTED_APP_CONTROL, JSApplication::getRequestedAppControl, kJSPropertyAttributeNone },
89 JSClassRef JSApplication::getClassRef() {
91 m_classRef = JSClassCreate(&m_classInfo);
96 void JSApplication::initialize(JSContextRef context, JSObjectRef object)
98 LoggerI(">> initialize");
101 void JSApplication::finalize(JSObjectRef object)
103 LoggerI(">> finalize");
104 JSApplicationPriv* priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
105 JSObjectSetPrivate(object, NULL);
106 LoggerD("Deleting JSApplication object");
110 JSValueRef JSApplication::makeObject(JSContextRef ctx, const ApplicationPtr value)
112 LoggerI("entered to ApplicationPtr ");
114 throw TypeMismatchException("Private object is NULL.");
117 JSApplicationPriv* priv = new JSApplicationPriv(ctx, value);
118 JSObjectRef target = JSObjectMake(ctx, getClassRef(), priv);
120 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(ctx);
121 JSUtil::setProperty(ctx, target, TIZEN_APPLICATION_APP_INFO, converter->toJSValueRefFromApplicationInformation(value->getAppInfo()),
122 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete );
123 JSUtil::setProperty(ctx, target, TIZEN_APPLICATION_APP_CONTEXT_ID, converter->toJSValueRef(value->getContextId()),
124 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete );
130 bool JSApplication::isObjectOfClass(JSContextRef context, JSValueRef value)
132 return JSValueIsObjectOfClass(context, value, getClassRef());
135 ApplicationPtr JSApplication::getPrivData(JSObjectRef object)
137 JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(object));
139 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
141 ApplicationPtr result = priv->getObject();
143 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
149 JSValueRef JSApplication::getProperty(JSContextRef context,
151 JSStringRef propertyName,
152 JSValueRef* exception)
155 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
156 ApplicationPtr privateData = getPrivData(object);
158 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_APP_INFO)) {
159 return converter->toJSValueRefFromApplicationInformation(privateData->getAppInfo());
160 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_APP_CONTEXT_ID)) {
161 return converter->toJSValueRef(privateData->getContextId());
163 } Catch(WrtDeviceApis::Commons::Exception) {
164 LoggerE("Exception: " << _rethrown_exception.GetMessage());
165 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
172 JSValueRef JSApplication::exit(JSContextRef context,
174 JSObjectRef thisObject,
175 size_t argumentCount,
176 const JSValueRef arguments[],
177 JSValueRef* exception)
180 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
183 JSApplicationManager::setTitleProperty(context, "tizen://exit");
184 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
185 return JSValueMakeUndefined(context);
187 } catch (const BasePlatformException &err) {
188 return JSWebAPIException::throwException(context, exception, err);
190 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
191 return JSWebAPIException::throwException(context, exception, err);
195 JSValueRef JSApplication::hide(JSContextRef context,
197 JSObjectRef thisObject,
198 size_t argumentCount,
199 const JSValueRef arguments[],
200 JSValueRef* exception)
203 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
206 JSApplicationManager::setTitleProperty(context, "tizen://hide");
207 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
208 return JSValueMakeUndefined(context);
210 } catch (const BasePlatformException &err) {
211 return JSWebAPIException::throwException(context, exception, err);
213 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
214 return JSWebAPIException::throwException(context, exception, err);
219 JSValueRef JSApplication::getRequestedAppControl(JSContextRef context,
221 JSObjectRef thisObject,
222 size_t argumentCount,
223 const JSValueRef arguments[],
224 JSValueRef* exception)
227 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
229 JSApplicationPriv *priv = static_cast<JSApplicationPriv*>(JSObjectGetPrivate(thisObject));
234 throw TypeMismatchException("No private object.");
237 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
238 EventApplicationGetRequestedAppControlPtr event(new EventApplicationGetRequestedAppControl());
239 ApplicationPtr app = priv->getObject();
241 JSObjectRef windowObject = JSContextGetGlobalObject(context);
242 JSValueRef encodedBundle = JSObjectGetProperty(context,
244 ScopedJSStringRef(JSStringCreateWithUTF8CString("__bundle")).get(),
246 if (JSValueIsUndefined(context, encodedBundle) || JSValueIsNull(context, encodedBundle)) {
247 LoggerE("encodedBundle"+converter->toString(encodedBundle));
248 return JSValueMakeNull(context);
251 event->setEncodedBundle(converter->toString(encodedBundle));
252 app->getRequestedAppControl(event);
254 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException) {
255 throw NotFoundException("No application control request found.");
256 } else if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::UnknownException) {
257 return JSValueMakeNull(context);
259 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
260 return converter->toJSValueRef(event->getRequestedAppControl());
262 } catch (const BasePlatformException &err) {
263 return JSWebAPIException::throwException(context, exception, err);
265 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
266 return JSWebAPIException::throwException(context, exception, err);