Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / renderer / xwalk_extension_module.h
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.
4
5 #ifndef XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_MODULE_H_
6 #define XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_MODULE_H_
7
8 #include <string>
9 #include "xwalk/extensions/renderer/xwalk_extension_client.h"
10 #include "xwalk/extensions/renderer/xwalk_module_system.h"
11
12 namespace WebKit {
13 class WebFrame;
14 }
15
16 namespace content {
17 class V8ValueConverter;
18 }
19
20 namespace xwalk {
21 namespace extensions {
22
23 class XWalkExtensionClient;
24 class XWalkModuleSystem;
25
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.
29 //
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 {
33  public:
34   XWalkExtensionModule(XWalkExtensionClient* client,
35                        XWalkModuleSystem* module_system,
36                        const std::string& extension_name,
37                        const std::string& extension_code);
38   virtual ~XWalkExtensionModule();
39
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);
44
45   std::string extension_name() const { return extension_name_; }
46
47  private:
48   // XWalkExtensionClient::InstanceHandler implementation.
49   void HandleMessageFromNative(const base::Value& msg) override;
50
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);
58
59   static XWalkExtensionModule* GetExtensionModule(
60       const v8::FunctionCallbackInfo<v8::Value>& info);
61
62   // Template for the 'extension' object exposed to the extension JS code.
63   v8::Persistent<v8::ObjectTemplate> object_template_;
64
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_;
68
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_;
72
73   std::string extension_name_;
74   std::string extension_code_;
75
76   // TODO(cmarcelo): Move to a single converter, since we always use same
77   // parameters.
78   scoped_ptr<content::V8ValueConverter> converter_;
79
80   XWalkExtensionClient* client_;
81   XWalkModuleSystem* module_system_;
82   int64_t instance_id_;
83 };
84
85 }  // namespace extensions
86 }  // namespace xwalk
87
88 #endif  // XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_MODULE_H_