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/renderer/npapi/plugin_channel_host.h"
7 #include "base/metrics/histogram.h"
8 #include "base/time/time.h"
9 #include "content/child/child_process.h"
10 #include "content/child/npapi/npobject_base.h"
11 #include "content/child/plugin_messages.h"
14 #include "ipc/ipc_channel_posix.h"
17 #include "third_party/WebKit/public/web/WebBindings.h"
19 // TODO(shess): Debugging for http://crbug.com/97285
21 // The hypothesis at #55 requires that RemoveRoute() be called between
22 // sending ViewHostMsg_OpenChannelToPlugin to the browser, and calling
23 // GetPluginChannelHost() on the result. This code detects that case
24 // and stores the backtrace of the RemoveRoute() in a breakpad key.
25 // The specific RemoveRoute() is not tracked (there could be multiple,
26 // and which is the one can't be known until the open completes), but
27 // the backtrace from any such nested call should be sufficient to
29 #if defined(OS_MACOSX)
30 #include "base/debug/crash_logging.h"
31 #include "base/debug/stack_trace.h"
32 #include "base/strings/sys_string_conversions.h"
36 // Breakpad key for the RemoveRoute() backtrace.
37 const char* kRemoveRouteTraceKey
= "remove_route_bt";
39 // Breakpad key for the OnChannelError() backtrace.
40 const char* kChannelErrorTraceKey
= "channel_error_bt";
42 // GetRemoveTrackingFlag() exposes this so that
43 // WebPluginDelegateProxy::Initialize() can do scoped set/reset. When
44 // true, RemoveRoute() knows WBDP::Initialize() is on the stack, and
45 // records the backtrace.
46 bool remove_tracking
= false;
53 #if defined(OS_MACOSX)
55 bool* PluginChannelHost::GetRemoveTrackingFlag() {
56 return &remove_tracking
;
61 PluginChannelHost
* PluginChannelHost::GetPluginChannelHost(
62 const IPC::ChannelHandle
& channel_handle
,
63 base::SingleThreadTaskRunner
* ipc_task_runner
) {
64 PluginChannelHost
* result
=
65 static_cast<PluginChannelHost
*>(NPChannelBase::GetChannel(
66 channel_handle
, IPC::Channel::MODE_CLIENT
, ClassFactory
,
67 ipc_task_runner
, true, ChildProcess::current()->GetShutDownEvent()));
71 PluginChannelHost::PluginChannelHost() : expecting_shutdown_(false) {
74 PluginChannelHost::~PluginChannelHost() {
77 bool PluginChannelHost::Init(base::SingleThreadTaskRunner
* ipc_task_runner
,
79 base::WaitableEvent
* shutdown_event
) {
80 return NPChannelBase::Init(ipc_task_runner
, create_pipe_now
, shutdown_event
);
83 int PluginChannelHost::GenerateRouteID() {
84 int route_id
= MSG_ROUTING_NONE
;
85 Send(new PluginMsg_GenerateRouteID(&route_id
));
90 void PluginChannelHost::AddRoute(int route_id
,
91 IPC::Listener
* listener
,
92 NPObjectBase
* npobject
) {
93 NPChannelBase::AddRoute(route_id
, listener
, npobject
);
96 proxies_
[route_id
] = listener
;
99 void PluginChannelHost::RemoveRoute(int route_id
) {
100 #if defined(OS_MACOSX)
101 if (remove_tracking
) {
102 base::debug::StackTrace trace
;
104 const void* const* addresses
= trace
.Addresses(&count
);
105 base::debug::SetCrashKeyFromAddresses(
106 kRemoveRouteTraceKey
, addresses
, count
);
110 proxies_
.erase(route_id
);
111 NPChannelBase::RemoveRoute(route_id
);
114 bool PluginChannelHost::OnControlMessageReceived(const IPC::Message
& message
) {
116 IPC_BEGIN_MESSAGE_MAP(PluginChannelHost
, message
)
117 IPC_MESSAGE_HANDLER(PluginHostMsg_SetException
, OnSetException
)
118 IPC_MESSAGE_HANDLER(PluginHostMsg_PluginShuttingDown
, OnPluginShuttingDown
)
119 IPC_MESSAGE_UNHANDLED(handled
= false)
120 IPC_END_MESSAGE_MAP()
125 void PluginChannelHost::OnSetException(const std::string
& message
) {
126 blink::WebBindings::setException(NULL
, message
.c_str());
129 void PluginChannelHost::OnPluginShuttingDown() {
130 expecting_shutdown_
= true;
133 bool PluginChannelHost::Send(IPC::Message
* msg
) {
134 if (msg
->is_sync()) {
135 base::TimeTicks
start_time(base::TimeTicks::Now());
136 bool result
= NPChannelBase::Send(msg
);
137 UMA_HISTOGRAM_TIMES("Plugin.SyncMessageTime",
138 base::TimeTicks::Now() - start_time
);
141 return NPChannelBase::Send(msg
);
144 void PluginChannelHost::OnChannelError() {
145 #if defined(OS_MACOSX)
146 if (remove_tracking
) {
147 base::debug::StackTrace trace
;
149 const void* const* addresses
= trace
.Addresses(&count
);
150 base::debug::SetCrashKeyFromAddresses(
151 kChannelErrorTraceKey
, addresses
, count
);
155 NPChannelBase::OnChannelError();
157 for (ProxyMap::iterator iter
= proxies_
.begin();
158 iter
!= proxies_
.end(); iter
++) {
159 iter
->second
->OnChannelError();
165 } // namespace content