Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / browser_plugin / browser_plugin_manager.cc
blob9cde47387883186a2913d9e515aa6f08ed9694b3
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/renderer/browser_plugin/browser_plugin.h"
12 #include "content/renderer/render_thread_impl.h"
13 #include "ipc/ipc_message_macros.h"
15 namespace content {
17 // static
18 BrowserPluginManager* BrowserPluginManager::Get() {
19 if (!RenderThreadImpl::current())
20 return nullptr;
21 return RenderThreadImpl::current()->browser_plugin_manager();
24 BrowserPluginManager::BrowserPluginManager() {
27 BrowserPluginManager::~BrowserPluginManager() {
30 void BrowserPluginManager::AddBrowserPlugin(
31 int browser_plugin_instance_id,
32 BrowserPlugin* browser_plugin) {
33 instances_.AddWithID(browser_plugin, browser_plugin_instance_id);
36 void BrowserPluginManager::RemoveBrowserPlugin(int browser_plugin_instance_id) {
37 instances_.Remove(browser_plugin_instance_id);
40 BrowserPlugin* BrowserPluginManager::GetBrowserPlugin(
41 int browser_plugin_instance_id) const {
42 return instances_.Lookup(browser_plugin_instance_id);
45 int BrowserPluginManager::GetNextInstanceID() {
46 return RenderThreadImpl::current()->GenerateRoutingID();
49 void BrowserPluginManager::UpdateFocusState() {
50 IDMap<BrowserPlugin>::iterator iter(&instances_);
51 while (!iter.IsAtEnd()) {
52 iter.GetCurrentValue()->UpdateGuestFocusState(blink::WebFocusTypeNone);
53 iter.Advance();
57 void BrowserPluginManager::Attach(int browser_plugin_instance_id) {
58 BrowserPlugin* plugin = GetBrowserPlugin(browser_plugin_instance_id);
59 if (plugin)
60 plugin->Attach();
63 void BrowserPluginManager::Detach(int browser_plugin_instance_id) {
64 BrowserPlugin* plugin = GetBrowserPlugin(browser_plugin_instance_id);
65 if (plugin)
66 plugin->Detach();
69 BrowserPlugin* BrowserPluginManager::CreateBrowserPlugin(
70 RenderFrame* render_frame,
71 const base::WeakPtr<BrowserPluginDelegate>& delegate) {
72 return new BrowserPlugin(render_frame, delegate);
75 void BrowserPluginManager::DidCommitCompositorFrame(
76 int render_frame_routing_id) {
77 IDMap<BrowserPlugin>::iterator iter(&instances_);
78 while (!iter.IsAtEnd()) {
79 if (iter.GetCurrentValue()->render_frame_routing_id() ==
80 render_frame_routing_id) {
81 iter.GetCurrentValue()->DidCommitCompositorFrame();
83 iter.Advance();
87 bool BrowserPluginManager::OnControlMessageReceived(
88 const IPC::Message& message) {
89 if (!BrowserPlugin::ShouldForwardToBrowserPlugin(message))
90 return false;
92 int browser_plugin_instance_id = browser_plugin::kInstanceIDNone;
93 // All allowed messages must have |browser_plugin_instance_id| as their
94 // first parameter.
95 base::PickleIterator iter(message);
96 bool success = iter.ReadInt(&browser_plugin_instance_id);
97 DCHECK(success);
98 BrowserPlugin* plugin = GetBrowserPlugin(browser_plugin_instance_id);
99 if (plugin && plugin->OnMessageReceived(message))
100 return true;
102 // TODO(fsamuel): This is probably forcing the compositor to continue working
103 // even on display:none. We should optimize this.
104 if (message.type() == BrowserPluginMsg_CompositorFrameSwapped::ID) {
105 OnCompositorFrameSwappedPluginUnavailable(message);
106 return true;
109 return false;
112 bool BrowserPluginManager::Send(IPC::Message* msg) {
113 return RenderThreadImpl::current()->Send(msg);
116 void BrowserPluginManager::OnCompositorFrameSwappedPluginUnavailable(
117 const IPC::Message& message) {
118 BrowserPluginMsg_CompositorFrameSwapped::Param param;
119 if (!BrowserPluginMsg_CompositorFrameSwapped::Read(&message, &param))
120 return;
122 FrameHostMsg_CompositorFrameSwappedACK_Params params;
123 params.producing_host_id = base::get<1>(param).producing_host_id;
124 params.producing_route_id = base::get<1>(param).producing_route_id;
125 params.output_surface_id = base::get<1>(param).output_surface_id;
126 Send(new BrowserPluginHostMsg_CompositorFrameSwappedACK(
127 base::get<0>(param), params));
130 } // namespace content