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 kGuestHangTimeout = 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 // |render_view_host| manages the ownership of this BrowserPluginGuestHelper. | |
41 new BrowserPluginGuestHelper(this, render_view_host); | |
42 } | |
43 | |
44 BrowserPluginGuest::~BrowserPluginGuest() { | |
45 } | |
46 | |
47 // static | |
48 BrowserPluginGuest* BrowserPluginGuest::Create( | |
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::ViewTakeFocus(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 damage_buffer_.reset(damage_buffer); | |
75 damage_buffer_size_ = size; | |
76 damage_buffer_scale_factor_ = scale_factor; | |
77 } | |
78 | |
79 void BrowserPluginGuest::UpdateRect( | |
80 RenderViewHost* render_view_host, | |
81 const ViewHostMsg_UpdateRect_Params& params) { | |
82 // This handler is only of interest to us for the 2D software rendering path. | |
83 // needs_ack should always be true for the 2D path. | |
84 // TODO(fsamuel): Do we need to do something different in the 3D case? | |
85 if (!params.needs_ack) | |
86 return; | |
87 | |
88 // Only copy damage if the guest's view size is equal to the damage buffer's | |
89 // size and the guest's scale factor is equal to the damage buffer's scale | |
90 // factor. | |
awong
2012/09/06 19:55:26
If the scale factor is unequal (I presume this can
Fady Samuel
2012/09/06 20:56:10
Scaling has nothing to do with zooming.
Scaling i
awong
2012/09/07 20:51:03
Ah. So should this really be a DCHECK?
I'm trying
lazyboy
2012/09/08 02:12:22
Fady to comment on this one.
On 2012/09/07 20:51:
Fady Samuel
2012/09/10 16:24:11
No, this shouldn't be a DCHECK. When we change the
awong
2012/09/10 19:21:07
Ah hah! That's the piece I was missing. Can we ad
lazyboy
2012/09/10 23:40:56
Added the line.
Done.
| |
91 if (params.view_size.width() == damage_buffer_size().width() && | |
92 params.view_size.height() == damage_buffer_size().height() && | |
93 params.scale_factor == damage_buffer_scale_factor()) { | |
94 TransportDIB* dib = render_view_host->GetProcess()-> | |
95 GetTransportDIB(params.bitmap); | |
96 if (dib) { | |
97 void* guest_memory = dib->memory(); | |
98 void* embedder_memory = damage_buffer_->memory(); | |
99 size_t size = std::min(dib->size(), damage_buffer_->size()); | |
100 memcpy(embedder_memory, guest_memory, size); | |
101 } | |
102 } | |
103 DCHECK(embedder_render_process_host()); | |
104 BrowserPluginMsg_UpdateRect_Params relay_params; | |
105 relay_params.bitmap_rect = params.bitmap_rect; | |
106 relay_params.dx = params.dx; | |
107 relay_params.dy = params.dy; | |
108 relay_params.scroll_rect = params.scroll_rect; | |
109 relay_params.copy_rects = params.copy_rects; | |
110 relay_params.view_size = params.view_size; | |
111 relay_params.scale_factor = params.scale_factor; | |
112 relay_params.is_resize_ack = ViewHostMsg_UpdateRect_Flags::is_resize_ack( | |
113 params.flags); | |
114 | |
115 // We need to send the ACK to the same render_view_host that issued | |
116 // the UpdateRect. We keep track of this correspondence via a message_id. | |
117 int message_id = pending_update_counter_++; | |
118 pending_updates_.AddWithID(render_view_host, message_id); | |
119 | |
120 gfx::Size param_size = gfx::Size(params.view_size.width(), | |
121 params.view_size.height()); | |
122 | |
123 SendMessageToEmbedder(new BrowserPluginMsg_UpdateRect(instance_id(), | |
124 message_id, | |
125 relay_params)); | |
126 } | |
127 | |
128 void BrowserPluginGuest::UpdateRectACK(int message_id, const gfx::Size& size) { | |
129 RenderViewHost* render_view_host = pending_updates_.Lookup(message_id); | |
130 // If the guest has crashed since it sent the initial ViewHostMsg_UpdateRect | |
131 // then the pending_updates_ map will have been cleared. | |
132 if (!render_view_host) | |
133 return; | |
134 pending_updates_.Remove(message_id); | |
135 render_view_host->Send( | |
136 new ViewMsg_UpdateRect_ACK(render_view_host->GetRoutingID())); | |
137 if (!size.IsEmpty()) | |
138 render_view_host->GetView()->SetSize(size); | |
139 } | |
140 | |
141 void BrowserPluginGuest::HandleInputEvent(RenderViewHost* render_view_host, | |
142 const gfx::Rect& guest_rect, | |
143 const WebKit::WebInputEvent& event, | |
144 IPC::Message* reply_message) { | |
145 guest_rect_ = guest_rect; | |
146 RenderViewHostImpl* guest_rvh = static_cast<RenderViewHostImpl*>( | |
147 web_contents()->GetRenderViewHost()); | |
148 IPC::Message* message = new ViewMsg_HandleInputEvent( | |
149 guest_rvh->GetRoutingID()); | |
150 | |
151 // Copy the WebInputEvent and modify the event type. The guest expects | |
152 // WebInputEvent::RawKeyDowns and not KeyDowns. | |
153 scoped_array<char> input_buffer(new char[event.size]); | |
154 memcpy(input_buffer.get(), &event, event.size); | |
155 WebKit::WebInputEvent* input_event = | |
156 reinterpret_cast<WebKit::WebInputEvent*>(input_buffer.get()); | |
157 if (event.type == WebKit::WebInputEvent::KeyDown) | |
158 input_event->type = WebKit::WebInputEvent::RawKeyDown; | |
159 | |
160 message->WriteData(input_buffer.get(), event.size); | |
161 // TODO(fsamuel): What do we need to do here? This is for keyboard shortcuts. | |
162 if (input_event->type == WebKit::WebInputEvent::RawKeyDown) | |
163 message->WriteBool(false); | |
164 guest_rvh->Send(message); | |
165 | |
166 DCHECK(!pending_input_event_reply_.get()); | |
167 pending_input_event_reply_.reset(reply_message); | |
168 guest_rvh->StartHangMonitorTimeout( | |
169 base::TimeDelta::FromMilliseconds(kGuestHangTimeout)); | |
170 } | |
171 | |
172 void BrowserPluginGuest::HandleInputEventAck(RenderViewHost* render_view_host, | |
173 bool handled) { | |
174 DCHECK(pending_input_event_reply_.get()); | |
175 IPC::Message* reply_message = pending_input_event_reply_.release(); | |
176 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, | |
177 handled, | |
178 cursor_); | |
179 SendMessageToEmbedder(reply_message); | |
180 RenderViewHostImpl* guest_rvh = | |
181 static_cast<RenderViewHostImpl*>(render_view_host); | |
182 guest_rvh->StopHangMonitorTimeout(); | |
183 } | |
184 | |
185 void BrowserPluginGuest::SetFocus(bool focused) { | |
186 RenderViewHost* render_view_host = web_contents()->GetRenderViewHost(); | |
187 render_view_host->Send( | |
188 new ViewMsg_SetFocus(render_view_host->GetRoutingID(), focused)); | |
189 } | |
190 | |
191 void BrowserPluginGuest::ShowWidget(RenderViewHost* render_view_host, | |
192 int route_id, | |
193 const gfx::Rect& initial_pos) { | |
194 gfx::Rect screen_pos(initial_pos); | |
195 screen_pos.Offset(guest_rect_.origin()); | |
196 static_cast<WebContentsImpl*>(web_contents())->ShowCreatedWidget(route_id, | |
197 screen_pos); | |
198 } | |
199 | |
200 void BrowserPluginGuest::SetCursor(const WebCursor& cursor) { | |
201 cursor_ = cursor; | |
202 } | |
203 | |
204 void BrowserPluginGuest::Destroy() { | |
205 WebContents* guest_web_contents = web_contents(); | |
206 DCHECK(guest_web_contents); | |
207 delete guest_web_contents; | |
208 } | |
209 | |
210 void BrowserPluginGuest::DidCommitProvisionalLoadForFrame( | |
211 int64 frame_id, | |
212 bool is_main_frame, | |
213 const GURL& url, | |
214 PageTransition transition_type, | |
215 RenderViewHost* render_view_host) { | |
216 // Inform its embedder of the updated URL. | |
217 DCHECK(embedder_render_process_host()); | |
218 if (is_main_frame) | |
219 SendMessageToEmbedder(new BrowserPluginMsg_DidNavigate(instance_id(), url)); | |
220 } | |
221 | |
222 void BrowserPluginGuest::RenderViewGone(base::TerminationStatus status) { | |
223 DCHECK(embedder_render_process_host()); | |
224 if (pending_input_event_reply_.get()) { | |
225 IPC::Message* reply_message = pending_input_event_reply_.release(); | |
226 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, | |
227 false, | |
228 cursor_); | |
229 SendMessageToEmbedder(reply_message); | |
230 } | |
231 SendMessageToEmbedder(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 } | |
238 | |
239 void BrowserPluginGuest::SendMessageToEmbedder(IPC::Message* msg) { | |
240 embedder_render_process_host()->Send(msg); | |
241 } | |
242 | |
243 } // namespace content | |
OLD | NEW |