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.
6 #ifndef XWALK_EXTENSIONS_RENDERER_XWALK_MODULE_SYSTEM_H_
7 #define XWALK_EXTENSIONS_RENDERER_XWALK_MODULE_SYSTEM_H_
16 namespace extensions {
18 class XWalkExtensionModule;
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 {
24 virtual v8::Handle<v8::Object> NewInstance() = 0;
25 virtual ~XWalkNativeModule() {}
29 class XWalkModuleSystem {
31 explicit XWalkModuleSystem(v8::Handle<v8::Context> context);
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);
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);
49 v8::Handle<v8::Context> GetV8Context();
52 struct ExtensionModuleEntry {
53 ExtensionModuleEntry(const std::string& name, XWalkExtensionModule* module,
54 const std::vector<std::string>& entry_points);
55 ~ExtensionModuleEntry();
57 XWalkExtensionModule* module;
59 std::vector<std::string> entry_points;
60 bool operator<(const ExtensionModuleEntry& other) const {
61 return name < other.name;
64 static bool IsPrefix(const ExtensionModuleEntry& first,
65 const ExtensionModuleEntry& second);
68 bool SetTrampolineAccessorForEntryPoint(
69 v8::Handle<v8::Context> context,
70 const std::string& entry_point,
71 v8::Local<v8::External> user_data);
73 static bool DeleteAccessorForEntryPoint(v8::Handle<v8::Context> context,
74 const std::string& entry_point);
76 bool InstallTrampoline(v8::Handle<v8::Context> context,
77 ExtensionModuleEntry* entry);
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(
88 v8::Local<v8::Value> data);
89 static v8::Handle<v8::Value> RefetchHolder(
91 v8::Local<v8::Value> data);
93 bool ContainsEntryPoint(const std::string& entry_point);
94 void MarkModulesWithTrampoline();
95 void DeleteExtensionModules();
97 void EnsureExtensionNamespaceIsReadOnly(v8::Handle<v8::Context> context,
98 const std::string& extension_name);
100 typedef std::vector<ExtensionModuleEntry> ExtensionModules;
101 ExtensionModules extension_modules_;
102 typedef std::map<std::string, XWalkNativeModule*> NativeModuleMap;
103 NativeModuleMap native_modules_;
105 v8::Persistent<v8::FunctionTemplate> require_native_template_;
106 v8::Persistent<v8::Object> function_data_;
108 // Points back to the current context, used when native wants to callback
109 // JavaScript. When WillReleaseScriptContext() is called, we dispose this
111 v8::Persistent<v8::Context> v8_context_;
114 } // namespace extensions
116 #endif // XWALK_EXTENSIONS_RENDERER_XWALK_MODULE_SYSTEM_H_