blob: 28ff55f021a13d23f73dee0eb5bf999a9cc44d03 [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]484955942010-08-19 16:13:1819#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4820#include "skia/ext/platform_canvas.h"
[email protected]d5282e72009-05-13 13:16:5221#include "third_party/skia/include/core/SkShader.h"
[email protected]8bd0fe62011-01-17 06:44:3722#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
23#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
24#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
25#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h"
26#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
27#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
[email protected]08397d52011-02-05 01:53:3828#include "ui/gfx/point.h"
29#include "ui/gfx/size.h"
[email protected]8c89e7792009-08-19 21:18:3430#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5031#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4632#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4833
34#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4935#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5236#include "third_party/skia/include/core/SkPixelRef.h"
37#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4838#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4439
[email protected]8bd0fe62011-01-17 06:44:3740#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2941
[email protected]fa7b1dc2010-06-23 17:53:0442using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3043using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2244using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0645using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2846using WebKit::WebNavigationPolicy;
47using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5948using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1849using WebKit::WebPopupType;
[email protected]b3f2b912009-04-09 16:18:5250using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1951using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5252using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2853using WebKit::WebTextDirection;
[email protected]fa7b1dc2010-06-23 17:53:0454using WebKit::WebTextInputType;
55using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1856using WebKit::WebWidget;
[email protected]62cb33cae2009-03-27 23:30:2257
[email protected]3e2b375b2010-04-07 17:03:1258RenderWidget::RenderWidget(RenderThreadBase* render_thread,
59 WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2960 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1161 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2962 opener_id_(MSG_ROUTING_NONE),
[email protected]8085dbc82008-09-26 22:53:4463 render_thread_(render_thread),
[email protected]659f73f2009-10-13 13:43:4264 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3565 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2966 next_paint_flags_(0),
[email protected]53d3f302009-12-21 04:42:0567 update_reply_pending_(false),
initial.commit09911bf2008-07-26 23:55:2968 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2969 is_hidden_(false),
70 needs_repainting_on_restore_(false),
71 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4972 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4873 closing_(false),
[email protected]fa7b1dc2010-06-23 17:53:0474 input_method_is_active_(false),
75 text_input_type_(WebKit::WebTextInputTypeNone),
[email protected]3e2b375b2010-04-07 17:03:1276 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4877 pending_window_rect_count_(0),
[email protected]edbcde932010-05-07 17:10:4678 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5079 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2180 animation_update_pending_(false),
81 animation_waiting_for_paint_(false) {
[email protected]8930d472009-02-21 08:05:2882 RenderProcess::current()->AddRefProcess();
[email protected]8085dbc82008-09-26 22:53:4483 DCHECK(render_thread_);
initial.commit09911bf2008-07-26 23:55:2984}
85
86RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:1187 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]b4d08452010-10-05 17:34:3588 if (current_paint_buf_) {
89 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
90 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:2991 }
[email protected]8930d472009-02-21 08:05:2892 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:2993}
94
[email protected]484955942010-08-19 16:13:1895// static
[email protected]8085dbc82008-09-26 22:53:4496RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]0ebf3872008-11-07 21:35:0397 RenderThreadBase* render_thread,
[email protected]3e2b375b2010-04-07 17:03:1298 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:2999 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]ad8e04a2010-11-01 04:16:27100 scoped_refptr<RenderWidget> widget(new RenderWidget(render_thread,
101 popup_type));
initial.commit09911bf2008-07-26 23:55:29102 widget->Init(opener_id); // adds reference
103 return widget;
104}
105
[email protected]484955942010-08-19 16:13:18106// static
107WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
108 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03109 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18110 break;
111 case WebKit::WebPopupTypeSelect:
112 case WebKit::WebPopupTypeSuggestion:
113 return WebPopupMenu::create(render_widget);
114 default:
115 NOTREACHED();
116 }
117 return NULL;
118}
119
initial.commit09911bf2008-07-26 23:55:29120void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18121 DoInit(opener_id,
122 RenderWidget::CreateWebWidget(this),
123 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
124}
125
[email protected]484955942010-08-19 16:13:18126void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06127 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18128 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29129 DCHECK(!webwidget_);
130
131 if (opener_id != MSG_ROUTING_NONE)
132 opener_id_ = opener_id;
133
[email protected]484955942010-08-19 16:13:18134 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29135
[email protected]484955942010-08-19 16:13:18136 bool result = render_thread_->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29137 if (result) {
[email protected]8085dbc82008-09-26 22:53:44138 render_thread_->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29139 // Take a reference on behalf of the RenderThread. This will be balanced
140 // when we receive ViewMsg_Close.
141 AddRef();
142 } else {
143 DCHECK(false);
144 }
145}
146
147// This is used to complete pending inits and non-pending inits. For non-
148// pending cases, the parent will be the same as the current parent. This
149// indicates we do not need to reparent or anything.
[email protected]18bcc3c2009-01-27 21:39:15150void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29151 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29152
153 host_window_ = parent_hwnd;
154
[email protected]6de74452009-02-25 18:04:59155 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29156}
157
[email protected]a95986a82010-12-24 06:19:28158bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
159 bool handled = true;
160 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
161 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
162 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
163 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
164 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
165 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
166 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
167 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
168 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
169 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
170 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
171 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
172 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
173 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
174 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
175 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
176 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
177 IPC_MESSAGE_UNHANDLED(handled = false)
178 IPC_END_MESSAGE_MAP()
179 return handled;
180}
initial.commit09911bf2008-07-26 23:55:29181
182bool RenderWidget::Send(IPC::Message* message) {
183 // Don't send any messages after the browser has told us to close.
184 if (closing_) {
185 delete message;
186 return false;
187 }
188
189 // If given a messsage without a routing ID, then assign our routing ID.
190 if (message->routing_id() == MSG_ROUTING_NONE)
191 message->set_routing_id(routing_id_);
192
[email protected]d3fc25652009-02-24 22:31:25193 return render_thread_->Send(message);
[email protected]8085dbc82008-09-26 22:53:44194}
195
initial.commit09911bf2008-07-26 23:55:29196// Got a response from the browser after the renderer decided to create a new
197// view.
[email protected]18bcc3c2009-01-27 21:39:15198void RenderWidget::OnCreatingNewAck(gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29199 DCHECK(routing_id_ != MSG_ROUTING_NONE);
200
201 CompleteInit(parent);
202}
203
204void RenderWidget::OnClose() {
205 if (closing_)
206 return;
207 closing_ = true;
208
209 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03210 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]8085dbc82008-09-26 22:53:44211 render_thread_->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03212 SetHidden(false);
213 }
initial.commit09911bf2008-07-26 23:55:29214
initial.commit09911bf2008-07-26 23:55:29215 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25216 // now. Post a task that only gets invoked when there are no nested message
217 // loops.
218 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
219 NewRunnableMethod(this, &RenderWidget::Close));
220
221 // Balances the AddRef taken when we called AddRoute.
222 Release();
initial.commit09911bf2008-07-26 23:55:29223}
224
[email protected]f21c613a2009-02-12 14:46:17225void RenderWidget::OnResize(const gfx::Size& new_size,
226 const gfx::Rect& resizer_rect) {
initial.commit09911bf2008-07-26 23:55:29227 // During shutdown we can just ignore this message.
228 if (!webwidget_)
229 return;
230
[email protected]e2356242010-11-16 22:33:03231 // We shouldn't be asked to resize to our current size.
232 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
233
[email protected]f21c613a2009-02-12 14:46:17234 // Remember the rect where the resize corner will be drawn.
235 resizer_rect_ = resizer_rect;
236
[email protected]e2356242010-11-16 22:33:03237 if (size_ == new_size)
238 return;
239
initial.commit09911bf2008-07-26 23:55:29240 // TODO(darin): We should not need to reset this here.
[email protected]bee16aab2009-08-26 15:55:03241 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29242 needs_repainting_on_restore_ = false;
243
initial.commit09911bf2008-07-26 23:55:29244 size_ = new_size;
245
246 // We should not be sent a Resize message if we have not ACK'd the previous
247 DCHECK(!next_paint_is_resize_ack());
248
[email protected]552e6002009-11-19 05:24:57249 paint_aggregator_.ClearPendingUpdate();
250
initial.commit09911bf2008-07-26 23:55:29251 // When resizing, we want to wait to paint before ACK'ing the resize. This
252 // ensures that we only resize as fast as we can paint. We only need to send
253 // an ACK if we are resized to a non-empty rect.
[email protected]4873c7d2009-07-16 06:36:28254 webwidget_->resize(new_size);
initial.commit09911bf2008-07-26 23:55:29255 if (!new_size.IsEmpty()) {
[email protected]a79d8a632010-11-18 22:35:56256 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46257 // Resize should have caused an invalidation of the entire view.
258 DCHECK(paint_aggregator_.HasPendingUpdate());
259 }
[email protected]2d5d09d52009-06-15 14:29:21260
initial.commit09911bf2008-07-26 23:55:29261 // We will send the Resize_ACK flag once we paint again.
262 set_next_paint_is_resize_ack();
263 }
264}
265
266void RenderWidget::OnWasHidden() {
267 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03268 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29269}
270
271void RenderWidget::OnWasRestored(bool needs_repainting) {
272 // During shutdown we can just ignore this message.
273 if (!webwidget_)
274 return;
275
276 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03277 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29278
279 if (!needs_repainting && !needs_repainting_on_restore_)
280 return;
281 needs_repainting_on_restore_ = false;
282
[email protected]d65adb12010-04-28 17:26:49283 // Tag the next paint as a restore ack, which is picked up by
284 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29285 set_next_paint_is_restore_ack();
286
287 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56288 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46289 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
290 } else {
291 scheduleComposite();
292 }
initial.commit09911bf2008-07-26 23:55:29293}
294
[email protected]53d3f302009-12-21 04:42:05295void RenderWidget::OnRequestMoveAck() {
296 DCHECK(pending_window_rect_count_);
297 pending_window_rect_count_--;
298}
299
300void RenderWidget::OnUpdateRectAck() {
301 DCHECK(update_reply_pending());
302 update_reply_pending_ = false;
303
[email protected]b4d08452010-10-05 17:34:35304 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
305 // have no current paint buffer.
306 if (current_paint_buf_) {
307 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
308 current_paint_buf_ = NULL;
309 }
310
[email protected]00c39612010-03-06 02:53:28311 // Notify subclasses.
312 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25313
initial.commit09911bf2008-07-26 23:55:29314 // Continue painting if necessary...
[email protected]552e6002009-11-19 05:24:57315 CallDoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29316}
317
initial.commit09911bf2008-07-26 23:55:29318void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
319 void* iter = NULL;
320
321 const char* data;
322 int data_length;
[email protected]5dd768212009-08-13 23:34:49323 handling_input_event_ = true;
324 if (!message.ReadData(&iter, &data, &data_length)) {
325 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29326 return;
[email protected]5dd768212009-08-13 23:34:49327 }
initial.commit09911bf2008-07-26 23:55:29328
329 const WebInputEvent* input_event =
330 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48331
332 bool is_keyboard_shortcut = false;
333 // is_keyboard_shortcut flag is only available for RawKeyDown events.
334 if (input_event->type == WebInputEvent::RawKeyDown)
335 message.ReadBool(&iter, &is_keyboard_shortcut);
336
initial.commit09911bf2008-07-26 23:55:29337 bool processed = false;
[email protected]867125a02009-12-10 06:01:48338 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
339 suppress_next_char_events_ = false;
340 if (webwidget_)
341 processed = webwidget_->handleInputEvent(*input_event);
342 }
343
344 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
345 // it's not processed by webkit, then we need to suppress the upcoming Char
346 // events.
347 if (!processed && is_keyboard_shortcut)
348 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29349
350 IPC::Message* response = new ViewHostMsg_HandleInputEvent_ACK(routing_id_);
351 response->WriteInt(input_event->type);
[email protected]e2824412009-02-27 01:57:05352 response->WriteBool(processed);
353
[email protected]353a34c2010-05-28 23:35:17354 if ((input_event->type == WebInputEvent::MouseMove ||
355 input_event->type == WebInputEvent::MouseWheel) &&
[email protected]552e6002009-11-19 05:24:57356 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24357 // We want to rate limit the input events in this case, so we'll wait for
358 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17359 if (pending_input_event_ack_.get()) {
360 // As two different kinds of events could cause us to postpone an ack
361 // we send it now, if we have one pending. The Browser should never
362 // send us the same kind of event we are delaying the ack for.
363 Send(pending_input_event_ack_.release());
364 }
[email protected]12fbad812009-09-01 18:21:24365 pending_input_event_ack_.reset(response);
366 } else {
367 Send(response);
368 }
369
[email protected]5dd768212009-08-13 23:34:49370 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48371
[email protected]867125a02009-12-10 06:01:48372 if (WebInputEvent::isKeyboardEventType(input_event->type))
[email protected]446705872009-09-10 07:22:48373 DidHandleKeyEvent();
[email protected]6a8ddba52010-09-05 04:38:06374 if (WebInputEvent::isMouseEventType(input_event->type))
375 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
initial.commit09911bf2008-07-26 23:55:29376}
377
378void RenderWidget::OnMouseCaptureLost() {
379 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28380 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29381}
382
383void RenderWidget::OnSetFocus(bool enable) {
384 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33385 if (webwidget_)
386 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29387}
388
389void RenderWidget::ClearFocus() {
390 // We may have got the focus from the browser before this gets processed, in
391 // which case we do not want to unfocus ourself.
392 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28393 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29394}
395
[email protected]2d5d09d52009-06-15 14:29:21396void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00397 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21398 skia::PlatformCanvas* canvas) {
[email protected]719b36f2010-12-22 20:36:46399
[email protected]4fb66842009-12-04 21:41:00400 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21401
402 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00403 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
404 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03405
[email protected]699ab0d2009-04-23 23:19:14406 // If there is a custom background, tile it.
407 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14408 SkPaint paint;
409 SkShader* shader = SkShader::CreateBitmapShader(background_,
410 SkShader::kRepeat_TileMode,
411 SkShader::kRepeat_TileMode);
412 paint.setShader(shader)->unref();
[email protected]8860e4f52009-06-25 01:01:52413 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
[email protected]699ab0d2009-04-23 23:19:14414 canvas->drawPaint(paint);
[email protected]699ab0d2009-04-23 23:19:14415 }
416
[email protected]719b36f2010-12-22 20:36:46417 // First see if this rect is a plugin that can paint itself faster.
418 TransportDIB* optimized_dib = NULL;
419 gfx::Rect optimized_copy_rect, optimized_copy_location;
420 webkit::ppapi::PluginInstance* optimized_instance =
421 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
422 &optimized_copy_location,
423 &optimized_copy_rect);
424 if (optimized_instance) {
425 // This plugin can be optimize-painted and we can just ask it to paint
426 // itself. We don't actually need the TransportDIB in this case.
427 //
428 // This is an optimization for PPAPI plugins that know they're on top of
429 // the page content. If this rect is inside such a plugin, we can save some
430 // time and avoid re-rendering the page content which we know will be
431 // covered by the plugin later (this time can be significant, especially
432 // for a playing movie that is invalidating a lot).
433 //
434 // In the plugin movie case, hopefully the similar call to
435 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
436 // painting, because that avoids copying the plugin image to a different
437 // paint rect. Unfortunately, if anything on the page is animating other
438 // than the movie, it break this optimization since the union of the
439 // invalid regions will be larger than the plugin.
440 //
441 // This code optimizes that case, where we can still avoid painting in
442 // WebKit and filling the background (which can be slow) and just painting
443 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
444 // required.
445 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27446 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46447 } else {
448 // Normal painting case.
449 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
450
451 // Flush to underlying bitmap. TODO(darin): is this needed?
452 canvas->getTopPlatformDevice().accessBitmap(false);
453 }
initial.commit09911bf2008-07-26 23:55:29454
[email protected]4fb66842009-12-04 21:41:00455 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00456 canvas->restore();
457}
458
459void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
460 skia::PlatformCanvas* canvas) {
461 static bool kPaintBorder =
462 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
463 if (!kPaintBorder)
464 return;
465
[email protected]53d3f302009-12-21 04:42:05466 // Cycle through these colors to help distinguish new paint rects.
467 const SkColor colors[] = {
468 SkColorSetARGB(0x3F, 0xFF, 0, 0),
469 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
470 SkColorSetARGB(0x3F, 0, 0, 0xFF),
471 };
472 static int color_selector = 0;
473
[email protected]4fb66842009-12-04 21:41:00474 SkPaint paint;
475 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05476 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00477 paint.setStrokeWidth(1);
478
479 SkIRect irect;
480 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
481 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29482}
483
[email protected]552e6002009-11-19 05:24:57484void RenderWidget::CallDoDeferredUpdate() {
485 DoDeferredUpdate();
[email protected]12fbad812009-09-01 18:21:24486
[email protected]d22d8732010-05-04 19:24:42487 if (pending_input_event_ack_.get())
488 Send(pending_input_event_ack_.release());
[email protected]12fbad812009-09-01 18:21:24489}
490
[email protected]5f8b1022011-01-21 23:34:50491void RenderWidget::UpdateAnimationsIfNeeded() {
[email protected]ee3d3ad2011-02-04 00:42:21492 if (webwidget_ && !is_hidden() && animation_update_pending_ &&
493 !animation_waiting_for_paint_) {
[email protected]134fb6e2011-01-29 00:56:57494 base::Time now = base::Time::Now();
495 if (now >= animation_floor_time_) {
[email protected]ee3d3ad2011-02-04 00:42:21496 UpdateAnimationsAndFloorTime();
497 // If updating the animation caused invalidations, make sure that we paint
498 // at least once before we call animate() again.
499 // Update layout first as that might cause further invalidations.
500 webwidget_->layout();
501 if (paint_aggregator_.HasPendingUpdate())
502 animation_waiting_for_paint_ = true;
[email protected]134fb6e2011-01-29 00:56:57503 } else {
504 // This code uses base::Time::Now() to calculate the floor and next fire
505 // time because javascript's Date object uses base::Time::Now(). The
506 // message loop uses base::TimeTicks, which on windows can have a
507 // different granularity than base::Time.
508 // The upshot of all this is that this function might be called before
509 // base::Time::Now() has advanced past the animation_floor_time_. To
510 // avoid exposing this delay to javascript, we keep posting delayed
[email protected]ee3d3ad2011-02-04 00:42:21511 // tasks until base::Time::Now() has advanced far enough.
[email protected]134fb6e2011-01-29 00:56:57512 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
513 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
514 this, &RenderWidget::UpdateAnimationsIfNeeded), delay);
515 }
[email protected]5f8b1022011-01-21 23:34:50516 }
517}
518
[email protected]ee3d3ad2011-02-04 00:42:21519void RenderWidget::UpdateAnimationsAndFloorTime() {
520 animation_update_pending_ = false;
521 animation_floor_time_ =
522 base::Time::Now() + base::TimeDelta::FromMilliseconds(16);
523 webwidget_->animate();
524}
525
[email protected]552e6002009-11-19 05:24:57526void RenderWidget::DoDeferredUpdate() {
[email protected]5f8b1022011-01-21 23:34:50527 if (!webwidget_ || update_reply_pending())
initial.commit09911bf2008-07-26 23:55:29528 return;
529
[email protected]552e6002009-11-19 05:24:57530 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29531 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57532 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29533 needs_repainting_on_restore_ = true;
534 return;
535 }
536
[email protected]ee3d3ad2011-02-04 00:42:21537 if (animation_update_pending_) {
538 if (animation_waiting_for_paint_) {
539 // If we have pending animation updates but need to paint before updating
540 // them, post a task to UpdateAnimationsIfNeeded that will either update
541 // animations directly (if the animation floor time has passed by the time
542 // the function runs) or post a delayed task if the floor time is not yet
543 // reached.
544 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
545 this, &RenderWidget::UpdateAnimationsIfNeeded));
546 } else if (base::Time::Now() > animation_floor_time_) {
547 // Otherwise, if it's time to update the animations go ahead.
548 UpdateAnimationsAndFloorTime();
549 }
550 }
551 animation_waiting_for_paint_ = false;
[email protected]5f8b1022011-01-21 23:34:50552
[email protected]f98d7e3c2010-09-13 22:30:46553 // Layout may generate more invalidation. It may also enable the
554 // GPU acceleration, so make sure to run layout before we send the
555 // GpuRenderingActivated message.
556 webwidget_->layout();
557
[email protected]5f8b1022011-01-21 23:34:50558 // Suppress painting if nothing is dirty. This has to be done after updating
559 // animations running layout as these may generate further invalidations.
560 if (!paint_aggregator_.HasPendingUpdate())
561 return;
562
[email protected]552e6002009-11-19 05:24:57563 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29564 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30565 PaintAggregator::PendingUpdate update;
566 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29567
[email protected]53d3f302009-12-21 04:42:05568 gfx::Rect scroll_damage = update.GetScrollDamage();
569 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29570
[email protected]ca4847f2010-09-24 05:39:15571 // A plugin may be able to do an optimized paint. First check this, in which
572 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46573 // This optimization allows PPAPI plugins that declare themselves on top of
574 // the page (like a traditional windowed plugin) to be able to animate (think
575 // movie playing) without repeatedly re-painting the page underneath, or
576 // copying the plugin backing store (since we can send the plugin's backing
577 // store directly to the browser).
578 //
579 // This optimization only works when the entire invalid region is contained
580 // within the plugin. There is a related optimization in PaintRect for the
581 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15582 TransportDIB::Id dib_id = TransportDIB::Id();
583 TransportDIB* dib = NULL;
[email protected]cef3362f2009-12-21 17:48:45584 std::vector<gfx::Rect> copy_rects;
[email protected]ca4847f2010-09-24 05:39:15585 gfx::Rect optimized_copy_rect, optimized_copy_location;
586 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56587 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15588 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
589 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27590 // Only update the part of the plugin that actually changed.
591 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]ca4847f2010-09-24 05:39:15592 bounds = optimized_copy_location;
593 copy_rects.push_back(optimized_copy_rect);
594 dib_id = dib->id();
[email protected]a79d8a632010-11-18 22:35:56595 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46596 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15597 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35598 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
599 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46600 if (!canvas.get()) {
601 NOTREACHED();
602 return;
603 }
[email protected]cef3362f2009-12-21 17:48:45604
[email protected]f98d7e3c2010-09-13 22:30:46605 // We may get back a smaller canvas than we asked for.
606 // TODO(darin): This seems like it could cause painting problems!
607 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
608 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
609 bounds.set_width(canvas->getDevice()->width());
610 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05611
[email protected]f98d7e3c2010-09-13 22:30:46612 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
613
[email protected]f98d7e3c2010-09-13 22:30:46614 // The scroll damage is just another rectangle to paint and copy.
615 copy_rects.swap(update.paint_rects);
616 if (!scroll_damage.IsEmpty())
617 copy_rects.push_back(scroll_damage);
618
619 for (size_t i = 0; i < copy_rects.size(); ++i)
620 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]ca4847f2010-09-24 05:39:15621
[email protected]b4d08452010-10-05 17:34:35622 dib_id = current_paint_buf_->id();
[email protected]f98d7e3c2010-09-13 22:30:46623 } else { // Accelerated compositing path
624 // Begin painting.
[email protected]50bd6452010-11-27 19:39:42625 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46626 }
627
628 // sending an ack to browser process that the paint is complete...
[email protected]53d3f302009-12-21 04:42:05629 ViewHostMsg_UpdateRect_Params params;
[email protected]36808ad2010-10-20 19:18:30630 params.bitmap = dib_id;
[email protected]53d3f302009-12-21 04:42:05631 params.bitmap_rect = bounds;
632 params.dx = update.scroll_delta.x();
633 params.dy = update.scroll_delta.y();
[email protected]a79d8a632010-11-18 22:35:56634 if (is_accelerated_compositing_active_) {
[email protected]b167ca662010-05-14 00:05:34635 // If painting is done via the gpu process then we clear out all damage
636 // rects to save the browser process from doing unecessary work.
637 params.scroll_rect = gfx::Rect();
638 params.copy_rects.clear();
639 } else {
640 params.scroll_rect = update.scroll_rect;
641 params.copy_rects.swap(copy_rects); // TODO(darin): clip to bounds?
642 }
[email protected]53d3f302009-12-21 04:42:05643 params.view_size = size_;
[email protected]e2356242010-11-16 22:33:03644 params.resizer_rect = resizer_rect_;
[email protected]53d3f302009-12-21 04:42:05645 params.plugin_window_moves.swap(plugin_window_moves_);
646 params.flags = next_paint_flags_;
[email protected]d54169e92011-01-21 09:19:52647 params.scroll_offset = GetScrollOffset();
[email protected]53d3f302009-12-21 04:42:05648
649 update_reply_pending_ = true;
650 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
651 next_paint_flags_ = 0;
652
[email protected]fa7b1dc2010-06-23 17:53:04653 UpdateInputMethod();
[email protected]00c39612010-03-06 02:53:28654
655 // Let derived classes know we've painted.
656 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29657}
658
659///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46660// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29661
[email protected]4873c7d2009-07-16 06:36:28662void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]a79d8a632010-11-18 22:35:56663 DCHECK(!is_accelerated_compositing_active_ ||
664 (rect.x == 0 && rect.y == 0 && rect.width == 1 && rect.height == 1));
[email protected]f98d7e3c2010-09-13 22:30:46665
[email protected]552e6002009-11-19 05:24:57666 // We only want one pending DoDeferredUpdate call at any time...
667 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29668
[email protected]552e6002009-11-19 05:24:57669 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48670 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57671 gfx::Rect damaged_rect = view_rect.Intersect(rect);
672 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29673 return;
674
[email protected]552e6002009-11-19 05:24:57675 paint_aggregator_.InvalidateRect(damaged_rect);
676
677 // We may not need to schedule another call to DoDeferredUpdate.
678 if (update_pending)
679 return;
680 if (!paint_aggregator_.HasPendingUpdate())
681 return;
[email protected]53d3f302009-12-21 04:42:05682 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57683 return;
684
685 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29686 // 1) Ensures that we call WebView::Paint without a bunch of other junk
687 // on the call stack.
688 // 2) Allows us to collect more damage rects before painting to help coalesce
689 // the work that we will need to do.
690 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57691 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29692}
693
[email protected]4873c7d2009-07-16 06:36:28694void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46695 // Drop scrolls on the floor when we are in compositing mode.
696 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56697 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46698 return;
699
[email protected]552e6002009-11-19 05:24:57700 // We only want one pending DoDeferredUpdate call at any time...
701 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29702
[email protected]552e6002009-11-19 05:24:57703 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48704 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57705 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
706 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29707 return;
708
[email protected]552e6002009-11-19 05:24:57709 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
710
711 // We may not need to schedule another call to DoDeferredUpdate.
712 if (update_pending)
713 return;
714 if (!paint_aggregator_.HasPendingUpdate())
715 return;
[email protected]53d3f302009-12-21 04:42:05716 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57717 return;
718
719 // Perform updating asynchronously. This serves two purposes:
720 // 1) Ensures that we call WebView::Paint without a bunch of other junk
721 // on the call stack.
722 // 2) Allows us to collect more damage rects before painting to help coalesce
723 // the work that we will need to do.
initial.commit09911bf2008-07-26 23:55:29724 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57725 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29726}
727
[email protected]a79d8a632010-11-18 22:35:56728void RenderWidget::didActivateAcceleratedCompositing(bool active) {
729 is_accelerated_compositing_active_ = active;
[email protected]50bd6452010-11-27 19:39:42730 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]a79d8a632010-11-18 22:35:56731 routing_id_, is_accelerated_compositing_active_));
732}
733
[email protected]f98d7e3c2010-09-13 22:30:46734void RenderWidget::scheduleComposite() {
735 // TODO(nduca): replace with something a little less hacky. The reason this
736 // hack is still used is because the Invalidate-DoDeferredUpdate loop
737 // contains a lot of host-renderer synchronization logic that is still
738 // important for the accelerated compositing case. The option of simply
739 // duplicating all that code is less desirable than "faking out" the
740 // invalidation path using a magical damage rect.
[email protected]e2356242010-11-16 22:33:03741 didInvalidateRect(WebRect(0, 0, 1, 1));
[email protected]f98d7e3c2010-09-13 22:30:46742}
743
[email protected]5f8b1022011-01-21 23:34:50744void RenderWidget::scheduleAnimation() {
[email protected]ee3d3ad2011-02-04 00:42:21745 if (!animation_update_pending_) {
746 animation_update_pending_ = true;
747 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
748 this, &RenderWidget::UpdateAnimationsIfNeeded));
749 }
[email protected]5f8b1022011-01-21 23:34:50750}
751
[email protected]4873c7d2009-07-16 06:36:28752void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:30753 // TODO(darin): Eliminate this temporary.
754 WebCursor cursor(cursor_info);
755
initial.commit09911bf2008-07-26 23:55:29756 // Only send a SetCursor message if we need to make a change.
757 if (!current_cursor_.IsEqual(cursor)) {
758 current_cursor_ = cursor;
759 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
760 }
761}
762
763// We are supposed to get a single call to Show for a newly created RenderWidget
764// that was created via RenderWidget::CreateWebView. So, we wait until this
765// point to dispatch the ShowWidget message.
766//
767// This method provides us with the information about how to display the newly
768// created RenderWidget (i.e., as a constrained popup or as a new tab).
769//
[email protected]4873c7d2009-07-16 06:36:28770void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:29771 DCHECK(!did_show_) << "received extraneous Show call";
772 DCHECK(routing_id_ != MSG_ROUTING_NONE);
773 DCHECK(opener_id_ != MSG_ROUTING_NONE);
774
[email protected]8de12d942010-11-17 20:42:44775 if (did_show_)
776 return;
777
778 did_show_ = true;
779 // NOTE: initial_pos_ may still have its default values at this point, but
780 // that's okay. It'll be ignored if as_popup is false, or the browser
781 // process will impose a default position otherwise.
782 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
783 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:29784}
785
[email protected]4873c7d2009-07-16 06:36:28786void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:29787}
788
[email protected]4873c7d2009-07-16 06:36:28789void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:29790}
791
[email protected]2533ce12009-05-09 00:02:24792void RenderWidget::DoDeferredClose() {
793 Send(new ViewHostMsg_Close(routing_id_));
794}
795
[email protected]4873c7d2009-07-16 06:36:28796void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:29797 // If a page calls window.close() twice, we'll end up here twice, but that's
798 // OK. It is safe to send multiple Close messages.
799
[email protected]2533ce12009-05-09 00:02:24800 // Ask the RenderWidgetHost to initiate close. We could be called from deep
801 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
802 // could be closed before the JS finishes executing. So instead, post a
803 // message back to the message loop, which won't run until the JS is
804 // complete, and then the Close message can be sent.
[email protected]75ae4492009-07-10 00:05:15805 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]2533ce12009-05-09 00:02:24806 this, &RenderWidget::DoDeferredClose));
initial.commit09911bf2008-07-26 23:55:29807}
808
809void RenderWidget::Close() {
810 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:28811 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:29812 webwidget_ = NULL;
813 }
814}
815
[email protected]4873c7d2009-07-16 06:36:28816WebRect RenderWidget::windowRect() {
817 if (pending_window_rect_count_)
818 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24819
[email protected]b3f2b912009-04-09 16:18:52820 gfx::Rect rect;
821 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28822 return rect;
initial.commit09911bf2008-07-26 23:55:29823}
824
[email protected]4873c7d2009-07-16 06:36:28825void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:29826 if (did_show_) {
827 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:24828 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:29829 } else {
830 initial_pos_ = pos;
831 }
832}
833
[email protected]2533ce12009-05-09 00:02:24834void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
835 pending_window_rect_ = rect;
836 pending_window_rect_count_++;
837}
838
[email protected]4873c7d2009-07-16 06:36:28839WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:24840 if (pending_window_rect_count_) {
841 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
842 // the RootWindowRect is probably going to return wrong results since the
843 // browser may not have processed the Move yet. There isn't really anything
844 // good to do in this case, and it shouldn't happen - since this size is
845 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:28846 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24847 }
848
[email protected]b3f2b912009-04-09 16:18:52849 gfx::Rect rect;
850 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28851 return rect;
[email protected]d4547452008-08-28 18:36:37852}
853
[email protected]4873c7d2009-07-16 06:36:28854WebRect RenderWidget::windowResizerRect() {
855 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:19856}
857
[email protected]fa7b1dc2010-06-23 17:53:04858void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:03859 // To prevent this renderer process from sending unnecessary IPC messages to
860 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:04861 // only during the input method attached to the browser process is active.
862 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:29863}
864
[email protected]fa7b1dc2010-06-23 17:53:04865void RenderWidget::OnImeSetComposition(
866 const string16& text,
867 const std::vector<WebCompositionUnderline>& underlines,
868 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:28869 if (!webwidget_)
870 return;
[email protected]fa7b1dc2010-06-23 17:53:04871 if (!webwidget_->setComposition(
872 text, WebVector<WebCompositionUnderline>(underlines),
873 selection_start, selection_end)) {
874 // If we failed to set the composition text, then we need to let the browser
875 // process to cancel the input method's ongoing composition session, to make
876 // sure we are in a consistent state.
877 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:26878 }
[email protected]fa7b1dc2010-06-23 17:53:04879}
880
[email protected]6b349652011-01-05 18:36:24881void RenderWidget::OnImeConfirmComposition(const string16& text) {
[email protected]fa7b1dc2010-06-23 17:53:04882 if (webwidget_)
[email protected]6b349652011-01-05 18:36:24883 webwidget_->confirmComposition(text);
initial.commit09911bf2008-07-26 23:55:29884}
885
[email protected]948f7ab72010-05-28 23:48:08886// This message causes the renderer to render an image of the
887// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:49888void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:09889 int tag,
[email protected]948f7ab72010-05-28 23:48:08890 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:49891 const gfx::Size& desired_size) {
[email protected]45c6aad32010-11-11 04:46:25892 if (!webwidget_ || !TransportDIB::is_valid(dib_handle)) {
893 if (TransportDIB::is_valid(dib_handle)) {
894 // Close our unused handle.
895#if defined(OS_WIN)
896 ::CloseHandle(dib_handle);
897#elif defined(OS_MACOSX)
898 base::SharedMemory::CloseHandle(dib_handle);
899#endif
900 }
[email protected]d65adb12010-04-28 17:26:49901 return;
[email protected]45c6aad32010-11-11 04:46:25902 }
[email protected]d65adb12010-04-28 17:26:49903
[email protected]948f7ab72010-05-28 23:48:08904 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:49905 // If one of these is empty, then we just return the dib we were
906 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:09907 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:49908 return;
909 }
910
911 // Map the given DIB ID into this process, and unmap it at the end
912 // of this function.
[email protected]45c6aad32010-11-11 04:46:25913 scoped_ptr<TransportDIB> paint_at_size_buffer(
914 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:30915
916 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:49917 float x_scale = static_cast<float>(desired_size.width()) /
918 static_cast<float>(canvas_size.width());
919 float y_scale = static_cast<float>(desired_size.height()) /
920 static_cast<float>(canvas_size.height());
921
[email protected]ee8d6fd2010-05-26 17:05:48922 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49923 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
924 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:48925 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49926
[email protected]36808ad2010-10-20 19:18:30927 scoped_ptr<skia::PlatformCanvas> canvas(
928 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
929 canvas_size.height()));
930 if (!canvas.get()) {
931 NOTREACHED();
932 return;
933 }
934
[email protected]d65adb12010-04-28 17:26:49935 // Reset bounds to what we actually received, but they should be the
936 // same.
937 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
938 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
939 bounds.set_width(canvas->getDevice()->width());
940 bounds.set_height(canvas->getDevice()->height());
941
942 canvas->save();
[email protected]948f7ab72010-05-28 23:48:08943 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:49944 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
945
[email protected]948f7ab72010-05-28 23:48:08946 // Have to make sure we're laid out at the right size before
947 // rendering.
948 gfx::Size old_size = webwidget_->size();
949 webwidget_->resize(page_size);
950 webwidget_->layout();
951
[email protected]d65adb12010-04-28 17:26:49952 // Paint the entire thing (using original bounds, not scaled bounds).
953 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
954 canvas->restore();
955
[email protected]948f7ab72010-05-28 23:48:08956 // Return the widget to its previous size.
957 webwidget_->resize(old_size);
958
[email protected]c88c9442010-07-19 18:55:09959 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:49960}
961
[email protected]ec7dc112008-08-06 05:30:12962void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
963 // During shutdown we can just ignore this message.
964 if (!webwidget_)
965 return;
966
967 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:56968 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46969 scheduleComposite();
970 } else {
971 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
972 didInvalidateRect(repaint_rect);
973 }
[email protected]ec7dc112008-08-06 05:30:12974}
975
[email protected]4873c7d2009-07-16 06:36:28976void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:11977 if (!webwidget_)
978 return;
[email protected]4873c7d2009-07-16 06:36:28979 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:11980}
981
[email protected]719b36f2010-12-22 20:36:46982webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:15983 const gfx::Rect& paint_bounds,
984 TransportDIB** dib,
985 gfx::Rect* location,
986 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:46987 // Bare RenderWidgets don't support optimized plugin painting.
988 return NULL;
[email protected]ca4847f2010-09-24 05:39:15989}
990
[email protected]bcaf2272011-02-15 15:29:43991gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:52992 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:43993 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:52994}
995
[email protected]bee16aab2009-08-26 15:55:03996void RenderWidget::SetHidden(bool hidden) {
997 if (is_hidden_ == hidden)
998 return;
999
1000 // The status has changed. Tell the RenderThread about it.
1001 is_hidden_ = hidden;
1002 if (is_hidden_)
1003 render_thread_->WidgetHidden();
1004 else
1005 render_thread_->WidgetRestored();
1006}
1007
[email protected]699ab0d2009-04-23 23:19:141008void RenderWidget::SetBackground(const SkBitmap& background) {
1009 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461010
[email protected]699ab0d2009-04-23 23:19:141011 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281012 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141013}
1014
[email protected]674741932009-02-04 23:44:461015bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051016 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461017}
1018
1019bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051020 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461021}
1022
1023void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051024 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461025}
1026
1027void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051028 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461029}
1030
1031void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051032 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461033}
1034
[email protected]fa7b1dc2010-06-23 17:53:041035void RenderWidget::UpdateInputMethod() {
1036 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291037 return;
[email protected]fa7b1dc2010-06-23 17:53:041038
1039 WebTextInputType new_type = WebKit::WebTextInputTypeNone;
1040 WebRect new_caret_bounds;
1041
1042 if (webwidget_) {
1043 new_type = webwidget_->textInputType();
1044 new_caret_bounds = webwidget_->caretOrSelectionBounds();
initial.commit09911bf2008-07-26 23:55:291045 }
[email protected]fa7b1dc2010-06-23 17:53:041046
1047 // Only sends text input type and caret bounds to the browser process if they
1048 // are changed.
1049 if (text_input_type_ != new_type || caret_bounds_ != new_caret_bounds) {
1050 text_input_type_ = new_type;
1051 caret_bounds_ = new_caret_bounds;
1052 Send(new ViewHostMsg_ImeUpdateTextInputState(
1053 routing_id(), new_type, new_caret_bounds));
initial.commit09911bf2008-07-26 23:55:291054 }
initial.commit09911bf2008-07-26 23:55:291055}
1056
[email protected]4873c7d2009-07-16 06:36:281057WebScreenInfo RenderWidget::screenInfo() {
1058 WebScreenInfo results;
1059 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1060 return results;
1061}
1062
[email protected]fa7b1dc2010-06-23 17:53:041063void RenderWidget::resetInputMethod() {
1064 if (!input_method_is_active_)
1065 return;
1066
1067 // If the last text input type is not None, then we should finish any
1068 // ongoing composition regardless of the new text input type.
1069 if (text_input_type_ != WebKit::WebTextInputTypeNone) {
1070 // If a composition text exists, then we need to let the browser process
1071 // to cancel the input method's ongoing composition session.
1072 if (webwidget_->confirmComposition())
1073 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1074 }
1075}
1076
[email protected]f103ab72009-09-02 17:10:591077void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501078 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291079 size_t i = 0;
1080 for (; i < plugin_window_moves_.size(); ++i) {
1081 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581082 if (move.rects_valid) {
1083 plugin_window_moves_[i] = move;
1084 } else {
1085 plugin_window_moves_[i].visible = move.visible;
1086 }
initial.commit09911bf2008-07-26 23:55:291087 break;
1088 }
1089 }
1090
1091 if (i == plugin_window_moves_.size())
1092 plugin_window_moves_.push_back(move);
1093}
[email protected]268654772009-08-06 23:02:041094
1095void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1096 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1097 i != plugin_window_moves_.end(); ++i) {
1098 if (i->window == window) {
1099 plugin_window_moves_.erase(i);
1100 break;
1101 }
1102 }
1103}