Bringup with updated chromium-efl(m56.0.2924)
[platform/framework/web/crosswalk-tizen.git] / extensions / renderer / xwalk_module_system.h
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #ifndef XWALK_EXTENSIONS_RENDERER_XWALK_MODULE_SYSTEM_H_
7 #define XWALK_EXTENSIONS_RENDERER_XWALK_MODULE_SYSTEM_H_
8
9 #include <v8/v8.h>
10
11 #include <map>
12 #include <memory>
13 #include <string>
14 #include <vector>
15
16 namespace extensions {
17
18 class XWalkExtensionModule;
19
20 // Interface used to expose objects via the requireNative() function in JS API
21 // code. Native modules should be registered with the module system.
22 class XWalkNativeModule {
23  public:
24   virtual v8::Handle<v8::Object> NewInstance() = 0;
25   virtual ~XWalkNativeModule() {}
26 };
27
28
29 class XWalkModuleSystem {
30  public:
31   explicit XWalkModuleSystem(v8::Handle<v8::Context> context);
32   ~XWalkModuleSystem();
33
34   static XWalkModuleSystem* GetModuleSystemFromContext(
35       v8::Handle<v8::Context> context);
36   static void SetModuleSystemInContext(
37       std::unique_ptr<XWalkModuleSystem> module_system,
38       v8::Handle<v8::Context> context);
39   static void ResetModuleSystemFromContext(v8::Handle<v8::Context> context);
40
41   void RegisterExtensionModule(std::unique_ptr<XWalkExtensionModule> module,
42                                const std::vector<std::string>& entry_points);
43   void RegisterNativeModule(const std::string& name,
44                             std::unique_ptr<XWalkNativeModule> module);
45   v8::Handle<v8::Object> RequireNative(const std::string& name);
46
47   void Initialize();
48
49   v8::Handle<v8::Context> GetV8Context();
50
51  private:
52   struct ExtensionModuleEntry {
53     ExtensionModuleEntry(const std::string& name, XWalkExtensionModule* module,
54                          const std::vector<std::string>& entry_points);
55     ~ExtensionModuleEntry();
56     std::string name;
57     XWalkExtensionModule* module;
58     bool use_trampoline;
59     std::vector<std::string> entry_points;
60     bool operator<(const ExtensionModuleEntry& other) const {
61       return name < other.name;
62     }
63
64     static bool IsPrefix(const ExtensionModuleEntry& first,
65                          const ExtensionModuleEntry& second);
66   };
67
68   bool SetTrampolineAccessorForEntryPoint(
69       v8::Handle<v8::Context> context,
70       const std::string& entry_point,
71       v8::Local<v8::External> user_data);
72
73   static bool DeleteAccessorForEntryPoint(v8::Handle<v8::Context> context,
74                                           const std::string& entry_point);
75
76   bool InstallTrampoline(v8::Handle<v8::Context> context,
77                          ExtensionModuleEntry* entry);
78
79   static void TrampolineCallback(
80       v8::Local<v8::Name> property,
81       const v8::PropertyCallbackInfo<v8::Value>& info);
82   static void TrampolineSetterCallback(
83       v8::Local<v8::Name> property,
84       v8::Local<v8::Value> value,
85       const v8::PropertyCallbackInfo<void>& info);
86   static void LoadExtensionForTrampoline(
87       v8::Isolate* isolate,
88       v8::Local<v8::Value> data);
89   static v8::Handle<v8::Value> RefetchHolder(
90     v8::Isolate* isolate,
91     v8::Local<v8::Value> data);
92
93   bool ContainsEntryPoint(const std::string& entry_point);
94   void MarkModulesWithTrampoline();
95   void DeleteExtensionModules();
96
97   void EnsureExtensionNamespaceIsReadOnly(v8::Handle<v8::Context> context,
98                                           const std::string& extension_name);
99
100   typedef std::vector<ExtensionModuleEntry> ExtensionModules;
101   ExtensionModules extension_modules_;
102   typedef std::map<std::string, XWalkNativeModule*> NativeModuleMap;
103   NativeModuleMap native_modules_;
104
105   v8::Persistent<v8::FunctionTemplate> require_native_template_;
106   v8::Persistent<v8::Object> function_data_;
107
108   // Points back to the current context, used when native wants to callback
109   // JavaScript. When WillReleaseScriptContext() is called, we dispose this
110   // persistent.
111   v8::Persistent<v8::Context> v8_context_;
112 };
113
114 }  // namespace extensions
115
116 #endif  // XWALK_EXTENSIONS_RENDERER_XWALK_MODULE_SYSTEM_H_