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.
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include "ApplicationContext.h"
23 #include <JSWebAPIErrorFactory.h>
24 #include "JSApplicationContext.h"
29 namespace Application {
31 using namespace WrtDeviceApis;
32 using namespace DeviceAPI::Common;
34 JSClassRef JSApplicationContext::m_classRef = NULL;
36 JSClassDefinition JSApplicationContext::m_classInfo = {
38 kJSClassAttributeNone,
39 TIZEN_INTERFACE_APPLICATION_CONTEXT,
48 NULL, //DeleteProperty,
49 NULL, //GetPropertyNames,
50 NULL, //CallAsFunction,
51 NULL, //CallAsConstructor,
56 JSStaticValue JSApplicationContext::m_property[] = {
57 { TIZEN_APPLICATION_CONTEXT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
58 { TIZEN_APPLICATION_CONTEXT_APP_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
62 JSClassRef DLL_EXPORT JSApplicationContext::getClassRef() {
64 m_classRef = JSClassCreate(&m_classInfo);
69 JSValueRef JSApplicationContext::createJSObject(JSContextRef context,
70 const std::string &appId,
71 const std::string &contextId)
73 ApplicationContextPtr privateData = ApplicationContextPtr(new ApplicationContext());
74 privateData->setAppId(appId);
75 privateData->setContextId(contextId);
77 JSApplicationContextPriv *priv = new JSApplicationContextPriv(context, privateData);
79 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
80 if (NULL == jsValueRef) {
81 LoggerE("object creation error");
82 return JSValueMakeUndefined(context);
88 void JSApplicationContext::initialize(JSContextRef context, JSObjectRef object)
92 void JSApplicationContext::finalize(JSObjectRef object)
94 JSApplicationContextPriv* priv = static_cast<JSApplicationContextPriv*>(JSObjectGetPrivate(object));
95 JSObjectSetPrivate(object, NULL);
99 bool JSApplicationContext::isObjectOfClass(JSContextRef context, JSValueRef value)
101 return JSValueIsObjectOfClass(context, value, getClassRef());
104 ApplicationContextPtr JSApplicationContext::getPrivData(JSContextRef context, JSObjectRef object)
106 JSApplicationContextPriv *priv = static_cast<JSApplicationContextPriv*>(JSObjectGetPrivate(object));
108 throw TypeMismatchException("Private object is null");
111 ApplicationContextPtr result = priv->getObject();
113 throw TypeMismatchException("Private object is null");
119 ApplicationContextPtr JSApplicationContext::getApplicationContext(JSContextRef context, JSValueRef value)
121 if (!isObjectOfClass(context, value)) {
122 throw TypeMismatchException("is not a object class");
125 JSObjectRef object = JSValueToObject(context, value, NULL);
127 throw TypeMismatchException("Private object is null");
130 JSApplicationContextPriv *priv = static_cast<JSApplicationContextPriv*>(JSObjectGetPrivate(object));
132 throw TypeMismatchException("Private object is null");
135 return priv->getObject();
139 JSValueRef JSApplicationContext::getProperty(JSContextRef context,
141 JSStringRef propertyName,
142 JSValueRef* exception)
145 CommonsJavaScript::Converter converter(context);
146 ApplicationContextPtr privateData = getPrivData(context, object);
148 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTEXT_APP_ID)) {
149 return converter.toJSValueRef(privateData->getAppId());
150 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTEXT_ID)) {
151 return converter.toJSValueRef(privateData->getContextId());
154 LoggerE("Exception occured while get property");
155 return JSValueMakeUndefined(context);
158 /* do not return undefined object to find method */