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_guest_delegate.h" |
| 6 |
| 7 #include "base/time.h" |
| 8 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 9 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 10 #include "content/browser/web_contents/web_contents_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 "content/public/browser/notification_details.h" |
| 17 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/notification_source.h" |
| 19 #include "content/public/browser/notification_types.h" |
| 20 #include "content/public/browser/web_contents_observer.h" |
| 21 #include "content/public/common/result_codes.h" |
| 22 #include "ui/gfx/size.h" |
| 23 |
| 24 namespace content { |
| 25 |
| 26 namespace { |
| 27 const int kGuestHangTimeout = 5000; |
| 28 } |
| 29 |
| 30 BrowserPluginHostGuestDelegate::BrowserPluginHostGuestDelegate( |
| 31 int instance_id, WebContentsImpl* web_contents) |
| 32 : WebContentsObserver(web_contents), |
| 33 embedder_render_process_host_(NULL), |
| 34 instance_id_(instance_id), |
| 35 damage_buffer_(NULL), |
| 36 pending_update_counter_(0) { |
| 37 } |
| 38 |
| 39 BrowserPluginHostGuestDelegate::~BrowserPluginHostGuestDelegate() { |
| 40 if (damage_buffer_) |
| 41 delete damage_buffer_; |
| 42 } |
| 43 |
| 44 bool BrowserPluginHostGuestDelegate::TakeFocus(bool reverse) { |
| 45 embedder_render_process_host()->Send( |
| 46 new BrowserPluginMsg_AdvanceFocus(instance_id(), reverse)); |
| 47 return true; |
| 48 } |
| 49 |
| 50 void BrowserPluginHostGuestDelegate::RendererUnresponsive(WebContents* source) { |
| 51 base::ProcessHandle process_handle = |
| 52 web_contents()->GetRenderProcessHost()->GetHandle(); |
| 53 base::KillProcess(process_handle, RESULT_CODE_HUNG, false); |
| 54 } |
| 55 |
| 56 void BrowserPluginHostGuestDelegate::SetDamageBuffer( |
| 57 TransportDIB* damage_buffer, const gfx::Size& size, float scale_factor) { |
| 58 if (damage_buffer_) |
| 59 delete damage_buffer_; |
| 60 damage_buffer_ = damage_buffer; |
| 61 damage_buffer_size_ = size; |
| 62 damage_buffer_scale_factor_ = scale_factor; |
| 63 } |
| 64 |
| 65 void BrowserPluginHostGuestDelegate::UpdateRect( |
| 66 RenderViewHost* render_view_host, |
| 67 const ViewHostMsg_UpdateRect_Params& params) { |
| 68 // This handler is only of interest to us for the 2D software rendering path. |
| 69 // needs_ack should always be true for the 2D path. |
| 70 // TODO(fsamuel): Do we need to do something different in the 3D case? |
| 71 if (!params.needs_ack) |
| 72 return; |
| 73 |
| 74 // Only copy damage if the guest's view size is equal to the damage buffer's |
| 75 // size and the guest's scale factor is equal to the damage buffer's scale |
| 76 // factor. |
| 77 if (params.view_size.width() == damage_buffer_size().width() && |
| 78 params.view_size.height() == damage_buffer_size().height() && |
| 79 params.scale_factor == damage_buffer_scale_factor()) { |
| 80 TransportDIB* dib = render_view_host->GetProcess()-> |
| 81 GetTransportDIB(params.bitmap); |
| 82 if (dib) { |
| 83 void* guest_memory = dib->memory(); |
| 84 void* embedder_memory = damage_buffer_->memory(); |
| 85 int size = std::min(dib->size(), damage_buffer_->size()); |
| 86 memcpy(embedder_memory, guest_memory, size); |
| 87 } |
| 88 } |
| 89 DCHECK(embedder_render_process_host()); |
| 90 BrowserPluginMsg_UpdateRect_Params relay_params; |
| 91 relay_params.bitmap_rect = params.bitmap_rect; |
| 92 relay_params.dx = params.dx; |
| 93 relay_params.dy = params.dy; |
| 94 relay_params.scroll_rect = params.scroll_rect; |
| 95 relay_params.copy_rects = params.copy_rects; |
| 96 relay_params.view_size = params.view_size; |
| 97 relay_params.scale_factor = params.scale_factor; |
| 98 relay_params.is_resize_ack = ViewHostMsg_UpdateRect_Flags::is_resize_ack( |
| 99 params.flags); |
| 100 |
| 101 // We need to send the ACK to the same render_view_host that issued |
| 102 // the UpdateRect. We keep track of this correspondence via a message_id. |
| 103 int message_id = pending_update_counter_++; |
| 104 pending_updates_.AddWithID(render_view_host, message_id); |
| 105 |
| 106 gfx::Size param_size = gfx::Size(params.view_size.width(), |
| 107 params.view_size.height()); |
| 108 content::NotificationService::current()->Notify( |
| 109 // TODO(lazyboy): Rename notification variable if this stays. |
| 110 NOTIFICATION_BROWSER_PLUGIN_UPDATE_RECT, |
| 111 content::Source<BrowserPluginHostGuestDelegate>(this), |
| 112 content::Details<const gfx::Size>(¶m_size)); |
| 113 |
| 114 embedder_render_process_host()->Send( |
| 115 new BrowserPluginMsg_UpdateRect(instance_id(), |
| 116 message_id, |
| 117 relay_params)); |
| 118 } |
| 119 |
| 120 void BrowserPluginHostGuestDelegate::UpdateRectACK(int message_id, |
| 121 const gfx::Size& size) { |
| 122 RenderViewHost* render_view_host = pending_updates_.Lookup(message_id); |
| 123 // If the guest has crashed since it sent the initial ViewHostMsg_UpdateRect |
| 124 // then the pending_updates_ map will have been cleared. |
| 125 if (!render_view_host) |
| 126 return; |
| 127 pending_updates_.Remove(message_id); |
| 128 render_view_host->Send( |
| 129 new ViewMsg_UpdateRect_ACK(render_view_host->GetRoutingID())); |
| 130 if (!size.IsEmpty()) |
| 131 render_view_host->GetView()->SetSize(size); |
| 132 } |
| 133 |
| 134 void BrowserPluginHostGuestDelegate::HandleInputEvent( |
| 135 RenderViewHost* render_view_host, |
| 136 const gfx::Rect& guest_rect, |
| 137 const WebKit::WebInputEvent& event, |
| 138 IPC::Message* reply_message) { |
| 139 guest_rect_ = guest_rect; |
| 140 RenderViewHostImpl* guest_rvh = static_cast<RenderViewHostImpl*>( |
| 141 web_contents()->GetRenderViewHost()); |
| 142 IPC::Message* message = new ViewMsg_HandleInputEvent( |
| 143 guest_rvh->GetRoutingID()); |
| 144 |
| 145 // Copy the WebInputEvent and modify the event type. The guest expects |
| 146 // WebInputEvent::RawKeyDowns and not KeyDowns. |
| 147 scoped_array<char> input_buffer(new char[event.size]); |
| 148 memcpy(input_buffer.get(), &event, event.size); |
| 149 WebKit::WebInputEvent* input_event = |
| 150 reinterpret_cast<WebKit::WebInputEvent*>(input_buffer.get()); |
| 151 if (event.type == WebKit::WebInputEvent::KeyDown) |
| 152 input_event->type = WebKit::WebInputEvent::RawKeyDown; |
| 153 |
| 154 message->WriteData(input_buffer.get(), event.size); |
| 155 // TODO(fsamuel): What do we need to do here? This is for keyboard shortcuts. |
| 156 if (input_event->type == WebKit::WebInputEvent::RawKeyDown) |
| 157 message->WriteBool(false); |
| 158 guest_rvh->Send(message); |
| 159 |
| 160 DCHECK(!pending_input_event_reply_.get()); |
| 161 pending_input_event_reply_.reset(reply_message); |
| 162 guest_rvh->StartHangMonitorTimeout( |
| 163 base::TimeDelta::FromMilliseconds(kGuestHangTimeout)); |
| 164 } |
| 165 |
| 166 void BrowserPluginHostGuestDelegate::HandleInputEventAck( |
| 167 RenderViewHost* render_view_host, bool handled) { |
| 168 DCHECK(pending_input_event_reply_.get()); |
| 169 IPC::Message* reply_message = pending_input_event_reply_.release(); |
| 170 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, |
| 171 handled, |
| 172 cursor_); |
| 173 embedder_render_process_host()->Send(reply_message); |
| 174 RenderViewHostImpl* guest_rvh = |
| 175 static_cast<RenderViewHostImpl*>(render_view_host); |
| 176 guest_rvh->StopHangMonitorTimeout(); |
| 177 } |
| 178 |
| 179 void BrowserPluginHostGuestDelegate::SetFocus(bool focused) { |
| 180 RenderViewHost* render_view_host = web_contents()->GetRenderViewHost(); |
| 181 render_view_host->Send( |
| 182 new ViewMsg_SetFocus(render_view_host->GetRoutingID(), focused)); |
| 183 } |
| 184 |
| 185 void BrowserPluginHostGuestDelegate::ShowWidget( |
| 186 RenderViewHost* render_view_host, |
| 187 int route_id, |
| 188 const gfx::Rect& initial_pos) { |
| 189 gfx::Rect screen_pos(initial_pos); |
| 190 screen_pos.Offset(guest_rect_.origin()); |
| 191 static_cast<WebContentsImpl*>(web_contents())->ShowCreatedWidget(route_id, |
| 192 screen_pos); |
| 193 } |
| 194 |
| 195 void BrowserPluginHostGuestDelegate::SetCursor(const WebCursor& cursor) { |
| 196 cursor_ = cursor; |
| 197 } |
| 198 |
| 199 void BrowserPluginHostGuestDelegate::Destroy() { |
| 200 WebContents* guest_web_contents = web_contents(); |
| 201 DCHECK(guest_web_contents); |
| 202 delete guest_web_contents; |
| 203 } |
| 204 |
| 205 void BrowserPluginHostGuestDelegate::DidCommitProvisionalLoadForFrame( |
| 206 int64 frame_id, |
| 207 bool is_main_frame, |
| 208 const GURL& url, |
| 209 PageTransition transition_type, |
| 210 RenderViewHost* render_view_host) { |
| 211 // Inform its embedder of the updated URL. |
| 212 // TODO(creis, fsamuel): Ensure this is safe/secure. |
| 213 DCHECK(embedder_render_process_host()); |
| 214 if (is_main_frame) { |
| 215 embedder_render_process_host()->Send( |
| 216 new BrowserPluginMsg_DidNavigate(instance_id(), url)); |
| 217 } |
| 218 } |
| 219 |
| 220 void BrowserPluginHostGuestDelegate::RenderViewGone( |
| 221 base::TerminationStatus status) { |
| 222 DCHECK(embedder_render_process_host()); |
| 223 if (pending_input_event_reply_.get()) { |
| 224 IPC::Message* reply_message = pending_input_event_reply_.release(); |
| 225 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, |
| 226 false, |
| 227 cursor_); |
| 228 embedder_render_process_host()->Send(reply_message); |
| 229 } |
| 230 embedder_render_process_host()->Send( |
| 231 new BrowserPluginMsg_GuestCrashed(instance_id())); |
| 232 IDMap<RenderViewHost>::const_iterator iter(&pending_updates_); |
| 233 while (!iter.IsAtEnd()) { |
| 234 pending_updates_.Remove(iter.GetCurrentKey()); |
| 235 iter.Advance(); |
| 236 } |
| 237 content::NotificationService::current()->Notify( |
| 238 // TODO(lazyboy): Rename notification variable if this stays. |
| 239 NOTIFICATION_BROWSER_PLUGIN_GUEST_CRASHED, |
| 240 content::Source<BrowserPluginHostGuestDelegate>(this), |
| 241 content::NotificationService::NoDetails()); |
| 242 } |
| 243 |
| 244 } // namespace content |
OLD | NEW |