blob: a1159096f7b6aa5dec1bd7eea8f819cd2fad0b96 [file] [log] [blame]
[email protected]c5218762011-03-24 17:48:511// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2cff0052011-03-18 16:51:445#include "content/renderer/render_widget.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]4fb66842009-12-04 21:41:007#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/logging.h"
9#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/scoped_ptr.h"
[email protected]661eb9d2009-02-03 02:11:4812#include "build/build_config.h"
[email protected]4fb66842009-12-04 21:41:0013#include "chrome/common/chrome_switches.h"
[email protected]674741932009-02-04 23:44:4614#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0215#include "chrome/common/render_messages_params.h"
[email protected]8085dbc82008-09-26 22:53:4416#include "chrome/renderer/render_process.h"
[email protected]00c39612010-03-06 02:53:2817#include "chrome/renderer/render_thread.h"
[email protected]778574e2011-03-21 22:03:5018#include "content/common/view_messages.h"
[email protected]acb94722011-03-18 01:33:3419#include "content/renderer/renderer_webkitclient_impl.h"
[email protected]71e2f0a2011-03-15 22:25:0820#include "gpu/common/gpu_trace_event.h"
[email protected]484955942010-08-19 16:13:1821#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4822#include "skia/ext/platform_canvas.h"
[email protected]d5282e72009-05-13 13:16:5223#include "third_party/skia/include/core/SkShader.h"
[email protected]8bd0fe62011-01-17 06:44:3724#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
25#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
26#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
27#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h"
28#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
29#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
[email protected]08397d52011-02-05 01:53:3830#include "ui/gfx/point.h"
31#include "ui/gfx/size.h"
[email protected]b9b751f22011-03-25 14:04:1232#include "ui/gfx/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3433#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5034#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4635#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4836
37#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4938#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5239#include "third_party/skia/include/core/SkPixelRef.h"
40#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4841#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4442
[email protected]8bd0fe62011-01-17 06:44:3743#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2944
[email protected]fa7b1dc2010-06-23 17:53:0445using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3046using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2247using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0648using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2849using WebKit::WebNavigationPolicy;
50using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5951using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1852using WebKit::WebPopupType;
[email protected]b3f2b912009-04-09 16:18:5253using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1954using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5255using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2856using WebKit::WebTextDirection;
[email protected]fa7b1dc2010-06-23 17:53:0457using WebKit::WebTextInputType;
58using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1859using WebKit::WebWidget;
[email protected]62cb33cae2009-03-27 23:30:2260
[email protected]3e2b375b2010-04-07 17:03:1261RenderWidget::RenderWidget(RenderThreadBase* render_thread,
62 WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2963 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1164 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2965 opener_id_(MSG_ROUTING_NONE),
[email protected]8085dbc82008-09-26 22:53:4466 render_thread_(render_thread),
[email protected]659f73f2009-10-13 13:43:4267 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3568 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2969 next_paint_flags_(0),
[email protected]53d3f302009-12-21 04:42:0570 update_reply_pending_(false),
initial.commit09911bf2008-07-26 23:55:2971 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2972 is_hidden_(false),
73 needs_repainting_on_restore_(false),
74 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4975 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4876 closing_(false),
[email protected]fa7b1dc2010-06-23 17:53:0477 input_method_is_active_(false),
78 text_input_type_(WebKit::WebTextInputTypeNone),
[email protected]3e2b375b2010-04-07 17:03:1279 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4880 pending_window_rect_count_(0),
[email protected]edbcde932010-05-07 17:10:4681 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5082 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2183 animation_update_pending_(false),
[email protected]52ccd0ea2011-02-16 01:09:0584 animation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:2885 RenderProcess::current()->AddRefProcess();
[email protected]8085dbc82008-09-26 22:53:4486 DCHECK(render_thread_);
initial.commit09911bf2008-07-26 23:55:2987}
88
89RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:1190 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]b4d08452010-10-05 17:34:3591 if (current_paint_buf_) {
92 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
93 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:2994 }
[email protected]8930d472009-02-21 08:05:2895 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:2996}
97
[email protected]484955942010-08-19 16:13:1898// static
[email protected]8085dbc82008-09-26 22:53:4499RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]0ebf3872008-11-07 21:35:03100 RenderThreadBase* render_thread,
[email protected]3e2b375b2010-04-07 17:03:12101 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:29102 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]ad8e04a2010-11-01 04:16:27103 scoped_refptr<RenderWidget> widget(new RenderWidget(render_thread,
104 popup_type));
initial.commit09911bf2008-07-26 23:55:29105 widget->Init(opener_id); // adds reference
106 return widget;
107}
108
[email protected]484955942010-08-19 16:13:18109// static
110WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
111 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03112 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18113 break;
114 case WebKit::WebPopupTypeSelect:
115 case WebKit::WebPopupTypeSuggestion:
116 return WebPopupMenu::create(render_widget);
117 default:
118 NOTREACHED();
119 }
120 return NULL;
121}
122
initial.commit09911bf2008-07-26 23:55:29123void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18124 DoInit(opener_id,
125 RenderWidget::CreateWebWidget(this),
126 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
127}
128
[email protected]484955942010-08-19 16:13:18129void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06130 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18131 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29132 DCHECK(!webwidget_);
133
134 if (opener_id != MSG_ROUTING_NONE)
135 opener_id_ = opener_id;
136
[email protected]484955942010-08-19 16:13:18137 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29138
[email protected]484955942010-08-19 16:13:18139 bool result = render_thread_->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29140 if (result) {
[email protected]8085dbc82008-09-26 22:53:44141 render_thread_->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29142 // Take a reference on behalf of the RenderThread. This will be balanced
143 // when we receive ViewMsg_Close.
144 AddRef();
145 } else {
146 DCHECK(false);
147 }
148}
149
150// This is used to complete pending inits and non-pending inits. For non-
151// pending cases, the parent will be the same as the current parent. This
152// indicates we do not need to reparent or anything.
[email protected]18bcc3c2009-01-27 21:39:15153void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29154 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29155
156 host_window_ = parent_hwnd;
157
[email protected]6de74452009-02-25 18:04:59158 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29159}
160
[email protected]a95986a82010-12-24 06:19:28161bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
162 bool handled = true;
163 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
164 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
165 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
166 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
167 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
168 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
169 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
170 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
171 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
172 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
173 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
174 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
175 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
176 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
177 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
178 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
179 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
180 IPC_MESSAGE_UNHANDLED(handled = false)
181 IPC_END_MESSAGE_MAP()
182 return handled;
183}
initial.commit09911bf2008-07-26 23:55:29184
185bool RenderWidget::Send(IPC::Message* message) {
186 // Don't send any messages after the browser has told us to close.
187 if (closing_) {
188 delete message;
189 return false;
190 }
191
192 // If given a messsage without a routing ID, then assign our routing ID.
193 if (message->routing_id() == MSG_ROUTING_NONE)
194 message->set_routing_id(routing_id_);
195
[email protected]d3fc25652009-02-24 22:31:25196 return render_thread_->Send(message);
[email protected]8085dbc82008-09-26 22:53:44197}
198
initial.commit09911bf2008-07-26 23:55:29199// Got a response from the browser after the renderer decided to create a new
200// view.
[email protected]18bcc3c2009-01-27 21:39:15201void RenderWidget::OnCreatingNewAck(gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29202 DCHECK(routing_id_ != MSG_ROUTING_NONE);
203
204 CompleteInit(parent);
205}
206
207void RenderWidget::OnClose() {
208 if (closing_)
209 return;
210 closing_ = true;
211
212 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03213 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]8085dbc82008-09-26 22:53:44214 render_thread_->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03215 SetHidden(false);
216 }
initial.commit09911bf2008-07-26 23:55:29217
initial.commit09911bf2008-07-26 23:55:29218 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25219 // now. Post a task that only gets invoked when there are no nested message
220 // loops.
221 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
222 NewRunnableMethod(this, &RenderWidget::Close));
223
224 // Balances the AddRef taken when we called AddRoute.
225 Release();
initial.commit09911bf2008-07-26 23:55:29226}
227
[email protected]f21c613a2009-02-12 14:46:17228void RenderWidget::OnResize(const gfx::Size& new_size,
229 const gfx::Rect& resizer_rect) {
initial.commit09911bf2008-07-26 23:55:29230 // During shutdown we can just ignore this message.
231 if (!webwidget_)
232 return;
233
[email protected]e2356242010-11-16 22:33:03234 // We shouldn't be asked to resize to our current size.
235 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
236
[email protected]f21c613a2009-02-12 14:46:17237 // Remember the rect where the resize corner will be drawn.
238 resizer_rect_ = resizer_rect;
239
[email protected]e2356242010-11-16 22:33:03240 if (size_ == new_size)
241 return;
242
initial.commit09911bf2008-07-26 23:55:29243 // TODO(darin): We should not need to reset this here.
[email protected]bee16aab2009-08-26 15:55:03244 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29245 needs_repainting_on_restore_ = false;
246
initial.commit09911bf2008-07-26 23:55:29247 size_ = new_size;
248
249 // We should not be sent a Resize message if we have not ACK'd the previous
250 DCHECK(!next_paint_is_resize_ack());
251
[email protected]552e6002009-11-19 05:24:57252 paint_aggregator_.ClearPendingUpdate();
253
initial.commit09911bf2008-07-26 23:55:29254 // When resizing, we want to wait to paint before ACK'ing the resize. This
255 // ensures that we only resize as fast as we can paint. We only need to send
256 // an ACK if we are resized to a non-empty rect.
[email protected]4873c7d2009-07-16 06:36:28257 webwidget_->resize(new_size);
initial.commit09911bf2008-07-26 23:55:29258 if (!new_size.IsEmpty()) {
[email protected]a79d8a632010-11-18 22:35:56259 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46260 // Resize should have caused an invalidation of the entire view.
261 DCHECK(paint_aggregator_.HasPendingUpdate());
262 }
[email protected]2d5d09d52009-06-15 14:29:21263
initial.commit09911bf2008-07-26 23:55:29264 // We will send the Resize_ACK flag once we paint again.
265 set_next_paint_is_resize_ack();
266 }
267}
268
269void RenderWidget::OnWasHidden() {
270 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03271 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29272}
273
274void RenderWidget::OnWasRestored(bool needs_repainting) {
275 // During shutdown we can just ignore this message.
276 if (!webwidget_)
277 return;
278
279 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03280 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29281
282 if (!needs_repainting && !needs_repainting_on_restore_)
283 return;
284 needs_repainting_on_restore_ = false;
285
[email protected]d65adb12010-04-28 17:26:49286 // Tag the next paint as a restore ack, which is picked up by
287 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29288 set_next_paint_is_restore_ack();
289
290 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56291 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46292 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
293 } else {
294 scheduleComposite();
295 }
initial.commit09911bf2008-07-26 23:55:29296}
297
[email protected]53d3f302009-12-21 04:42:05298void RenderWidget::OnRequestMoveAck() {
299 DCHECK(pending_window_rect_count_);
300 pending_window_rect_count_--;
301}
302
303void RenderWidget::OnUpdateRectAck() {
[email protected]c5218762011-03-24 17:48:51304 GPU_TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]53d3f302009-12-21 04:42:05305 DCHECK(update_reply_pending());
306 update_reply_pending_ = false;
307
[email protected]b4d08452010-10-05 17:34:35308 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
309 // have no current paint buffer.
310 if (current_paint_buf_) {
311 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
312 current_paint_buf_ = NULL;
313 }
314
[email protected]00c39612010-03-06 02:53:28315 // Notify subclasses.
316 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25317
initial.commit09911bf2008-07-26 23:55:29318 // Continue painting if necessary...
[email protected]552e6002009-11-19 05:24:57319 CallDoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29320}
321
initial.commit09911bf2008-07-26 23:55:29322void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
323 void* iter = NULL;
324
325 const char* data;
326 int data_length;
[email protected]5dd768212009-08-13 23:34:49327 handling_input_event_ = true;
328 if (!message.ReadData(&iter, &data, &data_length)) {
329 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29330 return;
[email protected]5dd768212009-08-13 23:34:49331 }
initial.commit09911bf2008-07-26 23:55:29332
333 const WebInputEvent* input_event =
334 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48335
336 bool is_keyboard_shortcut = false;
337 // is_keyboard_shortcut flag is only available for RawKeyDown events.
338 if (input_event->type == WebInputEvent::RawKeyDown)
339 message.ReadBool(&iter, &is_keyboard_shortcut);
340
initial.commit09911bf2008-07-26 23:55:29341 bool processed = false;
[email protected]867125a02009-12-10 06:01:48342 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
343 suppress_next_char_events_ = false;
344 if (webwidget_)
345 processed = webwidget_->handleInputEvent(*input_event);
346 }
347
348 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
349 // it's not processed by webkit, then we need to suppress the upcoming Char
350 // events.
351 if (!processed && is_keyboard_shortcut)
352 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29353
354 IPC::Message* response = new ViewHostMsg_HandleInputEvent_ACK(routing_id_);
355 response->WriteInt(input_event->type);
[email protected]e2824412009-02-27 01:57:05356 response->WriteBool(processed);
357
[email protected]353a34c2010-05-28 23:35:17358 if ((input_event->type == WebInputEvent::MouseMove ||
[email protected]d93fc462011-03-14 23:03:03359 input_event->type == WebInputEvent::MouseWheel ||
360 input_event->type == WebInputEvent::TouchMove) &&
[email protected]552e6002009-11-19 05:24:57361 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24362 // We want to rate limit the input events in this case, so we'll wait for
363 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17364 if (pending_input_event_ack_.get()) {
365 // As two different kinds of events could cause us to postpone an ack
366 // we send it now, if we have one pending. The Browser should never
367 // send us the same kind of event we are delaying the ack for.
368 Send(pending_input_event_ack_.release());
369 }
[email protected]12fbad812009-09-01 18:21:24370 pending_input_event_ack_.reset(response);
371 } else {
372 Send(response);
373 }
374
[email protected]5dd768212009-08-13 23:34:49375 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48376
[email protected]867125a02009-12-10 06:01:48377 if (WebInputEvent::isKeyboardEventType(input_event->type))
[email protected]446705872009-09-10 07:22:48378 DidHandleKeyEvent();
[email protected]6a8ddba52010-09-05 04:38:06379 if (WebInputEvent::isMouseEventType(input_event->type))
380 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
initial.commit09911bf2008-07-26 23:55:29381}
382
383void RenderWidget::OnMouseCaptureLost() {
384 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28385 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29386}
387
388void RenderWidget::OnSetFocus(bool enable) {
389 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33390 if (webwidget_)
391 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29392}
393
394void RenderWidget::ClearFocus() {
395 // We may have got the focus from the browser before this gets processed, in
396 // which case we do not want to unfocus ourself.
397 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28398 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29399}
400
[email protected]2d5d09d52009-06-15 14:29:21401void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00402 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21403 skia::PlatformCanvas* canvas) {
[email protected]719b36f2010-12-22 20:36:46404
[email protected]4fb66842009-12-04 21:41:00405 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21406
407 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00408 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
409 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03410
[email protected]699ab0d2009-04-23 23:19:14411 // If there is a custom background, tile it.
412 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14413 SkPaint paint;
414 SkShader* shader = SkShader::CreateBitmapShader(background_,
415 SkShader::kRepeat_TileMode,
416 SkShader::kRepeat_TileMode);
417 paint.setShader(shader)->unref();
[email protected]8860e4f52009-06-25 01:01:52418 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
[email protected]699ab0d2009-04-23 23:19:14419 canvas->drawPaint(paint);
[email protected]699ab0d2009-04-23 23:19:14420 }
421
[email protected]719b36f2010-12-22 20:36:46422 // First see if this rect is a plugin that can paint itself faster.
423 TransportDIB* optimized_dib = NULL;
424 gfx::Rect optimized_copy_rect, optimized_copy_location;
425 webkit::ppapi::PluginInstance* optimized_instance =
426 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
427 &optimized_copy_location,
428 &optimized_copy_rect);
429 if (optimized_instance) {
430 // This plugin can be optimize-painted and we can just ask it to paint
431 // itself. We don't actually need the TransportDIB in this case.
432 //
433 // This is an optimization for PPAPI plugins that know they're on top of
434 // the page content. If this rect is inside such a plugin, we can save some
435 // time and avoid re-rendering the page content which we know will be
436 // covered by the plugin later (this time can be significant, especially
437 // for a playing movie that is invalidating a lot).
438 //
439 // In the plugin movie case, hopefully the similar call to
440 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
441 // painting, because that avoids copying the plugin image to a different
442 // paint rect. Unfortunately, if anything on the page is animating other
443 // than the movie, it break this optimization since the union of the
444 // invalid regions will be larger than the plugin.
445 //
446 // This code optimizes that case, where we can still avoid painting in
447 // WebKit and filling the background (which can be slow) and just painting
448 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
449 // required.
450 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27451 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46452 } else {
453 // Normal painting case.
454 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
455
456 // Flush to underlying bitmap. TODO(darin): is this needed?
457 canvas->getTopPlatformDevice().accessBitmap(false);
458 }
initial.commit09911bf2008-07-26 23:55:29459
[email protected]4fb66842009-12-04 21:41:00460 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00461 canvas->restore();
462}
463
464void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
465 skia::PlatformCanvas* canvas) {
466 static bool kPaintBorder =
467 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
468 if (!kPaintBorder)
469 return;
470
[email protected]53d3f302009-12-21 04:42:05471 // Cycle through these colors to help distinguish new paint rects.
472 const SkColor colors[] = {
473 SkColorSetARGB(0x3F, 0xFF, 0, 0),
474 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
475 SkColorSetARGB(0x3F, 0, 0, 0xFF),
476 };
477 static int color_selector = 0;
478
[email protected]4fb66842009-12-04 21:41:00479 SkPaint paint;
480 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05481 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00482 paint.setStrokeWidth(1);
483
484 SkIRect irect;
485 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
486 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29487}
488
[email protected]52ccd0ea2011-02-16 01:09:05489void RenderWidget::AnimationCallback() {
490 animation_task_posted_ = false;
[email protected]7c4329e2011-02-18 22:02:59491 if (!animation_update_pending_)
492 return;
493 if (!animation_floor_time_.is_null()) {
494 // Record when we fired (according to base::Time::Now()) relative to when
495 // we posted the task to quantify how much the base::Time/base::TimeTicks
496 // skew is affecting animations.
497 base::TimeDelta animation_callback_delay = base::Time::Now() -
498 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
499 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
500 animation_callback_delay,
501 base::TimeDelta::FromMilliseconds(0),
502 base::TimeDelta::FromMilliseconds(30),
503 25);
504 }
[email protected]52ccd0ea2011-02-16 01:09:05505 CallDoDeferredUpdate();
[email protected]12fbad812009-09-01 18:21:24506}
507
[email protected]52ccd0ea2011-02-16 01:09:05508void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59509 if (!animation_update_pending_)
510 return;
511 base::Time now = base::Time::Now();
512 if (now >= animation_floor_time_) {
513 animation_floor_time_ = now + base::TimeDelta::FromMilliseconds(16);
514 // Set a timer to call us back after 16ms (targetting 60FPS) before
515 // running animation callbacks so that if a callback requests another
516 // we'll be sure to run it at the proper time.
517 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
518 this, &RenderWidget::AnimationCallback), 16);
519 animation_task_posted_ = true;
520 animation_update_pending_ = false;
521 // Explicitly pump the WebCore Timer queue to avoid starvation on OS X.
522 // See crbug.com/71735.
523 // TODO(jamesr) Remove this call once crbug.com/72007 is fixed.
524 RenderThread::current()->GetWebKitClientImpl()->DoTimeout();
525 webwidget_->animate();
526 return;
[email protected]5f8b1022011-01-21 23:34:50527 }
[email protected]7c4329e2011-02-18 22:02:59528 if (animation_task_posted_)
529 return;
530 // This code uses base::Time::Now() to calculate the floor and next fire
531 // time because javascript's Date object uses base::Time::Now(). The
532 // message loop uses base::TimeTicks, which on windows can have a
533 // different granularity than base::Time.
534 // The upshot of all this is that this function might be called before
535 // base::Time::Now() has advanced past the animation_floor_time_. To
536 // avoid exposing this delay to javascript, we keep posting delayed
537 // tasks until base::Time::Now() has advanced far enough.
538 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
539 animation_task_posted_ = true;
540 MessageLoop::current()->PostDelayedTask(FROM_HERE,
541 NewRunnableMethod(this, &RenderWidget::AnimationCallback), delay);
[email protected]5f8b1022011-01-21 23:34:50542}
543
[email protected]52ccd0ea2011-02-16 01:09:05544void RenderWidget::CallDoDeferredUpdate() {
545 DoDeferredUpdate();
546
547 if (pending_input_event_ack_.get())
548 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21549}
550
[email protected]552e6002009-11-19 05:24:57551void RenderWidget::DoDeferredUpdate() {
[email protected]c5218762011-03-24 17:48:51552 GPU_TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08553
[email protected]5f8b1022011-01-21 23:34:50554 if (!webwidget_ || update_reply_pending())
initial.commit09911bf2008-07-26 23:55:29555 return;
556
[email protected]552e6002009-11-19 05:24:57557 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29558 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57559 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29560 needs_repainting_on_restore_ = true;
561 return;
562 }
563
[email protected]52ccd0ea2011-02-16 01:09:05564 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50565
[email protected]f98d7e3c2010-09-13 22:30:46566 // Layout may generate more invalidation. It may also enable the
567 // GPU acceleration, so make sure to run layout before we send the
568 // GpuRenderingActivated message.
569 webwidget_->layout();
570
[email protected]5f8b1022011-01-21 23:34:50571 // Suppress painting if nothing is dirty. This has to be done after updating
572 // animations running layout as these may generate further invalidations.
573 if (!paint_aggregator_.HasPendingUpdate())
574 return;
575
[email protected]552e6002009-11-19 05:24:57576 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29577 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30578 PaintAggregator::PendingUpdate update;
579 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29580
[email protected]53d3f302009-12-21 04:42:05581 gfx::Rect scroll_damage = update.GetScrollDamage();
582 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29583
[email protected]27543452011-03-25 00:14:00584 // Compositing the page may disable accelerated compositing.
585 bool accelerated_compositing_was_active = is_accelerated_compositing_active_;
586
[email protected]ca4847f2010-09-24 05:39:15587 // A plugin may be able to do an optimized paint. First check this, in which
588 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46589 // This optimization allows PPAPI plugins that declare themselves on top of
590 // the page (like a traditional windowed plugin) to be able to animate (think
591 // movie playing) without repeatedly re-painting the page underneath, or
592 // copying the plugin backing store (since we can send the plugin's backing
593 // store directly to the browser).
594 //
595 // This optimization only works when the entire invalid region is contained
596 // within the plugin. There is a related optimization in PaintRect for the
597 // case where there may be multiple invalid regions.
[email protected]2650bd52011-03-25 00:34:44598 TransportDIB::Id dib_id = TransportDIB::Id();
[email protected]ca4847f2010-09-24 05:39:15599 TransportDIB* dib = NULL;
[email protected]cef3362f2009-12-21 17:48:45600 std::vector<gfx::Rect> copy_rects;
[email protected]ca4847f2010-09-24 05:39:15601 gfx::Rect optimized_copy_rect, optimized_copy_location;
602 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56603 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15604 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
605 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27606 // Only update the part of the plugin that actually changed.
607 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]ca4847f2010-09-24 05:39:15608 bounds = optimized_copy_location;
609 copy_rects.push_back(optimized_copy_rect);
610 dib_id = dib->id();
[email protected]a79d8a632010-11-18 22:35:56611 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46612 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15613 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35614 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
615 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46616 if (!canvas.get()) {
617 NOTREACHED();
618 return;
619 }
[email protected]cef3362f2009-12-21 17:48:45620
[email protected]f98d7e3c2010-09-13 22:30:46621 // We may get back a smaller canvas than we asked for.
622 // TODO(darin): This seems like it could cause painting problems!
623 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
624 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
625 bounds.set_width(canvas->getDevice()->width());
626 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05627
[email protected]f98d7e3c2010-09-13 22:30:46628 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
629
[email protected]f98d7e3c2010-09-13 22:30:46630 // The scroll damage is just another rectangle to paint and copy.
631 copy_rects.swap(update.paint_rects);
632 if (!scroll_damage.IsEmpty())
633 copy_rects.push_back(scroll_damage);
634
635 for (size_t i = 0; i < copy_rects.size(); ++i)
636 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]ca4847f2010-09-24 05:39:15637
[email protected]b4d08452010-10-05 17:34:35638 dib_id = current_paint_buf_->id();
[email protected]f98d7e3c2010-09-13 22:30:46639 } else { // Accelerated compositing path
640 // Begin painting.
[email protected]50bd6452010-11-27 19:39:42641 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46642 }
643
644 // sending an ack to browser process that the paint is complete...
[email protected]53d3f302009-12-21 04:42:05645 ViewHostMsg_UpdateRect_Params params;
[email protected]36808ad2010-10-20 19:18:30646 params.bitmap = dib_id;
[email protected]53d3f302009-12-21 04:42:05647 params.bitmap_rect = bounds;
648 params.dx = update.scroll_delta.x();
649 params.dy = update.scroll_delta.y();
[email protected]27543452011-03-25 00:14:00650 if (accelerated_compositing_was_active) {
[email protected]b167ca662010-05-14 00:05:34651 // If painting is done via the gpu process then we clear out all damage
652 // rects to save the browser process from doing unecessary work.
653 params.scroll_rect = gfx::Rect();
654 params.copy_rects.clear();
655 } else {
656 params.scroll_rect = update.scroll_rect;
657 params.copy_rects.swap(copy_rects); // TODO(darin): clip to bounds?
658 }
[email protected]53d3f302009-12-21 04:42:05659 params.view_size = size_;
[email protected]e2356242010-11-16 22:33:03660 params.resizer_rect = resizer_rect_;
[email protected]53d3f302009-12-21 04:42:05661 params.plugin_window_moves.swap(plugin_window_moves_);
662 params.flags = next_paint_flags_;
[email protected]d54169e92011-01-21 09:19:52663 params.scroll_offset = GetScrollOffset();
[email protected]53d3f302009-12-21 04:42:05664
665 update_reply_pending_ = true;
666 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
667 next_paint_flags_ = 0;
668
[email protected]fa7b1dc2010-06-23 17:53:04669 UpdateInputMethod();
[email protected]00c39612010-03-06 02:53:28670
671 // Let derived classes know we've painted.
672 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29673}
674
675///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46676// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29677
[email protected]4873c7d2009-07-16 06:36:28678void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:57679 // We only want one pending DoDeferredUpdate call at any time...
680 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29681
[email protected]552e6002009-11-19 05:24:57682 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48683 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57684 gfx::Rect damaged_rect = view_rect.Intersect(rect);
685 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29686 return;
687
[email protected]552e6002009-11-19 05:24:57688 paint_aggregator_.InvalidateRect(damaged_rect);
689
690 // We may not need to schedule another call to DoDeferredUpdate.
691 if (update_pending)
692 return;
693 if (!paint_aggregator_.HasPendingUpdate())
694 return;
[email protected]53d3f302009-12-21 04:42:05695 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57696 return;
697
698 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29699 // 1) Ensures that we call WebView::Paint without a bunch of other junk
700 // on the call stack.
701 // 2) Allows us to collect more damage rects before painting to help coalesce
702 // the work that we will need to do.
703 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57704 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29705}
706
[email protected]4873c7d2009-07-16 06:36:28707void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46708 // Drop scrolls on the floor when we are in compositing mode.
709 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56710 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46711 return;
712
[email protected]552e6002009-11-19 05:24:57713 // We only want one pending DoDeferredUpdate call at any time...
714 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29715
[email protected]552e6002009-11-19 05:24:57716 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48717 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57718 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
719 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29720 return;
721
[email protected]552e6002009-11-19 05:24:57722 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
723
724 // We may not need to schedule another call to DoDeferredUpdate.
725 if (update_pending)
726 return;
727 if (!paint_aggregator_.HasPendingUpdate())
728 return;
[email protected]53d3f302009-12-21 04:42:05729 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57730 return;
731
732 // Perform updating asynchronously. This serves two purposes:
733 // 1) Ensures that we call WebView::Paint without a bunch of other junk
734 // on the call stack.
735 // 2) Allows us to collect more damage rects before painting to help coalesce
736 // the work that we will need to do.
initial.commit09911bf2008-07-26 23:55:29737 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57738 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29739}
740
[email protected]a79d8a632010-11-18 22:35:56741void RenderWidget::didActivateAcceleratedCompositing(bool active) {
742 is_accelerated_compositing_active_ = active;
[email protected]50bd6452010-11-27 19:39:42743 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]a79d8a632010-11-18 22:35:56744 routing_id_, is_accelerated_compositing_active_));
745}
746
[email protected]f98d7e3c2010-09-13 22:30:46747void RenderWidget::scheduleComposite() {
748 // TODO(nduca): replace with something a little less hacky. The reason this
749 // hack is still used is because the Invalidate-DoDeferredUpdate loop
750 // contains a lot of host-renderer synchronization logic that is still
751 // important for the accelerated compositing case. The option of simply
752 // duplicating all that code is less desirable than "faking out" the
753 // invalidation path using a magical damage rect.
[email protected]e2356242010-11-16 22:33:03754 didInvalidateRect(WebRect(0, 0, 1, 1));
[email protected]f98d7e3c2010-09-13 22:30:46755}
756
[email protected]5f8b1022011-01-21 23:34:50757void RenderWidget::scheduleAnimation() {
[email protected]ee3d3ad2011-02-04 00:42:21758 if (!animation_update_pending_) {
759 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:05760 if (!animation_task_posted_) {
761 animation_task_posted_ = true;
762 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
763 this, &RenderWidget::AnimationCallback));
764 }
[email protected]ee3d3ad2011-02-04 00:42:21765 }
[email protected]5f8b1022011-01-21 23:34:50766}
767
[email protected]4873c7d2009-07-16 06:36:28768void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:30769 // TODO(darin): Eliminate this temporary.
770 WebCursor cursor(cursor_info);
771
initial.commit09911bf2008-07-26 23:55:29772 // Only send a SetCursor message if we need to make a change.
773 if (!current_cursor_.IsEqual(cursor)) {
774 current_cursor_ = cursor;
775 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
776 }
777}
778
779// We are supposed to get a single call to Show for a newly created RenderWidget
780// that was created via RenderWidget::CreateWebView. So, we wait until this
781// point to dispatch the ShowWidget message.
782//
783// This method provides us with the information about how to display the newly
784// created RenderWidget (i.e., as a constrained popup or as a new tab).
785//
[email protected]4873c7d2009-07-16 06:36:28786void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:29787 DCHECK(!did_show_) << "received extraneous Show call";
788 DCHECK(routing_id_ != MSG_ROUTING_NONE);
789 DCHECK(opener_id_ != MSG_ROUTING_NONE);
790
[email protected]8de12d942010-11-17 20:42:44791 if (did_show_)
792 return;
793
794 did_show_ = true;
795 // NOTE: initial_pos_ may still have its default values at this point, but
796 // that's okay. It'll be ignored if as_popup is false, or the browser
797 // process will impose a default position otherwise.
798 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
799 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:29800}
801
[email protected]4873c7d2009-07-16 06:36:28802void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:29803}
804
[email protected]4873c7d2009-07-16 06:36:28805void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:29806}
807
[email protected]2533ce12009-05-09 00:02:24808void RenderWidget::DoDeferredClose() {
809 Send(new ViewHostMsg_Close(routing_id_));
810}
811
[email protected]4873c7d2009-07-16 06:36:28812void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:29813 // If a page calls window.close() twice, we'll end up here twice, but that's
814 // OK. It is safe to send multiple Close messages.
815
[email protected]2533ce12009-05-09 00:02:24816 // Ask the RenderWidgetHost to initiate close. We could be called from deep
817 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
818 // could be closed before the JS finishes executing. So instead, post a
819 // message back to the message loop, which won't run until the JS is
820 // complete, and then the Close message can be sent.
[email protected]75ae4492009-07-10 00:05:15821 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]2533ce12009-05-09 00:02:24822 this, &RenderWidget::DoDeferredClose));
initial.commit09911bf2008-07-26 23:55:29823}
824
825void RenderWidget::Close() {
826 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:28827 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:29828 webwidget_ = NULL;
829 }
830}
831
[email protected]4873c7d2009-07-16 06:36:28832WebRect RenderWidget::windowRect() {
833 if (pending_window_rect_count_)
834 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24835
[email protected]b3f2b912009-04-09 16:18:52836 gfx::Rect rect;
837 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28838 return rect;
initial.commit09911bf2008-07-26 23:55:29839}
840
[email protected]4873c7d2009-07-16 06:36:28841void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:29842 if (did_show_) {
843 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:24844 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:29845 } else {
846 initial_pos_ = pos;
847 }
848}
849
[email protected]2533ce12009-05-09 00:02:24850void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
851 pending_window_rect_ = rect;
852 pending_window_rect_count_++;
853}
854
[email protected]4873c7d2009-07-16 06:36:28855WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:24856 if (pending_window_rect_count_) {
857 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
858 // the RootWindowRect is probably going to return wrong results since the
859 // browser may not have processed the Move yet. There isn't really anything
860 // good to do in this case, and it shouldn't happen - since this size is
861 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:28862 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24863 }
864
[email protected]b3f2b912009-04-09 16:18:52865 gfx::Rect rect;
866 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28867 return rect;
[email protected]d4547452008-08-28 18:36:37868}
869
[email protected]4873c7d2009-07-16 06:36:28870WebRect RenderWidget::windowResizerRect() {
871 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:19872}
873
[email protected]fa7b1dc2010-06-23 17:53:04874void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:03875 // To prevent this renderer process from sending unnecessary IPC messages to
876 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:04877 // only during the input method attached to the browser process is active.
878 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:29879}
880
[email protected]fa7b1dc2010-06-23 17:53:04881void RenderWidget::OnImeSetComposition(
882 const string16& text,
883 const std::vector<WebCompositionUnderline>& underlines,
884 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:28885 if (!webwidget_)
886 return;
[email protected]fa7b1dc2010-06-23 17:53:04887 if (!webwidget_->setComposition(
888 text, WebVector<WebCompositionUnderline>(underlines),
889 selection_start, selection_end)) {
890 // If we failed to set the composition text, then we need to let the browser
891 // process to cancel the input method's ongoing composition session, to make
892 // sure we are in a consistent state.
893 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:26894 }
[email protected]fa7b1dc2010-06-23 17:53:04895}
896
[email protected]6b349652011-01-05 18:36:24897void RenderWidget::OnImeConfirmComposition(const string16& text) {
[email protected]fa7b1dc2010-06-23 17:53:04898 if (webwidget_)
[email protected]6b349652011-01-05 18:36:24899 webwidget_->confirmComposition(text);
initial.commit09911bf2008-07-26 23:55:29900}
901
[email protected]948f7ab72010-05-28 23:48:08902// This message causes the renderer to render an image of the
903// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:49904void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:09905 int tag,
[email protected]948f7ab72010-05-28 23:48:08906 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:49907 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:00908 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
909 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:25910 // Close our unused handle.
911#if defined(OS_WIN)
912 ::CloseHandle(dib_handle);
913#elif defined(OS_MACOSX)
914 base::SharedMemory::CloseHandle(dib_handle);
915#endif
916 }
[email protected]d65adb12010-04-28 17:26:49917 return;
[email protected]45c6aad32010-11-11 04:46:25918 }
[email protected]d65adb12010-04-28 17:26:49919
[email protected]948f7ab72010-05-28 23:48:08920 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:49921 // If one of these is empty, then we just return the dib we were
922 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:09923 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:49924 return;
925 }
926
927 // Map the given DIB ID into this process, and unmap it at the end
928 // of this function.
[email protected]45c6aad32010-11-11 04:46:25929 scoped_ptr<TransportDIB> paint_at_size_buffer(
930 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:30931
932 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:49933 float x_scale = static_cast<float>(desired_size.width()) /
934 static_cast<float>(canvas_size.width());
935 float y_scale = static_cast<float>(desired_size.height()) /
936 static_cast<float>(canvas_size.height());
937
[email protected]ee8d6fd2010-05-26 17:05:48938 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49939 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
940 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:48941 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49942
[email protected]36808ad2010-10-20 19:18:30943 scoped_ptr<skia::PlatformCanvas> canvas(
944 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
945 canvas_size.height()));
946 if (!canvas.get()) {
947 NOTREACHED();
948 return;
949 }
950
[email protected]d65adb12010-04-28 17:26:49951 // Reset bounds to what we actually received, but they should be the
952 // same.
953 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
954 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
955 bounds.set_width(canvas->getDevice()->width());
956 bounds.set_height(canvas->getDevice()->height());
957
958 canvas->save();
[email protected]948f7ab72010-05-28 23:48:08959 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:49960 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
961
[email protected]948f7ab72010-05-28 23:48:08962 // Have to make sure we're laid out at the right size before
963 // rendering.
964 gfx::Size old_size = webwidget_->size();
965 webwidget_->resize(page_size);
966 webwidget_->layout();
967
[email protected]d65adb12010-04-28 17:26:49968 // Paint the entire thing (using original bounds, not scaled bounds).
969 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
970 canvas->restore();
971
[email protected]948f7ab72010-05-28 23:48:08972 // Return the widget to its previous size.
973 webwidget_->resize(old_size);
974
[email protected]c88c9442010-07-19 18:55:09975 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:49976}
977
[email protected]ec7dc112008-08-06 05:30:12978void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
979 // During shutdown we can just ignore this message.
980 if (!webwidget_)
981 return;
982
983 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:56984 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46985 scheduleComposite();
986 } else {
987 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
988 didInvalidateRect(repaint_rect);
989 }
[email protected]ec7dc112008-08-06 05:30:12990}
991
[email protected]4873c7d2009-07-16 06:36:28992void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:11993 if (!webwidget_)
994 return;
[email protected]4873c7d2009-07-16 06:36:28995 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:11996}
997
[email protected]719b36f2010-12-22 20:36:46998webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:15999 const gfx::Rect& paint_bounds,
1000 TransportDIB** dib,
1001 gfx::Rect* location,
1002 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461003 // Bare RenderWidgets don't support optimized plugin painting.
1004 return NULL;
[email protected]ca4847f2010-09-24 05:39:151005}
1006
[email protected]bcaf2272011-02-15 15:29:431007gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521008 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431009 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521010}
1011
[email protected]bee16aab2009-08-26 15:55:031012void RenderWidget::SetHidden(bool hidden) {
1013 if (is_hidden_ == hidden)
1014 return;
1015
1016 // The status has changed. Tell the RenderThread about it.
1017 is_hidden_ = hidden;
1018 if (is_hidden_)
1019 render_thread_->WidgetHidden();
1020 else
1021 render_thread_->WidgetRestored();
1022}
1023
[email protected]699ab0d2009-04-23 23:19:141024void RenderWidget::SetBackground(const SkBitmap& background) {
1025 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461026
[email protected]699ab0d2009-04-23 23:19:141027 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281028 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141029}
1030
[email protected]674741932009-02-04 23:44:461031bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051032 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461033}
1034
1035bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051036 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461037}
1038
1039void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051040 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461041}
1042
1043void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051044 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461045}
1046
1047void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051048 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461049}
1050
[email protected]fa7b1dc2010-06-23 17:53:041051void RenderWidget::UpdateInputMethod() {
1052 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291053 return;
[email protected]fa7b1dc2010-06-23 17:53:041054
1055 WebTextInputType new_type = WebKit::WebTextInputTypeNone;
1056 WebRect new_caret_bounds;
1057
1058 if (webwidget_) {
1059 new_type = webwidget_->textInputType();
1060 new_caret_bounds = webwidget_->caretOrSelectionBounds();
initial.commit09911bf2008-07-26 23:55:291061 }
[email protected]fa7b1dc2010-06-23 17:53:041062
1063 // Only sends text input type and caret bounds to the browser process if they
1064 // are changed.
1065 if (text_input_type_ != new_type || caret_bounds_ != new_caret_bounds) {
1066 text_input_type_ = new_type;
1067 caret_bounds_ = new_caret_bounds;
1068 Send(new ViewHostMsg_ImeUpdateTextInputState(
1069 routing_id(), new_type, new_caret_bounds));
initial.commit09911bf2008-07-26 23:55:291070 }
initial.commit09911bf2008-07-26 23:55:291071}
1072
[email protected]4873c7d2009-07-16 06:36:281073WebScreenInfo RenderWidget::screenInfo() {
1074 WebScreenInfo results;
1075 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1076 return results;
1077}
1078
[email protected]fa7b1dc2010-06-23 17:53:041079void RenderWidget::resetInputMethod() {
1080 if (!input_method_is_active_)
1081 return;
1082
1083 // If the last text input type is not None, then we should finish any
1084 // ongoing composition regardless of the new text input type.
1085 if (text_input_type_ != WebKit::WebTextInputTypeNone) {
1086 // If a composition text exists, then we need to let the browser process
1087 // to cancel the input method's ongoing composition session.
1088 if (webwidget_->confirmComposition())
1089 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1090 }
1091}
1092
[email protected]f103ab72009-09-02 17:10:591093void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501094 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291095 size_t i = 0;
1096 for (; i < plugin_window_moves_.size(); ++i) {
1097 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581098 if (move.rects_valid) {
1099 plugin_window_moves_[i] = move;
1100 } else {
1101 plugin_window_moves_[i].visible = move.visible;
1102 }
initial.commit09911bf2008-07-26 23:55:291103 break;
1104 }
1105 }
1106
1107 if (i == plugin_window_moves_.size())
1108 plugin_window_moves_.push_back(move);
1109}
[email protected]268654772009-08-06 23:02:041110
1111void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1112 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1113 i != plugin_window_moves_.end(); ++i) {
1114 if (i->window == window) {
1115 plugin_window_moves_.erase(i);
1116 break;
1117 }
1118 }
1119}