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"
7 #include "base/memory/scoped_ptr.h"
8 #include "content/common/browser_plugin/browser_plugin_constants.h"
9 #include "content/common/browser_plugin/browser_plugin_messages.h"
10 #include "content/common/frame_messages.h"
11 #include "content/public/renderer/browser_plugin_delegate.h"
12 #include "content/public/renderer/render_thread.h"
13 #include "content/renderer/browser_plugin/browser_plugin.h"
14 #include "ipc/ipc_message_macros.h"
19 BrowserPluginManager* BrowserPluginManager::Create(
20 RenderViewImpl* render_view) {
21 return new BrowserPluginManager(render_view);
24 BrowserPluginManager::BrowserPluginManager(RenderViewImpl* render_view)
25 : RenderViewObserver(render_view),
26 current_instance_id_(browser_plugin::kInstanceIDNone),
27 render_view_(render_view->AsWeakPtr()) {
30 BrowserPluginManager::~BrowserPluginManager() {
33 void BrowserPluginManager::AddBrowserPlugin(
34 int browser_plugin_instance_id,
35 BrowserPlugin* browser_plugin) {
36 instances_.AddWithID(browser_plugin, browser_plugin_instance_id);
39 void BrowserPluginManager::RemoveBrowserPlugin(int browser_plugin_instance_id) {
40 instances_.Remove(browser_plugin_instance_id);
43 BrowserPlugin* BrowserPluginManager::GetBrowserPlugin(
44 int browser_plugin_instance_id) const {
45 return instances_.Lookup(browser_plugin_instance_id);
48 int BrowserPluginManager::GetNextInstanceID() {
49 return ++current_instance_id_;
52 void BrowserPluginManager::UpdateDeviceScaleFactor() {
53 IDMap<BrowserPlugin>::iterator iter(&instances_);
54 while (!iter.IsAtEnd()) {
55 iter.GetCurrentValue()->UpdateDeviceScaleFactor();
60 void BrowserPluginManager::UpdateFocusState() {
61 IDMap<BrowserPlugin>::iterator iter(&instances_);
62 while (!iter.IsAtEnd()) {
63 iter.GetCurrentValue()->UpdateGuestFocusState();
68 void BrowserPluginManager::Attach(int browser_plugin_instance_id) {
69 BrowserPlugin* plugin = GetBrowserPlugin(browser_plugin_instance_id);
74 BrowserPlugin* BrowserPluginManager::CreateBrowserPlugin(
75 RenderViewImpl* render_view,
76 blink::WebFrame* frame,
77 scoped_ptr<BrowserPluginDelegate> delegate) {
78 return new BrowserPlugin(render_view, frame, delegate.Pass());
81 void BrowserPluginManager::DidCommitCompositorFrame() {
82 IDMap<BrowserPlugin>::iterator iter(&instances_);
83 while (!iter.IsAtEnd()) {
84 iter.GetCurrentValue()->DidCommitCompositorFrame();
89 bool BrowserPluginManager::OnMessageReceived(
90 const IPC::Message& message) {
91 if (!BrowserPlugin::ShouldForwardToBrowserPlugin(message))
94 int browser_plugin_instance_id = browser_plugin::kInstanceIDNone;
95 // All allowed messages must have |browser_plugin_instance_id| as their
97 PickleIterator iter(message);
98 bool success = iter.ReadInt(&browser_plugin_instance_id);
100 BrowserPlugin* plugin = GetBrowserPlugin(browser_plugin_instance_id);
101 if (plugin && plugin->OnMessageReceived(message))
104 // TODO(fsamuel): This is probably forcing the compositor to continue working
105 // even on display:none. We should optimize this.
106 if (message.type() == BrowserPluginMsg_CompositorFrameSwapped::ID) {
107 OnCompositorFrameSwappedPluginUnavailable(message);
114 bool BrowserPluginManager::Send(IPC::Message* msg) {
115 return RenderThread::Get()->Send(msg);
118 void BrowserPluginManager::OnCompositorFrameSwappedPluginUnavailable(
119 const IPC::Message& message) {
120 BrowserPluginMsg_CompositorFrameSwapped::Param param;
121 if (!BrowserPluginMsg_CompositorFrameSwapped::Read(&message, ¶m))
124 FrameHostMsg_CompositorFrameSwappedACK_Params params;
125 params.producing_host_id = param.b.producing_host_id;
126 params.producing_route_id = param.b.producing_route_id;
127 params.output_surface_id = param.b.output_surface_id;
128 Send(new BrowserPluginHostMsg_CompositorFrameSwappedACK(
129 routing_id(), param.a, params));
132 } // namespace content