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_guest.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/time.h" |
| 10 #include "content/browser/browser_plugin/browser_plugin_guest_helper.h" |
| 11 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" |
| 12 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 13 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 14 #include "content/browser/web_contents/web_contents_impl.h" |
| 15 #include "content/common/browser_plugin_messages.h" |
| 16 #include "content/common/view_messages.h" |
| 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/render_view_host.h" |
| 19 #include "content/public/browser/render_widget_host_view.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 // static |
| 27 BrowserPluginHostFactory* BrowserPluginGuest::factory_ = NULL; |
| 28 |
| 29 namespace { |
| 30 const int kGuestHangTimeoutMs = 5000; |
| 31 } |
| 32 |
| 33 BrowserPluginGuest::BrowserPluginGuest(int instance_id, |
| 34 WebContentsImpl* web_contents, |
| 35 RenderViewHost* render_view_host) |
| 36 : WebContentsObserver(web_contents), |
| 37 embedder_render_process_host_(NULL), |
| 38 instance_id_(instance_id), |
| 39 pending_update_counter_(0), |
| 40 guest_hang_timeout_ms_(kGuestHangTimeoutMs) { |
| 41 DCHECK(web_contents); |
| 42 // |render_view_host| manages the ownership of this BrowserPluginGuestHelper. |
| 43 new BrowserPluginGuestHelper(this, render_view_host); |
| 44 } |
| 45 |
| 46 BrowserPluginGuest::~BrowserPluginGuest() { |
| 47 } |
| 48 |
| 49 // static |
| 50 BrowserPluginGuest* BrowserPluginGuest::Create( |
| 51 int instance_id, |
| 52 WebContentsImpl* web_contents, |
| 53 content::RenderViewHost* render_view_host) { |
| 54 if (factory_) { |
| 55 return factory_->CreateBrowserPluginGuest(instance_id, |
| 56 web_contents, |
| 57 render_view_host); |
| 58 } |
| 59 return new BrowserPluginGuest(instance_id, web_contents, render_view_host); |
| 60 } |
| 61 |
| 62 bool BrowserPluginGuest::ViewTakeFocus(bool reverse) { |
| 63 SendMessageToEmbedder( |
| 64 new BrowserPluginMsg_AdvanceFocus(instance_id(), reverse)); |
| 65 return true; |
| 66 } |
| 67 |
| 68 void BrowserPluginGuest::RendererUnresponsive(WebContents* source) { |
| 69 base::ProcessHandle process_handle = |
| 70 web_contents()->GetRenderProcessHost()->GetHandle(); |
| 71 base::KillProcess(process_handle, RESULT_CODE_HUNG, false); |
| 72 } |
| 73 |
| 74 void BrowserPluginGuest::SetDamageBuffer( |
| 75 TransportDIB* damage_buffer, const gfx::Size& size, float scale_factor) { |
| 76 damage_buffer_.reset(damage_buffer); |
| 77 damage_buffer_size_ = size; |
| 78 damage_buffer_scale_factor_ = scale_factor; |
| 79 } |
| 80 |
| 81 void BrowserPluginGuest::UpdateRect( |
| 82 RenderViewHost* render_view_host, |
| 83 const ViewHostMsg_UpdateRect_Params& params) { |
| 84 RenderWidgetHostImpl* render_widget_host = |
| 85 RenderWidgetHostImpl::From(render_view_host); |
| 86 render_widget_host->ResetSizeAndRepaintPendingFlags(); |
| 87 // This handler is only of interest to us for the 2D software rendering path. |
| 88 // needs_ack should always be true for the 2D path. |
| 89 // TODO(fsamuel): Do we need to do something different in the 3D case? |
| 90 if (!params.needs_ack) |
| 91 return; |
| 92 |
| 93 // Only copy damage if the guest's view size is equal to the damage buffer's |
| 94 // size and the guest's scale factor is equal to the damage buffer's scale |
| 95 // factor. |
| 96 // The scaling change can happen due to asynchronous updates of the DPI on a |
| 97 // resolution change. |
| 98 if (params.view_size.width() == damage_buffer_size().width() && |
| 99 params.view_size.height() == damage_buffer_size().height() && |
| 100 params.scale_factor == damage_buffer_scale_factor()) { |
| 101 TransportDIB* dib = render_view_host->GetProcess()-> |
| 102 GetTransportDIB(params.bitmap); |
| 103 if (dib) { |
| 104 void* guest_memory = dib->memory(); |
| 105 void* embedder_memory = damage_buffer_->memory(); |
| 106 size_t size = std::min(dib->size(), damage_buffer_->size()); |
| 107 memcpy(embedder_memory, guest_memory, size); |
| 108 } |
| 109 } |
| 110 DCHECK(embedder_render_process_host()); |
| 111 BrowserPluginMsg_UpdateRect_Params relay_params; |
| 112 relay_params.bitmap_rect = params.bitmap_rect; |
| 113 relay_params.dx = params.dx; |
| 114 relay_params.dy = params.dy; |
| 115 relay_params.scroll_rect = params.scroll_rect; |
| 116 relay_params.copy_rects = params.copy_rects; |
| 117 relay_params.view_size = params.view_size; |
| 118 relay_params.scale_factor = params.scale_factor; |
| 119 relay_params.is_resize_ack = ViewHostMsg_UpdateRect_Flags::is_resize_ack( |
| 120 params.flags); |
| 121 |
| 122 // We need to send the ACK to the same render_view_host that issued |
| 123 // the UpdateRect. We keep track of this correspondence via a message_id. |
| 124 int message_id = pending_update_counter_++; |
| 125 pending_updates_.AddWithID(render_view_host, message_id); |
| 126 |
| 127 gfx::Size param_size = gfx::Size(params.view_size.width(), |
| 128 params.view_size.height()); |
| 129 |
| 130 SendMessageToEmbedder(new BrowserPluginMsg_UpdateRect(instance_id(), |
| 131 message_id, |
| 132 relay_params)); |
| 133 } |
| 134 |
| 135 void BrowserPluginGuest::UpdateRectACK(int message_id, const gfx::Size& size) { |
| 136 RenderViewHost* render_view_host = pending_updates_.Lookup(message_id); |
| 137 // If the guest has crashed since it sent the initial ViewHostMsg_UpdateRect |
| 138 // then the pending_updates_ map will have been cleared. |
| 139 if (!render_view_host) |
| 140 return; |
| 141 pending_updates_.Remove(message_id); |
| 142 render_view_host->Send( |
| 143 new ViewMsg_UpdateRect_ACK(render_view_host->GetRoutingID())); |
| 144 if (!size.IsEmpty()) |
| 145 render_view_host->GetView()->SetSize(size); |
| 146 } |
| 147 |
| 148 void BrowserPluginGuest::HandleInputEvent(RenderViewHost* render_view_host, |
| 149 const gfx::Rect& guest_rect, |
| 150 const WebKit::WebInputEvent& event, |
| 151 IPC::Message* reply_message) { |
| 152 DCHECK(!pending_input_event_reply_.get()); |
| 153 guest_rect_ = guest_rect; |
| 154 RenderViewHostImpl* guest_rvh = static_cast<RenderViewHostImpl*>( |
| 155 web_contents()->GetRenderViewHost()); |
| 156 IPC::Message* message = new ViewMsg_HandleInputEvent( |
| 157 guest_rvh->GetRoutingID()); |
| 158 |
| 159 // Copy the WebInputEvent and modify the event type. The guest expects |
| 160 // WebInputEvent::RawKeyDowns and not KeyDowns. |
| 161 scoped_array<char> input_buffer(new char[event.size]); |
| 162 memcpy(input_buffer.get(), &event, event.size); |
| 163 WebKit::WebInputEvent* input_event = |
| 164 reinterpret_cast<WebKit::WebInputEvent*>(input_buffer.get()); |
| 165 if (event.type == WebKit::WebInputEvent::KeyDown) |
| 166 input_event->type = WebKit::WebInputEvent::RawKeyDown; |
| 167 |
| 168 message->WriteData(input_buffer.get(), event.size); |
| 169 // TODO(fsamuel): What do we need to do here? This is for keyboard shortcuts. |
| 170 if (input_event->type == WebKit::WebInputEvent::RawKeyDown) |
| 171 message->WriteBool(false); |
| 172 bool sent = guest_rvh->Send(message); |
| 173 if (!sent) { |
| 174 // If the embedder is waiting for a previous input ack, a new input message |
| 175 // won't get sent to the guest, reply immediately with handled = false so |
| 176 // embedder doesn't hang. |
| 177 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams( |
| 178 reply_message, false /* handled */, cursor_); |
| 179 embedder_render_process_host()->Send(reply_message); |
| 180 return; |
| 181 } |
| 182 |
| 183 pending_input_event_reply_.reset(reply_message); |
| 184 // Input events are handled synchronously, meaning it blocks the embedder. We |
| 185 // set a hang monitor here that will kill the guest process (5s timeout) if we |
| 186 // don't receive an ack. This will kill all the guests that are running in the |
| 187 // same process (undesired behavior). |
| 188 // TODO(fsamuel,lazyboy): Find a way to get rid of guest process kill |
| 189 // behavior. http://crbug.com/147272. |
| 190 guest_rvh->StartHangMonitorTimeout( |
| 191 base::TimeDelta::FromMilliseconds(guest_hang_timeout_ms_)); |
| 192 } |
| 193 |
| 194 void BrowserPluginGuest::HandleInputEventAck(RenderViewHost* render_view_host, |
| 195 bool handled) { |
| 196 RenderViewHostImpl* guest_rvh = |
| 197 static_cast<RenderViewHostImpl*>(render_view_host); |
| 198 guest_rvh->StopHangMonitorTimeout(); |
| 199 DCHECK(pending_input_event_reply_.get()); |
| 200 IPC::Message* reply_message = pending_input_event_reply_.release(); |
| 201 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, |
| 202 handled, |
| 203 cursor_); |
| 204 SendMessageToEmbedder(reply_message); |
| 205 } |
| 206 |
| 207 void BrowserPluginGuest::SetFocus(bool focused) { |
| 208 RenderViewHost* render_view_host = web_contents()->GetRenderViewHost(); |
| 209 render_view_host->Send( |
| 210 new ViewMsg_SetFocus(render_view_host->GetRoutingID(), focused)); |
| 211 } |
| 212 |
| 213 void BrowserPluginGuest::ShowWidget(RenderViewHost* render_view_host, |
| 214 int route_id, |
| 215 const gfx::Rect& initial_pos) { |
| 216 gfx::Rect screen_pos(initial_pos); |
| 217 screen_pos.Offset(guest_rect_.origin()); |
| 218 static_cast<WebContentsImpl*>(web_contents())->ShowCreatedWidget(route_id, |
| 219 screen_pos); |
| 220 } |
| 221 |
| 222 void BrowserPluginGuest::SetCursor(const WebCursor& cursor) { |
| 223 cursor_ = cursor; |
| 224 } |
| 225 |
| 226 void BrowserPluginGuest::DidCommitProvisionalLoadForFrame( |
| 227 int64 frame_id, |
| 228 bool is_main_frame, |
| 229 const GURL& url, |
| 230 PageTransition transition_type, |
| 231 RenderViewHost* render_view_host) { |
| 232 // Inform its embedder of the updated URL. |
| 233 DCHECK(embedder_render_process_host()); |
| 234 if (is_main_frame) |
| 235 SendMessageToEmbedder(new BrowserPluginMsg_DidNavigate(instance_id(), url)); |
| 236 } |
| 237 |
| 238 void BrowserPluginGuest::RenderViewGone(base::TerminationStatus status) { |
| 239 DCHECK(embedder_render_process_host()); |
| 240 if (pending_input_event_reply_.get()) { |
| 241 IPC::Message* reply_message = pending_input_event_reply_.release(); |
| 242 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, |
| 243 false, |
| 244 cursor_); |
| 245 SendMessageToEmbedder(reply_message); |
| 246 } |
| 247 SendMessageToEmbedder(new BrowserPluginMsg_GuestCrashed(instance_id())); |
| 248 IDMap<RenderViewHost>::const_iterator iter(&pending_updates_); |
| 249 while (!iter.IsAtEnd()) { |
| 250 pending_updates_.Remove(iter.GetCurrentKey()); |
| 251 iter.Advance(); |
| 252 } |
| 253 } |
| 254 |
| 255 void BrowserPluginGuest::SendMessageToEmbedder(IPC::Message* msg) { |
| 256 embedder_render_process_host()->Send(msg); |
| 257 } |
| 258 |
| 259 } // namespace content |
OLD | NEW |