Removed unused VideoCaptureCapability parameters.
[chromium-blink-merge.git] / content / plugin / plugin_channel.cc
blobfb2401c11c230174e4e1864f9703e9a3e35fecac
1 // Copyright (c) 2012 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.
5 #include "content/plugin/plugin_channel.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/process/process_handle.h"
10 #include "base/strings/string_util.h"
11 #include "base/synchronization/lock.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "build/build_config.h"
14 #include "content/child/child_process.h"
15 #include "content/child/npapi/plugin_instance.h"
16 #include "content/child/npapi/webplugin_delegate_impl.h"
17 #include "content/child/plugin_messages.h"
18 #include "content/common/plugin_process_messages.h"
19 #include "content/plugin/plugin_thread.h"
20 #include "content/plugin/webplugin_delegate_stub.h"
21 #include "content/plugin/webplugin_proxy.h"
22 #include "content/public/common/content_switches.h"
23 #include "third_party/WebKit/public/web/WebBindings.h"
25 #if defined(OS_POSIX)
26 #include "base/posix/eintr_wrapper.h"
27 #include "ipc/ipc_channel_posix.h"
28 #endif
30 using WebKit::WebBindings;
32 namespace content {
34 namespace {
36 void PluginReleaseCallback() {
37 ChildProcess::current()->ReleaseProcess();
40 // How long we wait before releasing the plugin process.
41 const int kPluginReleaseTimeMinutes = 5;
43 } // namespace
45 // If a sync call to the renderer results in a modal dialog, we need to have a
46 // way to know so that we can run a nested message loop to simulate what would
47 // happen in a single process browser and avoid deadlock.
48 class PluginChannel::MessageFilter : public IPC::ChannelProxy::MessageFilter {
49 public:
50 MessageFilter() : channel_(NULL) { }
52 base::WaitableEvent* GetModalDialogEvent(int render_view_id) {
53 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
54 if (!modal_dialog_event_map_.count(render_view_id)) {
55 NOTREACHED();
56 return NULL;
59 return modal_dialog_event_map_[render_view_id].event;
62 // Decrement the ref count associated with the modal dialog event for the
63 // given tab.
64 void ReleaseModalDialogEvent(int render_view_id) {
65 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
66 if (!modal_dialog_event_map_.count(render_view_id)) {
67 NOTREACHED();
68 return;
71 if (--(modal_dialog_event_map_[render_view_id].refcount))
72 return;
74 // Delete the event when the stack unwinds as it could be in use now.
75 base::MessageLoop::current()->DeleteSoon(
76 FROM_HERE, modal_dialog_event_map_[render_view_id].event);
77 modal_dialog_event_map_.erase(render_view_id);
80 bool Send(IPC::Message* message) {
81 // Need this function for the IPC_MESSAGE_HANDLER_DELAY_REPLY macro.
82 return channel_->Send(message);
85 // IPC::ChannelProxy::MessageFilter:
86 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE {
87 channel_ = channel;
90 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
91 IPC_BEGIN_MESSAGE_MAP(PluginChannel::MessageFilter, message)
92 IPC_MESSAGE_HANDLER_DELAY_REPLY(PluginMsg_Init, OnInit)
93 IPC_MESSAGE_HANDLER(PluginMsg_SignalModalDialogEvent,
94 OnSignalModalDialogEvent)
95 IPC_MESSAGE_HANDLER(PluginMsg_ResetModalDialogEvent,
96 OnResetModalDialogEvent)
97 IPC_END_MESSAGE_MAP()
98 return message.type() == PluginMsg_SignalModalDialogEvent::ID ||
99 message.type() == PluginMsg_ResetModalDialogEvent::ID;
102 protected:
103 virtual ~MessageFilter() {
104 // Clean up in case of renderer crash.
105 for (ModalDialogEventMap::iterator i = modal_dialog_event_map_.begin();
106 i != modal_dialog_event_map_.end(); ++i) {
107 delete i->second.event;
111 private:
112 void OnInit(const PluginMsg_Init_Params& params, IPC::Message* reply_msg) {
113 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
114 if (modal_dialog_event_map_.count(params.host_render_view_routing_id)) {
115 modal_dialog_event_map_[params.host_render_view_routing_id].refcount++;
116 return;
119 WaitableEventWrapper wrapper;
120 wrapper.event = new base::WaitableEvent(true, false);
121 wrapper.refcount = 1;
122 modal_dialog_event_map_[params.host_render_view_routing_id] = wrapper;
125 void OnSignalModalDialogEvent(int render_view_id) {
126 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
127 if (modal_dialog_event_map_.count(render_view_id))
128 modal_dialog_event_map_[render_view_id].event->Signal();
131 void OnResetModalDialogEvent(int render_view_id) {
132 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
133 if (modal_dialog_event_map_.count(render_view_id))
134 modal_dialog_event_map_[render_view_id].event->Reset();
137 struct WaitableEventWrapper {
138 base::WaitableEvent* event;
139 int refcount; // There could be multiple plugin instances per tab.
141 typedef std::map<int, WaitableEventWrapper> ModalDialogEventMap;
142 ModalDialogEventMap modal_dialog_event_map_;
143 base::Lock modal_dialog_event_map_lock_;
145 IPC::Channel* channel_;
148 PluginChannel* PluginChannel::GetPluginChannel(
149 int renderer_id, base::MessageLoopProxy* ipc_message_loop) {
150 // Map renderer ID to a (single) channel to that process.
151 std::string channel_key = base::StringPrintf(
152 "%d.r%d", base::GetCurrentProcId(), renderer_id);
154 PluginChannel* channel =
155 static_cast<PluginChannel*>(NPChannelBase::GetChannel(
156 channel_key,
157 IPC::Channel::MODE_SERVER,
158 ClassFactory,
159 ipc_message_loop,
160 false,
161 ChildProcess::current()->GetShutDownEvent()));
163 if (channel)
164 channel->renderer_id_ = renderer_id;
166 return channel;
169 // static
170 void PluginChannel::NotifyRenderersOfPendingShutdown() {
171 Broadcast(new PluginHostMsg_PluginShuttingDown());
174 bool PluginChannel::Send(IPC::Message* msg) {
175 in_send_++;
176 if (log_messages_) {
177 VLOG(1) << "sending message @" << msg << " on channel @" << this
178 << " with type " << msg->type();
180 bool result = NPChannelBase::Send(msg);
181 in_send_--;
182 return result;
185 bool PluginChannel::OnMessageReceived(const IPC::Message& msg) {
186 if (log_messages_) {
187 VLOG(1) << "received message @" << &msg << " on channel @" << this
188 << " with type " << msg.type();
190 return NPChannelBase::OnMessageReceived(msg);
193 void PluginChannel::OnChannelError() {
194 NPChannelBase::OnChannelError();
195 CleanUp();
198 int PluginChannel::GenerateRouteID() {
199 static int last_id = 0;
200 return ++last_id;
203 base::WaitableEvent* PluginChannel::GetModalDialogEvent(int render_view_id) {
204 return filter_->GetModalDialogEvent(render_view_id);
207 PluginChannel::~PluginChannel() {
208 PluginThread::current()->Send(new PluginProcessHostMsg_ChannelDestroyed(
209 renderer_id_));
210 base::MessageLoop::current()->PostDelayedTask(
211 FROM_HERE,
212 base::Bind(&PluginReleaseCallback),
213 base::TimeDelta::FromMinutes(kPluginReleaseTimeMinutes));
216 void PluginChannel::CleanUp() {
217 // We need to clean up the stubs so that they call NPPDestroy. This will
218 // also lead to them releasing their reference on this object so that it can
219 // be deleted.
220 for (size_t i = 0; i < plugin_stubs_.size(); ++i)
221 RemoveRoute(plugin_stubs_[i]->instance_id());
223 // Need to addref this object temporarily because otherwise removing the last
224 // stub will cause the destructor of this object to be called, however at
225 // that point plugin_stubs_ will have one element and its destructor will be
226 // called twice.
227 scoped_refptr<PluginChannel> me(this);
229 plugin_stubs_.clear();
232 bool PluginChannel::Init(base::MessageLoopProxy* ipc_message_loop,
233 bool create_pipe_now,
234 base::WaitableEvent* shutdown_event) {
235 if (!NPChannelBase::Init(ipc_message_loop, create_pipe_now, shutdown_event))
236 return false;
238 channel_->AddFilter(filter_.get());
239 return true;
242 PluginChannel::PluginChannel()
243 : renderer_id_(-1),
244 in_send_(0),
245 incognito_(false),
246 filter_(new MessageFilter()),
247 npp_(new struct _NPP) {
248 set_send_unblocking_only_during_unblock_dispatch();
249 ChildProcess::current()->AddRefProcess();
250 const CommandLine* command_line = CommandLine::ForCurrentProcess();
251 log_messages_ = command_line->HasSwitch(switches::kLogPluginMessages);
253 // Register |npp_| as the default owner for any object we receive via IPC,
254 // and register it with WebBindings as a valid owner.
255 SetDefaultNPObjectOwner(npp_.get());
256 WebBindings::registerObjectOwner(npp_.get());
259 bool PluginChannel::OnControlMessageReceived(const IPC::Message& msg) {
260 bool handled = true;
261 IPC_BEGIN_MESSAGE_MAP(PluginChannel, msg)
262 IPC_MESSAGE_HANDLER(PluginMsg_CreateInstance, OnCreateInstance)
263 IPC_MESSAGE_HANDLER_DELAY_REPLY(PluginMsg_DestroyInstance,
264 OnDestroyInstance)
265 IPC_MESSAGE_HANDLER(PluginMsg_GenerateRouteID, OnGenerateRouteID)
266 IPC_MESSAGE_HANDLER(PluginProcessMsg_ClearSiteData, OnClearSiteData)
267 IPC_MESSAGE_HANDLER(PluginHostMsg_DidAbortLoading, OnDidAbortLoading)
268 IPC_MESSAGE_UNHANDLED(handled = false)
269 IPC_END_MESSAGE_MAP()
270 DCHECK(handled);
271 return handled;
274 void PluginChannel::OnCreateInstance(const std::string& mime_type,
275 int* instance_id) {
276 *instance_id = GenerateRouteID();
277 scoped_refptr<WebPluginDelegateStub> stub(new WebPluginDelegateStub(
278 mime_type, *instance_id, this));
279 AddRoute(*instance_id, stub.get(), NULL);
280 plugin_stubs_.push_back(stub);
283 void PluginChannel::OnDestroyInstance(int instance_id,
284 IPC::Message* reply_msg) {
285 for (size_t i = 0; i < plugin_stubs_.size(); ++i) {
286 if (plugin_stubs_[i]->instance_id() == instance_id) {
287 scoped_refptr<MessageFilter> filter(filter_);
288 int render_view_id =
289 plugin_stubs_[i]->webplugin()->host_render_view_routing_id();
290 plugin_stubs_.erase(plugin_stubs_.begin() + i);
291 Send(reply_msg);
292 RemoveRoute(instance_id);
293 // NOTE: *this* might be deleted as a result of calling RemoveRoute.
294 // Don't release the modal dialog event right away, but do it after the
295 // stack unwinds since the plugin can be destroyed later if it's in use
296 // right now.
297 base::MessageLoop::current()->PostNonNestableTask(
298 FROM_HERE,
299 base::Bind(&MessageFilter::ReleaseModalDialogEvent,
300 filter.get(),
301 render_view_id));
302 return;
306 NOTREACHED() << "Couldn't find WebPluginDelegateStub to destroy";
309 void PluginChannel::OnGenerateRouteID(int* route_id) {
310 *route_id = GenerateRouteID();
313 void PluginChannel::OnClearSiteData(const std::string& site,
314 uint64 flags,
315 uint64 max_age) {
316 bool success = false;
317 CommandLine* command_line = CommandLine::ForCurrentProcess();
318 base::FilePath path = command_line->GetSwitchValuePath(switches::kPluginPath);
319 scoped_refptr<PluginLib> plugin_lib(PluginLib::CreatePluginLib(path));
320 if (plugin_lib.get()) {
321 NPError err = plugin_lib->NP_Initialize();
322 if (err == NPERR_NO_ERROR) {
323 const char* site_str = site.empty() ? NULL : site.c_str();
324 err = plugin_lib->NP_ClearSiteData(site_str, flags, max_age);
325 std::string site_name =
326 site.empty() ? "NULL"
327 : base::StringPrintf("\"%s\"", site_str);
328 VLOG(1) << "NPP_ClearSiteData(" << site_name << ", " << flags << ", "
329 << max_age << ") returned " << err;
330 success = (err == NPERR_NO_ERROR);
333 Send(new PluginProcessHostMsg_ClearSiteDataResult(success));
336 void PluginChannel::OnDidAbortLoading(int render_view_id) {
337 for (size_t i = 0; i < plugin_stubs_.size(); ++i) {
338 if (plugin_stubs_[i]->webplugin()->host_render_view_routing_id() ==
339 render_view_id) {
340 plugin_stubs_[i]->delegate()->instance()->CloseStreams();
345 } // namespace content