1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_MODULE_H_
6 #define XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_MODULE_H_
9 #include "xwalk/extensions/renderer/xwalk_extension_client.h"
10 #include "xwalk/extensions/renderer/xwalk_module_system.h"
17 class V8ValueConverter;
21 namespace extensions {
23 class XWalkExtensionClient;
24 class XWalkModuleSystem;
26 // Responsible for running the JS code of a XWalkExtension. This includes
27 // creating and exposing an 'extension' object for the execution context of
28 // the extension JS code.
30 // We'll create one XWalkExtensionModule per extension/frame pair, so
31 // there'll be a set of different modules per v8::Context.
32 class XWalkExtensionModule : public XWalkExtensionClient::InstanceHandler {
34 XWalkExtensionModule(XWalkExtensionClient* client,
35 XWalkModuleSystem* module_system,
36 const std::string& extension_name,
37 const std::string& extension_code);
38 virtual ~XWalkExtensionModule();
40 // TODO(cmarcelo): Make this return a v8::Handle<v8::Object>, and
41 // let the module system set it to the appropriated object.
42 void LoadExtensionCode(v8::Handle<v8::Context> context,
43 v8::Handle<v8::Function> requireNative);
45 std::string extension_name() const { return extension_name_; }
48 // XWalkExtensionClient::InstanceHandler implementation.
49 void HandleMessageFromNative(const base::Value& msg) override;
51 // Callbacks for JS functions available in 'extension' object.
52 static void PostMessageCallback(
53 const v8::FunctionCallbackInfo<v8::Value>& info);
54 static void SendSyncMessageCallback(
55 const v8::FunctionCallbackInfo<v8::Value>& info);
56 static void SetMessageListenerCallback(
57 const v8::FunctionCallbackInfo<v8::Value>& info);
59 static XWalkExtensionModule* GetExtensionModule(
60 const v8::FunctionCallbackInfo<v8::Value>& info);
62 // Template for the 'extension' object exposed to the extension JS code.
63 v8::Persistent<v8::ObjectTemplate> object_template_;
65 // This JS object contains a pointer back to the XWalkExtensionModule, it is
66 // set as data for the function callbacks.
67 v8::Persistent<v8::Object> function_data_;
69 // Function to be called when the extension sends a message to its JS code.
70 // This value is registered by using 'extension.setMessageListener()'.
71 v8::Persistent<v8::Function> message_listener_;
73 std::string extension_name_;
74 std::string extension_code_;
76 // TODO(cmarcelo): Move to a single converter, since we always use same
78 scoped_ptr<content::V8ValueConverter> converter_;
80 XWalkExtensionClient* client_;
81 XWalkModuleSystem* module_system_;
85 } // namespace extensions
88 #endif // XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_MODULE_H_