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_SERVER_H_
6 #define XWALK_EXTENSIONS_COMMON_XWALK_EXTENSION_SERVER_H_
14 #include "base/memory/shared_memory.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/synchronization/lock.h"
17 #include "base/values.h"
18 #include "ipc/ipc_channel_proxy.h"
19 #include "ipc/ipc_listener.h"
20 #include "xwalk/extensions/common/xwalk_extension.h"
21 #include "xwalk/extensions/common/xwalk_external_extension.h"
23 struct XWalkExtensionServerMsg_ExtensionRegisterParams;
30 class RenderProcessHost;
38 namespace extensions {
40 class XWalkExtensionInstance;
42 // Manages the instances for a set of extensions. It communicates with one
43 // XWalkExtensionClient by means of IPC channel.
45 // This class is used both by in-process extensions running in the Browser
46 // Process, and by the external extensions running in the Extension Process.
47 class XWalkExtensionServer : public IPC::Listener,
48 public base::SupportsWeakPtr<XWalkExtensionServer> {
50 XWalkExtensionServer();
51 virtual ~XWalkExtensionServer();
53 // IPC::Listener Implementation.
54 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
55 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
57 // Different types of ExtensionServers are initialized with different
58 // permission delegates: For out-of-process extensions the extension
59 // process act as the delegate and dispatch permission request through
60 // IPC; For in-process extensions running in extension thread, we will
61 // give a delegate that will do an async method call and for UI thread
62 // extensions, doing synchronous request is not allowed.
63 void Initialize(IPC::Sender* sender);
64 bool Send(IPC::Message* msg);
66 bool RegisterExtension(scoped_ptr<XWalkExtension> extension);
67 bool ContainsExtension(const std::string& extension_name) const;
71 void set_permissions_delegate(XWalkExtension::PermissionsDelegate* delegate) {
72 permissions_delegate_ = delegate;
74 XWalkExtension::PermissionsDelegate* permissions_delegate() {
75 return permissions_delegate_;
78 // These Message Handlers can be accessed by a message filter when
79 // running on the browser process.
80 void OnCreateInstance(int64_t instance_id, std::string name);
82 std::vector<XWalkExtensionServerMsg_ExtensionRegisterParams>* reply);
85 struct InstanceExecutionData {
86 XWalkExtensionInstance* instance;
87 IPC::Message* pending_reply;
91 void OnDestroyInstance(int64_t instance_id);
92 void OnPostMessageToNative(int64_t instance_id, const base::ListValue& msg);
93 void OnSendSyncMessageToNative(int64_t instance_id,
94 const base::ListValue& msg, IPC::Message* ipc_reply);
96 void PostMessageToJSCallback(int64_t instance_id,
97 scoped_ptr<base::Value> msg);
99 void SendSyncReplyToJSCallback(int64_t instance_id,
100 scoped_ptr<base::Value> reply);
102 void DeleteInstanceMap();
104 bool ValidateExtensionEntryPoints(
105 const std::vector<std::string>& entry_points);
107 base::Lock sender_lock_;
108 IPC::Sender* sender_;
110 typedef std::map<std::string, XWalkExtension*> ExtensionMap;
111 ExtensionMap extensions_;
113 typedef std::map<int64_t, InstanceExecutionData> InstanceMap;
114 InstanceMap instances_;
116 // The exported symbols for extensions already registered.
117 typedef std::set<std::string> ExtensionSymbolsSet;
118 ExtensionSymbolsSet extension_symbols_;
120 base::ProcessHandle renderer_process_handle_;
122 XWalkExtension::PermissionsDelegate* permissions_delegate_;
125 std::vector<std::string> RegisterExternalExtensionsInDirectory(
126 XWalkExtensionServer* server, const base::FilePath& dir,
127 scoped_ptr<base::ValueMap> runtime_variables);
129 bool ValidateExtensionNameForTesting(const std::string& extension_name);
131 } // namespace extensions
134 #endif // XWALK_EXTENSIONS_COMMON_XWALK_EXTENSION_SERVER_H_