OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ppapi_plugin_process_host.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/file_path.h" | |
9 #include "base/process_util.h" | |
10 #include "chrome/browser/plugin_service.h" | |
11 #include "chrome/browser/renderer_host/render_message_filter.h" | |
12 #include "chrome/common/chrome_switches.h" | |
13 #include "chrome/common/render_messages.h" | |
14 #include "ipc/ipc_switches.h" | |
15 #include "ppapi/proxy/ppapi_messages.h" | |
16 | |
17 PpapiPluginProcessHost::PpapiPluginProcessHost() | |
18 : BrowserChildProcessHost( | |
19 ChildProcessInfo::PPAPI_PLUGIN_PROCESS, | |
20 PluginService::GetInstance()->resource_dispatcher_host()) { | |
21 } | |
22 | |
23 PpapiPluginProcessHost::~PpapiPluginProcessHost() { | |
24 CancelRequests(); | |
25 } | |
26 | |
27 bool PpapiPluginProcessHost::Init(const FilePath& path) { | |
28 plugin_path_ = path; | |
29 | |
30 if (!CreateChannel()) | |
31 return false; | |
32 | |
33 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); | |
34 CommandLine::StringType plugin_launcher = | |
35 browser_command_line.GetSwitchValueNative(switches::kPpapiPluginLauncher); | |
36 | |
37 FilePath exe_path = ChildProcessHost::GetChildPath(plugin_launcher.empty()); | |
38 if (exe_path.empty()) | |
39 return false; | |
40 | |
41 CommandLine* cmd_line = new CommandLine(exe_path); | |
42 cmd_line->AppendSwitchASCII(switches::kProcessType, | |
43 switches::kPpapiPluginProcess); | |
44 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id()); | |
45 | |
46 if (!plugin_launcher.empty()) | |
47 cmd_line->PrependWrapper(plugin_launcher); | |
48 | |
49 // On posix, having a plugin launcher means we need to use another process | |
50 // instead of just forking the zygote. | |
51 Launch( | |
52 #if defined(OS_WIN) | |
53 FilePath(), | |
54 #elif defined(OS_POSIX) | |
55 plugin_launcher.empty(), | |
56 base::environment_vector(), | |
57 #endif | |
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()); | |
89 } | |
90 | |
91 bool PpapiPluginProcessHost::CanShutdown() { | |
92 return true; | |
93 } | |
94 | |
95 void PpapiPluginProcessHost::OnProcessLaunched() { | |
96 } | |
97 | |
98 bool PpapiPluginProcessHost::OnMessageReceived(const IPC::Message& msg) { | |
99 bool handled = true; | |
100 IPC_BEGIN_MESSAGE_MAP(PpapiPluginProcessHost, msg) | |
101 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated, | |
102 OnRendererPluginChannelCreated) | |
103 IPC_MESSAGE_UNHANDLED(handled = false) | |
104 IPC_END_MESSAGE_MAP() | |
105 DCHECK(handled); | |
106 return handled; | |
107 } | |
108 | |
109 // Called when the browser <--> plugin channel has been established. | |
110 void PpapiPluginProcessHost::OnChannelConnected(int32 peer_pid) { | |
111 // This will actually load the plugin. Errors will actually not be reported | |
112 // back at this point. Instead, the plugin will fail to establish the | |
113 // connections when we request them on behalf of the renderer(s). | |
114 Send(new PpapiMsg_LoadPlugin(plugin_path_)); | |
115 | |
116 // Process all pending channel requests from the renderers. | |
117 for (size_t i = 0; i < pending_requests_.size(); i++) | |
118 RequestPluginChannel(pending_requests_[i]); | |
119 pending_requests_.clear(); | |
120 } | |
121 | |
122 // Called when the browser <--> plugin channel has an error. This normally | |
123 // means the plugin has crashed. | |
124 void PpapiPluginProcessHost::OnChannelError() { | |
125 // We don't need to notify the renderers that were communicating with the | |
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(); | |
130 } | |
131 | |
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( | |
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. | |
158 base::ProcessHandle plugin_process = GetChildProcessHandle(); | |
159 #if defined(OS_WIN) | |
160 base::ProcessHandle renderer_process; | |
161 int renderer_id; | |
162 client->GetChannelInfo(&renderer_process, &renderer_id); | |
163 | |
164 base::ProcessHandle renderers_plugin_handle = NULL; | |
165 ::DuplicateHandle(::GetCurrentProcess(), plugin_process, | |
166 renderer_process, &renderers_plugin_handle, | |
167 0, FALSE, DUPLICATE_SAME_ACCESS); | |
168 #elif defined(OS_POSIX) | |
169 // Don't need to duplicate anything on POSIX since it's just a PID. | |
170 base::ProcessHandle renderers_plugin_handle = plugin_process; | |
171 #endif | |
172 | |
173 client->OnChannelOpened(renderers_plugin_handle, channel_handle); | |
174 } | |
OLD | NEW |