Upstream version 11.39.244.0
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / browser / xwalk_extension_process_host.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_BROWSER_XWALK_EXTENSION_PROCESS_HOST_H_
6 #define XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_PROCESS_HOST_H_
7
8 #include <string>
9
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
14 #include "content/public/browser/browser_child_process_host_delegate.h"
15 #include "ipc/ipc_channel_handle.h"
16 #include "ipc/ipc_channel_proxy.h"
17 #include "ipc/ipc_sender.h"
18 #include "xwalk/extensions/common/xwalk_extension_permission_types.h"
19
20 namespace content {
21 class BrowserChildProcessHost;
22 class RenderProcessHost;
23 }
24
25 namespace xwalk {
26 namespace extensions {
27
28 // This class represents the browser side of the browser <-> extension process
29 // communication channel. It has to run some operations in IO thread for
30 // creating the extra process.
31 class XWalkExtensionProcessHost
32     : public content::BrowserChildProcessHostDelegate,
33       public IPC::Sender {
34  public:
35   class Delegate {
36    public:
37     virtual void OnExtensionProcessDied(XWalkExtensionProcessHost* eph,
38         int render_process_id) {}
39     virtual void OnExtensionProcessCreated(int render_process_id,
40                                            const IPC::ChannelHandle handle) {}
41     virtual void OnCheckAPIAccessControl(int render_process_id,
42                                          const std::string& extension_name,
43                                          const std::string& api_name,
44                                          const PermissionCallback& callback) {}
45     virtual bool OnRegisterPermissions(int render_process_id,
46                                        const std::string& extension_name,
47                                        const std::string& perm_table);
48     virtual void OnRenderChannelCreated(int render_process_id) {}
49
50    protected:
51     ~Delegate() {}
52   };
53
54   XWalkExtensionProcessHost(content::RenderProcessHost* render_process_host,
55                             const base::FilePath& external_extensions_path,
56                             XWalkExtensionProcessHost::Delegate* delegate,
57                             scoped_ptr<base::ValueMap> runtime_variables);
58   virtual ~XWalkExtensionProcessHost();
59
60   // IPC::Sender implementation
61   virtual bool Send(IPC::Message* msg) OVERRIDE;
62
63  private:
64   class RenderProcessMessageFilter;
65
66   void StartProcess();
67   void StopProcess();
68
69   // Handler for message from Render Process host, it is a synchronous message,
70   // that will be replied only when the extension process channel is created.
71   void OnGetExtensionProcessChannel(scoped_ptr<IPC::Message> reply);
72
73   // content::BrowserChildProcessHostDelegate implementation.
74   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
75   virtual void OnChannelError() OVERRIDE;
76   virtual void OnProcessLaunched() OVERRIDE;
77
78   // Message Handlers.
79   void OnRenderChannelCreated(const IPC::ChannelHandle& channel_id);
80
81   void ReplyChannelHandleToRenderProcess();
82
83   void OnCheckAPIAccessControl(const std::string& extension_name,
84       const std::string& api_name, IPC::Message* reply_msg);
85   void ReplyAccessControlToExtension(IPC::Message* reply_msg,
86       RuntimePermission perm);
87   void OnRegisterPermissions(const std::string& extension_name,
88       const std::string& perm_table, bool* result);
89
90   scoped_ptr<content::BrowserChildProcessHost> process_;
91   IPC::ChannelHandle ep_rp_channel_handle_;
92   content::RenderProcessHost* render_process_host_;
93   scoped_ptr<IPC::Message> pending_reply_for_render_process_;
94
95   // We use this filter to know when RP asked for the extension process channel.
96   // We keep the reference to invalidate the filter once we don't need it
97   // anymore.
98   //
99   // TODO(cmarcelo): Avoid having an extra filter, see if we can embed this
100   // handling in the existing filter we have in ExtensionData struct.
101   scoped_refptr<RenderProcessMessageFilter> render_process_message_filter_;
102
103   base::FilePath external_extensions_path_;
104
105   bool is_extension_process_channel_ready_;
106
107   XWalkExtensionProcessHost::Delegate* delegate_;
108
109   scoped_ptr<base::ValueMap> runtime_variables_;
110
111   // IPC channel for launcher to communicate with BP in service mode.
112   scoped_ptr<IPC::Channel> channel_;
113 };
114
115 }  // namespace extensions
116 }  // namespace xwalk
117
118 #endif  // XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_PROCESS_HOST_H_