Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src / js-overlay / deprecated / js_overlay_support.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  * @file        js_overlay_support.cpp
18  * @author
19  * @version     1.0
20  * @brief
21  */
22 #include <memory>
23 #include <dpl/log/log.h>
24 #include <dpl/foreach.h>
25 #include <JavaScriptCore/JavaScript.h>
26
27 #include <js_overlay_support.h>
28 #include <CommonsJavaScript/JSLifeManager.h>
29
30 namespace JSOverlaySupport {
31 class JSFunctionDispatcher
32 {
33   public:
34     struct PrivateData
35     {
36         //required to unprotect object in destructor;
37         JSContextRef context;
38         JSObjectPtr originalFunction;
39         JSObjectPtr overlayFunction;
40     };
41
42     static JSClassRef getClassRef();
43
44   private:
45     static JSClassDefinition m_classInfo;
46
47   private:
48     static void initialize(JSContextRef context, JSObjectRef object);
49
50     static void finalize(JSObjectRef object);
51
52     static JSValueRef callAsFunction(JSContextRef ctx,
53                                      JSObjectRef function,
54                                      JSObjectRef thisObject,
55                                      size_t argumentCount,
56                                      const JSValueRef arguments[],
57                                      JSValueRef* exception);
58 };
59
60 JSObjectPtr createWrappedFunction(
61     JSGlobalContextRef ctx,
62     const JSObjectPtr& originalFunction,
63     const JSObjectPtr& overlayFunction,
64     const std::string& name)
65 {
66     LogDebug("Creation overlay for function: " << name);
67     JSStringRef name_js = JSStringCreateWithUTF8CString(name.c_str());
68
69     JSFunctionDispatcher::PrivateData* priv =
70         new JSFunctionDispatcher::PrivateData;
71     priv->context = ctx;
72     priv->overlayFunction = overlayFunction;
73     priv->originalFunction = originalFunction;
74
75     auto fun = JSObjectMake(ctx, JSFunctionDispatcher::getClassRef(), priv);
76
77     JSStringRelease(name_js);
78
79     LogDebug("JSValueSafeProtect invoked for: "
80              << overlayFunction->getObject() << " "
81              << originalFunction->getObject() << " Wrapper:"
82              << fun);
83
84     //the value is unprotected in finalize of the JSFunctionDispatcher object
85     JSValueSafeProtect(ctx,
86                    static_cast<JSObjectRef>(
87                        overlayFunction->getObject()));
88
89     return JSObjectPtr(new JSObject(fun));
90 }
91
92 JSClassDefinition JSFunctionDispatcher::m_classInfo = {
93     0,
94     kJSClassAttributeNone,
95     "IGNORED",
96     0,
97     0,
98     0,
99     initialize,
100     finalize,
101     NULL,
102     NULL,
103     NULL,
104     NULL,
105     NULL,
106     callAsFunction,
107     NULL,
108     NULL,
109     NULL
110 };
111
112 JSClassRef JSFunctionDispatcher::getClassRef()
113 {
114     static auto classRef = JSClassCreate(&m_classInfo);
115     return classRef;
116 }
117
118 void JSFunctionDispatcher::initialize(JSContextRef /*context*/,
119                                       JSObjectRef /*object*/)
120 {
121     LogDebug("Initialize");
122 }
123
124 void JSFunctionDispatcher::finalize(JSObjectRef object)
125 {
126     LogDebug("finalize");
127
128     PrivateData* priv = static_cast<PrivateData*>(JSObjectGetPrivate(object));
129     if (priv) {
130         JSValueSafeUnprotect(priv->context,
131                          static_cast<JSObjectRef>(
132                              priv->overlayFunction->getObject()));
133         delete priv;
134     }
135 }
136
137 JSValueRef JSFunctionDispatcher::callAsFunction(
138     JSContextRef context,
139     JSObjectRef object,
140     JSObjectRef thisObject,
141     size_t argumentCount,
142     const JSValueRef arguments[],
143     JSValueRef* exception)
144 {
145     LogDebug("Dispatcher invoked");
146
147     JSValueRef result = JSValueMakeUndefined(context);
148
149     JSFunctionDispatcher::PrivateData* priv =
150         static_cast<JSFunctionDispatcher::PrivateData*>
151         (JSObjectGetPrivate(object));
152
153     if (!priv) {
154         LogError("Private object is NULL");
155         return result;
156     }
157
158     //call overlayed function
159     if (priv->overlayFunction->getObject()) {
160         LogDebug("Overlayed function will be invoked...");
161         result = JSObjectCallAsFunction(
162                 priv->context,
163                 static_cast<JSObjectRef>(
164                     priv->overlayFunction->getObject()),
165                 thisObject,
166                 argumentCount,
167                 arguments,
168                 exception);
169     }
170
171     //call original function
172     if (priv->originalFunction->getObject()) {
173         LogDebug("Original function will be invoked..");
174         result = JSObjectCallAsFunction(
175                 context,
176                 static_cast<JSObjectRef>(
177                     priv->originalFunction->getObject()),
178                 thisObject,
179                 argumentCount,
180                 arguments,
181                 exception);
182     }
183
184     LogDebug("Done");
185     return result;
186 }
187 }