[content shell] hook up testRunner.dumpEditingCallbacks
[chromium-blink-merge.git] / content / plugin / plugin_channel.cc
blobc579ea9da94bf6c9a3cba336a09c1a3e0b3f36a8
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_util.h"
10 #include "base/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/common/child_process.h"
15 #include "content/common/plugin_messages.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/plugin/plugin_thread.h"
18 #include "content/plugin/webplugin_delegate_stub.h"
19 #include "content/plugin/webplugin_proxy.h"
20 #include "webkit/plugins/npapi/plugin_instance.h"
22 #if defined(OS_POSIX)
23 #include "base/posix/eintr_wrapper.h"
24 #include "ipc/ipc_channel_posix.h"
25 #endif
27 namespace content {
29 namespace {
31 void PluginReleaseCallback() {
32 ChildProcess::current()->ReleaseProcess();
35 // How long we wait before releasing the plugin process.
36 const int kPluginReleaseTimeMinutes = 5;
38 } // namespace
40 // If a sync call to the renderer results in a modal dialog, we need to have a
41 // way to know so that we can run a nested message loop to simulate what would
42 // happen in a single process browser and avoid deadlock.
43 class PluginChannel::MessageFilter : public IPC::ChannelProxy::MessageFilter {
44 public:
45 MessageFilter() : channel_(NULL) { }
47 base::WaitableEvent* GetModalDialogEvent(int render_view_id) {
48 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
49 if (!modal_dialog_event_map_.count(render_view_id)) {
50 NOTREACHED();
51 return NULL;
54 return modal_dialog_event_map_[render_view_id].event;
57 // Decrement the ref count associated with the modal dialog event for the
58 // given tab.
59 void ReleaseModalDialogEvent(int render_view_id) {
60 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
61 if (!modal_dialog_event_map_.count(render_view_id)) {
62 NOTREACHED();
63 return;
66 if (--(modal_dialog_event_map_[render_view_id].refcount))
67 return;
69 // Delete the event when the stack unwinds as it could be in use now.
70 MessageLoop::current()->DeleteSoon(
71 FROM_HERE, modal_dialog_event_map_[render_view_id].event);
72 modal_dialog_event_map_.erase(render_view_id);
75 bool Send(IPC::Message* message) {
76 // Need this function for the IPC_MESSAGE_HANDLER_DELAY_REPLY macro.
77 return channel_->Send(message);
80 // IPC::ChannelProxy::MessageFilter:
81 void OnFilterAdded(IPC::Channel* channel) { channel_ = channel; }
83 bool OnMessageReceived(const IPC::Message& message) {
84 IPC_BEGIN_MESSAGE_MAP(PluginChannel::MessageFilter, message)
85 IPC_MESSAGE_HANDLER_DELAY_REPLY(PluginMsg_Init, OnInit)
86 IPC_MESSAGE_HANDLER(PluginMsg_SignalModalDialogEvent,
87 OnSignalModalDialogEvent)
88 IPC_MESSAGE_HANDLER(PluginMsg_ResetModalDialogEvent,
89 OnResetModalDialogEvent)
90 IPC_END_MESSAGE_MAP()
91 return message.type() == PluginMsg_SignalModalDialogEvent::ID ||
92 message.type() == PluginMsg_ResetModalDialogEvent::ID;
95 protected:
96 virtual ~MessageFilter() {
97 // Clean up in case of renderer crash.
98 for (ModalDialogEventMap::iterator i = modal_dialog_event_map_.begin();
99 i != modal_dialog_event_map_.end(); ++i) {
100 delete i->second.event;
104 private:
105 void OnInit(const PluginMsg_Init_Params& params, IPC::Message* reply_msg) {
106 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
107 if (modal_dialog_event_map_.count(params.host_render_view_routing_id)) {
108 modal_dialog_event_map_[params.host_render_view_routing_id].refcount++;
109 return;
112 WaitableEventWrapper wrapper;
113 wrapper.event = new base::WaitableEvent(true, false);
114 wrapper.refcount = 1;
115 modal_dialog_event_map_[params.host_render_view_routing_id] = wrapper;
118 void OnSignalModalDialogEvent(int render_view_id) {
119 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
120 if (modal_dialog_event_map_.count(render_view_id))
121 modal_dialog_event_map_[render_view_id].event->Signal();
124 void OnResetModalDialogEvent(int render_view_id) {
125 base::AutoLock auto_lock(modal_dialog_event_map_lock_);
126 if (modal_dialog_event_map_.count(render_view_id))
127 modal_dialog_event_map_[render_view_id].event->Reset();
130 struct WaitableEventWrapper {
131 base::WaitableEvent* event;
132 int refcount; // There could be multiple plugin instances per tab.
134 typedef std::map<int, WaitableEventWrapper> ModalDialogEventMap;
135 ModalDialogEventMap modal_dialog_event_map_;
136 base::Lock modal_dialog_event_map_lock_;
138 IPC::Channel* channel_;
141 PluginChannel* PluginChannel::GetPluginChannel(
142 int renderer_id, base::MessageLoopProxy* ipc_message_loop) {
143 // Map renderer ID to a (single) channel to that process.
144 std::string channel_key = StringPrintf(
145 "%d.r%d", base::GetCurrentProcId(), renderer_id);
147 PluginChannel* channel =
148 static_cast<PluginChannel*>(NPChannelBase::GetChannel(
149 channel_key,
150 IPC::Channel::MODE_SERVER,
151 ClassFactory,
152 ipc_message_loop,
153 false,
154 ChildProcess::current()->GetShutDownEvent()));
156 if (channel)
157 channel->renderer_id_ = renderer_id;
159 return channel;
162 // static
163 void PluginChannel::NotifyRenderersOfPendingShutdown() {
164 Broadcast(new PluginHostMsg_PluginShuttingDown());
167 bool PluginChannel::Send(IPC::Message* msg) {
168 in_send_++;
169 if (log_messages_) {
170 VLOG(1) << "sending message @" << msg << " on channel @" << this
171 << " with type " << msg->type();
173 bool result = NPChannelBase::Send(msg);
174 in_send_--;
175 return result;
178 bool PluginChannel::OnMessageReceived(const IPC::Message& msg) {
179 if (log_messages_) {
180 VLOG(1) << "received message @" << &msg << " on channel @" << this
181 << " with type " << msg.type();
183 return NPChannelBase::OnMessageReceived(msg);
186 void PluginChannel::OnChannelError() {
187 NPChannelBase::OnChannelError();
188 CleanUp();
191 int PluginChannel::GenerateRouteID() {
192 static int last_id = 0;
193 return ++last_id;
196 base::WaitableEvent* PluginChannel::GetModalDialogEvent(int render_view_id) {
197 return filter_->GetModalDialogEvent(render_view_id);
200 PluginChannel::~PluginChannel() {
201 MessageLoop::current()->PostDelayedTask(
202 FROM_HERE,
203 base::Bind(&PluginReleaseCallback),
204 base::TimeDelta::FromMinutes(kPluginReleaseTimeMinutes));
207 void PluginChannel::CleanUp() {
208 // We need to clean up the stubs so that they call NPPDestroy. This will
209 // also lead to them releasing their reference on this object so that it can
210 // be deleted.
211 for (size_t i = 0; i < plugin_stubs_.size(); ++i)
212 RemoveRoute(plugin_stubs_[i]->instance_id());
214 // Need to addref this object temporarily because otherwise removing the last
215 // stub will cause the destructor of this object to be called, however at
216 // that point plugin_stubs_ will have one element and its destructor will be
217 // called twice.
218 scoped_refptr<PluginChannel> me(this);
220 plugin_stubs_.clear();
223 bool PluginChannel::Init(base::MessageLoopProxy* ipc_message_loop,
224 bool create_pipe_now,
225 base::WaitableEvent* shutdown_event) {
226 if (!NPChannelBase::Init(ipc_message_loop, create_pipe_now, shutdown_event))
227 return false;
229 channel_->AddFilter(filter_.get());
230 return true;
233 PluginChannel::PluginChannel()
234 : renderer_id_(-1),
235 in_send_(0),
236 incognito_(false),
237 filter_(new MessageFilter()) {
238 set_send_unblocking_only_during_unblock_dispatch();
239 ChildProcess::current()->AddRefProcess();
240 const CommandLine* command_line = CommandLine::ForCurrentProcess();
241 log_messages_ = command_line->HasSwitch(switches::kLogPluginMessages);
244 bool PluginChannel::OnControlMessageReceived(const IPC::Message& msg) {
245 bool handled = true;
246 IPC_BEGIN_MESSAGE_MAP(PluginChannel, msg)
247 IPC_MESSAGE_HANDLER(PluginMsg_CreateInstance, OnCreateInstance)
248 IPC_MESSAGE_HANDLER_DELAY_REPLY(PluginMsg_DestroyInstance,
249 OnDestroyInstance)
250 IPC_MESSAGE_HANDLER(PluginMsg_GenerateRouteID, OnGenerateRouteID)
251 IPC_MESSAGE_HANDLER(PluginMsg_ClearSiteData, OnClearSiteData)
252 IPC_MESSAGE_UNHANDLED(handled = false)
253 IPC_END_MESSAGE_MAP()
254 DCHECK(handled);
255 return handled;
258 void PluginChannel::OnCreateInstance(const std::string& mime_type,
259 int* instance_id) {
260 *instance_id = GenerateRouteID();
261 scoped_refptr<WebPluginDelegateStub> stub(new WebPluginDelegateStub(
262 mime_type, *instance_id, this));
263 AddRoute(*instance_id, stub, NULL);
264 plugin_stubs_.push_back(stub);
267 void PluginChannel::OnDestroyInstance(int instance_id,
268 IPC::Message* reply_msg) {
269 for (size_t i = 0; i < plugin_stubs_.size(); ++i) {
270 if (plugin_stubs_[i]->instance_id() == instance_id) {
271 scoped_refptr<MessageFilter> filter(filter_);
272 int render_view_id =
273 plugin_stubs_[i]->webplugin()->host_render_view_routing_id();
274 plugin_stubs_.erase(plugin_stubs_.begin() + i);
275 Send(reply_msg);
276 RemoveRoute(instance_id);
277 // NOTE: *this* might be deleted as a result of calling RemoveRoute.
278 // Don't release the modal dialog event right away, but do it after the
279 // stack unwinds since the plugin can be destroyed later if it's in use
280 // right now.
281 MessageLoop::current()->PostNonNestableTask(FROM_HERE, base::Bind(
282 &MessageFilter::ReleaseModalDialogEvent, filter.get(),
283 render_view_id));
284 return;
288 NOTREACHED() << "Couldn't find WebPluginDelegateStub to destroy";
291 void PluginChannel::OnGenerateRouteID(int* route_id) {
292 *route_id = GenerateRouteID();
295 void PluginChannel::OnClearSiteData(const std::string& site,
296 uint64 flags,
297 uint64 max_age) {
298 bool success = false;
299 CommandLine* command_line = CommandLine::ForCurrentProcess();
300 FilePath path = command_line->GetSwitchValuePath(switches::kPluginPath);
301 scoped_refptr<webkit::npapi::PluginLib> plugin_lib(
302 webkit::npapi::PluginLib::CreatePluginLib(path));
303 if (plugin_lib.get()) {
304 NPError err = plugin_lib->NP_Initialize();
305 if (err == NPERR_NO_ERROR) {
306 const char* site_str = site.empty() ? NULL : site.c_str();
307 err = plugin_lib->NP_ClearSiteData(site_str, flags, max_age);
308 std::string site_name =
309 site.empty() ? "NULL"
310 : base::StringPrintf("\"%s\"", site_str);
311 VLOG(1) << "NPP_ClearSiteData(" << site_name << ", " << flags << ", "
312 << max_age << ") returned " << err;
313 success = (err == NPERR_NO_ERROR);
316 Send(new PluginHostMsg_ClearSiteDataResult(success));
319 } // namespace content