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