1 #ifndef WRT_XWALK_EXTENSION_MODULE_H_
2 #define WRT_XWALK_EXTENSION_MODULE_H_
7 #include <EWebKit_internal.h>
8 #include <JavaScriptCore/JavaScript.h>
14 #include "xwalk_extension_client.h"
18 class XWalkExtensionClient;
19 class XWalkModuleSystem;
21 // Responsible for running the JS code of a XWalkExtension. This includes
22 // creating and exposing an 'extension' object for the execution context of
23 // the extension JS code.
25 // We'll create one XWalkExtensionModule per extension/frame pair, so
26 // there'll be a set of different modules per v8::Context.
27 class XWalkExtensionModule : public XWalkExtensionClient::InstanceHandler {
29 XWalkExtensionModule(XWalkExtensionClient* client,
30 XWalkModuleSystem* module_system,
31 const std::string& extension_name,
32 std::function<std::string(void)> getter);
33 virtual ~XWalkExtensionModule();
35 // TODO(cmarcelo): Make this return a v8::Handle<v8::Object>, and
36 // let the module system set it to the appropriated object.
37 void LoadExtensionCode(JSContextRef context, JSObjectRef native_require);
39 std::string extension_name() const { return extension_name_; }
47 ChunkData& operator=(const ChunkData&) = delete;
48 ChunkData(const ChunkData&) = delete;
50 uint8_t* data() const { return data_; }
51 size_t length() const { return length_; }
52 void set_data(uint8_t* data) { data_ = data; }
53 void set_length(const size_t length) { length_ = length; }
60 // XWalkExtensionClient::InstanceHandler implementation.
61 virtual void HandleMessageFromNative(const std::string& msg);
62 virtual void HandleMessageFromNative(const std::string& msg, unsigned char* buffer, std::size_t len);
64 // Callbacks for JS functions available in 'extension' object.
65 static JSValueRef PostMessageCallback(JSContextRef context,
67 JSObjectRef thisObject,
69 const JSValueRef arguments[],
70 JSValueRef* exception);
71 static JSValueRef PostDataCallback(JSContextRef context,
73 JSObjectRef thisObject,
75 const JSValueRef arguments[],
76 JSValueRef* exception);
77 static JSValueRef SendSyncMessageCallback(JSContextRef context,
79 JSObjectRef thisObject,
81 const JSValueRef arguments[],
82 JSValueRef* exception);
83 static JSValueRef SendSyncDataCallback(JSContextRef context,
85 JSObjectRef thisObject,
87 const JSValueRef arguments[],
88 JSValueRef* exception);
89 static JSValueRef SendRuntimeMessageCallback(JSContextRef context,
91 JSObjectRef thisObject,
93 const JSValueRef arguments[],
94 JSValueRef* exception);
95 static JSValueRef SendRuntimeAsyncMessageCallback(JSContextRef context,
97 JSObjectRef thisObject,
99 const JSValueRef arguments[],
100 JSValueRef* exception);
101 static JSValueRef SendRuntimeSyncMessageCallback(JSContextRef context,
103 JSObjectRef thisObject,
104 size_t argumentCount,
105 const JSValueRef arguments[],
106 JSValueRef* exception);
108 static JSValueRef SetMessageListenerCallback(JSContextRef context,
110 JSObjectRef thisObject,
111 size_t argumentCount,
112 const JSValueRef arguments[],
113 JSValueRef* exception);
114 static JSValueRef SetDataListenerCallback(JSContextRef context,
116 JSObjectRef thisObject,
117 size_t argumentCount,
118 const JSValueRef arguments[],
119 JSValueRef* exception);
120 static JSValueRef ReceiveChunkDataCallback(JSContextRef context,
122 JSObjectRef thisObject,
123 size_t argumentCount,
124 const JSValueRef arguments[],
125 JSValueRef* exception);
127 static XWalkExtensionModule* GetExtensionModule(JSObjectRef info);
129 static JSClassDefinition class_definition_;
130 static JSStaticFunction static_functions_[];
131 static JSClassRef class_ref_;
133 JSObjectRef js_object_;
135 // Function to be called when the extension sends a message to its JS code.
136 // This value is registered by using 'extension.setMessageListener()'.
137 JSObjectRef message_listener_;
138 JSObjectRef data_listener_;
140 std::string extension_name_;
142 std::map<int, ChunkData> data_chunk_storage_;
144 XWalkExtensionClient* client_;
145 XWalkModuleSystem* module_system_;
146 int64_t instance_id_;
147 std::function<std::string(void)> extension_code_getter_;
152 #endif // WRT_XWALK_EXTENSION_MODULE_H_