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/browser_plugin/browser_plugin_manager.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "content/common/browser_plugin/browser_plugin_constants.h"
8 #include "content/common/browser_plugin/browser_plugin_messages.h"
9 #include "content/common/frame_messages.h"
10 #include "content/public/renderer/browser_plugin_delegate.h"
11 #include "content/public/renderer/content_renderer_client.h"
12 #include "content/renderer/browser_plugin/browser_plugin.h"
13 #include "content/renderer/render_thread_impl.h"
14 #include "ipc/ipc_message_macros.h"
19 BrowserPluginManager
* BrowserPluginManager::Get() {
20 if (!RenderThreadImpl::current())
22 return RenderThreadImpl::current()->browser_plugin_manager();
25 BrowserPluginManager::BrowserPluginManager() {
28 BrowserPluginManager::~BrowserPluginManager() {
31 void BrowserPluginManager::AddBrowserPlugin(
32 int browser_plugin_instance_id
,
33 BrowserPlugin
* browser_plugin
) {
34 instances_
.AddWithID(browser_plugin
, browser_plugin_instance_id
);
37 void BrowserPluginManager::RemoveBrowserPlugin(int browser_plugin_instance_id
) {
38 instances_
.Remove(browser_plugin_instance_id
);
41 BrowserPlugin
* BrowserPluginManager::GetBrowserPlugin(
42 int browser_plugin_instance_id
) const {
43 return instances_
.Lookup(browser_plugin_instance_id
);
46 int BrowserPluginManager::GetNextInstanceID() {
47 return RenderThreadImpl::current()->GenerateRoutingID();
50 void BrowserPluginManager::UpdateFocusState() {
51 IDMap
<BrowserPlugin
>::iterator
iter(&instances_
);
52 while (!iter
.IsAtEnd()) {
53 iter
.GetCurrentValue()->UpdateGuestFocusState(blink::WebFocusTypeNone
);
58 void BrowserPluginManager::Attach(int browser_plugin_instance_id
) {
59 BrowserPlugin
* plugin
= GetBrowserPlugin(browser_plugin_instance_id
);
64 void BrowserPluginManager::Detach(int browser_plugin_instance_id
) {
65 BrowserPlugin
* plugin
= GetBrowserPlugin(browser_plugin_instance_id
);
70 BrowserPlugin
* BrowserPluginManager::CreateBrowserPlugin(
71 RenderFrame
* render_frame
,
72 scoped_ptr
<BrowserPluginDelegate
> delegate
) {
73 return new BrowserPlugin(render_frame
, delegate
.Pass());
76 void BrowserPluginManager::DidCommitCompositorFrame(
77 int render_frame_routing_id
) {
78 IDMap
<BrowserPlugin
>::iterator
iter(&instances_
);
79 while (!iter
.IsAtEnd()) {
80 if (iter
.GetCurrentValue()->render_frame_routing_id() ==
81 render_frame_routing_id
) {
82 iter
.GetCurrentValue()->DidCommitCompositorFrame();
88 bool BrowserPluginManager::OnControlMessageReceived(
89 const IPC::Message
& message
) {
90 if (!BrowserPlugin::ShouldForwardToBrowserPlugin(message
) &&
91 !content::GetContentClient()->renderer()->
92 ShouldForwardToGuestContainer(message
)) {
96 int browser_plugin_instance_id
= browser_plugin::kInstanceIDNone
;
97 // All allowed messages must have |browser_plugin_instance_id| as their
99 PickleIterator
iter(message
);
100 bool success
= iter
.ReadInt(&browser_plugin_instance_id
);
102 BrowserPlugin
* plugin
= GetBrowserPlugin(browser_plugin_instance_id
);
103 if (plugin
&& plugin
->OnMessageReceived(message
))
106 // TODO(fsamuel): This is probably forcing the compositor to continue working
107 // even on display:none. We should optimize this.
108 if (message
.type() == BrowserPluginMsg_CompositorFrameSwapped::ID
) {
109 OnCompositorFrameSwappedPluginUnavailable(message
);
116 bool BrowserPluginManager::Send(IPC::Message
* msg
) {
117 return RenderThreadImpl::current()->Send(msg
);
120 void BrowserPluginManager::OnCompositorFrameSwappedPluginUnavailable(
121 const IPC::Message
& message
) {
122 BrowserPluginMsg_CompositorFrameSwapped::Param param
;
123 if (!BrowserPluginMsg_CompositorFrameSwapped::Read(&message
, ¶m
))
126 FrameHostMsg_CompositorFrameSwappedACK_Params params
;
127 params
.producing_host_id
= get
<1>(param
).producing_host_id
;
128 params
.producing_route_id
= get
<1>(param
).producing_route_id
;
129 params
.output_surface_id
= get
<1>(param
).output_surface_id
;
130 Send(new BrowserPluginHostMsg_CompositorFrameSwappedACK(
131 get
<0>(param
), params
));
134 } // namespace content