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