OLD | NEW |
(Empty) | |
| 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/browser/browser_plugin/browser_plugin_host_helper.h" |
| 6 |
| 7 #include "base/time.h" |
| 8 #include "content/browser/browser_plugin/browser_plugin_host.h" |
| 9 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 10 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 11 #include "content/common/browser_plugin_messages.h" |
| 12 #include "content/common/view_messages.h" |
| 13 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/browser/render_widget_host_view.h" |
| 16 #include "ui/gfx/size.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 BrowserPluginHostHelper::BrowserPluginHostHelper( |
| 21 BrowserPluginHost* browser_plugin_host, |
| 22 RenderViewHost* render_view_host) |
| 23 : RenderViewHostObserver(render_view_host), |
| 24 browser_plugin_host_(browser_plugin_host) { |
| 25 } |
| 26 |
| 27 BrowserPluginHostHelper::~BrowserPluginHostHelper() { |
| 28 } |
| 29 |
| 30 bool BrowserPluginHostHelper::Send(IPC::Message* message) { |
| 31 return RenderViewHostObserver::Send(message); |
| 32 } |
| 33 |
| 34 bool BrowserPluginHostHelper::OnMessageReceived(const IPC::Message& message) { |
| 35 bool handled = true; |
| 36 IPC_BEGIN_MESSAGE_MAP(BrowserPluginHostHelper, message) |
| 37 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateGuest, |
| 38 OnNavigateGuest) |
| 39 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ResizeGuest, OnResizeGuest) |
| 40 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_UpdateRect_ACK, OnUpdateRectACK) |
| 41 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_SetFocus, OnSetFocus) |
| 42 IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginHostMsg_HandleInputEvent, |
| 43 OnHandleInputEvent(*static_cast<const IPC::SyncMessage*>(&message), |
| 44 &handled)) |
| 45 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_PluginDestroyed, OnPluginDestroyed) |
| 46 IPC_MESSAGE_UNHANDLED(handled = false) |
| 47 IPC_END_MESSAGE_MAP() |
| 48 |
| 49 // We want to intercept certain messages if they are guests. |
| 50 if (!browser_plugin_host_->embedder_render_process_host()) |
| 51 return handled; |
| 52 |
| 53 handled = true; |
| 54 IPC_BEGIN_MESSAGE_MAP(BrowserPluginHostHelper, message) |
| 55 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateRect, OnUpdateRect) |
| 56 IPC_MESSAGE_HANDLER(ViewHostMsg_HandleInputEvent_ACK, |
| 57 OnHandleInputEventAck) |
| 58 IPC_MESSAGE_HANDLER(ViewHostMsg_TakeFocus, OnTakeFocus) |
| 59 IPC_MESSAGE_HANDLER(ViewHostMsg_ShowWidget, OnShowWidget) |
| 60 IPC_MESSAGE_HANDLER(ViewHostMsg_SetCursor, OnSetCursor) |
| 61 IPC_MESSAGE_UNHANDLED(handled = false) |
| 62 IPC_END_MESSAGE_MAP() |
| 63 |
| 64 return handled; |
| 65 } |
| 66 |
| 67 void BrowserPluginHostHelper::OnNavigateGuest( |
| 68 int instance_id, |
| 69 long long frame_id, |
| 70 const std::string& src, |
| 71 const gfx::Size& size) { |
| 72 browser_plugin_host_->NavigateGuest( |
| 73 render_view_host(), |
| 74 instance_id, |
| 75 frame_id, |
| 76 src, |
| 77 size); |
| 78 } |
| 79 |
| 80 void BrowserPluginHostHelper::OnResizeGuest( |
| 81 int instance_id, |
| 82 const BrowserPluginHostMsg_ResizeGuest_Params& params) { |
| 83 TransportDIB* damage_buffer = NULL; |
| 84 #if defined(OS_WIN) |
| 85 // On Windows we need to duplicate the handle from the remote process |
| 86 HANDLE section; |
| 87 DuplicateHandle(GetHandle(), |
| 88 params.damage_buffer_id.handle, |
| 89 GetCurrentProcess(), |
| 90 §ion, |
| 91 STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ | FILE_MAP_WRITE, |
| 92 FALSE, 0); |
| 93 damage_buffer = TransportDIB::Map(section); |
| 94 #elif defined(OS_MACOSX) |
| 95 // On OSX, the browser allocates all DIBs and keeps a file descriptor around |
| 96 // for each. |
| 97 damage_buffer = widget_helper_->MapTransportDIB(params.damage_buffer_id); |
| 98 #elif defined(OS_ANDROID) |
| 99 damage_buffer = TransportDIB::Map(params.damage_buffer_id); |
| 100 #elif defined(OS_POSIX) |
| 101 damage_buffer = TransportDIB::Map(params.damage_buffer_id.shmkey); |
| 102 #endif // defined(OS_POSIX) |
| 103 DCHECK(damage_buffer); |
| 104 // TODO(fsamuel): Schedule this later so that we don't stall the embedder for |
| 105 // too long. |
| 106 browser_plugin_host_->ResizeGuest(instance_id, |
| 107 damage_buffer, |
| 108 params.width, |
| 109 params.height, |
| 110 params.resize_pending, |
| 111 params.scale_factor); |
| 112 } |
| 113 |
| 114 void BrowserPluginHostHelper::OnUpdateRect( |
| 115 const ViewHostMsg_UpdateRect_Params& params) { |
| 116 RenderWidgetHostImpl* render_widget_host = |
| 117 RenderWidgetHostImpl::From(render_view_host()); |
| 118 render_widget_host->ResetFlags(); |
| 119 browser_plugin_host_->UpdateRect(render_view_host(), params); |
| 120 } |
| 121 |
| 122 void BrowserPluginHostHelper::OnHandleInputEventAck( |
| 123 WebKit::WebInputEvent::Type event_type, |
| 124 bool processed) { |
| 125 browser_plugin_host_->HandleInputEventAck(render_view_host(), processed); |
| 126 } |
| 127 |
| 128 void BrowserPluginHostHelper::OnTakeFocus(bool reverse) { |
| 129 browser_plugin_host_->TakeFocus(reverse); |
| 130 } |
| 131 |
| 132 void BrowserPluginHostHelper::OnShowWidget( |
| 133 int route_id, |
| 134 const gfx::Rect& initial_pos) { |
| 135 browser_plugin_host_->ShowWidget(render_view_host(), route_id, initial_pos); |
| 136 } |
| 137 |
| 138 void BrowserPluginHostHelper::OnSetCursor(const WebCursor& cursor) { |
| 139 browser_plugin_host_->SetCursor(cursor); |
| 140 } |
| 141 |
| 142 void BrowserPluginHostHelper::OnUpdateRectACK(int instance_id, |
| 143 int message_id, |
| 144 const gfx::Size& size) { |
| 145 |
| 146 BrowserPluginHost* guest = browser_plugin_host_-> |
| 147 GetGuestByInstanceID(instance_id); |
| 148 guest->UpdateRectACK(message_id, size); |
| 149 } |
| 150 |
| 151 void BrowserPluginHostHelper::OnHandleInputEvent( |
| 152 const IPC::SyncMessage& message, |
| 153 bool* handled) { |
| 154 *handled = true; |
| 155 PickleIterator iter(message); |
| 156 |
| 157 // TODO(fsamuel): This appears to be a monotonically increasing value. |
| 158 int instance_id = -1; |
| 159 const char* guest_rect_data = NULL; |
| 160 int guest_rect_data_length = -1; |
| 161 const char* input_event_data = NULL; |
| 162 int input_event_data_length = -1; |
| 163 if (!iter.SkipBytes(4) || |
| 164 !message.ReadInt(&iter, &instance_id) || |
| 165 !message.ReadData(&iter, &guest_rect_data, &guest_rect_data_length) || |
| 166 !message.ReadData(&iter, &input_event_data, &input_event_data_length)) { |
| 167 *handled = false; |
| 168 return; |
| 169 } |
| 170 const gfx::Rect* guest_rect = |
| 171 reinterpret_cast<const gfx::Rect*>(guest_rect_data); |
| 172 const WebKit::WebInputEvent* input_event = |
| 173 reinterpret_cast<const WebKit::WebInputEvent*>(input_event_data); |
| 174 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( |
| 175 render_view_host()); |
| 176 |
| 177 // Convert the window coordinates into screen coordinates. |
| 178 gfx::Rect guest_screen_rect(*guest_rect); |
| 179 if (rvh->GetView()) |
| 180 guest_screen_rect.Offset( |
| 181 rvh->GetView()->GetViewBounds().origin()); |
| 182 |
| 183 IPC::Message* reply_message = |
| 184 IPC::SyncMessage::GenerateReply(&message); |
| 185 BrowserPluginHost* guest = browser_plugin_host_-> |
| 186 GetGuestByInstanceID(instance_id); |
| 187 guest->HandleInputEvent(rvh, |
| 188 guest_screen_rect, |
| 189 *input_event, |
| 190 reply_message); |
| 191 } |
| 192 |
| 193 void BrowserPluginHostHelper::OnSetFocus(int instance_id, |
| 194 bool focused) { |
| 195 BrowserPluginHost* guest = browser_plugin_host_-> |
| 196 GetGuestByInstanceID(instance_id); |
| 197 if (guest) |
| 198 guest->SetFocus(focused); |
| 199 } |
| 200 |
| 201 void BrowserPluginHostHelper::OnPluginDestroyed(int instance_id) { |
| 202 browser_plugin_host_->DestroyGuestByInstanceID(instance_id); |
| 203 } |
| 204 |
| 205 } // namespace content |
OLD | NEW |