Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / renderer / browser_plugin / browser_plugin_manager.cc
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.
4
5 #include "content/renderer/browser_plugin/browser_plugin_manager.h"
6
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"
15
16 namespace content {
17
18 // static
19 BrowserPluginManager* BrowserPluginManager::Create(
20     RenderViewImpl* render_view) {
21   return new BrowserPluginManager(render_view);
22 }
23
24 BrowserPluginManager::BrowserPluginManager(RenderViewImpl* render_view)
25     : RenderViewObserver(render_view),
26       current_instance_id_(browser_plugin::kInstanceIDNone),
27       render_view_(render_view->AsWeakPtr()) {
28 }
29
30 BrowserPluginManager::~BrowserPluginManager() {
31 }
32
33 void BrowserPluginManager::AddBrowserPlugin(
34     int browser_plugin_instance_id,
35     BrowserPlugin* browser_plugin) {
36   instances_.AddWithID(browser_plugin, browser_plugin_instance_id);
37 }
38
39 void BrowserPluginManager::RemoveBrowserPlugin(int browser_plugin_instance_id) {
40   instances_.Remove(browser_plugin_instance_id);
41 }
42
43 BrowserPlugin* BrowserPluginManager::GetBrowserPlugin(
44     int browser_plugin_instance_id) const {
45   return instances_.Lookup(browser_plugin_instance_id);
46 }
47
48 int BrowserPluginManager::GetNextInstanceID() {
49   return ++current_instance_id_;
50 }
51
52 void BrowserPluginManager::UpdateDeviceScaleFactor() {
53   IDMap<BrowserPlugin>::iterator iter(&instances_);
54   while (!iter.IsAtEnd()) {
55     iter.GetCurrentValue()->UpdateDeviceScaleFactor();
56     iter.Advance();
57   }
58 }
59
60 void BrowserPluginManager::UpdateFocusState() {
61   IDMap<BrowserPlugin>::iterator iter(&instances_);
62   while (!iter.IsAtEnd()) {
63     iter.GetCurrentValue()->UpdateGuestFocusState();
64     iter.Advance();
65   }
66 }
67
68 void BrowserPluginManager::Attach(int browser_plugin_instance_id) {
69   BrowserPlugin* plugin = GetBrowserPlugin(browser_plugin_instance_id);
70   if (plugin)
71     plugin->Attach();
72 }
73
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());
79 }
80
81 void BrowserPluginManager::DidCommitCompositorFrame() {
82   IDMap<BrowserPlugin>::iterator iter(&instances_);
83   while (!iter.IsAtEnd()) {
84     iter.GetCurrentValue()->DidCommitCompositorFrame();
85     iter.Advance();
86   }
87 }
88
89 bool BrowserPluginManager::OnMessageReceived(
90     const IPC::Message& message) {
91   if (!BrowserPlugin::ShouldForwardToBrowserPlugin(message))
92     return false;
93
94   int browser_plugin_instance_id = browser_plugin::kInstanceIDNone;
95   // All allowed messages must have |browser_plugin_instance_id| as their
96   // first parameter.
97   PickleIterator iter(message);
98   bool success = iter.ReadInt(&browser_plugin_instance_id);
99   DCHECK(success);
100   BrowserPlugin* plugin = GetBrowserPlugin(browser_plugin_instance_id);
101   if (plugin && plugin->OnMessageReceived(message))
102     return true;
103
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);
108     return true;
109   }
110
111   return false;
112 }
113
114 bool BrowserPluginManager::Send(IPC::Message* msg) {
115   return RenderThread::Get()->Send(msg);
116 }
117
118 void BrowserPluginManager::OnCompositorFrameSwappedPluginUnavailable(
119     const IPC::Message& message) {
120   BrowserPluginMsg_CompositorFrameSwapped::Param param;
121   if (!BrowserPluginMsg_CompositorFrameSwapped::Read(&message, &param))
122     return;
123
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));
130 }
131
132 }  // namespace content