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_COMMON_XWALK_EXTENSION_H_
6 #define XWALK_EXTENSIONS_COMMON_XWALK_EXTENSION_H_
10 #include "base/callback.h"
11 #include "base/values.h"
14 namespace extensions {
16 // Crosswalk Extensions are used to provide native code functionality associated
17 // with a JS API. The native side and JS side communicate by exchanging
18 // messages. The message semantics are up to the extension implementer. The
19 // architecture allows us to run the native side of the extension in a separated
22 // In Crosswalk we provide a C API for external extensions implemented separated
23 // out of Crosswalk binary. Those are loaded when application starts. See
24 // XWalkExternalExtension for it's implementation and XW_Extension.h for the C
27 class XWalkExtensionInstance;
29 // XWalkExtension is a factory class to be implemented by each extension, and
30 // used to create extension instance objects. It also holds information valid
31 // for all the instances, like the JavaScript API. See also
32 // XWalkExtensionInstance.
33 class XWalkExtension {
35 class PermissionsDelegate {
37 // The delegate is responsible for caching the requests for the sake of
39 virtual bool CheckAPIAccessControl(const std::string& extension_name,
40 const std::string& api_name);
41 virtual bool RegisterPermissions(const std::string& extension_name,
42 const std::string& perm_table);
44 ~PermissionsDelegate() {}
47 virtual ~XWalkExtension();
49 virtual XWalkExtensionInstance* CreateInstance() = 0;
51 std::string name() const { return name_; }
52 std::string javascript_api() const { return javascript_api_; }
54 // Returns a list of entry points for which the extension should be loaded
55 // when accessed. Entry points are used when the extension needs to have
56 // objects outside the namespace that is implicitly created using its name.
57 virtual const base::ListValue& entry_points() const;
59 void set_permissions_delegate(XWalkExtension::PermissionsDelegate* delegate) {
60 permissions_delegate_ = delegate;
63 bool CheckAPIAccessControl(const char* api_name) const;
64 bool RegisterPermissions(const char* perm_table) const;
68 void set_name(const std::string& name) { name_ = name; }
69 void set_javascript_api(const std::string& javascript_api) {
70 javascript_api_ = javascript_api;
72 void set_entry_points(const std::vector<std::string>& entry_points) {
73 entry_points_.AppendStrings(entry_points);
77 // Name of extension, used for dispatching messages.
80 // JavaScript API code that will be executed in the render process. It allows
81 // the extension provide a function or object based interface on top of the
83 std::string javascript_api_;
85 // FIXME(jeez): convert this to std::vector<std::string> to avoid
86 // extra conversions later on.
87 base::ListValue entry_points_;
89 // Permission check delegate for both in and out of process extensions.
90 PermissionsDelegate* permissions_delegate_;
92 DISALLOW_COPY_AND_ASSIGN(XWalkExtension);
95 // XWalkExtensionInstance represents an instance of a certain extension, which
96 // is created per ScriptContext created by Crosswalk (which happens for every
99 // XWalkExtensionInstance objects allow us to keep separated state for each
101 class XWalkExtensionInstance {
103 virtual ~XWalkExtensionInstance();
105 // Allow to handle messages sent from JavaScript code running in renderer
107 virtual void HandleMessage(scoped_ptr<base::Value> msg) = 0;
109 // Allow to handle synchronous messages sent from JavaScript code. Renderer
110 // will block until SendSyncReplyToJS() is called with the reply. The reply
111 // can be sent after HandleSyncMessage() function returns.
112 virtual void HandleSyncMessage(scoped_ptr<base::Value> msg);
114 // Callbacks used by extension instance to communicate back to JS. These are
115 // set by the extension system. Callbacks will take the ownership of the
117 typedef base::Callback<void(scoped_ptr<base::Value> msg)> PostMessageCallback;
118 typedef base::Callback<void(scoped_ptr<base::Value> msg)>
119 SendSyncReplyCallback;
121 void SetPostMessageCallback(const PostMessageCallback& callback);
122 void SetSendSyncReplyCallback(const SendSyncReplyCallback& callback);
124 // Function to be used by extensions Instances to post messages back to
125 // JavaScript in the renderer process. This function will take the ownership
127 void PostMessageToJS(scoped_ptr<base::Value> msg) {
128 post_message_.Run(msg.Pass());
132 XWalkExtensionInstance();
134 // Unblocks the renderer waiting on a SyncMessage.
135 void SendSyncReplyToJS(scoped_ptr<base::Value> reply) {
136 send_sync_reply_.Run(reply.Pass());
140 PostMessageCallback post_message_;
141 SendSyncReplyCallback send_sync_reply_;
143 DISALLOW_COPY_AND_ASSIGN(XWalkExtensionInstance);
146 } // namespace extensions
149 #endif // XWALK_EXTENSIONS_COMMON_XWALK_EXTENSION_H_