wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Application / JSApplicationCert.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 #include <JSWebAPIException.h>
24
25 //#include <Commons/Exception.h>
26 //#include <JSWebAPIexception.h>
27 //#include <JSTizenExceptionFactory.h>
28 //#include <JSTizenException.h>
29
30 #include "ApplicationCert.h"
31 #include "JSApplicationCert.h"
32 #include <Logger.h>
33
34 namespace DeviceAPI {
35 namespace Application { 
36         
37 using namespace WrtDeviceApis;
38 using namespace DeviceAPI::Common;
39
40
41 JSClassRef JSApplicationCert::m_classRef = NULL;
42
43 JSClassDefinition JSApplicationCert::m_classInfo = {
44     0,
45     kJSClassAttributeNone,
46     TIZEN_INTERFACE_APPLICATION_CERT,
47     0,
48     m_property,
49     0,
50     initialize,
51     finalize,
52     NULL,     //HasProperty,
53     NULL,       //GetProperty,
54     NULL,     //SetProperty,
55     NULL,     //DeleteProperty,
56     NULL,     //GetPropertyNames,
57     NULL,     //CallAsFunction,
58     NULL,     //CallAsConstructor,
59     NULL,
60     NULL,     //ConvertToType
61 };
62
63 JSStaticValue JSApplicationCert::m_property[] = {
64     { TIZEN_APPLICATION_CERT_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
65     { TIZEN_APPLICATION_CERT_VALUE, getProperty, NULL, kJSPropertyAttributeReadOnly },
66     { 0, 0, 0, 0 }
67 };
68
69 JSClassRef JSApplicationCert::getClassRef() {
70         if (!m_classRef) {
71                 m_classRef = JSClassCreate(&m_classInfo);
72         }
73         return m_classRef;
74 }
75
76
77 void JSApplicationCert::initialize(JSContextRef context, JSObjectRef object)
78 {
79         LoggerI(">> initialize");
80 }
81
82 void JSApplicationCert::finalize(JSObjectRef object)
83 {
84     LoggerI(">> finalize");
85     JSApplicationCertPriv* priv = static_cast<JSApplicationCertPriv*>(JSObjectGetPrivate(object));
86     JSObjectSetPrivate(object, NULL);
87     LoggerD("Deleting JSApplicationCert object");
88     delete priv;                
89 }
90
91 bool JSApplicationCert::isObjectOfClass(JSContextRef context, JSValueRef value)
92 {
93         return JSValueIsObjectOfClass(context, value, getClassRef());
94 }
95
96 ApplicationCertPtr JSApplicationCert::getPrivData(JSObjectRef object)
97 {
98         JSApplicationCertPriv *priv = static_cast<JSApplicationCertPriv*>(JSObjectGetPrivate(object));
99         if (!priv) {
100                 throw TypeMismatchException("Private object is null");
101         }
102         ApplicationCertPtr result = priv->getObject();
103         if (!result) {
104                 throw TypeMismatchException("Private object is null");
105         }
106         return result;
107 }
108
109
110 JSValueRef JSApplicationCert::getProperty(JSContextRef context,
111         JSObjectRef object,
112         JSStringRef propertyName,
113         JSValueRef* exception)
114 {
115         try     {
116                 CommonsJavaScript::Converter converter(context);
117                 ApplicationCertPtr privateData = getPrivData(object);
118
119                 LoggerE("JSApplicationCert::getProperty = " << propertyName);
120         
121                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CERT_TYPE)) {
122                         return converter.toJSValueRef(privateData->getType());          
123                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CERT_VALUE)) {
124                         return converter.toJSValueRef(privateData->getValue());
125                 }
126         } catch (const BasePlatformException &err) {
127         return JSWebAPIException::throwException(context, exception, err);
128     } catch (...) {
129         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
130         return JSWebAPIException::throwException(context, exception, err);
131     }
132         
133         return JSValueMakeUndefined(context);
134 }
135
136 }
137 }