wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Callhistory / JSRemoteParty.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 <dpl/shared_ptr.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include <JSTizenExceptionFactory.h>
24 #include <JSTizenException.h> 
25 #include "CallHistoryDefine.h"
26 #include "RemoteParty.h"
27 #include "JSRemoteParty.h"
28 #include "Converter.h"
29 #include <Logger.h>
30
31 using namespace WrtDeviceApis;
32 using namespace WrtDeviceApis::Commons;
33 using namespace WrtDeviceApis::CommonsJavaScript;
34 using namespace DeviceAPI::Common;
35
36 namespace DeviceAPI {
37 namespace CallHistory {
38         
39 JSClassDefinition JSRemoteParty::m_classInfo = {
40         0,
41         kJSClassAttributeNone,
42         "remoteparty",
43         0,
44         m_property,
45         0,
46         initialize,
47         finalize,
48         NULL,     //HasProperty,
49         getProperty,
50         setProperty, // NULL,     //SetProperty,
51         NULL,     //DeleteProperty,
52         NULL,     //GetPropertyNames,
53         NULL,     //CallAsFunction,
54         NULL,     //CallAsConstructor,
55         hasInstance,
56         NULL,     //ConvertToType
57 };
58
59 JSStaticValue JSRemoteParty::m_property[] = {
60         { STR_REMOTE_PARTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
61         { STR_PERSIONID, getProperty, NULL, kJSPropertyAttributeReadOnly },
62         { 0, 0, 0, 0 }
63 };
64
65 const JSClassRef JSRemoteParty::getClassRef()
66 {
67         if (!m_jsClassRef) {
68                 m_jsClassRef = JSClassCreate(&m_classInfo);
69         }
70         return m_jsClassRef;
71 }
72
73 const JSClassDefinition* JSRemoteParty::getClassInfo()
74 {
75         return &m_classInfo;
76 }
77
78 JSClassRef JSRemoteParty::m_jsClassRef = JSClassCreate(JSRemoteParty::getClassInfo());
79
80 JSObjectRef JSRemoteParty::createJSObject(JSContextRef context,
81         const RemoteParty &remoteParty)
82 {
83         RemotePartyPtr RemotePartyProps(new RemoteParty(remoteParty));
84         JSRemotePartyPriv *priv = new JSRemotePartyPriv(context, RemotePartyPtr(RemotePartyProps));
85         if (!priv) {
86                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
87         }
88         return JSObjectMake(context, getClassRef(), priv);
89 }
90
91 RemotePartyPtr JSRemoteParty::getRemoteParty(JSContextRef context, JSValueRef value)
92 {
93         if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
94                 Throw(WrtDeviceApis::Commons::ConversionException);
95         }
96         JSObjectRef object = JSValueToObject(context, value, NULL);
97         if (!object) {
98                 Throw(WrtDeviceApis::Commons::ConversionException);
99         }
100         JSRemotePartyPriv *priv = static_cast<JSRemotePartyPriv*>(JSObjectGetPrivate(object));
101         if (!priv) {
102                 Throw(WrtDeviceApis::Commons::ConversionException);
103         }
104         return priv->getObject();
105 }
106
107 void JSRemoteParty::initialize(JSContextRef context, JSObjectRef object)
108 {
109 }
110
111 void JSRemoteParty::finalize(JSObjectRef object)
112 {
113         JSRemotePartyPriv* priv = static_cast<JSRemotePartyPriv*>(JSObjectGetPrivate(object));
114         if (priv != NULL) {
115                 JSObjectSetPrivate(object, NULL);
116                 delete priv;
117         }
118 }
119
120 JSValueRef JSRemoteParty::getProperty(JSContextRef context,
121         JSObjectRef object,
122         JSStringRef propertyName,
123         JSValueRef* exception)
124 {
125         JSRemotePartyPriv *priv = static_cast<JSRemotePartyPriv*>(JSObjectGetPrivate(object));
126         if (!priv) {
127                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : Invalid private pointer");
128         }
129
130         try {
131                 RemotePartyPtr remoteParty = priv->getObject();
132                 Converter convert(context);
133
134                 if (JSStringIsEqualToUTF8CString(propertyName, STR_REMOTE_PARTY)) {
135                         return convert.toJSValueRef(remoteParty->getRemoteParty());
136                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_PERSIONID)) {
137                         return convert.toJSValueRef(remoteParty->getPersonId());
138                 }
139         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
140                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
141         } catch(const WrtDeviceApis::Commons::Exception& ex) {
142                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
143         }
144         return NULL;
145 }
146
147 bool JSRemoteParty::setProperty(JSContextRef context,
148                 JSObjectRef object,
149                 JSStringRef propertyName,
150                 JSValueRef value,
151                 JSValueRef* exception)
152 {
153         return false;
154 }
155
156
157 bool JSRemoteParty::hasInstance(JSContextRef context,
158         JSObjectRef constructor,
159         JSValueRef possibleInstance,
160         JSValueRef* exception)
161 {
162         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
163 }
164 }
165 }
166