merge with master
[platform/framework/web/wrt-plugins-common.git] / src / js-overlay / js_function_manager.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_function_manager.cpp
18  * @author  Grzegorz Krawczyk (g.krawczyk@samgsung.com)
19  * @author  Yunchan Cho (yunchan.cho@samgsung.com)
20  * @version
21  * @brief
22  */
23
24 #include <dpl/singleton_safe_impl.h>
25 #include <js_function_manager.h>
26 #include <js_iframe_support.h>
27 #include <js_overlay_functions.h>
28 #include <js_overlay_addEventListener.h>
29 #include <wrt_plugin_export.h>
30
31 using namespace WrtPlugins::W3C;
32
33 IMPLEMENT_SAFE_SINGLETON(JsFunctionManager)
34
35 namespace {
36 const char* JSPRINT_NAME = "jsPrint";
37 const char* JSGLOBAL_OBJECT = "GLOBAL_OBJECT";
38 const char* JSHOOK_NAME = "jsHook";
39 const char* ADD_EVENT_LISTENER_NAME = "addEventListener";
40 }
41
42 namespace JavaScriptFunctions {
43 //options
44 class_definition_options_t jsHookfunctionsOptions = {
45     JS_FUNCTION,
46     CREATE_INSTANCE,
47     NONE_NOTICE,
48     USE_OVERLAYED,     //ignored
49     NULL,
50     NULL,
51     reinterpret_cast<js_function_impl>(JSCFunctions::JavaScriptHookProc)
52 };
53
54 class_definition_options_t jsPrintfunctionsOptions = {
55     JS_FUNCTION,
56     CREATE_INSTANCE,
57     NONE_NOTICE,
58     USE_OVERLAYED,     //ignored
59     NULL,
60     NULL,
61     reinterpret_cast<js_function_impl>(JSCFunctions::JavaScriptPrintProc)
62 };
63
64 class_definition_options_t addEventListenerOptions = {
65     JS_FUNCTION,
66     CREATE_INSTANCE,
67     ALWAYS_NOTICE,
68     OVERLAYED_BEFORE_ORIGINAL,
69     IFrameSupport::RegisterAddEventListener,
70     NULL,
71     reinterpret_cast<js_function_impl>(AddEventListenerSupport::
72                                            AddEventListener)
73 };
74
75 js_entity_definition_t jsPrint = {
76     JSGLOBAL_OBJECT,
77     JSPRINT_NAME,
78     "",
79     NULL,
80     NULL,
81     &jsPrintfunctionsOptions
82 };
83
84 js_entity_definition_t jsHook = {
85     JSGLOBAL_OBJECT,
86     JSHOOK_NAME,
87     "",
88     NULL,
89     NULL,
90     &jsHookfunctionsOptions
91 };
92
93 js_entity_definition_t addEventListener = {
94     JSGLOBAL_OBJECT,
95     ADD_EVENT_LISTENER_NAME,
96     "",
97     NULL,
98     NULL,
99     &addEventListenerOptions
100 };
101 const js_entity_definition_ptr_t jsPrintPtr = &jsPrint;
102 const js_entity_definition_ptr_t jsHookPtr = &jsHook;
103 const js_entity_definition_ptr_t addEventListenerPtr = &addEventListener;
104 }
105
106 bool JsFunctionManager::initialize()
107 {
108     LogInfo("JSObjectDeclaration for js functions are intialized");
109     JSObjectDeclarationPtr jsPrintObj(
110         new JSObjectDeclaration(JavaScriptFunctions::jsPrintPtr));
111
112     JSObjectDeclarationPtr jsHookObj(
113         new JSObjectDeclaration(JavaScriptFunctions::jsHookPtr));
114
115     JSObjectDeclarationPtr addEventListenerObj(
116         new JSObjectDeclaration(JavaScriptFunctions::addEventListenerPtr));
117
118     m_functions.push_back(jsPrintObj);
119     m_functions.push_back(jsHookObj);
120     m_functions.push_back(addEventListenerObj);
121
122     return true;
123 }
124
125 JsFunctionManager::Functions JsFunctionManager::getFunctions()
126 {
127     LogInfo("get standard js fucntions");
128     static bool initialized = initialize();
129     (void) initialized;
130     return m_functions;
131 }