blob: 2ebb16e96fef89fefa984827522778140e6af39d [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"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3811#include "base/metrics/histogram.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]8085dbc82008-09-26 22:53:4415#include "chrome/renderer/render_process.h"
[email protected]778574e2011-03-21 22:03:5016#include "content/common/view_messages.h"
[email protected]10e6ab572011-04-14 23:42:0017#include "content/renderer/render_thread.h"
[email protected]acb94722011-03-18 01:33:3418#include "content/renderer/renderer_webkitclient_impl.h"
[email protected]71e2f0a2011-03-15 22:25:0819#include "gpu/common/gpu_trace_event.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"
26#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h"
27#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
28#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
[email protected]08397d52011-02-05 01:53:3829#include "ui/gfx/point.h"
30#include "ui/gfx/size.h"
[email protected]b9b751f22011-03-25 14:04:1231#include "ui/gfx/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3432#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5033#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4634#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4835
36#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4937#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5238#include "third_party/skia/include/core/SkPixelRef.h"
39#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4840#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4441
[email protected]8bd0fe62011-01-17 06:44:3742#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2943
[email protected]fa7b1dc2010-06-23 17:53:0444using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3045using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2246using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0647using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2848using WebKit::WebNavigationPolicy;
49using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5950using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1851using WebKit::WebPopupType;
[email protected]b3f2b912009-04-09 16:18:5252using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1953using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5254using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2855using WebKit::WebTextDirection;
[email protected]fa7b1dc2010-06-23 17:53:0456using WebKit::WebTextInputType;
57using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1858using WebKit::WebWidget;
[email protected]62cb33cae2009-03-27 23:30:2259
[email protected]3e2b375b2010-04-07 17:03:1260RenderWidget::RenderWidget(RenderThreadBase* render_thread,
61 WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2962 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1163 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2964 opener_id_(MSG_ROUTING_NONE),
[email protected]8085dbc82008-09-26 22:53:4465 render_thread_(render_thread),
[email protected]659f73f2009-10-13 13:43:4266 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3567 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2968 next_paint_flags_(0),
[email protected]53d3f302009-12-21 04:42:0569 update_reply_pending_(false),
initial.commit09911bf2008-07-26 23:55:2970 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2971 is_hidden_(false),
72 needs_repainting_on_restore_(false),
73 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4974 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4875 closing_(false),
[email protected]fa7b1dc2010-06-23 17:53:0476 input_method_is_active_(false),
77 text_input_type_(WebKit::WebTextInputTypeNone),
[email protected]3e2b375b2010-04-07 17:03:1278 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4879 pending_window_rect_count_(0),
[email protected]edbcde932010-05-07 17:10:4680 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5081 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2182 animation_update_pending_(false),
[email protected]52ccd0ea2011-02-16 01:09:0583 animation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:2884 RenderProcess::current()->AddRefProcess();
[email protected]8085dbc82008-09-26 22:53:4485 DCHECK(render_thread_);
initial.commit09911bf2008-07-26 23:55:2986}
87
88RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:1189 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]b4d08452010-10-05 17:34:3590 if (current_paint_buf_) {
91 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
92 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:2993 }
[email protected]8930d472009-02-21 08:05:2894 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:2995}
96
[email protected]484955942010-08-19 16:13:1897// static
[email protected]8085dbc82008-09-26 22:53:4498RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]0ebf3872008-11-07 21:35:0399 RenderThreadBase* render_thread,
[email protected]3e2b375b2010-04-07 17:03:12100 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:29101 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]ad8e04a2010-11-01 04:16:27102 scoped_refptr<RenderWidget> widget(new RenderWidget(render_thread,
103 popup_type));
initial.commit09911bf2008-07-26 23:55:29104 widget->Init(opener_id); // adds reference
105 return widget;
106}
107
[email protected]484955942010-08-19 16:13:18108// static
109WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
110 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03111 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18112 break;
113 case WebKit::WebPopupTypeSelect:
114 case WebKit::WebPopupTypeSuggestion:
115 return WebPopupMenu::create(render_widget);
116 default:
117 NOTREACHED();
118 }
119 return NULL;
120}
121
initial.commit09911bf2008-07-26 23:55:29122void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18123 DoInit(opener_id,
124 RenderWidget::CreateWebWidget(this),
125 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
126}
127
[email protected]484955942010-08-19 16:13:18128void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06129 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18130 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29131 DCHECK(!webwidget_);
132
133 if (opener_id != MSG_ROUTING_NONE)
134 opener_id_ = opener_id;
135
[email protected]484955942010-08-19 16:13:18136 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29137
[email protected]484955942010-08-19 16:13:18138 bool result = render_thread_->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29139 if (result) {
[email protected]8085dbc82008-09-26 22:53:44140 render_thread_->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29141 // Take a reference on behalf of the RenderThread. This will be balanced
142 // when we receive ViewMsg_Close.
143 AddRef();
144 } else {
145 DCHECK(false);
146 }
147}
148
149// This is used to complete pending inits and non-pending inits. For non-
150// pending cases, the parent will be the same as the current parent. This
151// indicates we do not need to reparent or anything.
[email protected]18bcc3c2009-01-27 21:39:15152void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29153 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29154
155 host_window_ = parent_hwnd;
156
[email protected]6de74452009-02-25 18:04:59157 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29158}
159
[email protected]a95986a82010-12-24 06:19:28160bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
161 bool handled = true;
162 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
163 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
164 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
165 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
166 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
167 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
168 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
169 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
170 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
171 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
172 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
173 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
174 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
175 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
176 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
177 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
178 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
179 IPC_MESSAGE_UNHANDLED(handled = false)
180 IPC_END_MESSAGE_MAP()
181 return handled;
182}
initial.commit09911bf2008-07-26 23:55:29183
184bool RenderWidget::Send(IPC::Message* message) {
185 // Don't send any messages after the browser has told us to close.
186 if (closing_) {
187 delete message;
188 return false;
189 }
190
191 // If given a messsage without a routing ID, then assign our routing ID.
192 if (message->routing_id() == MSG_ROUTING_NONE)
193 message->set_routing_id(routing_id_);
194
[email protected]d3fc25652009-02-24 22:31:25195 return render_thread_->Send(message);
[email protected]8085dbc82008-09-26 22:53:44196}
197
initial.commit09911bf2008-07-26 23:55:29198// Got a response from the browser after the renderer decided to create a new
199// view.
[email protected]18bcc3c2009-01-27 21:39:15200void RenderWidget::OnCreatingNewAck(gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29201 DCHECK(routing_id_ != MSG_ROUTING_NONE);
202
203 CompleteInit(parent);
204}
205
206void RenderWidget::OnClose() {
207 if (closing_)
208 return;
209 closing_ = true;
210
211 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03212 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]8085dbc82008-09-26 22:53:44213 render_thread_->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03214 SetHidden(false);
215 }
initial.commit09911bf2008-07-26 23:55:29216
initial.commit09911bf2008-07-26 23:55:29217 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25218 // now. Post a task that only gets invoked when there are no nested message
219 // loops.
220 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
221 NewRunnableMethod(this, &RenderWidget::Close));
222
223 // Balances the AddRef taken when we called AddRoute.
224 Release();
initial.commit09911bf2008-07-26 23:55:29225}
226
[email protected]f21c613a2009-02-12 14:46:17227void RenderWidget::OnResize(const gfx::Size& new_size,
228 const gfx::Rect& resizer_rect) {
initial.commit09911bf2008-07-26 23:55:29229 // During shutdown we can just ignore this message.
230 if (!webwidget_)
231 return;
232
[email protected]e2356242010-11-16 22:33:03233 // We shouldn't be asked to resize to our current size.
234 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
235
[email protected]f21c613a2009-02-12 14:46:17236 // Remember the rect where the resize corner will be drawn.
237 resizer_rect_ = resizer_rect;
238
[email protected]e2356242010-11-16 22:33:03239 if (size_ == new_size)
240 return;
241
initial.commit09911bf2008-07-26 23:55:29242 // TODO(darin): We should not need to reset this here.
[email protected]bee16aab2009-08-26 15:55:03243 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29244 needs_repainting_on_restore_ = false;
245
initial.commit09911bf2008-07-26 23:55:29246 size_ = new_size;
247
248 // We should not be sent a Resize message if we have not ACK'd the previous
249 DCHECK(!next_paint_is_resize_ack());
250
[email protected]552e6002009-11-19 05:24:57251 paint_aggregator_.ClearPendingUpdate();
252
initial.commit09911bf2008-07-26 23:55:29253 // When resizing, we want to wait to paint before ACK'ing the resize. This
254 // ensures that we only resize as fast as we can paint. We only need to send
255 // an ACK if we are resized to a non-empty rect.
[email protected]4873c7d2009-07-16 06:36:28256 webwidget_->resize(new_size);
initial.commit09911bf2008-07-26 23:55:29257 if (!new_size.IsEmpty()) {
[email protected]a79d8a632010-11-18 22:35:56258 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46259 // Resize should have caused an invalidation of the entire view.
260 DCHECK(paint_aggregator_.HasPendingUpdate());
261 }
[email protected]2d5d09d52009-06-15 14:29:21262
initial.commit09911bf2008-07-26 23:55:29263 // We will send the Resize_ACK flag once we paint again.
264 set_next_paint_is_resize_ack();
265 }
266}
267
268void RenderWidget::OnWasHidden() {
269 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03270 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29271}
272
273void RenderWidget::OnWasRestored(bool needs_repainting) {
274 // During shutdown we can just ignore this message.
275 if (!webwidget_)
276 return;
277
278 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03279 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29280
281 if (!needs_repainting && !needs_repainting_on_restore_)
282 return;
283 needs_repainting_on_restore_ = false;
284
[email protected]d65adb12010-04-28 17:26:49285 // Tag the next paint as a restore ack, which is picked up by
286 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29287 set_next_paint_is_restore_ack();
288
289 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56290 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46291 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
292 } else {
293 scheduleComposite();
294 }
initial.commit09911bf2008-07-26 23:55:29295}
296
[email protected]53d3f302009-12-21 04:42:05297void RenderWidget::OnRequestMoveAck() {
298 DCHECK(pending_window_rect_count_);
299 pending_window_rect_count_--;
300}
301
302void RenderWidget::OnUpdateRectAck() {
[email protected]c5218762011-03-24 17:48:51303 GPU_TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]53d3f302009-12-21 04:42:05304 DCHECK(update_reply_pending());
305 update_reply_pending_ = false;
306
[email protected]b4d08452010-10-05 17:34:35307 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
308 // have no current paint buffer.
309 if (current_paint_buf_) {
310 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
311 current_paint_buf_ = NULL;
312 }
313
[email protected]00c39612010-03-06 02:53:28314 // Notify subclasses.
315 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25316
initial.commit09911bf2008-07-26 23:55:29317 // Continue painting if necessary...
[email protected]552e6002009-11-19 05:24:57318 CallDoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29319}
320
initial.commit09911bf2008-07-26 23:55:29321void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
322 void* iter = NULL;
323
324 const char* data;
325 int data_length;
[email protected]5dd768212009-08-13 23:34:49326 handling_input_event_ = true;
327 if (!message.ReadData(&iter, &data, &data_length)) {
328 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29329 return;
[email protected]5dd768212009-08-13 23:34:49330 }
initial.commit09911bf2008-07-26 23:55:29331
332 const WebInputEvent* input_event =
333 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48334
335 bool is_keyboard_shortcut = false;
336 // is_keyboard_shortcut flag is only available for RawKeyDown events.
337 if (input_event->type == WebInputEvent::RawKeyDown)
338 message.ReadBool(&iter, &is_keyboard_shortcut);
339
initial.commit09911bf2008-07-26 23:55:29340 bool processed = false;
[email protected]867125a02009-12-10 06:01:48341 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
342 suppress_next_char_events_ = false;
343 if (webwidget_)
344 processed = webwidget_->handleInputEvent(*input_event);
345 }
346
347 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
348 // it's not processed by webkit, then we need to suppress the upcoming Char
349 // events.
350 if (!processed && is_keyboard_shortcut)
351 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29352
353 IPC::Message* response = new ViewHostMsg_HandleInputEvent_ACK(routing_id_);
354 response->WriteInt(input_event->type);
[email protected]e2824412009-02-27 01:57:05355 response->WriteBool(processed);
356
[email protected]353a34c2010-05-28 23:35:17357 if ((input_event->type == WebInputEvent::MouseMove ||
[email protected]d93fc462011-03-14 23:03:03358 input_event->type == WebInputEvent::MouseWheel ||
359 input_event->type == WebInputEvent::TouchMove) &&
[email protected]552e6002009-11-19 05:24:57360 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24361 // We want to rate limit the input events in this case, so we'll wait for
362 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17363 if (pending_input_event_ack_.get()) {
364 // As two different kinds of events could cause us to postpone an ack
365 // we send it now, if we have one pending. The Browser should never
366 // send us the same kind of event we are delaying the ack for.
367 Send(pending_input_event_ack_.release());
368 }
[email protected]12fbad812009-09-01 18:21:24369 pending_input_event_ack_.reset(response);
370 } else {
371 Send(response);
372 }
373
[email protected]5dd768212009-08-13 23:34:49374 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48375
[email protected]867125a02009-12-10 06:01:48376 if (WebInputEvent::isKeyboardEventType(input_event->type))
[email protected]446705872009-09-10 07:22:48377 DidHandleKeyEvent();
[email protected]6a8ddba52010-09-05 04:38:06378 if (WebInputEvent::isMouseEventType(input_event->type))
379 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
initial.commit09911bf2008-07-26 23:55:29380}
381
382void RenderWidget::OnMouseCaptureLost() {
383 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28384 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29385}
386
387void RenderWidget::OnSetFocus(bool enable) {
388 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33389 if (webwidget_)
390 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29391}
392
393void RenderWidget::ClearFocus() {
394 // We may have got the focus from the browser before this gets processed, in
395 // which case we do not want to unfocus ourself.
396 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28397 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29398}
399
[email protected]2d5d09d52009-06-15 14:29:21400void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00401 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21402 skia::PlatformCanvas* canvas) {
[email protected]719b36f2010-12-22 20:36:46403
[email protected]4fb66842009-12-04 21:41:00404 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21405
406 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00407 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
408 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03409
[email protected]699ab0d2009-04-23 23:19:14410 // If there is a custom background, tile it.
411 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14412 SkPaint paint;
413 SkShader* shader = SkShader::CreateBitmapShader(background_,
414 SkShader::kRepeat_TileMode,
415 SkShader::kRepeat_TileMode);
416 paint.setShader(shader)->unref();
[email protected]8860e4f52009-06-25 01:01:52417 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
[email protected]699ab0d2009-04-23 23:19:14418 canvas->drawPaint(paint);
[email protected]699ab0d2009-04-23 23:19:14419 }
420
[email protected]719b36f2010-12-22 20:36:46421 // First see if this rect is a plugin that can paint itself faster.
422 TransportDIB* optimized_dib = NULL;
423 gfx::Rect optimized_copy_rect, optimized_copy_location;
424 webkit::ppapi::PluginInstance* optimized_instance =
425 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
426 &optimized_copy_location,
427 &optimized_copy_rect);
428 if (optimized_instance) {
429 // This plugin can be optimize-painted and we can just ask it to paint
430 // itself. We don't actually need the TransportDIB in this case.
431 //
432 // This is an optimization for PPAPI plugins that know they're on top of
433 // the page content. If this rect is inside such a plugin, we can save some
434 // time and avoid re-rendering the page content which we know will be
435 // covered by the plugin later (this time can be significant, especially
436 // for a playing movie that is invalidating a lot).
437 //
438 // In the plugin movie case, hopefully the similar call to
439 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
440 // painting, because that avoids copying the plugin image to a different
441 // paint rect. Unfortunately, if anything on the page is animating other
442 // than the movie, it break this optimization since the union of the
443 // invalid regions will be larger than the plugin.
444 //
445 // This code optimizes that case, where we can still avoid painting in
446 // WebKit and filling the background (which can be slow) and just painting
447 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
448 // required.
449 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27450 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46451 } else {
452 // Normal painting case.
453 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
454
455 // Flush to underlying bitmap. TODO(darin): is this needed?
456 canvas->getTopPlatformDevice().accessBitmap(false);
457 }
initial.commit09911bf2008-07-26 23:55:29458
[email protected]4fb66842009-12-04 21:41:00459 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00460 canvas->restore();
461}
462
463void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
464 skia::PlatformCanvas* canvas) {
465 static bool kPaintBorder =
466 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
467 if (!kPaintBorder)
468 return;
469
[email protected]53d3f302009-12-21 04:42:05470 // Cycle through these colors to help distinguish new paint rects.
471 const SkColor colors[] = {
472 SkColorSetARGB(0x3F, 0xFF, 0, 0),
473 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
474 SkColorSetARGB(0x3F, 0, 0, 0xFF),
475 };
476 static int color_selector = 0;
477
[email protected]4fb66842009-12-04 21:41:00478 SkPaint paint;
479 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05480 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00481 paint.setStrokeWidth(1);
482
483 SkIRect irect;
484 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
485 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29486}
487
[email protected]52ccd0ea2011-02-16 01:09:05488void RenderWidget::AnimationCallback() {
489 animation_task_posted_ = false;
[email protected]7c4329e2011-02-18 22:02:59490 if (!animation_update_pending_)
491 return;
492 if (!animation_floor_time_.is_null()) {
493 // Record when we fired (according to base::Time::Now()) relative to when
494 // we posted the task to quantify how much the base::Time/base::TimeTicks
495 // skew is affecting animations.
496 base::TimeDelta animation_callback_delay = base::Time::Now() -
497 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
498 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
499 animation_callback_delay,
500 base::TimeDelta::FromMilliseconds(0),
501 base::TimeDelta::FromMilliseconds(30),
502 25);
503 }
[email protected]52ccd0ea2011-02-16 01:09:05504 CallDoDeferredUpdate();
[email protected]12fbad812009-09-01 18:21:24505}
506
[email protected]52ccd0ea2011-02-16 01:09:05507void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59508 if (!animation_update_pending_)
509 return;
510 base::Time now = base::Time::Now();
511 if (now >= animation_floor_time_) {
512 animation_floor_time_ = now + base::TimeDelta::FromMilliseconds(16);
513 // Set a timer to call us back after 16ms (targetting 60FPS) before
514 // running animation callbacks so that if a callback requests another
515 // we'll be sure to run it at the proper time.
516 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
517 this, &RenderWidget::AnimationCallback), 16);
518 animation_task_posted_ = true;
519 animation_update_pending_ = false;
520 // Explicitly pump the WebCore Timer queue to avoid starvation on OS X.
521 // See crbug.com/71735.
522 // TODO(jamesr) Remove this call once crbug.com/72007 is fixed.
523 RenderThread::current()->GetWebKitClientImpl()->DoTimeout();
524 webwidget_->animate();
525 return;
[email protected]5f8b1022011-01-21 23:34:50526 }
[email protected]7c4329e2011-02-18 22:02:59527 if (animation_task_posted_)
528 return;
529 // This code uses base::Time::Now() to calculate the floor and next fire
530 // time because javascript's Date object uses base::Time::Now(). The
531 // message loop uses base::TimeTicks, which on windows can have a
532 // different granularity than base::Time.
533 // The upshot of all this is that this function might be called before
534 // base::Time::Now() has advanced past the animation_floor_time_. To
535 // avoid exposing this delay to javascript, we keep posting delayed
536 // tasks until base::Time::Now() has advanced far enough.
537 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
538 animation_task_posted_ = true;
539 MessageLoop::current()->PostDelayedTask(FROM_HERE,
540 NewRunnableMethod(this, &RenderWidget::AnimationCallback), delay);
[email protected]5f8b1022011-01-21 23:34:50541}
542
[email protected]52ccd0ea2011-02-16 01:09:05543void RenderWidget::CallDoDeferredUpdate() {
544 DoDeferredUpdate();
545
546 if (pending_input_event_ack_.get())
547 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21548}
549
[email protected]552e6002009-11-19 05:24:57550void RenderWidget::DoDeferredUpdate() {
[email protected]c5218762011-03-24 17:48:51551 GPU_TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08552
[email protected]5f8b1022011-01-21 23:34:50553 if (!webwidget_ || update_reply_pending())
initial.commit09911bf2008-07-26 23:55:29554 return;
555
[email protected]552e6002009-11-19 05:24:57556 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29557 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57558 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29559 needs_repainting_on_restore_ = true;
560 return;
561 }
562
[email protected]52ccd0ea2011-02-16 01:09:05563 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50564
[email protected]f98d7e3c2010-09-13 22:30:46565 // Layout may generate more invalidation. It may also enable the
566 // GPU acceleration, so make sure to run layout before we send the
567 // GpuRenderingActivated message.
568 webwidget_->layout();
569
[email protected]5f8b1022011-01-21 23:34:50570 // Suppress painting if nothing is dirty. This has to be done after updating
571 // animations running layout as these may generate further invalidations.
572 if (!paint_aggregator_.HasPendingUpdate())
573 return;
574
[email protected]552e6002009-11-19 05:24:57575 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29576 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30577 PaintAggregator::PendingUpdate update;
578 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29579
[email protected]53d3f302009-12-21 04:42:05580 gfx::Rect scroll_damage = update.GetScrollDamage();
581 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29582
[email protected]27543452011-03-25 00:14:00583 // Compositing the page may disable accelerated compositing.
584 bool accelerated_compositing_was_active = is_accelerated_compositing_active_;
585
[email protected]ca4847f2010-09-24 05:39:15586 // A plugin may be able to do an optimized paint. First check this, in which
587 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46588 // This optimization allows PPAPI plugins that declare themselves on top of
589 // the page (like a traditional windowed plugin) to be able to animate (think
590 // movie playing) without repeatedly re-painting the page underneath, or
591 // copying the plugin backing store (since we can send the plugin's backing
592 // store directly to the browser).
593 //
594 // This optimization only works when the entire invalid region is contained
595 // within the plugin. There is a related optimization in PaintRect for the
596 // case where there may be multiple invalid regions.
[email protected]2650bd52011-03-25 00:34:44597 TransportDIB::Id dib_id = TransportDIB::Id();
[email protected]ca4847f2010-09-24 05:39:15598 TransportDIB* dib = NULL;
[email protected]cef3362f2009-12-21 17:48:45599 std::vector<gfx::Rect> copy_rects;
[email protected]ca4847f2010-09-24 05:39:15600 gfx::Rect optimized_copy_rect, optimized_copy_location;
601 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56602 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15603 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
604 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27605 // Only update the part of the plugin that actually changed.
606 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]ca4847f2010-09-24 05:39:15607 bounds = optimized_copy_location;
608 copy_rects.push_back(optimized_copy_rect);
609 dib_id = dib->id();
[email protected]a79d8a632010-11-18 22:35:56610 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46611 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15612 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35613 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
614 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46615 if (!canvas.get()) {
616 NOTREACHED();
617 return;
618 }
[email protected]cef3362f2009-12-21 17:48:45619
[email protected]f98d7e3c2010-09-13 22:30:46620 // We may get back a smaller canvas than we asked for.
621 // TODO(darin): This seems like it could cause painting problems!
622 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
623 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
624 bounds.set_width(canvas->getDevice()->width());
625 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05626
[email protected]f98d7e3c2010-09-13 22:30:46627 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
628
[email protected]f98d7e3c2010-09-13 22:30:46629 // The scroll damage is just another rectangle to paint and copy.
630 copy_rects.swap(update.paint_rects);
631 if (!scroll_damage.IsEmpty())
632 copy_rects.push_back(scroll_damage);
633
634 for (size_t i = 0; i < copy_rects.size(); ++i)
635 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]ca4847f2010-09-24 05:39:15636
[email protected]b4d08452010-10-05 17:34:35637 dib_id = current_paint_buf_->id();
[email protected]f98d7e3c2010-09-13 22:30:46638 } else { // Accelerated compositing path
639 // Begin painting.
[email protected]50bd6452010-11-27 19:39:42640 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46641 }
642
643 // sending an ack to browser process that the paint is complete...
[email protected]53d3f302009-12-21 04:42:05644 ViewHostMsg_UpdateRect_Params params;
[email protected]36808ad2010-10-20 19:18:30645 params.bitmap = dib_id;
[email protected]53d3f302009-12-21 04:42:05646 params.bitmap_rect = bounds;
647 params.dx = update.scroll_delta.x();
648 params.dy = update.scroll_delta.y();
[email protected]27543452011-03-25 00:14:00649 if (accelerated_compositing_was_active) {
[email protected]b167ca662010-05-14 00:05:34650 // If painting is done via the gpu process then we clear out all damage
651 // rects to save the browser process from doing unecessary work.
652 params.scroll_rect = gfx::Rect();
653 params.copy_rects.clear();
654 } else {
655 params.scroll_rect = update.scroll_rect;
656 params.copy_rects.swap(copy_rects); // TODO(darin): clip to bounds?
657 }
[email protected]53d3f302009-12-21 04:42:05658 params.view_size = size_;
[email protected]e2356242010-11-16 22:33:03659 params.resizer_rect = resizer_rect_;
[email protected]53d3f302009-12-21 04:42:05660 params.plugin_window_moves.swap(plugin_window_moves_);
661 params.flags = next_paint_flags_;
[email protected]d54169e92011-01-21 09:19:52662 params.scroll_offset = GetScrollOffset();
[email protected]53d3f302009-12-21 04:42:05663
664 update_reply_pending_ = true;
665 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
666 next_paint_flags_ = 0;
667
[email protected]fa7b1dc2010-06-23 17:53:04668 UpdateInputMethod();
[email protected]00c39612010-03-06 02:53:28669
670 // Let derived classes know we've painted.
671 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29672}
673
674///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46675// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29676
[email protected]4873c7d2009-07-16 06:36:28677void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:57678 // We only want one pending DoDeferredUpdate call at any time...
679 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29680
[email protected]552e6002009-11-19 05:24:57681 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48682 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57683 gfx::Rect damaged_rect = view_rect.Intersect(rect);
684 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29685 return;
686
[email protected]552e6002009-11-19 05:24:57687 paint_aggregator_.InvalidateRect(damaged_rect);
688
689 // We may not need to schedule another call to DoDeferredUpdate.
690 if (update_pending)
691 return;
692 if (!paint_aggregator_.HasPendingUpdate())
693 return;
[email protected]53d3f302009-12-21 04:42:05694 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57695 return;
696
697 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29698 // 1) Ensures that we call WebView::Paint without a bunch of other junk
699 // on the call stack.
700 // 2) Allows us to collect more damage rects before painting to help coalesce
701 // the work that we will need to do.
702 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57703 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29704}
705
[email protected]4873c7d2009-07-16 06:36:28706void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46707 // Drop scrolls on the floor when we are in compositing mode.
708 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56709 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46710 return;
711
[email protected]552e6002009-11-19 05:24:57712 // We only want one pending DoDeferredUpdate call at any time...
713 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29714
[email protected]552e6002009-11-19 05:24:57715 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48716 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57717 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
718 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29719 return;
720
[email protected]552e6002009-11-19 05:24:57721 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
722
723 // We may not need to schedule another call to DoDeferredUpdate.
724 if (update_pending)
725 return;
726 if (!paint_aggregator_.HasPendingUpdate())
727 return;
[email protected]53d3f302009-12-21 04:42:05728 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57729 return;
730
731 // Perform updating asynchronously. This serves two purposes:
732 // 1) Ensures that we call WebView::Paint without a bunch of other junk
733 // on the call stack.
734 // 2) Allows us to collect more damage rects before painting to help coalesce
735 // the work that we will need to do.
initial.commit09911bf2008-07-26 23:55:29736 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57737 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29738}
739
[email protected]a79d8a632010-11-18 22:35:56740void RenderWidget::didActivateAcceleratedCompositing(bool active) {
741 is_accelerated_compositing_active_ = active;
[email protected]50bd6452010-11-27 19:39:42742 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]a79d8a632010-11-18 22:35:56743 routing_id_, is_accelerated_compositing_active_));
744}
745
[email protected]f98d7e3c2010-09-13 22:30:46746void RenderWidget::scheduleComposite() {
747 // TODO(nduca): replace with something a little less hacky. The reason this
748 // hack is still used is because the Invalidate-DoDeferredUpdate loop
749 // contains a lot of host-renderer synchronization logic that is still
750 // important for the accelerated compositing case. The option of simply
751 // duplicating all that code is less desirable than "faking out" the
752 // invalidation path using a magical damage rect.
[email protected]e2356242010-11-16 22:33:03753 didInvalidateRect(WebRect(0, 0, 1, 1));
[email protected]f98d7e3c2010-09-13 22:30:46754}
755
[email protected]5f8b1022011-01-21 23:34:50756void RenderWidget::scheduleAnimation() {
[email protected]ee3d3ad2011-02-04 00:42:21757 if (!animation_update_pending_) {
758 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:05759 if (!animation_task_posted_) {
760 animation_task_posted_ = true;
761 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
762 this, &RenderWidget::AnimationCallback));
763 }
[email protected]ee3d3ad2011-02-04 00:42:21764 }
[email protected]5f8b1022011-01-21 23:34:50765}
766
[email protected]4873c7d2009-07-16 06:36:28767void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:30768 // TODO(darin): Eliminate this temporary.
769 WebCursor cursor(cursor_info);
770
initial.commit09911bf2008-07-26 23:55:29771 // Only send a SetCursor message if we need to make a change.
772 if (!current_cursor_.IsEqual(cursor)) {
773 current_cursor_ = cursor;
774 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
775 }
776}
777
778// We are supposed to get a single call to Show for a newly created RenderWidget
779// that was created via RenderWidget::CreateWebView. So, we wait until this
780// point to dispatch the ShowWidget message.
781//
782// This method provides us with the information about how to display the newly
783// created RenderWidget (i.e., as a constrained popup or as a new tab).
784//
[email protected]4873c7d2009-07-16 06:36:28785void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:29786 DCHECK(!did_show_) << "received extraneous Show call";
787 DCHECK(routing_id_ != MSG_ROUTING_NONE);
788 DCHECK(opener_id_ != MSG_ROUTING_NONE);
789
[email protected]8de12d942010-11-17 20:42:44790 if (did_show_)
791 return;
792
793 did_show_ = true;
794 // NOTE: initial_pos_ may still have its default values at this point, but
795 // that's okay. It'll be ignored if as_popup is false, or the browser
796 // process will impose a default position otherwise.
797 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
798 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:29799}
800
[email protected]4873c7d2009-07-16 06:36:28801void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:29802}
803
[email protected]4873c7d2009-07-16 06:36:28804void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:29805}
806
[email protected]2533ce12009-05-09 00:02:24807void RenderWidget::DoDeferredClose() {
808 Send(new ViewHostMsg_Close(routing_id_));
809}
810
[email protected]4873c7d2009-07-16 06:36:28811void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:29812 // If a page calls window.close() twice, we'll end up here twice, but that's
813 // OK. It is safe to send multiple Close messages.
814
[email protected]2533ce12009-05-09 00:02:24815 // Ask the RenderWidgetHost to initiate close. We could be called from deep
816 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
817 // could be closed before the JS finishes executing. So instead, post a
818 // message back to the message loop, which won't run until the JS is
819 // complete, and then the Close message can be sent.
[email protected]75ae4492009-07-10 00:05:15820 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]2533ce12009-05-09 00:02:24821 this, &RenderWidget::DoDeferredClose));
initial.commit09911bf2008-07-26 23:55:29822}
823
824void RenderWidget::Close() {
825 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:28826 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:29827 webwidget_ = NULL;
828 }
829}
830
[email protected]4873c7d2009-07-16 06:36:28831WebRect RenderWidget::windowRect() {
832 if (pending_window_rect_count_)
833 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24834
[email protected]b3f2b912009-04-09 16:18:52835 gfx::Rect rect;
836 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28837 return rect;
initial.commit09911bf2008-07-26 23:55:29838}
839
[email protected]4873c7d2009-07-16 06:36:28840void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:29841 if (did_show_) {
842 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:24843 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:29844 } else {
845 initial_pos_ = pos;
846 }
847}
848
[email protected]2533ce12009-05-09 00:02:24849void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
850 pending_window_rect_ = rect;
851 pending_window_rect_count_++;
852}
853
[email protected]4873c7d2009-07-16 06:36:28854WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:24855 if (pending_window_rect_count_) {
856 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
857 // the RootWindowRect is probably going to return wrong results since the
858 // browser may not have processed the Move yet. There isn't really anything
859 // good to do in this case, and it shouldn't happen - since this size is
860 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:28861 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24862 }
863
[email protected]b3f2b912009-04-09 16:18:52864 gfx::Rect rect;
865 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28866 return rect;
[email protected]d4547452008-08-28 18:36:37867}
868
[email protected]4873c7d2009-07-16 06:36:28869WebRect RenderWidget::windowResizerRect() {
870 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:19871}
872
[email protected]fa7b1dc2010-06-23 17:53:04873void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:03874 // To prevent this renderer process from sending unnecessary IPC messages to
875 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:04876 // only during the input method attached to the browser process is active.
877 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:29878}
879
[email protected]fa7b1dc2010-06-23 17:53:04880void RenderWidget::OnImeSetComposition(
881 const string16& text,
882 const std::vector<WebCompositionUnderline>& underlines,
883 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:28884 if (!webwidget_)
885 return;
[email protected]fa7b1dc2010-06-23 17:53:04886 if (!webwidget_->setComposition(
887 text, WebVector<WebCompositionUnderline>(underlines),
888 selection_start, selection_end)) {
889 // If we failed to set the composition text, then we need to let the browser
890 // process to cancel the input method's ongoing composition session, to make
891 // sure we are in a consistent state.
892 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:26893 }
[email protected]fa7b1dc2010-06-23 17:53:04894}
895
[email protected]6b349652011-01-05 18:36:24896void RenderWidget::OnImeConfirmComposition(const string16& text) {
[email protected]fa7b1dc2010-06-23 17:53:04897 if (webwidget_)
[email protected]6b349652011-01-05 18:36:24898 webwidget_->confirmComposition(text);
initial.commit09911bf2008-07-26 23:55:29899}
900
[email protected]948f7ab72010-05-28 23:48:08901// This message causes the renderer to render an image of the
902// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:49903void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:09904 int tag,
[email protected]948f7ab72010-05-28 23:48:08905 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:49906 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:00907 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
908 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:25909 // Close our unused handle.
910#if defined(OS_WIN)
911 ::CloseHandle(dib_handle);
912#elif defined(OS_MACOSX)
913 base::SharedMemory::CloseHandle(dib_handle);
914#endif
915 }
[email protected]d65adb12010-04-28 17:26:49916 return;
[email protected]45c6aad32010-11-11 04:46:25917 }
[email protected]d65adb12010-04-28 17:26:49918
[email protected]948f7ab72010-05-28 23:48:08919 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:49920 // If one of these is empty, then we just return the dib we were
921 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:09922 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:49923 return;
924 }
925
926 // Map the given DIB ID into this process, and unmap it at the end
927 // of this function.
[email protected]45c6aad32010-11-11 04:46:25928 scoped_ptr<TransportDIB> paint_at_size_buffer(
929 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:30930
931 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:49932 float x_scale = static_cast<float>(desired_size.width()) /
933 static_cast<float>(canvas_size.width());
934 float y_scale = static_cast<float>(desired_size.height()) /
935 static_cast<float>(canvas_size.height());
936
[email protected]ee8d6fd2010-05-26 17:05:48937 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49938 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
939 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:48940 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49941
[email protected]36808ad2010-10-20 19:18:30942 scoped_ptr<skia::PlatformCanvas> canvas(
943 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
944 canvas_size.height()));
945 if (!canvas.get()) {
946 NOTREACHED();
947 return;
948 }
949
[email protected]d65adb12010-04-28 17:26:49950 // Reset bounds to what we actually received, but they should be the
951 // same.
952 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
953 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
954 bounds.set_width(canvas->getDevice()->width());
955 bounds.set_height(canvas->getDevice()->height());
956
957 canvas->save();
[email protected]948f7ab72010-05-28 23:48:08958 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:49959 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
960
[email protected]948f7ab72010-05-28 23:48:08961 // Have to make sure we're laid out at the right size before
962 // rendering.
963 gfx::Size old_size = webwidget_->size();
964 webwidget_->resize(page_size);
965 webwidget_->layout();
966
[email protected]d65adb12010-04-28 17:26:49967 // Paint the entire thing (using original bounds, not scaled bounds).
968 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
969 canvas->restore();
970
[email protected]948f7ab72010-05-28 23:48:08971 // Return the widget to its previous size.
972 webwidget_->resize(old_size);
973
[email protected]c88c9442010-07-19 18:55:09974 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:49975}
976
[email protected]ec7dc112008-08-06 05:30:12977void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
978 // During shutdown we can just ignore this message.
979 if (!webwidget_)
980 return;
981
982 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:56983 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46984 scheduleComposite();
985 } else {
986 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
987 didInvalidateRect(repaint_rect);
988 }
[email protected]ec7dc112008-08-06 05:30:12989}
990
[email protected]4873c7d2009-07-16 06:36:28991void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:11992 if (!webwidget_)
993 return;
[email protected]4873c7d2009-07-16 06:36:28994 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:11995}
996
[email protected]719b36f2010-12-22 20:36:46997webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:15998 const gfx::Rect& paint_bounds,
999 TransportDIB** dib,
1000 gfx::Rect* location,
1001 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461002 // Bare RenderWidgets don't support optimized plugin painting.
1003 return NULL;
[email protected]ca4847f2010-09-24 05:39:151004}
1005
[email protected]bcaf2272011-02-15 15:29:431006gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521007 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431008 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521009}
1010
[email protected]bee16aab2009-08-26 15:55:031011void RenderWidget::SetHidden(bool hidden) {
1012 if (is_hidden_ == hidden)
1013 return;
1014
1015 // The status has changed. Tell the RenderThread about it.
1016 is_hidden_ = hidden;
1017 if (is_hidden_)
1018 render_thread_->WidgetHidden();
1019 else
1020 render_thread_->WidgetRestored();
1021}
1022
[email protected]699ab0d2009-04-23 23:19:141023void RenderWidget::SetBackground(const SkBitmap& background) {
1024 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461025
[email protected]699ab0d2009-04-23 23:19:141026 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281027 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141028}
1029
[email protected]674741932009-02-04 23:44:461030bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051031 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461032}
1033
1034bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051035 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461036}
1037
1038void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051039 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461040}
1041
1042void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051043 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461044}
1045
1046void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051047 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461048}
1049
[email protected]fa7b1dc2010-06-23 17:53:041050void RenderWidget::UpdateInputMethod() {
1051 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291052 return;
[email protected]fa7b1dc2010-06-23 17:53:041053
1054 WebTextInputType new_type = WebKit::WebTextInputTypeNone;
1055 WebRect new_caret_bounds;
1056
1057 if (webwidget_) {
1058 new_type = webwidget_->textInputType();
1059 new_caret_bounds = webwidget_->caretOrSelectionBounds();
initial.commit09911bf2008-07-26 23:55:291060 }
[email protected]fa7b1dc2010-06-23 17:53:041061
1062 // Only sends text input type and caret bounds to the browser process if they
1063 // are changed.
1064 if (text_input_type_ != new_type || caret_bounds_ != new_caret_bounds) {
1065 text_input_type_ = new_type;
1066 caret_bounds_ = new_caret_bounds;
1067 Send(new ViewHostMsg_ImeUpdateTextInputState(
1068 routing_id(), new_type, new_caret_bounds));
initial.commit09911bf2008-07-26 23:55:291069 }
initial.commit09911bf2008-07-26 23:55:291070}
1071
[email protected]4873c7d2009-07-16 06:36:281072WebScreenInfo RenderWidget::screenInfo() {
1073 WebScreenInfo results;
1074 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1075 return results;
1076}
1077
[email protected]fa7b1dc2010-06-23 17:53:041078void RenderWidget::resetInputMethod() {
1079 if (!input_method_is_active_)
1080 return;
1081
1082 // If the last text input type is not None, then we should finish any
1083 // ongoing composition regardless of the new text input type.
1084 if (text_input_type_ != WebKit::WebTextInputTypeNone) {
1085 // If a composition text exists, then we need to let the browser process
1086 // to cancel the input method's ongoing composition session.
1087 if (webwidget_->confirmComposition())
1088 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1089 }
1090}
1091
[email protected]f103ab72009-09-02 17:10:591092void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501093 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291094 size_t i = 0;
1095 for (; i < plugin_window_moves_.size(); ++i) {
1096 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581097 if (move.rects_valid) {
1098 plugin_window_moves_[i] = move;
1099 } else {
1100 plugin_window_moves_[i].visible = move.visible;
1101 }
initial.commit09911bf2008-07-26 23:55:291102 break;
1103 }
1104 }
1105
1106 if (i == plugin_window_moves_.size())
1107 plugin_window_moves_.push_back(move);
1108}
[email protected]268654772009-08-06 23:02:041109
1110void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1111 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1112 i != plugin_window_moves_.end(); ++i) {
1113 if (i->window == window) {
1114 plugin_window_moves_.erase(i);
1115 break;
1116 }
1117 }
1118}