blob: 7572b7ed824ad6023fbbc08f6a0338cb9d508e66 [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"
[email protected]366ae242011-05-10 02:23:588#include "base/debug/trace_event.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3812#include "base/metrics/histogram.h"
[email protected]661eb9d2009-02-03 02:11:4813#include "build/build_config.h"
[email protected]38b592902011-04-16 02:08:4214#include "content/common/content_switches.h"
[email protected]992db4c2011-05-12 15:37:1515#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5016#include "content/common/view_messages.h"
[email protected]8704f89b2011-04-15 00:30:0517#include "content/renderer/render_process.h"
[email protected]10e6ab572011-04-14 23:42:0018#include "content/renderer/render_thread.h"
[email protected]acb94722011-03-18 01:33:3419#include "content/renderer/renderer_webkitclient_impl.h"
[email protected]484955942010-08-19 16:13:1820#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4821#include "skia/ext/platform_canvas.h"
[email protected]d5282e72009-05-13 13:16:5222#include "third_party/skia/include/core/SkShader.h"
[email protected]8bd0fe62011-01-17 06:44:3723#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
24#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
25#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0126#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3727#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]d4cff272011-05-02 15:46:0153using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5254using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1955using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5256using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2857using WebKit::WebTextDirection;
[email protected]fa7b1dc2010-06-23 17:53:0458using WebKit::WebTextInputType;
59using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1860using WebKit::WebWidget;
[email protected]62cb33cae2009-03-27 23:30:2261
[email protected]3e2b375b2010-04-07 17:03:1262RenderWidget::RenderWidget(RenderThreadBase* render_thread,
63 WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2964 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1165 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2966 opener_id_(MSG_ROUTING_NONE),
[email protected]8085dbc82008-09-26 22:53:4467 render_thread_(render_thread),
[email protected]659f73f2009-10-13 13:43:4268 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3569 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2970 next_paint_flags_(0),
[email protected]53d3f302009-12-21 04:42:0571 update_reply_pending_(false),
initial.commit09911bf2008-07-26 23:55:2972 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2973 is_hidden_(false),
74 needs_repainting_on_restore_(false),
75 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4976 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4877 closing_(false),
[email protected]992db4c2011-05-12 15:37:1578 is_swapped_out_(false),
[email protected]fa7b1dc2010-06-23 17:53:0479 input_method_is_active_(false),
80 text_input_type_(WebKit::WebTextInputTypeNone),
[email protected]3e2b375b2010-04-07 17:03:1281 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4882 pending_window_rect_count_(0),
[email protected]edbcde932010-05-07 17:10:4683 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5084 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2185 animation_update_pending_(false),
[email protected]52ccd0ea2011-02-16 01:09:0586 animation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:2887 RenderProcess::current()->AddRefProcess();
[email protected]8085dbc82008-09-26 22:53:4488 DCHECK(render_thread_);
initial.commit09911bf2008-07-26 23:55:2989}
90
91RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:1192 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]b4d08452010-10-05 17:34:3593 if (current_paint_buf_) {
94 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
95 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:2996 }
[email protected]992db4c2011-05-12 15:37:1597 // If we are swapped out, we have released already.
98 if (!is_swapped_out_)
99 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29100}
101
[email protected]484955942010-08-19 16:13:18102// static
[email protected]8085dbc82008-09-26 22:53:44103RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]0ebf3872008-11-07 21:35:03104 RenderThreadBase* render_thread,
[email protected]3e2b375b2010-04-07 17:03:12105 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:29106 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]ad8e04a2010-11-01 04:16:27107 scoped_refptr<RenderWidget> widget(new RenderWidget(render_thread,
108 popup_type));
initial.commit09911bf2008-07-26 23:55:29109 widget->Init(opener_id); // adds reference
110 return widget;
111}
112
[email protected]484955942010-08-19 16:13:18113// static
114WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
115 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03116 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18117 break;
118 case WebKit::WebPopupTypeSelect:
119 case WebKit::WebPopupTypeSuggestion:
120 return WebPopupMenu::create(render_widget);
121 default:
122 NOTREACHED();
123 }
124 return NULL;
125}
126
initial.commit09911bf2008-07-26 23:55:29127void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18128 DoInit(opener_id,
129 RenderWidget::CreateWebWidget(this),
130 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
131}
132
[email protected]484955942010-08-19 16:13:18133void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06134 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18135 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29136 DCHECK(!webwidget_);
137
138 if (opener_id != MSG_ROUTING_NONE)
139 opener_id_ = opener_id;
140
[email protected]484955942010-08-19 16:13:18141 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29142
[email protected]484955942010-08-19 16:13:18143 bool result = render_thread_->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29144 if (result) {
[email protected]8085dbc82008-09-26 22:53:44145 render_thread_->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29146 // Take a reference on behalf of the RenderThread. This will be balanced
147 // when we receive ViewMsg_Close.
148 AddRef();
149 } else {
150 DCHECK(false);
151 }
152}
153
154// This is used to complete pending inits and non-pending inits. For non-
155// pending cases, the parent will be the same as the current parent. This
156// indicates we do not need to reparent or anything.
[email protected]c03a6e72011-04-19 21:42:06157void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd,
158 gfx::PluginWindowHandle compositing_surface) {
initial.commit09911bf2008-07-26 23:55:29159 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29160
161 host_window_ = parent_hwnd;
[email protected]c03a6e72011-04-19 21:42:06162 compositing_surface_ = compositing_surface;
initial.commit09911bf2008-07-26 23:55:29163
[email protected]6de74452009-02-25 18:04:59164 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29165}
166
[email protected]992db4c2011-05-12 15:37:15167void RenderWidget::SetSwappedOut(bool is_swapped_out) {
168 // We should only toggle between states.
169 DCHECK(is_swapped_out_ != is_swapped_out);
170 is_swapped_out_ = is_swapped_out;
171
172 // If we are swapping out, we will call ReleaseProcess, allowing the process
173 // to exit if all of its RenderViews are swapped out. We wait until the
174 // WasSwappedOut call to do this, to avoid showing the sad tab.
175 // If we are swapping in, we call AddRefProcess to prevent the process from
176 // exiting.
177 if (!is_swapped_out)
178 RenderProcess::current()->AddRefProcess();
179}
180
[email protected]a95986a82010-12-24 06:19:28181bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
182 bool handled = true;
183 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
184 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
185 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
186 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
187 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
188 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]992db4c2011-05-12 15:37:15189 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28190 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
191 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
192 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
193 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
194 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
195 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
196 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
197 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
198 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
199 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
200 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
201 IPC_MESSAGE_UNHANDLED(handled = false)
202 IPC_END_MESSAGE_MAP()
203 return handled;
204}
initial.commit09911bf2008-07-26 23:55:29205
206bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15207 // Don't send any messages after the browser has told us to close, and filter
208 // most outgoing messages while swapped out.
209 if ((is_swapped_out_ &&
210 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
211 closing_) {
initial.commit09911bf2008-07-26 23:55:29212 delete message;
213 return false;
214 }
215
216 // If given a messsage without a routing ID, then assign our routing ID.
217 if (message->routing_id() == MSG_ROUTING_NONE)
218 message->set_routing_id(routing_id_);
219
[email protected]d3fc25652009-02-24 22:31:25220 return render_thread_->Send(message);
[email protected]8085dbc82008-09-26 22:53:44221}
222
initial.commit09911bf2008-07-26 23:55:29223// Got a response from the browser after the renderer decided to create a new
224// view.
[email protected]c03a6e72011-04-19 21:42:06225void RenderWidget::OnCreatingNewAck(
226 gfx::NativeViewId parent,
227 gfx::PluginWindowHandle compositing_surface) {
initial.commit09911bf2008-07-26 23:55:29228 DCHECK(routing_id_ != MSG_ROUTING_NONE);
229
[email protected]c03a6e72011-04-19 21:42:06230 CompleteInit(parent, compositing_surface);
initial.commit09911bf2008-07-26 23:55:29231}
232
233void RenderWidget::OnClose() {
234 if (closing_)
235 return;
236 closing_ = true;
237
238 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03239 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]8085dbc82008-09-26 22:53:44240 render_thread_->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03241 SetHidden(false);
242 }
initial.commit09911bf2008-07-26 23:55:29243
initial.commit09911bf2008-07-26 23:55:29244 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25245 // now. Post a task that only gets invoked when there are no nested message
246 // loops.
247 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
248 NewRunnableMethod(this, &RenderWidget::Close));
249
250 // Balances the AddRef taken when we called AddRoute.
251 Release();
initial.commit09911bf2008-07-26 23:55:29252}
253
[email protected]f21c613a2009-02-12 14:46:17254void RenderWidget::OnResize(const gfx::Size& new_size,
255 const gfx::Rect& resizer_rect) {
initial.commit09911bf2008-07-26 23:55:29256 // During shutdown we can just ignore this message.
257 if (!webwidget_)
258 return;
259
[email protected]e2356242010-11-16 22:33:03260 // We shouldn't be asked to resize to our current size.
261 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
262
[email protected]f21c613a2009-02-12 14:46:17263 // Remember the rect where the resize corner will be drawn.
264 resizer_rect_ = resizer_rect;
265
[email protected]e2356242010-11-16 22:33:03266 if (size_ == new_size)
267 return;
268
initial.commit09911bf2008-07-26 23:55:29269 // TODO(darin): We should not need to reset this here.
[email protected]bee16aab2009-08-26 15:55:03270 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29271 needs_repainting_on_restore_ = false;
272
initial.commit09911bf2008-07-26 23:55:29273 size_ = new_size;
274
275 // We should not be sent a Resize message if we have not ACK'd the previous
276 DCHECK(!next_paint_is_resize_ack());
277
[email protected]552e6002009-11-19 05:24:57278 paint_aggregator_.ClearPendingUpdate();
279
initial.commit09911bf2008-07-26 23:55:29280 // When resizing, we want to wait to paint before ACK'ing the resize. This
281 // ensures that we only resize as fast as we can paint. We only need to send
282 // an ACK if we are resized to a non-empty rect.
[email protected]4873c7d2009-07-16 06:36:28283 webwidget_->resize(new_size);
initial.commit09911bf2008-07-26 23:55:29284 if (!new_size.IsEmpty()) {
[email protected]a79d8a632010-11-18 22:35:56285 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46286 // Resize should have caused an invalidation of the entire view.
287 DCHECK(paint_aggregator_.HasPendingUpdate());
288 }
[email protected]2d5d09d52009-06-15 14:29:21289
initial.commit09911bf2008-07-26 23:55:29290 // We will send the Resize_ACK flag once we paint again.
291 set_next_paint_is_resize_ack();
292 }
293}
294
295void RenderWidget::OnWasHidden() {
296 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03297 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29298}
299
300void RenderWidget::OnWasRestored(bool needs_repainting) {
301 // During shutdown we can just ignore this message.
302 if (!webwidget_)
303 return;
304
305 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03306 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29307
308 if (!needs_repainting && !needs_repainting_on_restore_)
309 return;
310 needs_repainting_on_restore_ = false;
311
[email protected]d65adb12010-04-28 17:26:49312 // Tag the next paint as a restore ack, which is picked up by
313 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29314 set_next_paint_is_restore_ack();
315
316 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56317 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46318 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
319 } else {
320 scheduleComposite();
321 }
initial.commit09911bf2008-07-26 23:55:29322}
323
[email protected]992db4c2011-05-12 15:37:15324void RenderWidget::OnWasSwappedOut() {
325 // If we have been swapped out and no one else is using this process,
326 // it's safe to exit now. If we get swapped back in, we will call
327 // AddRefProcess in SetSwappedOut.
328 if (is_swapped_out_)
329 RenderProcess::current()->ReleaseProcess();
330}
331
[email protected]53d3f302009-12-21 04:42:05332void RenderWidget::OnRequestMoveAck() {
333 DCHECK(pending_window_rect_count_);
334 pending_window_rect_count_--;
335}
336
337void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58338 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]53d3f302009-12-21 04:42:05339 DCHECK(update_reply_pending());
340 update_reply_pending_ = false;
341
[email protected]b4d08452010-10-05 17:34:35342 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
343 // have no current paint buffer.
344 if (current_paint_buf_) {
345 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
346 current_paint_buf_ = NULL;
347 }
348
[email protected]00c39612010-03-06 02:53:28349 // Notify subclasses.
350 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25351
initial.commit09911bf2008-07-26 23:55:29352 // Continue painting if necessary...
[email protected]552e6002009-11-19 05:24:57353 CallDoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29354}
355
initial.commit09911bf2008-07-26 23:55:29356void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
357 void* iter = NULL;
358
359 const char* data;
360 int data_length;
[email protected]5dd768212009-08-13 23:34:49361 handling_input_event_ = true;
362 if (!message.ReadData(&iter, &data, &data_length)) {
363 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29364 return;
[email protected]5dd768212009-08-13 23:34:49365 }
initial.commit09911bf2008-07-26 23:55:29366
367 const WebInputEvent* input_event =
368 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48369
370 bool is_keyboard_shortcut = false;
371 // is_keyboard_shortcut flag is only available for RawKeyDown events.
372 if (input_event->type == WebInputEvent::RawKeyDown)
373 message.ReadBool(&iter, &is_keyboard_shortcut);
374
initial.commit09911bf2008-07-26 23:55:29375 bool processed = false;
[email protected]867125a02009-12-10 06:01:48376 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
377 suppress_next_char_events_ = false;
378 if (webwidget_)
379 processed = webwidget_->handleInputEvent(*input_event);
380 }
381
382 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
383 // it's not processed by webkit, then we need to suppress the upcoming Char
384 // events.
385 if (!processed && is_keyboard_shortcut)
386 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29387
388 IPC::Message* response = new ViewHostMsg_HandleInputEvent_ACK(routing_id_);
389 response->WriteInt(input_event->type);
[email protected]e2824412009-02-27 01:57:05390 response->WriteBool(processed);
391
[email protected]353a34c2010-05-28 23:35:17392 if ((input_event->type == WebInputEvent::MouseMove ||
[email protected]d93fc462011-03-14 23:03:03393 input_event->type == WebInputEvent::MouseWheel ||
394 input_event->type == WebInputEvent::TouchMove) &&
[email protected]552e6002009-11-19 05:24:57395 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24396 // We want to rate limit the input events in this case, so we'll wait for
397 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17398 if (pending_input_event_ack_.get()) {
399 // As two different kinds of events could cause us to postpone an ack
400 // we send it now, if we have one pending. The Browser should never
401 // send us the same kind of event we are delaying the ack for.
402 Send(pending_input_event_ack_.release());
403 }
[email protected]12fbad812009-09-01 18:21:24404 pending_input_event_ack_.reset(response);
405 } else {
406 Send(response);
407 }
408
[email protected]5dd768212009-08-13 23:34:49409 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48410
[email protected]867125a02009-12-10 06:01:48411 if (WebInputEvent::isKeyboardEventType(input_event->type))
[email protected]446705872009-09-10 07:22:48412 DidHandleKeyEvent();
[email protected]6a8ddba52010-09-05 04:38:06413 if (WebInputEvent::isMouseEventType(input_event->type))
414 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
initial.commit09911bf2008-07-26 23:55:29415}
416
417void RenderWidget::OnMouseCaptureLost() {
418 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28419 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29420}
421
422void RenderWidget::OnSetFocus(bool enable) {
423 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33424 if (webwidget_)
425 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29426}
427
428void RenderWidget::ClearFocus() {
429 // We may have got the focus from the browser before this gets processed, in
430 // which case we do not want to unfocus ourself.
431 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28432 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29433}
434
[email protected]2d5d09d52009-06-15 14:29:21435void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00436 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21437 skia::PlatformCanvas* canvas) {
[email protected]719b36f2010-12-22 20:36:46438
[email protected]4fb66842009-12-04 21:41:00439 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21440
441 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00442 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
443 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03444
[email protected]699ab0d2009-04-23 23:19:14445 // If there is a custom background, tile it.
446 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14447 SkPaint paint;
448 SkShader* shader = SkShader::CreateBitmapShader(background_,
449 SkShader::kRepeat_TileMode,
450 SkShader::kRepeat_TileMode);
451 paint.setShader(shader)->unref();
[email protected]8860e4f52009-06-25 01:01:52452 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
[email protected]699ab0d2009-04-23 23:19:14453 canvas->drawPaint(paint);
[email protected]699ab0d2009-04-23 23:19:14454 }
455
[email protected]719b36f2010-12-22 20:36:46456 // First see if this rect is a plugin that can paint itself faster.
457 TransportDIB* optimized_dib = NULL;
458 gfx::Rect optimized_copy_rect, optimized_copy_location;
459 webkit::ppapi::PluginInstance* optimized_instance =
460 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
461 &optimized_copy_location,
462 &optimized_copy_rect);
463 if (optimized_instance) {
464 // This plugin can be optimize-painted and we can just ask it to paint
465 // itself. We don't actually need the TransportDIB in this case.
466 //
467 // This is an optimization for PPAPI plugins that know they're on top of
468 // the page content. If this rect is inside such a plugin, we can save some
469 // time and avoid re-rendering the page content which we know will be
470 // covered by the plugin later (this time can be significant, especially
471 // for a playing movie that is invalidating a lot).
472 //
473 // In the plugin movie case, hopefully the similar call to
474 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
475 // painting, because that avoids copying the plugin image to a different
476 // paint rect. Unfortunately, if anything on the page is animating other
477 // than the movie, it break this optimization since the union of the
478 // invalid regions will be larger than the plugin.
479 //
480 // This code optimizes that case, where we can still avoid painting in
481 // WebKit and filling the background (which can be slow) and just painting
482 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
483 // required.
484 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27485 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46486 } else {
487 // Normal painting case.
488 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
489
490 // Flush to underlying bitmap. TODO(darin): is this needed?
491 canvas->getTopPlatformDevice().accessBitmap(false);
492 }
initial.commit09911bf2008-07-26 23:55:29493
[email protected]4fb66842009-12-04 21:41:00494 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00495 canvas->restore();
496}
497
498void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
499 skia::PlatformCanvas* canvas) {
500 static bool kPaintBorder =
501 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
502 if (!kPaintBorder)
503 return;
504
[email protected]53d3f302009-12-21 04:42:05505 // Cycle through these colors to help distinguish new paint rects.
506 const SkColor colors[] = {
507 SkColorSetARGB(0x3F, 0xFF, 0, 0),
508 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
509 SkColorSetARGB(0x3F, 0, 0, 0xFF),
510 };
511 static int color_selector = 0;
512
[email protected]4fb66842009-12-04 21:41:00513 SkPaint paint;
514 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05515 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00516 paint.setStrokeWidth(1);
517
518 SkIRect irect;
519 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
520 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29521}
522
[email protected]52ccd0ea2011-02-16 01:09:05523void RenderWidget::AnimationCallback() {
524 animation_task_posted_ = false;
[email protected]7c4329e2011-02-18 22:02:59525 if (!animation_update_pending_)
526 return;
527 if (!animation_floor_time_.is_null()) {
528 // Record when we fired (according to base::Time::Now()) relative to when
529 // we posted the task to quantify how much the base::Time/base::TimeTicks
530 // skew is affecting animations.
531 base::TimeDelta animation_callback_delay = base::Time::Now() -
532 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
533 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
534 animation_callback_delay,
535 base::TimeDelta::FromMilliseconds(0),
536 base::TimeDelta::FromMilliseconds(30),
537 25);
538 }
[email protected]52ccd0ea2011-02-16 01:09:05539 CallDoDeferredUpdate();
[email protected]12fbad812009-09-01 18:21:24540}
541
[email protected]52ccd0ea2011-02-16 01:09:05542void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59543 if (!animation_update_pending_)
544 return;
545 base::Time now = base::Time::Now();
546 if (now >= animation_floor_time_) {
547 animation_floor_time_ = now + base::TimeDelta::FromMilliseconds(16);
548 // Set a timer to call us back after 16ms (targetting 60FPS) before
549 // running animation callbacks so that if a callback requests another
550 // we'll be sure to run it at the proper time.
551 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
552 this, &RenderWidget::AnimationCallback), 16);
553 animation_task_posted_ = true;
554 animation_update_pending_ = false;
555 // Explicitly pump the WebCore Timer queue to avoid starvation on OS X.
556 // See crbug.com/71735.
557 // TODO(jamesr) Remove this call once crbug.com/72007 is fixed.
558 RenderThread::current()->GetWebKitClientImpl()->DoTimeout();
559 webwidget_->animate();
560 return;
[email protected]5f8b1022011-01-21 23:34:50561 }
[email protected]7c4329e2011-02-18 22:02:59562 if (animation_task_posted_)
563 return;
564 // This code uses base::Time::Now() to calculate the floor and next fire
565 // time because javascript's Date object uses base::Time::Now(). The
566 // message loop uses base::TimeTicks, which on windows can have a
567 // different granularity than base::Time.
568 // The upshot of all this is that this function might be called before
569 // base::Time::Now() has advanced past the animation_floor_time_. To
570 // avoid exposing this delay to javascript, we keep posting delayed
571 // tasks until base::Time::Now() has advanced far enough.
572 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
573 animation_task_posted_ = true;
574 MessageLoop::current()->PostDelayedTask(FROM_HERE,
575 NewRunnableMethod(this, &RenderWidget::AnimationCallback), delay);
[email protected]5f8b1022011-01-21 23:34:50576}
577
[email protected]52ccd0ea2011-02-16 01:09:05578void RenderWidget::CallDoDeferredUpdate() {
579 DoDeferredUpdate();
580
581 if (pending_input_event_ack_.get())
582 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21583}
584
[email protected]552e6002009-11-19 05:24:57585void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58586 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08587
[email protected]5f8b1022011-01-21 23:34:50588 if (!webwidget_ || update_reply_pending())
initial.commit09911bf2008-07-26 23:55:29589 return;
590
[email protected]552e6002009-11-19 05:24:57591 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29592 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57593 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29594 needs_repainting_on_restore_ = true;
595 return;
596 }
597
[email protected]52ccd0ea2011-02-16 01:09:05598 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50599
[email protected]f98d7e3c2010-09-13 22:30:46600 // Layout may generate more invalidation. It may also enable the
601 // GPU acceleration, so make sure to run layout before we send the
602 // GpuRenderingActivated message.
603 webwidget_->layout();
604
[email protected]5f8b1022011-01-21 23:34:50605 // Suppress painting if nothing is dirty. This has to be done after updating
606 // animations running layout as these may generate further invalidations.
607 if (!paint_aggregator_.HasPendingUpdate())
608 return;
609
[email protected]552e6002009-11-19 05:24:57610 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29611 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30612 PaintAggregator::PendingUpdate update;
613 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29614
[email protected]53d3f302009-12-21 04:42:05615 gfx::Rect scroll_damage = update.GetScrollDamage();
616 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29617
[email protected]27543452011-03-25 00:14:00618 // Compositing the page may disable accelerated compositing.
619 bool accelerated_compositing_was_active = is_accelerated_compositing_active_;
620
[email protected]ca4847f2010-09-24 05:39:15621 // A plugin may be able to do an optimized paint. First check this, in which
622 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46623 // This optimization allows PPAPI plugins that declare themselves on top of
624 // the page (like a traditional windowed plugin) to be able to animate (think
625 // movie playing) without repeatedly re-painting the page underneath, or
626 // copying the plugin backing store (since we can send the plugin's backing
627 // store directly to the browser).
628 //
629 // This optimization only works when the entire invalid region is contained
630 // within the plugin. There is a related optimization in PaintRect for the
631 // case where there may be multiple invalid regions.
[email protected]2650bd52011-03-25 00:34:44632 TransportDIB::Id dib_id = TransportDIB::Id();
[email protected]ca4847f2010-09-24 05:39:15633 TransportDIB* dib = NULL;
[email protected]cef3362f2009-12-21 17:48:45634 std::vector<gfx::Rect> copy_rects;
[email protected]ca4847f2010-09-24 05:39:15635 gfx::Rect optimized_copy_rect, optimized_copy_location;
636 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56637 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15638 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
639 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27640 // Only update the part of the plugin that actually changed.
641 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]ca4847f2010-09-24 05:39:15642 bounds = optimized_copy_location;
643 copy_rects.push_back(optimized_copy_rect);
644 dib_id = dib->id();
[email protected]a79d8a632010-11-18 22:35:56645 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46646 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15647 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35648 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
649 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46650 if (!canvas.get()) {
651 NOTREACHED();
652 return;
653 }
[email protected]cef3362f2009-12-21 17:48:45654
[email protected]f98d7e3c2010-09-13 22:30:46655 // We may get back a smaller canvas than we asked for.
656 // TODO(darin): This seems like it could cause painting problems!
657 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
658 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
659 bounds.set_width(canvas->getDevice()->width());
660 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05661
[email protected]f98d7e3c2010-09-13 22:30:46662 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
663
[email protected]f98d7e3c2010-09-13 22:30:46664 // The scroll damage is just another rectangle to paint and copy.
665 copy_rects.swap(update.paint_rects);
666 if (!scroll_damage.IsEmpty())
667 copy_rects.push_back(scroll_damage);
668
669 for (size_t i = 0; i < copy_rects.size(); ++i)
670 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]ca4847f2010-09-24 05:39:15671
[email protected]b4d08452010-10-05 17:34:35672 dib_id = current_paint_buf_->id();
[email protected]f98d7e3c2010-09-13 22:30:46673 } else { // Accelerated compositing path
674 // Begin painting.
[email protected]50bd6452010-11-27 19:39:42675 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46676 }
677
678 // sending an ack to browser process that the paint is complete...
[email protected]53d3f302009-12-21 04:42:05679 ViewHostMsg_UpdateRect_Params params;
[email protected]36808ad2010-10-20 19:18:30680 params.bitmap = dib_id;
[email protected]53d3f302009-12-21 04:42:05681 params.bitmap_rect = bounds;
682 params.dx = update.scroll_delta.x();
683 params.dy = update.scroll_delta.y();
[email protected]27543452011-03-25 00:14:00684 if (accelerated_compositing_was_active) {
[email protected]b167ca662010-05-14 00:05:34685 // If painting is done via the gpu process then we clear out all damage
686 // rects to save the browser process from doing unecessary work.
687 params.scroll_rect = gfx::Rect();
688 params.copy_rects.clear();
689 } else {
690 params.scroll_rect = update.scroll_rect;
691 params.copy_rects.swap(copy_rects); // TODO(darin): clip to bounds?
692 }
[email protected]53d3f302009-12-21 04:42:05693 params.view_size = size_;
[email protected]e2356242010-11-16 22:33:03694 params.resizer_rect = resizer_rect_;
[email protected]53d3f302009-12-21 04:42:05695 params.plugin_window_moves.swap(plugin_window_moves_);
696 params.flags = next_paint_flags_;
[email protected]d54169e92011-01-21 09:19:52697 params.scroll_offset = GetScrollOffset();
[email protected]53d3f302009-12-21 04:42:05698
699 update_reply_pending_ = true;
700 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
701 next_paint_flags_ = 0;
702
[email protected]fa7b1dc2010-06-23 17:53:04703 UpdateInputMethod();
[email protected]00c39612010-03-06 02:53:28704
705 // Let derived classes know we've painted.
706 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29707}
708
709///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46710// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29711
[email protected]4873c7d2009-07-16 06:36:28712void RenderWidget::didInvalidateRect(const WebRect& rect) {
[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 invalidated 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(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_.InvalidateRect(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:
initial.commit09911bf2008-07-26 23:55:29733 // 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.
737 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]4873c7d2009-07-16 06:36:28741void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46742 // Drop scrolls on the floor when we are in compositing mode.
743 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56744 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46745 return;
746
[email protected]552e6002009-11-19 05:24:57747 // We only want one pending DoDeferredUpdate call at any time...
748 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29749
[email protected]552e6002009-11-19 05:24:57750 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48751 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57752 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
753 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29754 return;
755
[email protected]552e6002009-11-19 05:24:57756 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
757
758 // We may not need to schedule another call to DoDeferredUpdate.
759 if (update_pending)
760 return;
761 if (!paint_aggregator_.HasPendingUpdate())
762 return;
[email protected]53d3f302009-12-21 04:42:05763 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57764 return;
765
766 // Perform updating asynchronously. This serves two purposes:
767 // 1) Ensures that we call WebView::Paint without a bunch of other junk
768 // on the call stack.
769 // 2) Allows us to collect more damage rects before painting to help coalesce
770 // the work that we will need to do.
initial.commit09911bf2008-07-26 23:55:29771 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57772 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29773}
774
[email protected]a79d8a632010-11-18 22:35:56775void RenderWidget::didActivateAcceleratedCompositing(bool active) {
776 is_accelerated_compositing_active_ = active;
[email protected]50bd6452010-11-27 19:39:42777 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]a79d8a632010-11-18 22:35:56778 routing_id_, is_accelerated_compositing_active_));
779}
780
[email protected]f98d7e3c2010-09-13 22:30:46781void RenderWidget::scheduleComposite() {
782 // TODO(nduca): replace with something a little less hacky. The reason this
783 // hack is still used is because the Invalidate-DoDeferredUpdate loop
784 // contains a lot of host-renderer synchronization logic that is still
785 // important for the accelerated compositing case. The option of simply
786 // duplicating all that code is less desirable than "faking out" the
787 // invalidation path using a magical damage rect.
[email protected]e2356242010-11-16 22:33:03788 didInvalidateRect(WebRect(0, 0, 1, 1));
[email protected]f98d7e3c2010-09-13 22:30:46789}
790
[email protected]5f8b1022011-01-21 23:34:50791void RenderWidget::scheduleAnimation() {
[email protected]ee3d3ad2011-02-04 00:42:21792 if (!animation_update_pending_) {
793 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:05794 if (!animation_task_posted_) {
795 animation_task_posted_ = true;
796 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
797 this, &RenderWidget::AnimationCallback));
798 }
[email protected]ee3d3ad2011-02-04 00:42:21799 }
[email protected]5f8b1022011-01-21 23:34:50800}
801
[email protected]4873c7d2009-07-16 06:36:28802void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:30803 // TODO(darin): Eliminate this temporary.
804 WebCursor cursor(cursor_info);
805
initial.commit09911bf2008-07-26 23:55:29806 // Only send a SetCursor message if we need to make a change.
807 if (!current_cursor_.IsEqual(cursor)) {
808 current_cursor_ = cursor;
809 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
810 }
811}
812
813// We are supposed to get a single call to Show for a newly created RenderWidget
814// that was created via RenderWidget::CreateWebView. So, we wait until this
815// point to dispatch the ShowWidget message.
816//
817// This method provides us with the information about how to display the newly
818// created RenderWidget (i.e., as a constrained popup or as a new tab).
819//
[email protected]4873c7d2009-07-16 06:36:28820void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:29821 DCHECK(!did_show_) << "received extraneous Show call";
822 DCHECK(routing_id_ != MSG_ROUTING_NONE);
823 DCHECK(opener_id_ != MSG_ROUTING_NONE);
824
[email protected]8de12d942010-11-17 20:42:44825 if (did_show_)
826 return;
827
828 did_show_ = true;
829 // NOTE: initial_pos_ may still have its default values at this point, but
830 // that's okay. It'll be ignored if as_popup is false, or the browser
831 // process will impose a default position otherwise.
832 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
833 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:29834}
835
[email protected]4873c7d2009-07-16 06:36:28836void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:29837}
838
[email protected]4873c7d2009-07-16 06:36:28839void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:29840}
841
[email protected]2533ce12009-05-09 00:02:24842void RenderWidget::DoDeferredClose() {
843 Send(new ViewHostMsg_Close(routing_id_));
844}
845
[email protected]4873c7d2009-07-16 06:36:28846void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:29847 // If a page calls window.close() twice, we'll end up here twice, but that's
848 // OK. It is safe to send multiple Close messages.
849
[email protected]2533ce12009-05-09 00:02:24850 // Ask the RenderWidgetHost to initiate close. We could be called from deep
851 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
852 // could be closed before the JS finishes executing. So instead, post a
853 // message back to the message loop, which won't run until the JS is
854 // complete, and then the Close message can be sent.
[email protected]75ae4492009-07-10 00:05:15855 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]2533ce12009-05-09 00:02:24856 this, &RenderWidget::DoDeferredClose));
initial.commit09911bf2008-07-26 23:55:29857}
858
859void RenderWidget::Close() {
860 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:28861 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:29862 webwidget_ = NULL;
863 }
864}
865
[email protected]4873c7d2009-07-16 06:36:28866WebRect RenderWidget::windowRect() {
867 if (pending_window_rect_count_)
868 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24869
[email protected]b3f2b912009-04-09 16:18:52870 gfx::Rect rect;
871 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28872 return rect;
initial.commit09911bf2008-07-26 23:55:29873}
874
[email protected]4873c7d2009-07-16 06:36:28875void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:29876 if (did_show_) {
877 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:24878 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:29879 } else {
880 initial_pos_ = pos;
881 }
882}
883
[email protected]2533ce12009-05-09 00:02:24884void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
885 pending_window_rect_ = rect;
886 pending_window_rect_count_++;
887}
888
[email protected]4873c7d2009-07-16 06:36:28889WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:24890 if (pending_window_rect_count_) {
891 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
892 // the RootWindowRect is probably going to return wrong results since the
893 // browser may not have processed the Move yet. There isn't really anything
894 // good to do in this case, and it shouldn't happen - since this size is
895 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:28896 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24897 }
898
[email protected]b3f2b912009-04-09 16:18:52899 gfx::Rect rect;
900 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28901 return rect;
[email protected]d4547452008-08-28 18:36:37902}
903
[email protected]4873c7d2009-07-16 06:36:28904WebRect RenderWidget::windowResizerRect() {
905 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:19906}
907
[email protected]fa7b1dc2010-06-23 17:53:04908void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:03909 // To prevent this renderer process from sending unnecessary IPC messages to
910 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:04911 // only during the input method attached to the browser process is active.
912 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:29913}
914
[email protected]fa7b1dc2010-06-23 17:53:04915void RenderWidget::OnImeSetComposition(
916 const string16& text,
917 const std::vector<WebCompositionUnderline>& underlines,
918 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:28919 if (!webwidget_)
920 return;
[email protected]d4cff272011-05-02 15:46:01921 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:04922 text, WebVector<WebCompositionUnderline>(underlines),
923 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:01924 // Setting the IME composition was successful. Send the new composition
925 // range to the browser.
926 ui::Range range(ui::Range::InvalidRange());
927 size_t location, length;
928 if (webwidget_->compositionRange(&location, &length)) {
929 range.set_start(location);
930 range.set_end(location + length);
931 }
932 // The IME was cancelled via the Esc key, so just send back the caret.
933 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
934 range.set_start(location);
935 range.set_end(location + length);
936 }
937 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
938 } else {
[email protected]fa7b1dc2010-06-23 17:53:04939 // If we failed to set the composition text, then we need to let the browser
940 // process to cancel the input method's ongoing composition session, to make
941 // sure we are in a consistent state.
942 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:01943
944 // Send an updated IME range with just the caret range.
945 ui::Range range(ui::Range::InvalidRange());
946 size_t location, length;
947 if (webwidget_->caretOrSelectionRange(&location, &length)) {
948 range.set_start(location);
949 range.set_end(location + length);
950 }
951 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]7f00efa2010-04-15 05:01:26952 }
[email protected]fa7b1dc2010-06-23 17:53:04953}
954
[email protected]6b349652011-01-05 18:36:24955void RenderWidget::OnImeConfirmComposition(const string16& text) {
[email protected]fa7b1dc2010-06-23 17:53:04956 if (webwidget_)
[email protected]6b349652011-01-05 18:36:24957 webwidget_->confirmComposition(text);
[email protected]d4cff272011-05-02 15:46:01958 // Send an updated IME range with just the caret range.
959 ui::Range range(ui::Range::InvalidRange());
960 size_t location, length;
961 if (webwidget_->caretOrSelectionRange(&location, &length)) {
962 range.set_start(location);
963 range.set_end(location + length);
964 }
965 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
initial.commit09911bf2008-07-26 23:55:29966}
967
[email protected]948f7ab72010-05-28 23:48:08968// This message causes the renderer to render an image of the
969// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:49970void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:09971 int tag,
[email protected]948f7ab72010-05-28 23:48:08972 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:49973 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:00974 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
975 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:25976 // Close our unused handle.
977#if defined(OS_WIN)
978 ::CloseHandle(dib_handle);
979#elif defined(OS_MACOSX)
980 base::SharedMemory::CloseHandle(dib_handle);
981#endif
982 }
[email protected]d65adb12010-04-28 17:26:49983 return;
[email protected]45c6aad32010-11-11 04:46:25984 }
[email protected]d65adb12010-04-28 17:26:49985
[email protected]948f7ab72010-05-28 23:48:08986 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:49987 // If one of these is empty, then we just return the dib we were
988 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:09989 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:49990 return;
991 }
992
993 // Map the given DIB ID into this process, and unmap it at the end
994 // of this function.
[email protected]45c6aad32010-11-11 04:46:25995 scoped_ptr<TransportDIB> paint_at_size_buffer(
996 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:30997
998 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:49999 float x_scale = static_cast<float>(desired_size.width()) /
1000 static_cast<float>(canvas_size.width());
1001 float y_scale = static_cast<float>(desired_size.height()) /
1002 static_cast<float>(canvas_size.height());
1003
[email protected]ee8d6fd2010-05-26 17:05:481004 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491005 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1006 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481007 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491008
[email protected]36808ad2010-10-20 19:18:301009 scoped_ptr<skia::PlatformCanvas> canvas(
1010 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1011 canvas_size.height()));
1012 if (!canvas.get()) {
1013 NOTREACHED();
1014 return;
1015 }
1016
[email protected]d65adb12010-04-28 17:26:491017 // Reset bounds to what we actually received, but they should be the
1018 // same.
1019 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1020 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1021 bounds.set_width(canvas->getDevice()->width());
1022 bounds.set_height(canvas->getDevice()->height());
1023
1024 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081025 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491026 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1027
[email protected]948f7ab72010-05-28 23:48:081028 // Have to make sure we're laid out at the right size before
1029 // rendering.
1030 gfx::Size old_size = webwidget_->size();
1031 webwidget_->resize(page_size);
1032 webwidget_->layout();
1033
[email protected]d65adb12010-04-28 17:26:491034 // Paint the entire thing (using original bounds, not scaled bounds).
1035 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1036 canvas->restore();
1037
[email protected]948f7ab72010-05-28 23:48:081038 // Return the widget to its previous size.
1039 webwidget_->resize(old_size);
1040
[email protected]c88c9442010-07-19 18:55:091041 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491042}
1043
[email protected]ec7dc112008-08-06 05:30:121044void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1045 // During shutdown we can just ignore this message.
1046 if (!webwidget_)
1047 return;
1048
1049 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561050 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461051 scheduleComposite();
1052 } else {
1053 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1054 didInvalidateRect(repaint_rect);
1055 }
[email protected]ec7dc112008-08-06 05:30:121056}
1057
[email protected]4873c7d2009-07-16 06:36:281058void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111059 if (!webwidget_)
1060 return;
[email protected]4873c7d2009-07-16 06:36:281061 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111062}
1063
[email protected]719b36f2010-12-22 20:36:461064webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151065 const gfx::Rect& paint_bounds,
1066 TransportDIB** dib,
1067 gfx::Rect* location,
1068 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461069 // Bare RenderWidgets don't support optimized plugin painting.
1070 return NULL;
[email protected]ca4847f2010-09-24 05:39:151071}
1072
[email protected]bcaf2272011-02-15 15:29:431073gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521074 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431075 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521076}
1077
[email protected]bee16aab2009-08-26 15:55:031078void RenderWidget::SetHidden(bool hidden) {
1079 if (is_hidden_ == hidden)
1080 return;
1081
1082 // The status has changed. Tell the RenderThread about it.
1083 is_hidden_ = hidden;
1084 if (is_hidden_)
1085 render_thread_->WidgetHidden();
1086 else
1087 render_thread_->WidgetRestored();
1088}
1089
[email protected]699ab0d2009-04-23 23:19:141090void RenderWidget::SetBackground(const SkBitmap& background) {
1091 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461092
[email protected]699ab0d2009-04-23 23:19:141093 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281094 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141095}
1096
[email protected]674741932009-02-04 23:44:461097bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051098 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461099}
1100
1101bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051102 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461103}
1104
1105void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051106 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461107}
1108
1109void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051110 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461111}
1112
1113void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051114 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461115}
1116
[email protected]fa7b1dc2010-06-23 17:53:041117void RenderWidget::UpdateInputMethod() {
1118 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291119 return;
[email protected]fa7b1dc2010-06-23 17:53:041120
1121 WebTextInputType new_type = WebKit::WebTextInputTypeNone;
1122 WebRect new_caret_bounds;
1123
1124 if (webwidget_) {
1125 new_type = webwidget_->textInputType();
1126 new_caret_bounds = webwidget_->caretOrSelectionBounds();
initial.commit09911bf2008-07-26 23:55:291127 }
[email protected]fa7b1dc2010-06-23 17:53:041128
1129 // Only sends text input type and caret bounds to the browser process if they
1130 // are changed.
1131 if (text_input_type_ != new_type || caret_bounds_ != new_caret_bounds) {
1132 text_input_type_ = new_type;
1133 caret_bounds_ = new_caret_bounds;
1134 Send(new ViewHostMsg_ImeUpdateTextInputState(
1135 routing_id(), new_type, new_caret_bounds));
initial.commit09911bf2008-07-26 23:55:291136 }
initial.commit09911bf2008-07-26 23:55:291137}
1138
[email protected]4873c7d2009-07-16 06:36:281139WebScreenInfo RenderWidget::screenInfo() {
1140 WebScreenInfo results;
1141 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1142 return results;
1143}
1144
[email protected]fa7b1dc2010-06-23 17:53:041145void RenderWidget::resetInputMethod() {
1146 if (!input_method_is_active_)
1147 return;
1148
1149 // If the last text input type is not None, then we should finish any
1150 // ongoing composition regardless of the new text input type.
1151 if (text_input_type_ != WebKit::WebTextInputTypeNone) {
1152 // If a composition text exists, then we need to let the browser process
1153 // to cancel the input method's ongoing composition session.
1154 if (webwidget_->confirmComposition())
1155 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1156 }
[email protected]d4cff272011-05-02 15:46:011157
1158 // Send an updated IME range with the current caret rect.
1159 ui::Range range(ui::Range::InvalidRange());
1160 size_t location, length;
1161 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1162 range.set_start(location);
1163 range.set_end(location + length);
1164 }
1165 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]fa7b1dc2010-06-23 17:53:041166}
1167
[email protected]f103ab72009-09-02 17:10:591168void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501169 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291170 size_t i = 0;
1171 for (; i < plugin_window_moves_.size(); ++i) {
1172 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581173 if (move.rects_valid) {
1174 plugin_window_moves_[i] = move;
1175 } else {
1176 plugin_window_moves_[i].visible = move.visible;
1177 }
initial.commit09911bf2008-07-26 23:55:291178 break;
1179 }
1180 }
1181
1182 if (i == plugin_window_moves_.size())
1183 plugin_window_moves_.push_back(move);
1184}
[email protected]268654772009-08-06 23:02:041185
1186void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1187 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1188 i != plugin_window_moves_.end(); ++i) {
1189 if (i->window == window) {
1190 plugin_window_moves_.erase(i);
1191 break;
1192 }
1193 }
1194}