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 // render_view_host manages the ownership of this BrowserPluginGuestHelper. |
| 40 new BrowserPluginGuestHelper(this, render_view_host); |
| 41 } |
| 42 |
| 43 BrowserPluginGuest::~BrowserPluginGuest() { |
| 44 if (damage_buffer_) |
| 45 delete damage_buffer_; |
| 46 } |
| 47 |
| 48 // static |
| 49 BrowserPluginGuest* BrowserPluginGuest::Create( |
| 50 int instance_id, |
| 51 WebContentsImpl* web_contents, |
| 52 content::RenderViewHost* render_view_host) { |
| 53 if (factory_) { |
| 54 return factory_->CreateBrowserPluginGuest(instance_id, |
| 55 web_contents, |
| 56 render_view_host); |
| 57 } |
| 58 return new BrowserPluginGuest(instance_id, web_contents, render_view_host); |
| 59 } |
| 60 |
| 61 bool BrowserPluginGuest::TakeFocus(bool reverse) { |
| 62 SendMessageToEmbedder( |
| 63 new BrowserPluginMsg_AdvanceFocus(instance_id(), reverse)); |
| 64 return true; |
| 65 } |
| 66 |
| 67 void BrowserPluginGuest::RendererUnresponsive(WebContents* source) { |
| 68 base::ProcessHandle process_handle = |
| 69 web_contents()->GetRenderProcessHost()->GetHandle(); |
| 70 base::KillProcess(process_handle, RESULT_CODE_HUNG, false); |
| 71 } |
| 72 |
| 73 void BrowserPluginGuest::SetDamageBuffer( |
| 74 TransportDIB* damage_buffer, const gfx::Size& size, float scale_factor) { |
| 75 if (damage_buffer_) |
| 76 delete damage_buffer_; |
| 77 damage_buffer_ = damage_buffer; |
| 78 damage_buffer_size_ = size; |
| 79 damage_buffer_scale_factor_ = scale_factor; |
| 80 } |
| 81 |
| 82 void BrowserPluginGuest::UpdateRect( |
| 83 RenderViewHost* render_view_host, |
| 84 const ViewHostMsg_UpdateRect_Params& params) { |
| 85 // This handler is only of interest to us for the 2D software rendering path. |
| 86 // needs_ack should always be true for the 2D path. |
| 87 // TODO(fsamuel): Do we need to do something different in the 3D case? |
| 88 if (!params.needs_ack) |
| 89 return; |
| 90 |
| 91 // Only copy damage if the guest's view size is equal to the damage buffer's |
| 92 // size and the guest's scale factor is equal to the damage buffer's scale |
| 93 // factor. |
| 94 if (params.view_size.width() == damage_buffer_size().width() && |
| 95 params.view_size.height() == damage_buffer_size().height() && |
| 96 params.scale_factor == damage_buffer_scale_factor()) { |
| 97 TransportDIB* dib = render_view_host->GetProcess()-> |
| 98 GetTransportDIB(params.bitmap); |
| 99 if (dib) { |
| 100 void* guest_memory = dib->memory(); |
| 101 void* embedder_memory = damage_buffer_->memory(); |
| 102 int size = std::min(dib->size(), damage_buffer_->size()); |
| 103 memcpy(embedder_memory, guest_memory, size); |
| 104 } |
| 105 } |
| 106 DCHECK(embedder_render_process_host()); |
| 107 BrowserPluginMsg_UpdateRect_Params relay_params; |
| 108 relay_params.bitmap_rect = params.bitmap_rect; |
| 109 relay_params.dx = params.dx; |
| 110 relay_params.dy = params.dy; |
| 111 relay_params.scroll_rect = params.scroll_rect; |
| 112 relay_params.copy_rects = params.copy_rects; |
| 113 relay_params.view_size = params.view_size; |
| 114 relay_params.scale_factor = params.scale_factor; |
| 115 relay_params.is_resize_ack = ViewHostMsg_UpdateRect_Flags::is_resize_ack( |
| 116 params.flags); |
| 117 |
| 118 // We need to send the ACK to the same render_view_host that issued |
| 119 // the UpdateRect. We keep track of this correspondence via a message_id. |
| 120 int message_id = pending_update_counter_++; |
| 121 pending_updates_.AddWithID(render_view_host, message_id); |
| 122 |
| 123 gfx::Size param_size = gfx::Size(params.view_size.width(), |
| 124 params.view_size.height()); |
| 125 |
| 126 SendMessageToEmbedder(new BrowserPluginMsg_UpdateRect(instance_id(), |
| 127 message_id, |
| 128 relay_params)); |
| 129 } |
| 130 |
| 131 void BrowserPluginGuest::UpdateRectACK(int message_id, const gfx::Size& size) { |
| 132 RenderViewHost* render_view_host = pending_updates_.Lookup(message_id); |
| 133 // If the guest has crashed since it sent the initial ViewHostMsg_UpdateRect |
| 134 // then the pending_updates_ map will have been cleared. |
| 135 if (!render_view_host) |
| 136 return; |
| 137 pending_updates_.Remove(message_id); |
| 138 render_view_host->Send( |
| 139 new ViewMsg_UpdateRect_ACK(render_view_host->GetRoutingID())); |
| 140 if (!size.IsEmpty()) |
| 141 render_view_host->GetView()->SetSize(size); |
| 142 } |
| 143 |
| 144 void BrowserPluginGuest::HandleInputEvent(RenderViewHost* render_view_host, |
| 145 const gfx::Rect& guest_rect, |
| 146 const WebKit::WebInputEvent& event, |
| 147 IPC::Message* reply_message) { |
| 148 guest_rect_ = guest_rect; |
| 149 RenderViewHostImpl* guest_rvh = static_cast<RenderViewHostImpl*>( |
| 150 web_contents()->GetRenderViewHost()); |
| 151 IPC::Message* message = new ViewMsg_HandleInputEvent( |
| 152 guest_rvh->GetRoutingID()); |
| 153 |
| 154 // Copy the WebInputEvent and modify the event type. The guest expects |
| 155 // WebInputEvent::RawKeyDowns and not KeyDowns. |
| 156 scoped_array<char> input_buffer(new char[event.size]); |
| 157 memcpy(input_buffer.get(), &event, event.size); |
| 158 WebKit::WebInputEvent* input_event = |
| 159 reinterpret_cast<WebKit::WebInputEvent*>(input_buffer.get()); |
| 160 if (event.type == WebKit::WebInputEvent::KeyDown) |
| 161 input_event->type = WebKit::WebInputEvent::RawKeyDown; |
| 162 |
| 163 message->WriteData(input_buffer.get(), event.size); |
| 164 // TODO(fsamuel): What do we need to do here? This is for keyboard shortcuts. |
| 165 if (input_event->type == WebKit::WebInputEvent::RawKeyDown) |
| 166 message->WriteBool(false); |
| 167 guest_rvh->Send(message); |
| 168 |
| 169 DCHECK(!pending_input_event_reply_.get()); |
| 170 pending_input_event_reply_.reset(reply_message); |
| 171 guest_rvh->StartHangMonitorTimeout( |
| 172 base::TimeDelta::FromMilliseconds(kGuestHangTimeout)); |
| 173 } |
| 174 |
| 175 void BrowserPluginGuest::HandleInputEventAck(RenderViewHost* render_view_host, |
| 176 bool handled) { |
| 177 DCHECK(pending_input_event_reply_.get()); |
| 178 IPC::Message* reply_message = pending_input_event_reply_.release(); |
| 179 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, |
| 180 handled, |
| 181 cursor_); |
| 182 SendMessageToEmbedder(reply_message); |
| 183 RenderViewHostImpl* guest_rvh = |
| 184 static_cast<RenderViewHostImpl*>(render_view_host); |
| 185 guest_rvh->StopHangMonitorTimeout(); |
| 186 } |
| 187 |
| 188 void BrowserPluginGuest::SetFocus(bool focused) { |
| 189 RenderViewHost* render_view_host = web_contents()->GetRenderViewHost(); |
| 190 render_view_host->Send( |
| 191 new ViewMsg_SetFocus(render_view_host->GetRoutingID(), focused)); |
| 192 } |
| 193 |
| 194 void BrowserPluginGuest::ShowWidget(RenderViewHost* render_view_host, |
| 195 int route_id, |
| 196 const gfx::Rect& initial_pos) { |
| 197 gfx::Rect screen_pos(initial_pos); |
| 198 screen_pos.Offset(guest_rect_.origin()); |
| 199 static_cast<WebContentsImpl*>(web_contents())->ShowCreatedWidget(route_id, |
| 200 screen_pos); |
| 201 } |
| 202 |
| 203 void BrowserPluginGuest::SetCursor(const WebCursor& cursor) { |
| 204 cursor_ = cursor; |
| 205 } |
| 206 |
| 207 void BrowserPluginGuest::Destroy() { |
| 208 WebContents* guest_web_contents = web_contents(); |
| 209 DCHECK(guest_web_contents); |
| 210 delete guest_web_contents; |
| 211 } |
| 212 |
| 213 void BrowserPluginGuest::DidCommitProvisionalLoadForFrame( |
| 214 int64 frame_id, |
| 215 bool is_main_frame, |
| 216 const GURL& url, |
| 217 PageTransition transition_type, |
| 218 RenderViewHost* render_view_host) { |
| 219 // Inform its embedder of the updated URL. |
| 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 |