Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Call / JSCallApi.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17
18 #include <dpl/log.h>
19 #include <dpl/shared_ptr.h>
20
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include <CommonsJavaScript/Utils.h>
25 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
26
27 #include "JSCallApi.h"
28 #include "JSCallHistory.h"
29 #include "Converter.h"
30
31 using namespace std;
32 using namespace DPL;
33 using namespace WrtDeviceApis;
34 using namespace WrtDeviceApis::Commons;
35 using namespace WrtDeviceApis::CommonsJavaScript;
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39
40 JSClassRef JSCallApi::m_jsClassRef = NULL;
41
42 JSClassDefinition JSCallApi::m_classInfo =
43 {
44         0,
45         kJSClassAttributeNone,
46         "call",
47         NULL,
48         m_property,
49         NULL,
50         initialize,
51         finalize,
52         NULL,
53         NULL,
54         NULL,
55         NULL,
56         NULL,
57         NULL,
58         NULL,
59         hasInstance,
60         NULL
61 };
62
63 JSStaticValue JSCallApi::m_property[] = {
64         { "history", getProperty, NULL, kJSPropertyAttributeReadOnly },
65         { 0, 0, 0, 0 }
66 };
67
68 const JSClassRef JSCallApi::getClassRef() {
69         if (!m_jsClassRef) {
70                 m_jsClassRef = JSClassCreate(&m_classInfo);
71         }
72         return m_jsClassRef;
73 }
74
75 const JSClassDefinition* JSCallApi::getClassInfo(){
76         return &m_classInfo;
77 }
78
79 void JSCallApi::initialize(JSContextRef context, JSObjectRef object) {
80         JSCallApiPriv *priv = static_cast<JSCallApiPriv*>(JSObjectGetPrivate(object));
81
82         if (priv == NULL) {
83                 priv = new JSCallApiPriv(context);
84
85                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
86                         LogError("Object can't store private data.");
87                         delete priv;
88                 }
89         }
90 }
91
92 void JSCallApi::finalize(JSObjectRef object) {
93         JSCallApiPriv* priv = static_cast<JSCallApiPriv*>(JSObjectGetPrivate(object));
94
95         if (priv != NULL) {
96                 JSObjectSetPrivate(object, NULL);
97                 delete priv;
98         }
99 }
100
101 JSValueRef JSCallApi::getProperty(JSContextRef context,
102         JSObjectRef object,
103         JSStringRef propertyName,
104         JSValueRef* exception)
105 {
106         JSCallApiPriv *priv = static_cast<JSCallApiPriv*>(JSObjectGetPrivate(object));
107         if (!priv) {
108                 return JSValueMakeUndefined(context);
109         }
110
111         JSContextRef globalContext = priv->getContext();
112
113         try {
114                 Converter convert(context);
115                 if(JSStringIsEqualToUTF8CString(propertyName, "history")) {
116                         return TizenApis::Tizen1_0::JSCallHistory::createJSObject(globalContext, object);
117                 }
118         } catch(Commons::Exception) {
119                 LogWarning("trying to get incorrect value");
120         }
121         return JSValueMakeUndefined(context);
122 }
123
124 bool JSCallApi::hasInstance(JSContextRef context, JSObjectRef constructor,
125         JSValueRef possibleInstance, JSValueRef* exception) {
126         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
127 }
128
129 }
130 }
131