Update change log and spec for wrt-plugins-tizen_0.4.52
[framework/web/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 <cassert>
19 #include <memory>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include "ApplicationContext.h"
23 #include <JSWebAPIErrorFactory.h>
24 #include "JSApplicationContext.h"
25 #include <Export.h>
26 #include <Logger.h>
27
28 namespace DeviceAPI {
29 namespace Application { 
30         
31 using namespace WrtDeviceApis;
32 using namespace DeviceAPI::Common;
33
34 JSClassRef JSApplicationContext::m_classRef = NULL;
35
36 JSClassDefinition JSApplicationContext::m_classInfo = {
37     0,
38     kJSClassAttributeNone,
39     TIZEN_INTERFACE_APPLICATION_CONTEXT,
40     0,
41     m_property,
42     0,
43     initialize,
44     finalize,
45     NULL,     //HasProperty,
46     NULL,       //GetProperty,
47     NULL,    //SetProperty,
48     NULL,     //DeleteProperty,
49     NULL,     //GetPropertyNames,
50     NULL,     //CallAsFunction,
51     NULL,     //CallAsConstructor,
52     NULL,
53     NULL,     //ConvertToType
54 };
55
56 JSStaticValue JSApplicationContext::m_property[] = {
57     { TIZEN_APPLICATION_CONTEXT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
58         { TIZEN_APPLICATION_CONTEXT_APP_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
59     { 0, 0, 0, 0 }
60 };
61
62 JSClassRef DLL_EXPORT JSApplicationContext::getClassRef() {
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         ApplicationContextPtr privateData = ApplicationContextPtr(new ApplicationContext());
74         privateData->setAppId(appId);
75         privateData->setContextId(contextId);
76         
77         JSApplicationContextPriv *priv = new JSApplicationContextPriv(context, privateData);
78         
79         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
80         if (NULL == jsValueRef) {
81                 LoggerE("object creation error");
82                 return JSValueMakeUndefined(context);
83         }
84         
85         return jsValueRef;
86 }
87
88 void JSApplicationContext::initialize(JSContextRef context, JSObjectRef object)
89 {
90 }
91
92 void JSApplicationContext::finalize(JSObjectRef object)
93 {
94     JSApplicationContextPriv* priv = static_cast<JSApplicationContextPriv*>(JSObjectGetPrivate(object));
95     JSObjectSetPrivate(object, NULL);
96     delete priv;                
97 }
98
99 bool JSApplicationContext::isObjectOfClass(JSContextRef context, JSValueRef value)
100 {
101         return JSValueIsObjectOfClass(context, value, getClassRef());
102 }
103
104 ApplicationContextPtr JSApplicationContext::getPrivData(JSContextRef context, JSObjectRef object)
105 {
106         JSApplicationContextPriv *priv = static_cast<JSApplicationContextPriv*>(JSObjectGetPrivate(object));
107         if (!priv) {
108                 throw TypeMismatchException("Private object is null");
109         }
110         
111         ApplicationContextPtr result = priv->getObject();
112         if (!result) {
113                 throw TypeMismatchException("Private object is null");
114         }
115         
116         return result;
117 }
118
119 ApplicationContextPtr JSApplicationContext::getApplicationContext(JSContextRef context, JSValueRef value)
120 {
121         if (!isObjectOfClass(context, value)) {
122                 throw TypeMismatchException("is not a object class");
123         }
124         
125         JSObjectRef object = JSValueToObject(context, value, NULL);
126         if (!object) {
127                 throw TypeMismatchException("Private object is null");
128         }
129         
130         JSApplicationContextPriv *priv = static_cast<JSApplicationContextPriv*>(JSObjectGetPrivate(object));
131         if (!priv) {
132                 throw TypeMismatchException("Private object is null");
133         }
134         
135         return priv->getObject();
136 }
137
138
139 JSValueRef JSApplicationContext::getProperty(JSContextRef context,
140         JSObjectRef object,
141         JSStringRef propertyName,
142         JSValueRef* exception)
143 {
144         try {
145                 CommonsJavaScript::Converter converter(context);
146                 ApplicationContextPtr privateData = getPrivData(context, object);
147         
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());
152                 } 
153         } catch (...) {
154         LoggerE("Exception occured while get property");
155                 return JSValueMakeUndefined(context);
156     }
157         
158         /* do not return undefined object to find method */
159     return NULL;
160 }
161
162
163 }
164 }