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 #include "xwalk/extensions/browser/xwalk_extension_process_host.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/files/file_path.h"
12 #include "content/public/browser/browser_child_process_host.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/common/child_process_host.h"
16 #include "content/public/common/process_type.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/common/sandboxed_process_launcher_delegate.h"
19 #include "ipc/ipc_message.h"
20 #include "ipc/ipc_switches.h"
21 #include "ipc/message_filter.h"
22 #include "xwalk/extensions/common/xwalk_extension_messages.h"
23 #include "xwalk/extensions/common/xwalk_extension_switches.h"
24 #include "xwalk/runtime/common/xwalk_switches.h"
26 using content::BrowserThread;
29 namespace extensions {
31 // This filter is used by ExtensionProcessHost to intercept when Render Process
32 // ask for the Extension Channel handle (that is created by extension process).
33 class XWalkExtensionProcessHost::RenderProcessMessageFilter
34 : public IPC::MessageFilter {
36 explicit RenderProcessMessageFilter(XWalkExtensionProcessHost* eph)
39 // This exists to fulfill the requirement for delayed reply handling, since it
40 // needs to send a message back if the parameters couldn't be correctly read
41 // from the original message received. See DispatchDealyReplyWithSendParams().
42 bool Send(IPC::Message* message) {
44 return eph_->render_process_host_->Send(message);
54 // IPC::ChannelProxy::MessageFilter implementation.
55 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
57 IPC_BEGIN_MESSAGE_MAP(RenderProcessMessageFilter, message)
58 IPC_MESSAGE_HANDLER_DELAY_REPLY(
59 XWalkExtensionProcessHostMsg_GetExtensionProcessChannel,
60 OnGetExtensionProcessChannel)
61 IPC_MESSAGE_UNHANDLED(handled = false)
66 void OnGetExtensionProcessChannel(IPC::Message* reply) {
67 scoped_ptr<IPC::Message> scoped_reply(reply);
69 eph_->OnGetExtensionProcessChannel(scoped_reply.Pass());
72 virtual ~RenderProcessMessageFilter() {}
74 XWalkExtensionProcessHost* eph_;
77 class ExtensionSandboxedProcessLauncherDelegate
78 : public content::SandboxedProcessLauncherDelegate {
80 explicit ExtensionSandboxedProcessLauncherDelegate(
81 content::ChildProcessHost* host)
83 : ipc_fd_(host->TakeClientFileDescriptor())
86 virtual ~ExtensionSandboxedProcessLauncherDelegate() {}
89 virtual bool ShouldSandbox() OVERRIDE {
92 #elif defined(OS_POSIX)
93 virtual int GetIpcFd() OVERRIDE {
103 DISALLOW_COPY_AND_ASSIGN(ExtensionSandboxedProcessLauncherDelegate);
106 bool XWalkExtensionProcessHost::Delegate::OnRegisterPermissions(
107 int render_process_id,
108 const std::string& extension_name,
109 const std::string& perm_table) {
113 XWalkExtensionProcessHost::XWalkExtensionProcessHost(
114 content::RenderProcessHost* render_process_host,
115 const base::FilePath& external_extensions_path,
116 XWalkExtensionProcessHost::Delegate* delegate,
117 const base::ValueMap& runtime_variables)
118 : ep_rp_channel_handle_(""),
119 render_process_host_(render_process_host),
120 render_process_message_filter_(new RenderProcessMessageFilter(this)),
121 external_extensions_path_(external_extensions_path),
122 is_extension_process_channel_ready_(false),
124 runtime_variables_(runtime_variables) {
125 render_process_host_->GetChannel()->AddFilter(render_process_message_filter_);
126 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
127 base::Bind(&XWalkExtensionProcessHost::StartProcess,
128 base::Unretained(this)));
131 XWalkExtensionProcessHost::~XWalkExtensionProcessHost() {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
133 render_process_message_filter_->Invalidate();
139 void ToListValue(base::ValueMap* vm, base::ListValue* lv) {
142 for (base::ValueMap::iterator it = vm->begin(); it != vm->end(); it++) {
143 base::DictionaryValue* dv = new base::DictionaryValue();
144 dv->Set(it->first, it->second);
151 void XWalkExtensionProcessHost::StartProcess() {
152 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
153 CHECK(!process_ || !channel_);
155 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
156 if (cmd_line->HasSwitch(switches::kXWalkRunAsService)) {
157 #if defined(OS_LINUX)
158 std::string channel_id =
159 IPC::Channel::GenerateVerifiedChannelID(std::string());
160 channel_.reset(new IPC::Channel(
161 channel_id, IPC::Channel::MODE_SERVER, this));
162 if (!channel_->Connect())
164 IPC::ChannelHandle channel_handle(channel_id,
165 base::FileDescriptor(channel_->TakeClientFileDescriptor(), true));
166 BrowserThread::PostTask(
167 BrowserThread::UI, FROM_HERE,
169 &XWalkExtensionProcessHost::Delegate::OnExtensionProcessCreated,
170 base::Unretained(delegate_), render_process_host_->GetID(),
176 process_.reset(content::BrowserChildProcessHost::Create(
177 content::PROCESS_TYPE_CONTENT_END, this));
179 std::string channel_id = process_->GetHost()->CreateChannel();
180 CHECK(!channel_id.empty());
182 CommandLine::StringType extension_cmd_prefix;
183 #if defined(OS_POSIX)
184 const CommandLine &browser_command_line = *CommandLine::ForCurrentProcess();
185 extension_cmd_prefix = browser_command_line.GetSwitchValueNative(
186 switches::kXWalkExtensionCmdPrefix);
189 #if defined(OS_LINUX)
190 int flags = extension_cmd_prefix.empty() ?
191 content::ChildProcessHost::CHILD_ALLOW_SELF :
192 content::ChildProcessHost::CHILD_NORMAL;
194 int flags = content::ChildProcessHost::CHILD_NORMAL;
197 base::FilePath exe_path = content::ChildProcessHost::GetChildPath(flags);
198 if (exe_path.empty())
201 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
202 cmd_line->AppendSwitchASCII(switches::kProcessType,
203 switches::kXWalkExtensionProcess);
204 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
205 if (!extension_cmd_prefix.empty())
206 cmd_line->PrependWrapper(extension_cmd_prefix);
209 new ExtensionSandboxedProcessLauncherDelegate(process_->GetHost()),
213 base::ListValue runtime_variables_lv;
214 ToListValue(&const_cast<base::ValueMap&>(runtime_variables_),
215 &runtime_variables_lv);
216 Send(new XWalkExtensionProcessMsg_RegisterExtensions(
217 external_extensions_path_, runtime_variables_lv));
220 void XWalkExtensionProcessHost::StopProcess() {
227 void XWalkExtensionProcessHost::OnGetExtensionProcessChannel(
228 scoped_ptr<IPC::Message> reply) {
229 pending_reply_for_render_process_ = reply.Pass();
230 ReplyChannelHandleToRenderProcess();
233 bool XWalkExtensionProcessHost::OnMessageReceived(const IPC::Message& message) {
235 IPC_BEGIN_MESSAGE_MAP(XWalkExtensionProcessHost, message)
237 XWalkExtensionProcessHostMsg_RenderProcessChannelCreated,
238 OnRenderChannelCreated)
239 IPC_MESSAGE_HANDLER_DELAY_REPLY(
240 XWalkExtensionProcessHostMsg_CheckAPIAccessControl,
241 OnCheckAPIAccessControl)
243 XWalkExtensionProcessHostMsg_RegisterPermissions,
244 OnRegisterPermissions)
245 IPC_MESSAGE_UNHANDLED(handled = false)
246 IPC_END_MESSAGE_MAP()
250 void XWalkExtensionProcessHost::OnChannelError() {
251 // This function is called just before
252 // BrowserChildProcessHostImpl::OnChildDisconnected gets called. Please refer
253 // to ChildProcessHostImpl::OnChannelError() from child_process_host_impl.cc.
254 // This means that content::BrowserChildProcessHost (process_, in our case)
255 // is about to delete its delegate, which is us!
256 // We should alert our XWalkExtensionProcessHost::Delegate, since it will
257 // most likely have a pointer to us that needs to be invalidated.
259 VLOG(1) << "\n\nExtensionProcess crashed";
261 delegate_->OnExtensionProcessDied(this, render_process_host_->GetID());
264 void XWalkExtensionProcessHost::OnProcessLaunched() {
265 VLOG(1) << "\n\nExtensionProcess was started!";
268 void XWalkExtensionProcessHost::OnRenderChannelCreated(
269 const IPC::ChannelHandle& handle) {
270 is_extension_process_channel_ready_ = true;
271 ep_rp_channel_handle_ = handle;
272 ReplyChannelHandleToRenderProcess();
275 void XWalkExtensionProcessHost::ReplyChannelHandleToRenderProcess() {
276 // Replying the channel handle to RP depends on two events:
277 // - EP already notified EPH that new channel was created (for RP<->EP).
278 // - RP already asked for the channel handle.
280 // The order for this events is not determined, so we call this function from
281 // both, and the second execution will send the reply.
282 if (!is_extension_process_channel_ready_
283 || !pending_reply_for_render_process_)
286 XWalkExtensionProcessHostMsg_GetExtensionProcessChannel::WriteReplyParams(
287 pending_reply_for_render_process_.get(), ep_rp_channel_handle_);
289 render_process_host_->Send(pending_reply_for_render_process_.release());
292 void XWalkExtensionProcessHost::ReplyAccessControlToExtension(
293 IPC::Message* reply_msg,
294 RuntimePermission perm) {
295 XWalkExtensionProcessHostMsg_CheckAPIAccessControl
296 ::WriteReplyParams(reply_msg, perm);
300 void XWalkExtensionProcessHost::OnCheckAPIAccessControl(
301 const std::string& extension_name,
302 const std::string& api_name, IPC::Message* reply_msg) {
304 delegate_->OnCheckAPIAccessControl(render_process_host_->GetID(),
305 extension_name, api_name,
306 base::Bind(&XWalkExtensionProcessHost::ReplyAccessControlToExtension,
307 base::Unretained(this),
311 void XWalkExtensionProcessHost::OnRegisterPermissions(
312 const std::string& extension_name,
313 const std::string& perm_table, bool* result) {
315 *result = delegate_->OnRegisterPermissions(
316 render_process_host_->GetID(), extension_name, perm_table);
319 bool XWalkExtensionProcessHost::Send(IPC::Message* msg) {
321 return process_->GetHost()->Send(msg);
323 return channel_->Send(msg);
327 } // namespace extensions