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.
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include "ApplicationContext.h"
22 #include <JSWebAPIErrorFactory.h>
23 #include "JSApplicationContext.h"
28 namespace Application {
30 using namespace WrtDeviceApis;
31 using namespace DeviceAPI::Common;
33 JSClassRef JSApplicationContext::m_classRef = NULL;
35 JSClassDefinition JSApplicationContext::m_classInfo = {
37 kJSClassAttributeNone,
38 TIZEN_INTERFACE_APPLICATION_CONTEXT,
47 NULL, //DeleteProperty,
48 NULL, //GetPropertyNames,
49 NULL, //CallAsFunction,
50 NULL, //CallAsConstructor,
55 JSStaticValue JSApplicationContext::m_property[] = {
56 { TIZEN_APPLICATION_CONTEXT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
57 { TIZEN_APPLICATION_CONTEXT_APP_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
61 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)
74 ApplicationContextPtr privateData = ApplicationContextPtr(new ApplicationContext());
75 privateData->setAppId(appId);
76 privateData->setContextId(contextId);
78 JSApplicationContextPriv *priv = new JSApplicationContextPriv(context, privateData);
80 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
81 if (NULL == jsValueRef) {
82 LOGE("object creation error");
83 return JSValueMakeUndefined(context);
89 void JSApplicationContext::initialize(JSContextRef context, JSObjectRef object)
94 void JSApplicationContext::finalize(JSObjectRef object)
97 JSApplicationContextPriv* priv = static_cast<JSApplicationContextPriv*>(
98 JSObjectGetPrivate(object));
99 JSObjectSetPrivate(object, NULL);
103 bool JSApplicationContext::isObjectOfClass(JSContextRef context, JSValueRef value)
106 return JSValueIsObjectOfClass(context, value, getClassRef());
109 ApplicationContextPtr JSApplicationContext::getPrivData(JSContextRef context,
113 JSApplicationContextPriv *priv = static_cast<JSApplicationContextPriv*>(
114 JSObjectGetPrivate(object));
116 throw TypeMismatchException("Private object is null");
119 ApplicationContextPtr result = priv->getObject();
121 throw TypeMismatchException("Private object is null");
127 ApplicationContextPtr JSApplicationContext::getApplicationContext(JSContextRef context,
131 if (!isObjectOfClass(context, value)) {
132 throw TypeMismatchException("is not a object class");
135 JSObjectRef object = JSValueToObject(context, value, NULL);
137 throw TypeMismatchException("Private object is null");
140 JSApplicationContextPriv *priv = static_cast<JSApplicationContextPriv*>(
141 JSObjectGetPrivate(object));
143 throw TypeMismatchException("Private object is null");
146 return priv->getObject();
150 JSValueRef JSApplicationContext::getProperty(JSContextRef context,
152 JSStringRef propertyName,
153 JSValueRef* exception)
157 CommonsJavaScript::Converter converter(context);
158 ApplicationContextPtr privateData = getPrivData(context, object);
160 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTEXT_APP_ID)) {
161 return converter.toJSValueRef(privateData->getAppId());
162 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTEXT_ID)) {
163 return converter.toJSValueRef(privateData->getContextId());
166 LOGE("Exception occured while get property");
167 return JSValueMakeUndefined(context);
170 /* do not return undefined object to find method */