Update change log and spec for wrt-plugins-tizen_0.4.52
[framework/web/wrt-plugins-tizen.git] / src / Application / JSApplicationMetaData.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
19 #include <cassert>
20 #include <memory>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/Converter.h>
23
24 //#include <Commons/Exception.h>
25 #include <JSWebAPIErrorFactory.h>
26
27 #include "ApplicationMetaData.h"
28 #include "JSApplicationMetaData.h"
29 #include <Export.h>
30 #include <Logger.h>
31
32 namespace DeviceAPI {
33 namespace Application { 
34         
35 using namespace WrtDeviceApis;
36 using namespace DeviceAPI::Common;
37
38
39 JSClassRef JSApplicationMetaData::m_classRef = NULL;
40
41 JSClassDefinition JSApplicationMetaData::m_classInfo = {
42     0,
43     kJSClassAttributeNone,
44     TIZEN_INTERFACE_APPLICATION_META_DATA,
45     0,
46     m_property,
47     0,
48     initialize,
49     finalize,
50     NULL,     //HasProperty,
51     NULL,       //GetProperty,
52     NULL,     //SetProperty,
53     NULL,     //DeleteProperty,
54     NULL,     //GetPropertyNames,
55     NULL,     //CallAsFunction,
56     NULL,     //CallAsConstructor,
57     NULL,
58     NULL,     //ConvertToType
59 };
60
61 JSStaticValue JSApplicationMetaData::m_property[] = {
62     { TIZEN_APPLICATION_META_DATA_KEY, getProperty, NULL, kJSPropertyAttributeReadOnly },
63     { TIZEN_APPLICATION_META_DATA_VALUE, getProperty, NULL, kJSPropertyAttributeReadOnly },
64     { 0, 0, 0, 0 }
65 };
66
67 JSClassRef DLL_EXPORT JSApplicationMetaData::getClassRef() {
68         if (!m_classRef) {
69                 m_classRef = JSClassCreate(&m_classInfo);
70         }
71         return m_classRef;
72 }
73
74
75 void JSApplicationMetaData::initialize(JSContextRef context, JSObjectRef object)
76 {
77 }
78
79 void JSApplicationMetaData::finalize(JSObjectRef object)
80 {
81     JSApplicationMetaDataPriv* priv = static_cast<JSApplicationMetaDataPriv*>(JSObjectGetPrivate(object));
82     JSObjectSetPrivate(object, NULL);
83     delete priv;                
84 }
85
86 bool JSApplicationMetaData::isObjectOfClass(JSContextRef context, JSValueRef value)
87 {
88         return JSValueIsObjectOfClass(context, value, getClassRef());
89 }
90
91 ApplicationMetaDataPtr JSApplicationMetaData::getPrivData(JSObjectRef object)
92 {
93         JSApplicationMetaDataPriv *priv = static_cast<JSApplicationMetaDataPriv*>(JSObjectGetPrivate(object));
94         if (!priv) {
95                 throw TypeMismatchException("Private object is null");
96         }
97         ApplicationMetaDataPtr result = priv->getObject();
98         if (!result) {
99                 throw TypeMismatchException("Private object is null");
100         }
101         return result;
102 }
103
104
105 JSValueRef JSApplicationMetaData::getProperty(JSContextRef context,
106         JSObjectRef object,
107         JSStringRef propertyName,
108         JSValueRef* exception)
109 {
110         try     {
111                 CommonsJavaScript::Converter converter(context);
112                 ApplicationMetaDataPtr privateData = getPrivData(object);
113
114                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_META_DATA_KEY)) {
115                         return converter.toJSValueRef(privateData->getKey());           
116                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_META_DATA_VALUE)) {
117                         std::string value = privateData->getValue();
118
119                         // if key is not exist, return NULL
120                         if (value.empty()) {
121                                 return JSValueMakeNull(context);
122                         }
123                         
124                         // if key does not have value, return empty string.
125                         if (value.compare("(null)") == 0) {
126                                 value.clear();
127                         }
128                         
129                         return converter.toJSValueRef(value);
130                 }
131         } catch (...) {
132         LoggerE("Exception occured while get property");
133                 return JSValueMakeUndefined(context);
134     }
135         
136         /* do not return undefined object to find method */
137     return NULL;
138 }
139
140 }
141 }