tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Application / JSApplicationContext.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <memory>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include "ApplicationContext.h"
22 #include <JSWebAPIErrorFactory.h>
23 #include "JSApplicationContext.h"
24 #include <Export.h>
25 #include <Logger.h>
26
27 namespace DeviceAPI {
28 namespace Application {
29
30 using namespace WrtDeviceApis;
31 using namespace DeviceAPI::Common;
32
33 JSClassRef JSApplicationContext::m_classRef = NULL;
34
35 JSClassDefinition JSApplicationContext::m_classInfo = {
36     0,
37     kJSClassAttributeNone,
38     TIZEN_INTERFACE_APPLICATION_CONTEXT,
39     0,
40     m_property,
41     0,
42     initialize,
43     finalize,
44     NULL,     //HasProperty,
45     NULL,   //GetProperty,
46     NULL,    //SetProperty,
47     NULL,     //DeleteProperty,
48     NULL,     //GetPropertyNames,
49     NULL,     //CallAsFunction,
50     NULL,     //CallAsConstructor,
51     NULL,
52     NULL,     //ConvertToType
53 };
54
55 JSStaticValue JSApplicationContext::m_property[] = {
56     { TIZEN_APPLICATION_CONTEXT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
57     { TIZEN_APPLICATION_CONTEXT_APP_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
58     { 0, 0, 0, 0 }
59 };
60
61 JSClassRef DLL_EXPORT JSApplicationContext::getClassRef() {
62     LOGD("Entered");
63     if (!m_classRef) {
64         m_classRef = JSClassCreate(&m_classInfo);
65     }
66     return m_classRef;
67 }
68
69 JSValueRef JSApplicationContext::createJSObject(JSContextRef context,
70     const std::string &appId,
71     const std::string &contextId)
72 {
73     LOGD("Entered");
74     ApplicationContextPtr privateData = ApplicationContextPtr(new ApplicationContext());
75     privateData->setAppId(appId);
76     privateData->setContextId(contextId);
77
78     JSApplicationContextPriv *priv = new JSApplicationContextPriv(context, privateData);
79
80     JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
81     if (NULL == jsValueRef) {
82         LOGE("object creation error");
83         return JSValueMakeUndefined(context);
84     }
85
86     return jsValueRef;
87 }
88
89 void JSApplicationContext::initialize(JSContextRef context, JSObjectRef object)
90 {
91     LOGD("Entered");
92 }
93
94 void JSApplicationContext::finalize(JSObjectRef object)
95 {
96     LOGD("Entered");
97     JSApplicationContextPriv* priv = static_cast<JSApplicationContextPriv*>(
98             JSObjectGetPrivate(object));
99     JSObjectSetPrivate(object, NULL);
100     delete priv;
101 }
102
103 bool JSApplicationContext::isObjectOfClass(JSContextRef context, JSValueRef value)
104 {
105     LOGD("Entered");
106     return JSValueIsObjectOfClass(context, value, getClassRef());
107 }
108
109 ApplicationContextPtr JSApplicationContext::getPrivData(JSContextRef context,
110         JSObjectRef object)
111 {
112     LOGD("Entered");
113     JSApplicationContextPriv *priv = static_cast<JSApplicationContextPriv*>(
114             JSObjectGetPrivate(object));
115     if (!priv) {
116         throw TypeMismatchException("Private object is null");
117     }
118
119     ApplicationContextPtr result = priv->getObject();
120     if (!result) {
121         throw TypeMismatchException("Private object is null");
122     }
123
124     return result;
125 }
126
127 ApplicationContextPtr JSApplicationContext::getApplicationContext(JSContextRef context,
128         JSValueRef value)
129 {
130     LOGD("Entered");
131     if (!isObjectOfClass(context, value)) {
132         throw TypeMismatchException("is not a object class");
133     }
134
135     JSObjectRef object = JSValueToObject(context, value, NULL);
136     if (!object) {
137         throw TypeMismatchException("Private object is null");
138     }
139
140     JSApplicationContextPriv *priv = static_cast<JSApplicationContextPriv*>(
141             JSObjectGetPrivate(object));
142     if (!priv) {
143         throw TypeMismatchException("Private object is null");
144     }
145
146     return priv->getObject();
147 }
148
149
150 JSValueRef JSApplicationContext::getProperty(JSContextRef context,
151     JSObjectRef object,
152     JSStringRef propertyName,
153     JSValueRef* exception)
154 {
155     LOGD("Entered");
156     try {
157         CommonsJavaScript::Converter converter(context);
158         ApplicationContextPtr privateData = getPrivData(context, object);
159
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());
164         }
165     } catch (...) {
166         LOGE("Exception occured while get property");
167         return JSValueMakeUndefined(context);
168     }
169
170     /* do not return undefined object to find method */
171     return NULL;
172 }
173
174
175 }
176 }