blob: 4637167195c2cdcf78ce025f5ea43002ec01fb8d [file] [log] [blame]
[email protected]00c39612010-03-06 02:53:281// Copyright (c) 2010 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
5#include "chrome/renderer/render_widget.h"
6
[email protected]6c077af2010-03-19 14:01:337#include "app/surface/transport_dib.h"
[email protected]4fb66842009-12-04 21:41:008#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/logging.h"
10#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3811#include "base/metrics/histogram.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/scoped_ptr.h"
[email protected]661eb9d2009-02-03 02:11:4813#include "build/build_config.h"
[email protected]4fb66842009-12-04 21:41:0014#include "chrome/common/chrome_switches.h"
[email protected]674741932009-02-04 23:44:4615#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0216#include "chrome/common/render_messages_params.h"
[email protected]8085dbc82008-09-26 22:53:4417#include "chrome/renderer/render_process.h"
[email protected]00c39612010-03-06 02:53:2818#include "chrome/renderer/render_thread.h"
[email protected]08f42e72011-02-16 07:34:4019#include "chrome/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"
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]8c89e7792009-08-19 21:18:3431#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5032#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4633#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4834
35#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4936#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5237#include "third_party/skia/include/core/SkPixelRef.h"
38#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4839#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4440
[email protected]8bd0fe62011-01-17 06:44:3741#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2942
[email protected]fa7b1dc2010-06-23 17:53:0443using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3044using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2245using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0646using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2847using WebKit::WebNavigationPolicy;
48using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5949using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1850using WebKit::WebPopupType;
[email protected]b3f2b912009-04-09 16:18:5251using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1952using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5253using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2854using WebKit::WebTextDirection;
[email protected]fa7b1dc2010-06-23 17:53:0455using WebKit::WebTextInputType;
56using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1857using WebKit::WebWidget;
[email protected]62cb33cae2009-03-27 23:30:2258
[email protected]3e2b375b2010-04-07 17:03:1259RenderWidget::RenderWidget(RenderThreadBase* render_thread,
60 WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2961 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1162 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2963 opener_id_(MSG_ROUTING_NONE),
[email protected]8085dbc82008-09-26 22:53:4464 render_thread_(render_thread),
[email protected]659f73f2009-10-13 13:43:4265 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3566 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2967 next_paint_flags_(0),
[email protected]53d3f302009-12-21 04:42:0568 update_reply_pending_(false),
initial.commit09911bf2008-07-26 23:55:2969 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2970 is_hidden_(false),
71 needs_repainting_on_restore_(false),
72 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4973 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4874 closing_(false),
[email protected]fa7b1dc2010-06-23 17:53:0475 input_method_is_active_(false),
76 text_input_type_(WebKit::WebTextInputTypeNone),
[email protected]3e2b375b2010-04-07 17:03:1277 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4878 pending_window_rect_count_(0),
[email protected]edbcde932010-05-07 17:10:4679 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5080 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2181 animation_update_pending_(false),
[email protected]52ccd0ea2011-02-16 01:09:0582 animation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:2883 RenderProcess::current()->AddRefProcess();
[email protected]8085dbc82008-09-26 22:53:4484 DCHECK(render_thread_);
initial.commit09911bf2008-07-26 23:55:2985}
86
87RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:1188 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]b4d08452010-10-05 17:34:3589 if (current_paint_buf_) {
90 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
91 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:2992 }
[email protected]8930d472009-02-21 08:05:2893 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:2994}
95
[email protected]484955942010-08-19 16:13:1896// static
[email protected]8085dbc82008-09-26 22:53:4497RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]0ebf3872008-11-07 21:35:0398 RenderThreadBase* render_thread,
[email protected]3e2b375b2010-04-07 17:03:1299 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:29100 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]ad8e04a2010-11-01 04:16:27101 scoped_refptr<RenderWidget> widget(new RenderWidget(render_thread,
102 popup_type));
initial.commit09911bf2008-07-26 23:55:29103 widget->Init(opener_id); // adds reference
104 return widget;
105}
106
[email protected]484955942010-08-19 16:13:18107// static
108WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
109 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03110 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18111 break;
112 case WebKit::WebPopupTypeSelect:
113 case WebKit::WebPopupTypeSuggestion:
114 return WebPopupMenu::create(render_widget);
115 default:
116 NOTREACHED();
117 }
118 return NULL;
119}
120
initial.commit09911bf2008-07-26 23:55:29121void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18122 DoInit(opener_id,
123 RenderWidget::CreateWebWidget(this),
124 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
125}
126
[email protected]484955942010-08-19 16:13:18127void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06128 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18129 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29130 DCHECK(!webwidget_);
131
132 if (opener_id != MSG_ROUTING_NONE)
133 opener_id_ = opener_id;
134
[email protected]484955942010-08-19 16:13:18135 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29136
[email protected]484955942010-08-19 16:13:18137 bool result = render_thread_->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29138 if (result) {
[email protected]8085dbc82008-09-26 22:53:44139 render_thread_->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29140 // Take a reference on behalf of the RenderThread. This will be balanced
141 // when we receive ViewMsg_Close.
142 AddRef();
143 } else {
144 DCHECK(false);
145 }
146}
147
148// This is used to complete pending inits and non-pending inits. For non-
149// pending cases, the parent will be the same as the current parent. This
150// indicates we do not need to reparent or anything.
[email protected]18bcc3c2009-01-27 21:39:15151void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29152 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29153
154 host_window_ = parent_hwnd;
155
[email protected]6de74452009-02-25 18:04:59156 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29157}
158
[email protected]a95986a82010-12-24 06:19:28159bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
160 bool handled = true;
161 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
162 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
163 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
164 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
165 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
166 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
167 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
168 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
169 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
170 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
171 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
172 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
173 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
174 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
175 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
176 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
177 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
178 IPC_MESSAGE_UNHANDLED(handled = false)
179 IPC_END_MESSAGE_MAP()
180 return handled;
181}
initial.commit09911bf2008-07-26 23:55:29182
183bool RenderWidget::Send(IPC::Message* message) {
184 // Don't send any messages after the browser has told us to close.
185 if (closing_) {
186 delete message;
187 return false;
188 }
189
190 // If given a messsage without a routing ID, then assign our routing ID.
191 if (message->routing_id() == MSG_ROUTING_NONE)
192 message->set_routing_id(routing_id_);
193
[email protected]d3fc25652009-02-24 22:31:25194 return render_thread_->Send(message);
[email protected]8085dbc82008-09-26 22:53:44195}
196
initial.commit09911bf2008-07-26 23:55:29197// Got a response from the browser after the renderer decided to create a new
198// view.
[email protected]18bcc3c2009-01-27 21:39:15199void RenderWidget::OnCreatingNewAck(gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29200 DCHECK(routing_id_ != MSG_ROUTING_NONE);
201
202 CompleteInit(parent);
203}
204
205void RenderWidget::OnClose() {
206 if (closing_)
207 return;
208 closing_ = true;
209
210 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03211 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]8085dbc82008-09-26 22:53:44212 render_thread_->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03213 SetHidden(false);
214 }
initial.commit09911bf2008-07-26 23:55:29215
initial.commit09911bf2008-07-26 23:55:29216 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25217 // now. Post a task that only gets invoked when there are no nested message
218 // loops.
219 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
220 NewRunnableMethod(this, &RenderWidget::Close));
221
222 // Balances the AddRef taken when we called AddRoute.
223 Release();
initial.commit09911bf2008-07-26 23:55:29224}
225
[email protected]f21c613a2009-02-12 14:46:17226void RenderWidget::OnResize(const gfx::Size& new_size,
227 const gfx::Rect& resizer_rect) {
initial.commit09911bf2008-07-26 23:55:29228 // During shutdown we can just ignore this message.
229 if (!webwidget_)
230 return;
231
[email protected]e2356242010-11-16 22:33:03232 // We shouldn't be asked to resize to our current size.
233 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
234
[email protected]f21c613a2009-02-12 14:46:17235 // Remember the rect where the resize corner will be drawn.
236 resizer_rect_ = resizer_rect;
237
[email protected]e2356242010-11-16 22:33:03238 if (size_ == new_size)
239 return;
240
initial.commit09911bf2008-07-26 23:55:29241 // TODO(darin): We should not need to reset this here.
[email protected]bee16aab2009-08-26 15:55:03242 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29243 needs_repainting_on_restore_ = false;
244
initial.commit09911bf2008-07-26 23:55:29245 size_ = new_size;
246
247 // We should not be sent a Resize message if we have not ACK'd the previous
248 DCHECK(!next_paint_is_resize_ack());
249
[email protected]552e6002009-11-19 05:24:57250 paint_aggregator_.ClearPendingUpdate();
251
initial.commit09911bf2008-07-26 23:55:29252 // When resizing, we want to wait to paint before ACK'ing the resize. This
253 // ensures that we only resize as fast as we can paint. We only need to send
254 // an ACK if we are resized to a non-empty rect.
[email protected]4873c7d2009-07-16 06:36:28255 webwidget_->resize(new_size);
initial.commit09911bf2008-07-26 23:55:29256 if (!new_size.IsEmpty()) {
[email protected]a79d8a632010-11-18 22:35:56257 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46258 // Resize should have caused an invalidation of the entire view.
259 DCHECK(paint_aggregator_.HasPendingUpdate());
260 }
[email protected]2d5d09d52009-06-15 14:29:21261
initial.commit09911bf2008-07-26 23:55:29262 // We will send the Resize_ACK flag once we paint again.
263 set_next_paint_is_resize_ack();
264 }
265}
266
267void RenderWidget::OnWasHidden() {
268 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03269 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29270}
271
272void RenderWidget::OnWasRestored(bool needs_repainting) {
273 // During shutdown we can just ignore this message.
274 if (!webwidget_)
275 return;
276
277 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03278 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29279
280 if (!needs_repainting && !needs_repainting_on_restore_)
281 return;
282 needs_repainting_on_restore_ = false;
283
[email protected]d65adb12010-04-28 17:26:49284 // Tag the next paint as a restore ack, which is picked up by
285 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29286 set_next_paint_is_restore_ack();
287
288 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56289 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46290 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
291 } else {
292 scheduleComposite();
293 }
initial.commit09911bf2008-07-26 23:55:29294}
295
[email protected]53d3f302009-12-21 04:42:05296void RenderWidget::OnRequestMoveAck() {
297 DCHECK(pending_window_rect_count_);
298 pending_window_rect_count_--;
299}
300
301void RenderWidget::OnUpdateRectAck() {
302 DCHECK(update_reply_pending());
303 update_reply_pending_ = false;
304
[email protected]b4d08452010-10-05 17:34:35305 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
306 // have no current paint buffer.
307 if (current_paint_buf_) {
308 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
309 current_paint_buf_ = NULL;
310 }
311
[email protected]00c39612010-03-06 02:53:28312 // Notify subclasses.
313 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25314
initial.commit09911bf2008-07-26 23:55:29315 // Continue painting if necessary...
[email protected]552e6002009-11-19 05:24:57316 CallDoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29317}
318
initial.commit09911bf2008-07-26 23:55:29319void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
320 void* iter = NULL;
321
322 const char* data;
323 int data_length;
[email protected]5dd768212009-08-13 23:34:49324 handling_input_event_ = true;
325 if (!message.ReadData(&iter, &data, &data_length)) {
326 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29327 return;
[email protected]5dd768212009-08-13 23:34:49328 }
initial.commit09911bf2008-07-26 23:55:29329
330 const WebInputEvent* input_event =
331 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48332
333 bool is_keyboard_shortcut = false;
334 // is_keyboard_shortcut flag is only available for RawKeyDown events.
335 if (input_event->type == WebInputEvent::RawKeyDown)
336 message.ReadBool(&iter, &is_keyboard_shortcut);
337
initial.commit09911bf2008-07-26 23:55:29338 bool processed = false;
[email protected]867125a02009-12-10 06:01:48339 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
340 suppress_next_char_events_ = false;
341 if (webwidget_)
342 processed = webwidget_->handleInputEvent(*input_event);
343 }
344
345 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
346 // it's not processed by webkit, then we need to suppress the upcoming Char
347 // events.
348 if (!processed && is_keyboard_shortcut)
349 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29350
351 IPC::Message* response = new ViewHostMsg_HandleInputEvent_ACK(routing_id_);
352 response->WriteInt(input_event->type);
[email protected]e2824412009-02-27 01:57:05353 response->WriteBool(processed);
354
[email protected]353a34c2010-05-28 23:35:17355 if ((input_event->type == WebInputEvent::MouseMove ||
356 input_event->type == WebInputEvent::MouseWheel) &&
[email protected]552e6002009-11-19 05:24:57357 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24358 // We want to rate limit the input events in this case, so we'll wait for
359 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17360 if (pending_input_event_ack_.get()) {
361 // As two different kinds of events could cause us to postpone an ack
362 // we send it now, if we have one pending. The Browser should never
363 // send us the same kind of event we are delaying the ack for.
364 Send(pending_input_event_ack_.release());
365 }
[email protected]12fbad812009-09-01 18:21:24366 pending_input_event_ack_.reset(response);
367 } else {
368 Send(response);
369 }
370
[email protected]5dd768212009-08-13 23:34:49371 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48372
[email protected]867125a02009-12-10 06:01:48373 if (WebInputEvent::isKeyboardEventType(input_event->type))
[email protected]446705872009-09-10 07:22:48374 DidHandleKeyEvent();
[email protected]6a8ddba52010-09-05 04:38:06375 if (WebInputEvent::isMouseEventType(input_event->type))
376 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
initial.commit09911bf2008-07-26 23:55:29377}
378
379void RenderWidget::OnMouseCaptureLost() {
380 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28381 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29382}
383
384void RenderWidget::OnSetFocus(bool enable) {
385 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33386 if (webwidget_)
387 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29388}
389
390void RenderWidget::ClearFocus() {
391 // We may have got the focus from the browser before this gets processed, in
392 // which case we do not want to unfocus ourself.
393 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28394 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29395}
396
[email protected]2d5d09d52009-06-15 14:29:21397void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00398 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21399 skia::PlatformCanvas* canvas) {
[email protected]719b36f2010-12-22 20:36:46400
[email protected]4fb66842009-12-04 21:41:00401 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21402
403 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00404 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
405 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03406
[email protected]699ab0d2009-04-23 23:19:14407 // If there is a custom background, tile it.
408 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14409 SkPaint paint;
410 SkShader* shader = SkShader::CreateBitmapShader(background_,
411 SkShader::kRepeat_TileMode,
412 SkShader::kRepeat_TileMode);
413 paint.setShader(shader)->unref();
[email protected]8860e4f52009-06-25 01:01:52414 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
[email protected]699ab0d2009-04-23 23:19:14415 canvas->drawPaint(paint);
[email protected]699ab0d2009-04-23 23:19:14416 }
417
[email protected]719b36f2010-12-22 20:36:46418 // First see if this rect is a plugin that can paint itself faster.
419 TransportDIB* optimized_dib = NULL;
420 gfx::Rect optimized_copy_rect, optimized_copy_location;
421 webkit::ppapi::PluginInstance* optimized_instance =
422 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
423 &optimized_copy_location,
424 &optimized_copy_rect);
425 if (optimized_instance) {
426 // This plugin can be optimize-painted and we can just ask it to paint
427 // itself. We don't actually need the TransportDIB in this case.
428 //
429 // This is an optimization for PPAPI plugins that know they're on top of
430 // the page content. If this rect is inside such a plugin, we can save some
431 // time and avoid re-rendering the page content which we know will be
432 // covered by the plugin later (this time can be significant, especially
433 // for a playing movie that is invalidating a lot).
434 //
435 // In the plugin movie case, hopefully the similar call to
436 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
437 // painting, because that avoids copying the plugin image to a different
438 // paint rect. Unfortunately, if anything on the page is animating other
439 // than the movie, it break this optimization since the union of the
440 // invalid regions will be larger than the plugin.
441 //
442 // This code optimizes that case, where we can still avoid painting in
443 // WebKit and filling the background (which can be slow) and just painting
444 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
445 // required.
446 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27447 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46448 } else {
449 // Normal painting case.
450 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
451
452 // Flush to underlying bitmap. TODO(darin): is this needed?
453 canvas->getTopPlatformDevice().accessBitmap(false);
454 }
initial.commit09911bf2008-07-26 23:55:29455
[email protected]4fb66842009-12-04 21:41:00456 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00457 canvas->restore();
458}
459
460void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
461 skia::PlatformCanvas* canvas) {
462 static bool kPaintBorder =
463 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
464 if (!kPaintBorder)
465 return;
466
[email protected]53d3f302009-12-21 04:42:05467 // Cycle through these colors to help distinguish new paint rects.
468 const SkColor colors[] = {
469 SkColorSetARGB(0x3F, 0xFF, 0, 0),
470 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
471 SkColorSetARGB(0x3F, 0, 0, 0xFF),
472 };
473 static int color_selector = 0;
474
[email protected]4fb66842009-12-04 21:41:00475 SkPaint paint;
476 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05477 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00478 paint.setStrokeWidth(1);
479
480 SkIRect irect;
481 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
482 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29483}
484
[email protected]52ccd0ea2011-02-16 01:09:05485void RenderWidget::AnimationCallback() {
486 animation_task_posted_ = false;
[email protected]7c4329e2011-02-18 22:02:59487 if (!animation_update_pending_)
488 return;
489 if (!animation_floor_time_.is_null()) {
490 // Record when we fired (according to base::Time::Now()) relative to when
491 // we posted the task to quantify how much the base::Time/base::TimeTicks
492 // skew is affecting animations.
493 base::TimeDelta animation_callback_delay = base::Time::Now() -
494 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
495 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
496 animation_callback_delay,
497 base::TimeDelta::FromMilliseconds(0),
498 base::TimeDelta::FromMilliseconds(30),
499 25);
500 }
[email protected]52ccd0ea2011-02-16 01:09:05501 CallDoDeferredUpdate();
[email protected]12fbad812009-09-01 18:21:24502}
503
[email protected]52ccd0ea2011-02-16 01:09:05504void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59505 if (!animation_update_pending_)
506 return;
507 base::Time now = base::Time::Now();
508 if (now >= animation_floor_time_) {
509 animation_floor_time_ = now + base::TimeDelta::FromMilliseconds(16);
510 // Set a timer to call us back after 16ms (targetting 60FPS) before
511 // running animation callbacks so that if a callback requests another
512 // we'll be sure to run it at the proper time.
513 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
514 this, &RenderWidget::AnimationCallback), 16);
515 animation_task_posted_ = true;
516 animation_update_pending_ = false;
517 // Explicitly pump the WebCore Timer queue to avoid starvation on OS X.
518 // See crbug.com/71735.
519 // TODO(jamesr) Remove this call once crbug.com/72007 is fixed.
520 RenderThread::current()->GetWebKitClientImpl()->DoTimeout();
521 webwidget_->animate();
522 return;
[email protected]5f8b1022011-01-21 23:34:50523 }
[email protected]7c4329e2011-02-18 22:02:59524 if (animation_task_posted_)
525 return;
526 // This code uses base::Time::Now() to calculate the floor and next fire
527 // time because javascript's Date object uses base::Time::Now(). The
528 // message loop uses base::TimeTicks, which on windows can have a
529 // different granularity than base::Time.
530 // The upshot of all this is that this function might be called before
531 // base::Time::Now() has advanced past the animation_floor_time_. To
532 // avoid exposing this delay to javascript, we keep posting delayed
533 // tasks until base::Time::Now() has advanced far enough.
534 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
535 animation_task_posted_ = true;
536 MessageLoop::current()->PostDelayedTask(FROM_HERE,
537 NewRunnableMethod(this, &RenderWidget::AnimationCallback), delay);
[email protected]5f8b1022011-01-21 23:34:50538}
539
[email protected]52ccd0ea2011-02-16 01:09:05540void RenderWidget::CallDoDeferredUpdate() {
541 DoDeferredUpdate();
542
543 if (pending_input_event_ack_.get())
544 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21545}
546
[email protected]552e6002009-11-19 05:24:57547void RenderWidget::DoDeferredUpdate() {
[email protected]5f8b1022011-01-21 23:34:50548 if (!webwidget_ || update_reply_pending())
initial.commit09911bf2008-07-26 23:55:29549 return;
550
[email protected]552e6002009-11-19 05:24:57551 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29552 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57553 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29554 needs_repainting_on_restore_ = true;
555 return;
556 }
557
[email protected]52ccd0ea2011-02-16 01:09:05558 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50559
[email protected]f98d7e3c2010-09-13 22:30:46560 // Layout may generate more invalidation. It may also enable the
561 // GPU acceleration, so make sure to run layout before we send the
562 // GpuRenderingActivated message.
563 webwidget_->layout();
564
[email protected]5f8b1022011-01-21 23:34:50565 // Suppress painting if nothing is dirty. This has to be done after updating
566 // animations running layout as these may generate further invalidations.
567 if (!paint_aggregator_.HasPendingUpdate())
568 return;
569
[email protected]552e6002009-11-19 05:24:57570 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29571 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30572 PaintAggregator::PendingUpdate update;
573 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29574
[email protected]53d3f302009-12-21 04:42:05575 gfx::Rect scroll_damage = update.GetScrollDamage();
576 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29577
[email protected]ca4847f2010-09-24 05:39:15578 // A plugin may be able to do an optimized paint. First check this, in which
579 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46580 // This optimization allows PPAPI plugins that declare themselves on top of
581 // the page (like a traditional windowed plugin) to be able to animate (think
582 // movie playing) without repeatedly re-painting the page underneath, or
583 // copying the plugin backing store (since we can send the plugin's backing
584 // store directly to the browser).
585 //
586 // This optimization only works when the entire invalid region is contained
587 // within the plugin. There is a related optimization in PaintRect for the
588 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15589 TransportDIB::Id dib_id = TransportDIB::Id();
590 TransportDIB* dib = NULL;
[email protected]cef3362f2009-12-21 17:48:45591 std::vector<gfx::Rect> copy_rects;
[email protected]ca4847f2010-09-24 05:39:15592 gfx::Rect optimized_copy_rect, optimized_copy_location;
593 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56594 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15595 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
596 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27597 // Only update the part of the plugin that actually changed.
598 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]ca4847f2010-09-24 05:39:15599 bounds = optimized_copy_location;
600 copy_rects.push_back(optimized_copy_rect);
601 dib_id = dib->id();
[email protected]a79d8a632010-11-18 22:35:56602 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46603 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15604 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35605 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
606 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46607 if (!canvas.get()) {
608 NOTREACHED();
609 return;
610 }
[email protected]cef3362f2009-12-21 17:48:45611
[email protected]f98d7e3c2010-09-13 22:30:46612 // We may get back a smaller canvas than we asked for.
613 // TODO(darin): This seems like it could cause painting problems!
614 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
615 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
616 bounds.set_width(canvas->getDevice()->width());
617 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05618
[email protected]f98d7e3c2010-09-13 22:30:46619 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
620
[email protected]f98d7e3c2010-09-13 22:30:46621 // The scroll damage is just another rectangle to paint and copy.
622 copy_rects.swap(update.paint_rects);
623 if (!scroll_damage.IsEmpty())
624 copy_rects.push_back(scroll_damage);
625
626 for (size_t i = 0; i < copy_rects.size(); ++i)
627 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]ca4847f2010-09-24 05:39:15628
[email protected]b4d08452010-10-05 17:34:35629 dib_id = current_paint_buf_->id();
[email protected]f98d7e3c2010-09-13 22:30:46630 } else { // Accelerated compositing path
631 // Begin painting.
[email protected]50bd6452010-11-27 19:39:42632 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46633 }
634
635 // sending an ack to browser process that the paint is complete...
[email protected]53d3f302009-12-21 04:42:05636 ViewHostMsg_UpdateRect_Params params;
[email protected]36808ad2010-10-20 19:18:30637 params.bitmap = dib_id;
[email protected]53d3f302009-12-21 04:42:05638 params.bitmap_rect = bounds;
639 params.dx = update.scroll_delta.x();
640 params.dy = update.scroll_delta.y();
[email protected]a79d8a632010-11-18 22:35:56641 if (is_accelerated_compositing_active_) {
[email protected]b167ca662010-05-14 00:05:34642 // If painting is done via the gpu process then we clear out all damage
643 // rects to save the browser process from doing unecessary work.
644 params.scroll_rect = gfx::Rect();
645 params.copy_rects.clear();
646 } else {
647 params.scroll_rect = update.scroll_rect;
648 params.copy_rects.swap(copy_rects); // TODO(darin): clip to bounds?
649 }
[email protected]53d3f302009-12-21 04:42:05650 params.view_size = size_;
[email protected]e2356242010-11-16 22:33:03651 params.resizer_rect = resizer_rect_;
[email protected]53d3f302009-12-21 04:42:05652 params.plugin_window_moves.swap(plugin_window_moves_);
653 params.flags = next_paint_flags_;
[email protected]d54169e92011-01-21 09:19:52654 params.scroll_offset = GetScrollOffset();
[email protected]53d3f302009-12-21 04:42:05655
656 update_reply_pending_ = true;
657 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
658 next_paint_flags_ = 0;
659
[email protected]fa7b1dc2010-06-23 17:53:04660 UpdateInputMethod();
[email protected]00c39612010-03-06 02:53:28661
662 // Let derived classes know we've painted.
663 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29664}
665
666///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46667// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29668
[email protected]4873c7d2009-07-16 06:36:28669void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:57670 // We only want one pending DoDeferredUpdate call at any time...
671 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29672
[email protected]552e6002009-11-19 05:24:57673 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48674 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57675 gfx::Rect damaged_rect = view_rect.Intersect(rect);
676 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29677 return;
678
[email protected]552e6002009-11-19 05:24:57679 paint_aggregator_.InvalidateRect(damaged_rect);
680
681 // We may not need to schedule another call to DoDeferredUpdate.
682 if (update_pending)
683 return;
684 if (!paint_aggregator_.HasPendingUpdate())
685 return;
[email protected]53d3f302009-12-21 04:42:05686 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57687 return;
688
689 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29690 // 1) Ensures that we call WebView::Paint without a bunch of other junk
691 // on the call stack.
692 // 2) Allows us to collect more damage rects before painting to help coalesce
693 // the work that we will need to do.
694 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57695 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29696}
697
[email protected]4873c7d2009-07-16 06:36:28698void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46699 // Drop scrolls on the floor when we are in compositing mode.
700 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56701 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46702 return;
703
[email protected]552e6002009-11-19 05:24:57704 // We only want one pending DoDeferredUpdate call at any time...
705 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29706
[email protected]552e6002009-11-19 05:24:57707 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48708 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57709 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
710 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29711 return;
712
[email protected]552e6002009-11-19 05:24:57713 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
714
715 // We may not need to schedule another call to DoDeferredUpdate.
716 if (update_pending)
717 return;
718 if (!paint_aggregator_.HasPendingUpdate())
719 return;
[email protected]53d3f302009-12-21 04:42:05720 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57721 return;
722
723 // Perform updating asynchronously. This serves two purposes:
724 // 1) Ensures that we call WebView::Paint without a bunch of other junk
725 // on the call stack.
726 // 2) Allows us to collect more damage rects before painting to help coalesce
727 // the work that we will need to do.
initial.commit09911bf2008-07-26 23:55:29728 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57729 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29730}
731
[email protected]a79d8a632010-11-18 22:35:56732void RenderWidget::didActivateAcceleratedCompositing(bool active) {
733 is_accelerated_compositing_active_ = active;
[email protected]50bd6452010-11-27 19:39:42734 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]a79d8a632010-11-18 22:35:56735 routing_id_, is_accelerated_compositing_active_));
736}
737
[email protected]f98d7e3c2010-09-13 22:30:46738void RenderWidget::scheduleComposite() {
739 // TODO(nduca): replace with something a little less hacky. The reason this
740 // hack is still used is because the Invalidate-DoDeferredUpdate loop
741 // contains a lot of host-renderer synchronization logic that is still
742 // important for the accelerated compositing case. The option of simply
743 // duplicating all that code is less desirable than "faking out" the
744 // invalidation path using a magical damage rect.
[email protected]e2356242010-11-16 22:33:03745 didInvalidateRect(WebRect(0, 0, 1, 1));
[email protected]f98d7e3c2010-09-13 22:30:46746}
747
[email protected]5f8b1022011-01-21 23:34:50748void RenderWidget::scheduleAnimation() {
[email protected]ee3d3ad2011-02-04 00:42:21749 if (!animation_update_pending_) {
750 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:05751 if (!animation_task_posted_) {
752 animation_task_posted_ = true;
753 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
754 this, &RenderWidget::AnimationCallback));
755 }
[email protected]ee3d3ad2011-02-04 00:42:21756 }
[email protected]5f8b1022011-01-21 23:34:50757}
758
[email protected]4873c7d2009-07-16 06:36:28759void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:30760 // TODO(darin): Eliminate this temporary.
761 WebCursor cursor(cursor_info);
762
initial.commit09911bf2008-07-26 23:55:29763 // Only send a SetCursor message if we need to make a change.
764 if (!current_cursor_.IsEqual(cursor)) {
765 current_cursor_ = cursor;
766 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
767 }
768}
769
770// We are supposed to get a single call to Show for a newly created RenderWidget
771// that was created via RenderWidget::CreateWebView. So, we wait until this
772// point to dispatch the ShowWidget message.
773//
774// This method provides us with the information about how to display the newly
775// created RenderWidget (i.e., as a constrained popup or as a new tab).
776//
[email protected]4873c7d2009-07-16 06:36:28777void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:29778 DCHECK(!did_show_) << "received extraneous Show call";
779 DCHECK(routing_id_ != MSG_ROUTING_NONE);
780 DCHECK(opener_id_ != MSG_ROUTING_NONE);
781
[email protected]8de12d942010-11-17 20:42:44782 if (did_show_)
783 return;
784
785 did_show_ = true;
786 // NOTE: initial_pos_ may still have its default values at this point, but
787 // that's okay. It'll be ignored if as_popup is false, or the browser
788 // process will impose a default position otherwise.
789 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
790 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:29791}
792
[email protected]4873c7d2009-07-16 06:36:28793void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:29794}
795
[email protected]4873c7d2009-07-16 06:36:28796void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:29797}
798
[email protected]2533ce12009-05-09 00:02:24799void RenderWidget::DoDeferredClose() {
800 Send(new ViewHostMsg_Close(routing_id_));
801}
802
[email protected]4873c7d2009-07-16 06:36:28803void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:29804 // If a page calls window.close() twice, we'll end up here twice, but that's
805 // OK. It is safe to send multiple Close messages.
806
[email protected]2533ce12009-05-09 00:02:24807 // Ask the RenderWidgetHost to initiate close. We could be called from deep
808 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
809 // could be closed before the JS finishes executing. So instead, post a
810 // message back to the message loop, which won't run until the JS is
811 // complete, and then the Close message can be sent.
[email protected]75ae4492009-07-10 00:05:15812 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]2533ce12009-05-09 00:02:24813 this, &RenderWidget::DoDeferredClose));
initial.commit09911bf2008-07-26 23:55:29814}
815
816void RenderWidget::Close() {
817 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:28818 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:29819 webwidget_ = NULL;
820 }
821}
822
[email protected]4873c7d2009-07-16 06:36:28823WebRect RenderWidget::windowRect() {
824 if (pending_window_rect_count_)
825 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24826
[email protected]b3f2b912009-04-09 16:18:52827 gfx::Rect rect;
828 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28829 return rect;
initial.commit09911bf2008-07-26 23:55:29830}
831
[email protected]4873c7d2009-07-16 06:36:28832void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:29833 if (did_show_) {
834 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:24835 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:29836 } else {
837 initial_pos_ = pos;
838 }
839}
840
[email protected]2533ce12009-05-09 00:02:24841void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
842 pending_window_rect_ = rect;
843 pending_window_rect_count_++;
844}
845
[email protected]4873c7d2009-07-16 06:36:28846WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:24847 if (pending_window_rect_count_) {
848 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
849 // the RootWindowRect is probably going to return wrong results since the
850 // browser may not have processed the Move yet. There isn't really anything
851 // good to do in this case, and it shouldn't happen - since this size is
852 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:28853 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24854 }
855
[email protected]b3f2b912009-04-09 16:18:52856 gfx::Rect rect;
857 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28858 return rect;
[email protected]d4547452008-08-28 18:36:37859}
860
[email protected]4873c7d2009-07-16 06:36:28861WebRect RenderWidget::windowResizerRect() {
862 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:19863}
864
[email protected]fa7b1dc2010-06-23 17:53:04865void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:03866 // To prevent this renderer process from sending unnecessary IPC messages to
867 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:04868 // only during the input method attached to the browser process is active.
869 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:29870}
871
[email protected]fa7b1dc2010-06-23 17:53:04872void RenderWidget::OnImeSetComposition(
873 const string16& text,
874 const std::vector<WebCompositionUnderline>& underlines,
875 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:28876 if (!webwidget_)
877 return;
[email protected]fa7b1dc2010-06-23 17:53:04878 if (!webwidget_->setComposition(
879 text, WebVector<WebCompositionUnderline>(underlines),
880 selection_start, selection_end)) {
881 // If we failed to set the composition text, then we need to let the browser
882 // process to cancel the input method's ongoing composition session, to make
883 // sure we are in a consistent state.
884 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:26885 }
[email protected]fa7b1dc2010-06-23 17:53:04886}
887
[email protected]6b349652011-01-05 18:36:24888void RenderWidget::OnImeConfirmComposition(const string16& text) {
[email protected]fa7b1dc2010-06-23 17:53:04889 if (webwidget_)
[email protected]6b349652011-01-05 18:36:24890 webwidget_->confirmComposition(text);
initial.commit09911bf2008-07-26 23:55:29891}
892
[email protected]948f7ab72010-05-28 23:48:08893// This message causes the renderer to render an image of the
894// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:49895void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:09896 int tag,
[email protected]948f7ab72010-05-28 23:48:08897 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:49898 const gfx::Size& desired_size) {
[email protected]45c6aad32010-11-11 04:46:25899 if (!webwidget_ || !TransportDIB::is_valid(dib_handle)) {
900 if (TransportDIB::is_valid(dib_handle)) {
901 // Close our unused handle.
902#if defined(OS_WIN)
903 ::CloseHandle(dib_handle);
904#elif defined(OS_MACOSX)
905 base::SharedMemory::CloseHandle(dib_handle);
906#endif
907 }
[email protected]d65adb12010-04-28 17:26:49908 return;
[email protected]45c6aad32010-11-11 04:46:25909 }
[email protected]d65adb12010-04-28 17:26:49910
[email protected]948f7ab72010-05-28 23:48:08911 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:49912 // If one of these is empty, then we just return the dib we were
913 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:09914 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:49915 return;
916 }
917
918 // Map the given DIB ID into this process, and unmap it at the end
919 // of this function.
[email protected]45c6aad32010-11-11 04:46:25920 scoped_ptr<TransportDIB> paint_at_size_buffer(
921 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:30922
923 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:49924 float x_scale = static_cast<float>(desired_size.width()) /
925 static_cast<float>(canvas_size.width());
926 float y_scale = static_cast<float>(desired_size.height()) /
927 static_cast<float>(canvas_size.height());
928
[email protected]ee8d6fd2010-05-26 17:05:48929 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49930 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
931 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:48932 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49933
[email protected]36808ad2010-10-20 19:18:30934 scoped_ptr<skia::PlatformCanvas> canvas(
935 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
936 canvas_size.height()));
937 if (!canvas.get()) {
938 NOTREACHED();
939 return;
940 }
941
[email protected]d65adb12010-04-28 17:26:49942 // Reset bounds to what we actually received, but they should be the
943 // same.
944 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
945 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
946 bounds.set_width(canvas->getDevice()->width());
947 bounds.set_height(canvas->getDevice()->height());
948
949 canvas->save();
[email protected]948f7ab72010-05-28 23:48:08950 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:49951 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
952
[email protected]948f7ab72010-05-28 23:48:08953 // Have to make sure we're laid out at the right size before
954 // rendering.
955 gfx::Size old_size = webwidget_->size();
956 webwidget_->resize(page_size);
957 webwidget_->layout();
958
[email protected]d65adb12010-04-28 17:26:49959 // Paint the entire thing (using original bounds, not scaled bounds).
960 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
961 canvas->restore();
962
[email protected]948f7ab72010-05-28 23:48:08963 // Return the widget to its previous size.
964 webwidget_->resize(old_size);
965
[email protected]c88c9442010-07-19 18:55:09966 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:49967}
968
[email protected]ec7dc112008-08-06 05:30:12969void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
970 // During shutdown we can just ignore this message.
971 if (!webwidget_)
972 return;
973
974 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:56975 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46976 scheduleComposite();
977 } else {
978 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
979 didInvalidateRect(repaint_rect);
980 }
[email protected]ec7dc112008-08-06 05:30:12981}
982
[email protected]4873c7d2009-07-16 06:36:28983void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:11984 if (!webwidget_)
985 return;
[email protected]4873c7d2009-07-16 06:36:28986 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:11987}
988
[email protected]719b36f2010-12-22 20:36:46989webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:15990 const gfx::Rect& paint_bounds,
991 TransportDIB** dib,
992 gfx::Rect* location,
993 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:46994 // Bare RenderWidgets don't support optimized plugin painting.
995 return NULL;
[email protected]ca4847f2010-09-24 05:39:15996}
997
[email protected]bcaf2272011-02-15 15:29:43998gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:52999 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431000 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521001}
1002
[email protected]bee16aab2009-08-26 15:55:031003void RenderWidget::SetHidden(bool hidden) {
1004 if (is_hidden_ == hidden)
1005 return;
1006
1007 // The status has changed. Tell the RenderThread about it.
1008 is_hidden_ = hidden;
1009 if (is_hidden_)
1010 render_thread_->WidgetHidden();
1011 else
1012 render_thread_->WidgetRestored();
1013}
1014
[email protected]699ab0d2009-04-23 23:19:141015void RenderWidget::SetBackground(const SkBitmap& background) {
1016 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461017
[email protected]699ab0d2009-04-23 23:19:141018 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281019 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141020}
1021
[email protected]674741932009-02-04 23:44:461022bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051023 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461024}
1025
1026bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051027 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461028}
1029
1030void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051031 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461032}
1033
1034void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051035 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461036}
1037
1038void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051039 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461040}
1041
[email protected]fa7b1dc2010-06-23 17:53:041042void RenderWidget::UpdateInputMethod() {
1043 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291044 return;
[email protected]fa7b1dc2010-06-23 17:53:041045
1046 WebTextInputType new_type = WebKit::WebTextInputTypeNone;
1047 WebRect new_caret_bounds;
1048
1049 if (webwidget_) {
1050 new_type = webwidget_->textInputType();
1051 new_caret_bounds = webwidget_->caretOrSelectionBounds();
initial.commit09911bf2008-07-26 23:55:291052 }
[email protected]fa7b1dc2010-06-23 17:53:041053
1054 // Only sends text input type and caret bounds to the browser process if they
1055 // are changed.
1056 if (text_input_type_ != new_type || caret_bounds_ != new_caret_bounds) {
1057 text_input_type_ = new_type;
1058 caret_bounds_ = new_caret_bounds;
1059 Send(new ViewHostMsg_ImeUpdateTextInputState(
1060 routing_id(), new_type, new_caret_bounds));
initial.commit09911bf2008-07-26 23:55:291061 }
initial.commit09911bf2008-07-26 23:55:291062}
1063
[email protected]4873c7d2009-07-16 06:36:281064WebScreenInfo RenderWidget::screenInfo() {
1065 WebScreenInfo results;
1066 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1067 return results;
1068}
1069
[email protected]fa7b1dc2010-06-23 17:53:041070void RenderWidget::resetInputMethod() {
1071 if (!input_method_is_active_)
1072 return;
1073
1074 // If the last text input type is not None, then we should finish any
1075 // ongoing composition regardless of the new text input type.
1076 if (text_input_type_ != WebKit::WebTextInputTypeNone) {
1077 // If a composition text exists, then we need to let the browser process
1078 // to cancel the input method's ongoing composition session.
1079 if (webwidget_->confirmComposition())
1080 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1081 }
1082}
1083
[email protected]f103ab72009-09-02 17:10:591084void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501085 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291086 size_t i = 0;
1087 for (; i < plugin_window_moves_.size(); ++i) {
1088 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581089 if (move.rects_valid) {
1090 plugin_window_moves_[i] = move;
1091 } else {
1092 plugin_window_moves_[i].visible = move.visible;
1093 }
initial.commit09911bf2008-07-26 23:55:291094 break;
1095 }
1096 }
1097
1098 if (i == plugin_window_moves_.size())
1099 plugin_window_moves_.push_back(move);
1100}
[email protected]268654772009-08-06 23:02:041101
1102void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1103 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1104 i != plugin_window_moves_.end(); ++i) {
1105 if (i->window == window) {
1106 plugin_window_moves_.erase(i);
1107 break;
1108 }
1109 }
1110}