e79fc4820b035dac3e8451b2a377f31cbeab9917
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / common / xwalk_extension_server.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_COMMON_XWALK_EXTENSION_SERVER_H_
6 #define XWALK_EXTENSIONS_COMMON_XWALK_EXTENSION_SERVER_H_
7
8 #include <stdint.h>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
13
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"
22
23 struct XWalkExtensionServerMsg_ExtensionRegisterParams;
24
25 namespace base {
26 class FilePath;
27 }
28
29 namespace content {
30 class RenderProcessHost;
31 }
32
33 namespace IPC {
34 class Sender;
35 }
36
37 namespace xwalk {
38 namespace extensions {
39
40 class XWalkExtensionInstance;
41
42 // Manages the instances for a set of extensions. It communicates with one
43 // XWalkExtensionClient by means of IPC channel.
44 //
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> {
49  public:
50   XWalkExtensionServer();
51   virtual ~XWalkExtensionServer();
52
53   // IPC::Listener Implementation.
54   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
55   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
56
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);
65
66   bool RegisterExtension(scoped_ptr<XWalkExtension> extension);
67   bool ContainsExtension(const std::string& extension_name) const;
68
69   void Invalidate();
70
71   void set_permissions_delegate(XWalkExtension::PermissionsDelegate* delegate) {
72     permissions_delegate_ = delegate;
73   }
74   XWalkExtension::PermissionsDelegate* permissions_delegate() {
75     return permissions_delegate_;
76   }
77
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);
81   void OnGetExtensions(
82       std::vector<XWalkExtensionServerMsg_ExtensionRegisterParams>* reply);
83
84  private:
85   struct InstanceExecutionData {
86     XWalkExtensionInstance* instance;
87     IPC::Message* pending_reply;
88   };
89
90   // Message Handlers
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);
95
96   void PostMessageToJSCallback(int64_t instance_id,
97                                scoped_ptr<base::Value> msg);
98
99   void SendSyncReplyToJSCallback(int64_t instance_id,
100                                  scoped_ptr<base::Value> reply);
101
102   void DeleteInstanceMap();
103
104   bool ValidateExtensionEntryPoints(const base::ListValue& entry_points);
105
106   base::Lock sender_lock_;
107   IPC::Sender* sender_;
108
109   typedef std::map<std::string, XWalkExtension*> ExtensionMap;
110   ExtensionMap extensions_;
111
112   typedef std::map<int64_t, InstanceExecutionData> InstanceMap;
113   InstanceMap instances_;
114
115   // The exported symbols for extensions already registered.
116   typedef std::set<std::string> ExtensionSymbolsSet;
117   ExtensionSymbolsSet extension_symbols_;
118
119   base::ProcessHandle renderer_process_handle_;
120
121   XWalkExtension::PermissionsDelegate* permissions_delegate_;
122 };
123
124 std::vector<std::string> RegisterExternalExtensionsInDirectory(
125     XWalkExtensionServer* server, const base::FilePath& dir,
126     scoped_ptr<base::ValueMap> runtime_variables);
127
128 bool ValidateExtensionNameForTesting(const std::string& extension_name);
129
130 }  // namespace extensions
131 }  // namespace xwalk
132
133 #endif  // XWALK_EXTENSIONS_COMMON_XWALK_EXTENSION_SERVER_H_