Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / plugin-loading / javascript_interface.h
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       JavaScriptInterface.h
18  * @author     Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @author     Piotr Fatyga (p.fatyga@samsung.com)
20  * @version    0.1
21  * @brief
22  */
23 #ifndef WRT_SRC_PLUGIN_SERVICE_WEBKIT_INTERFACE_H_
24 #define WRT_SRC_PLUGIN_SERVICE_WEBKIT_INTERFACE_H_
25
26 #include <string>
27 #include <vector>
28 #include <memory>
29 #include <list>
30 #include <dpl/noncopyable.h>
31 #include <dpl/singleton.h>
32 #include <Commons/JSObjectDeclaration.h>
33 #include <Commons/JSObject.h>
34
35 //forward declaration of JSConectexRef
36 extern "C" {
37     typedef const struct OpaqueJSContext* JSContextRef;
38     typedef struct OpaqueJSContext* JSGlobalContextRef;
39     typedef struct OpaqueJSValue* JSObjectRef;
40 }
41
42 class JavaScriptInterface : DPL::Noncopyable
43 {
44   public:
45
46     typedef std::vector<std::string> PropertiesList;
47
48     typedef std::list<JSObjectPtr> ObjectsList;
49     typedef std::shared_ptr<ObjectsList> ObjectsListPtr;
50
51   public:
52     JSObjectPtr getGlobalObject(JSGlobalContextRef context) const;
53
54     // object creation
55     JSObjectPtr createObject(JSGlobalContextRef context,
56                              const JSObjectDeclarationPtr& declaration);
57
58     //properties
59     void setObjectProperty(JSGlobalContextRef context,
60                            const JSObjectPtr& parentObject,
61                            const std::string &propertyName,
62                            const JSObjectPtr& propertyObject);
63
64     void removeObjectProperty(JSGlobalContextRef context,
65                               const JSObjectPtr& parentObject,
66                               const std::string &propertyName);
67
68     PropertiesList getObjectPropertiesList(JSGlobalContextRef context,
69                                            const JSObjectPtr& object) const;
70
71     JSObjectPtr copyObjectToIframe(JSGlobalContextRef context,
72                                    const JSObjectPtr& iframe,
73                                    const std::string& name);
74
75     ObjectsListPtr getIframesList(JSGlobalContextRef context) const;
76
77     void removeIframes(JSGlobalContextRef context);
78
79     void invokeGarbageCollector(JSGlobalContextRef context);
80
81     JSObjectPtr getJSObjectProperty(JSGlobalContextRef ctx,
82                             const JSObjectPtr& frame,
83                             const std::string& name) const;
84
85   private:
86     JavaScriptInterface()
87     {
88     }
89
90     JSObjectPtr makeJSFunctionObject(
91             JSGlobalContextRef context,
92             const std::string &name,
93             js_function_impl functionImplementation) const;
94     JSObjectPtr makeJSClassObject(
95             JSGlobalContextRef context,
96             JSObjectDeclaration::ConstClassTemplate classTemplate) const;
97     JSObjectPtr makeJSObjectBasedOnInterface(
98             JSGlobalContextRef context,
99             const std::string &interfaceName) const;
100     JSObjectPtr makeJSInterface(
101             JSGlobalContextRef context,
102             JSObjectDeclaration::ConstClassTemplate classTemplate,
103             JSObjectDeclaration::ConstructorCallback constructorCallback) const;
104
105     ObjectsListPtr getIframesList(JSContextRef context,
106             JSObjectRef object) const;
107
108     friend class DPL::Singleton<JavaScriptInterface>;
109 };
110
111 typedef DPL::Singleton<JavaScriptInterface> JavaScriptInterfaceSingleton;
112
113 #endif