| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ppapi_plugin_process_host.h" | 5 #include "chrome/browser/ppapi_plugin_process_host.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 10 #include "chrome/browser/plugin_service.h" |
| 10 #include "chrome/browser/renderer_host/render_message_filter.h" | 11 #include "chrome/browser/renderer_host/render_message_filter.h" |
| 11 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
| 13 #include "ipc/ipc_switches.h" | 14 #include "ipc/ipc_switches.h" |
| 14 #include "ppapi/proxy/ppapi_messages.h" | 15 #include "ppapi/proxy/ppapi_messages.h" |
| 15 | 16 |
| 16 PpapiPluginProcessHost::PpapiPluginProcessHost(RenderMessageFilter* filter) | 17 PpapiPluginProcessHost::PpapiPluginProcessHost() |
| 17 : BrowserChildProcessHost(ChildProcessInfo::PPAPI_PLUGIN_PROCESS, | 18 : BrowserChildProcessHost( |
| 18 filter->resource_dispatcher_host()), | 19 ChildProcessInfo::PPAPI_PLUGIN_PROCESS, |
| 19 filter_(filter) { | 20 PluginService::GetInstance()->resource_dispatcher_host()) { |
| 20 } | 21 } |
| 21 | 22 |
| 22 PpapiPluginProcessHost::~PpapiPluginProcessHost() { | 23 PpapiPluginProcessHost::~PpapiPluginProcessHost() { |
| 24 CancelRequests(); |
| 23 } | 25 } |
| 24 | 26 |
| 25 void PpapiPluginProcessHost::Init(const FilePath& path, | 27 bool PpapiPluginProcessHost::Init(const FilePath& path) { |
| 26 IPC::Message* reply_msg) { | |
| 27 plugin_path_ = path; | 28 plugin_path_ = path; |
| 28 reply_msg_.reset(reply_msg); | |
| 29 | 29 |
| 30 if (!CreateChannel()) { | 30 if (!CreateChannel()) |
| 31 ReplyToRenderer(base::kNullProcessHandle, IPC::ChannelHandle()); | 31 return false; |
| 32 return; | |
| 33 } | |
| 34 | 32 |
| 35 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); | 33 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); |
| 36 CommandLine::StringType plugin_launcher = | 34 CommandLine::StringType plugin_launcher = |
| 37 browser_command_line.GetSwitchValueNative(switches::kPpapiPluginLauncher); | 35 browser_command_line.GetSwitchValueNative(switches::kPpapiPluginLauncher); |
| 38 | 36 |
| 39 FilePath exe_path = ChildProcessHost::GetChildPath(plugin_launcher.empty()); | 37 FilePath exe_path = ChildProcessHost::GetChildPath(plugin_launcher.empty()); |
| 40 if (exe_path.empty()) { | 38 if (exe_path.empty()) |
| 41 ReplyToRenderer(base::kNullProcessHandle, IPC::ChannelHandle()); | 39 return false; |
| 42 return; | |
| 43 } | |
| 44 | 40 |
| 45 CommandLine* cmd_line = new CommandLine(exe_path); | 41 CommandLine* cmd_line = new CommandLine(exe_path); |
| 46 cmd_line->AppendSwitchASCII(switches::kProcessType, | 42 cmd_line->AppendSwitchASCII(switches::kProcessType, |
| 47 switches::kPpapiPluginProcess); | 43 switches::kPpapiPluginProcess); |
| 48 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id()); | 44 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id()); |
| 49 | 45 |
| 50 if (!plugin_launcher.empty()) | 46 if (!plugin_launcher.empty()) |
| 51 cmd_line->PrependWrapper(plugin_launcher); | 47 cmd_line->PrependWrapper(plugin_launcher); |
| 52 | 48 |
| 53 // On posix, having a plugin launcher means we need to use another process | 49 // On posix, having a plugin launcher means we need to use another process |
| 54 // instead of just forking the zygote. | 50 // instead of just forking the zygote. |
| 55 Launch( | 51 Launch( |
| 56 #if defined(OS_WIN) | 52 #if defined(OS_WIN) |
| 57 FilePath(), | 53 FilePath(), |
| 58 #elif defined(OS_POSIX) | 54 #elif defined(OS_POSIX) |
| 59 plugin_launcher.empty(), | 55 plugin_launcher.empty(), |
| 60 base::environment_vector(), | 56 base::environment_vector(), |
| 61 #endif | 57 #endif |
| 62 cmd_line); | 58 cmd_line); |
| 59 return true; |
| 60 } |
| 61 |
| 62 void PpapiPluginProcessHost::OpenChannelToPlugin(Client* client) { |
| 63 if (opening_channel()) { |
| 64 // The channel is already in the process of being opened. Put |
| 65 // this "open channel" request into a queue of requests that will |
| 66 // be run once the channel is open. |
| 67 pending_requests_.push_back(client); |
| 68 return; |
| 69 } |
| 70 |
| 71 // We already have an open channel, send a request right away to plugin. |
| 72 RequestPluginChannel(client); |
| 73 } |
| 74 |
| 75 void PpapiPluginProcessHost::RequestPluginChannel(Client* client) { |
| 76 base::ProcessHandle process_handle; |
| 77 int renderer_id; |
| 78 client->GetChannelInfo(&process_handle, &renderer_id); |
| 79 |
| 80 // We can't send any sync messages from the browser because it might lead to |
| 81 // a hang. See the similar code in PluginProcessHost for more description. |
| 82 PpapiMsg_CreateChannel* msg = new PpapiMsg_CreateChannel(process_handle, |
| 83 renderer_id); |
| 84 msg->set_unblock(true); |
| 85 if (Send(msg)) |
| 86 sent_requests_.push(client); |
| 87 else |
| 88 client->OnChannelOpened(base::kNullProcessHandle, IPC::ChannelHandle()); |
| 63 } | 89 } |
| 64 | 90 |
| 65 bool PpapiPluginProcessHost::CanShutdown() { | 91 bool PpapiPluginProcessHost::CanShutdown() { |
| 66 return true; | 92 return true; |
| 67 } | 93 } |
| 68 | 94 |
| 69 void PpapiPluginProcessHost::OnProcessLaunched() { | 95 void PpapiPluginProcessHost::OnProcessLaunched() { |
| 70 } | 96 } |
| 71 | 97 |
| 72 bool PpapiPluginProcessHost::OnMessageReceived(const IPC::Message& msg) { | 98 bool PpapiPluginProcessHost::OnMessageReceived(const IPC::Message& msg) { |
| 73 bool handled = true; | 99 bool handled = true; |
| 74 IPC_BEGIN_MESSAGE_MAP(PpapiPluginProcessHost, msg) | 100 IPC_BEGIN_MESSAGE_MAP(PpapiPluginProcessHost, msg) |
| 75 IPC_MESSAGE_HANDLER(PpapiHostMsg_PluginLoaded, OnPluginLoaded) | 101 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated, |
| 102 OnRendererPluginChannelCreated) |
| 76 IPC_MESSAGE_UNHANDLED(handled = false) | 103 IPC_MESSAGE_UNHANDLED(handled = false) |
| 77 IPC_END_MESSAGE_MAP() | 104 IPC_END_MESSAGE_MAP() |
| 78 DCHECK(handled); | 105 DCHECK(handled); |
| 79 return handled; | 106 return handled; |
| 80 } | 107 } |
| 81 | 108 |
| 109 // Called when the browser <--> plugin channel has been established. |
| 82 void PpapiPluginProcessHost::OnChannelConnected(int32 peer_pid) { | 110 void PpapiPluginProcessHost::OnChannelConnected(int32 peer_pid) { |
| 83 #if defined(OS_WIN) | 111 // This will actually load the plugin. Errors will actually not be reported |
| 84 base::ProcessHandle plugins_renderer_handle = NULL; | 112 // back at this point. Instead, the plugin will fail to establish the |
| 85 ::DuplicateHandle(::GetCurrentProcess(), filter_->peer_handle(), | 113 // connections when we request them on behalf of the renderer(s). |
| 86 GetChildProcessHandle(), &plugins_renderer_handle, | 114 Send(new PpapiMsg_LoadPlugin(plugin_path_)); |
| 87 0, FALSE, DUPLICATE_SAME_ACCESS); | |
| 88 #elif defined(OS_POSIX) | |
| 89 base::ProcessHandle plugins_renderer_handle = filter_->peer_handle(); | |
| 90 #endif | |
| 91 | 115 |
| 92 PpapiMsg_LoadPlugin* msg = new PpapiMsg_LoadPlugin( | 116 // Process all pending channel requests from the renderers. |
| 93 plugins_renderer_handle, plugin_path_, filter_->render_process_id()); | 117 for (size_t i = 0; i < pending_requests_.size(); i++) |
| 94 if (!Send(msg)) // Just send an empty handle on failure. | 118 RequestPluginChannel(pending_requests_[i]); |
| 95 ReplyToRenderer(base::kNullProcessHandle, IPC::ChannelHandle()); | 119 pending_requests_.clear(); |
| 96 // This function will result in OnChannelCreated getting called to finish. | |
| 97 } | 120 } |
| 98 | 121 |
| 122 // Called when the browser <--> plugin channel has an error. This normally |
| 123 // means the plugin has crashed. |
| 99 void PpapiPluginProcessHost::OnChannelError() { | 124 void PpapiPluginProcessHost::OnChannelError() { |
| 100 if (reply_msg_.get()) | 125 // We don't need to notify the renderers that were communicating with the |
| 101 ReplyToRenderer(base::kNullProcessHandle, IPC::ChannelHandle()); | 126 // plugin since they have their own channels which will go into the error |
| 127 // state at the same time. Instead, we just need to notify any renderers |
| 128 // that have requested a connection but have not yet received one. |
| 129 CancelRequests(); |
| 102 } | 130 } |
| 103 | 131 |
| 104 void PpapiPluginProcessHost::OnPluginLoaded( | 132 void PpapiPluginProcessHost::CancelRequests() { |
| 133 for (size_t i = 0; i < pending_requests_.size(); i++) { |
| 134 pending_requests_[i]->OnChannelOpened(base::kNullProcessHandle, |
| 135 IPC::ChannelHandle()); |
| 136 } |
| 137 pending_requests_.clear(); |
| 138 |
| 139 while (!sent_requests_.empty()) { |
| 140 sent_requests_.front()->OnChannelOpened(base::kNullProcessHandle, |
| 141 IPC::ChannelHandle()); |
| 142 sent_requests_.pop(); |
| 143 } |
| 144 } |
| 145 |
| 146 // Called when a new plugin <--> renderer channel has been created. |
| 147 void PpapiPluginProcessHost::OnRendererPluginChannelCreated( |
| 105 const IPC::ChannelHandle& channel_handle) { | 148 const IPC::ChannelHandle& channel_handle) { |
| 149 if (sent_requests_.empty()) |
| 150 return; |
| 151 |
| 152 // All requests should be processed FIFO, so the next item in the |
| 153 // sent_requests_ queue should be the one that the plugin just created. |
| 154 Client* client = sent_requests_.front(); |
| 155 sent_requests_.pop(); |
| 156 |
| 157 // Prepare the handle to send to the renderer. |
| 106 base::ProcessHandle plugin_process = GetChildProcessHandle(); | 158 base::ProcessHandle plugin_process = GetChildProcessHandle(); |
| 107 #if defined(OS_WIN) | 159 #if defined(OS_WIN) |
| 160 base::ProcessHandle renderer_process; |
| 161 int renderer_id; |
| 162 client->GetChannelInfo(&renderer_process, &renderer_id); |
| 163 |
| 108 base::ProcessHandle renderers_plugin_handle = NULL; | 164 base::ProcessHandle renderers_plugin_handle = NULL; |
| 109 ::DuplicateHandle(::GetCurrentProcess(), plugin_process, | 165 ::DuplicateHandle(::GetCurrentProcess(), plugin_process, |
| 110 filter_->peer_handle(), &renderers_plugin_handle, | 166 renderer_process, &renderers_plugin_handle, |
| 111 0, FALSE, DUPLICATE_SAME_ACCESS); | 167 0, FALSE, DUPLICATE_SAME_ACCESS); |
| 112 #elif defined(OS_POSIX) | 168 #elif defined(OS_POSIX) |
| 113 // Don't need to duplicate anything on POSIX since it's just a PID. | 169 // Don't need to duplicate anything on POSIX since it's just a PID. |
| 114 base::ProcessHandle renderers_plugin_handle = plugin_process; | 170 base::ProcessHandle renderers_plugin_handle = plugin_process; |
| 115 #endif | 171 #endif |
| 116 ReplyToRenderer(renderers_plugin_handle, channel_handle); | 172 |
| 173 client->OnChannelOpened(renderers_plugin_handle, channel_handle); |
| 117 } | 174 } |
| 118 | |
| 119 void PpapiPluginProcessHost::ReplyToRenderer( | |
| 120 base::ProcessHandle plugin_handle, | |
| 121 const IPC::ChannelHandle& channel_handle) { | |
| 122 DCHECK(reply_msg_.get()); | |
| 123 ViewHostMsg_OpenChannelToPepperPlugin::WriteReplyParams(reply_msg_.get(), | |
| 124 plugin_handle, | |
| 125 channel_handle); | |
| 126 filter_->Send(reply_msg_.release()); | |
| 127 } | |
| OLD | NEW |