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