blob: d54bbb9828f9fbde7960d9aacfa7a63266c2d69a [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]e0fc2f12010-03-14 23:30:5919#include "gfx/point.h"
20#include "gfx/size.h"
[email protected]484955942010-08-19 16:13:1821#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4822#include "skia/ext/platform_canvas.h"
[email protected]d5282e72009-05-13 13:16:5223#include "third_party/skia/include/core/SkShader.h"
[email protected]418ed5ab2009-11-12 01:14:4924#include "third_party/WebKit/WebKit/chromium/public/WebCursorInfo.h"
25#include "third_party/WebKit/WebKit/chromium/public/WebPopupMenu.h"
26#include "third_party/WebKit/WebKit/chromium/public/WebPopupMenuInfo.h"
27#include "third_party/WebKit/WebKit/chromium/public/WebRect.h"
28#include "third_party/WebKit/WebKit/chromium/public/WebScreenInfo.h"
29#include "third_party/WebKit/WebKit/chromium/public/WebSize.h"
[email protected]7a4de7a62010-08-17 18:38:2430#include "webkit/glue/plugins/webplugin.h"
[email protected]8c89e7792009-08-19 21:18:3431#include "webkit/glue/webkit_glue.h"
[email protected]661eb9d2009-02-03 02:11:4832
33#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4934#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5235#include "third_party/skia/include/core/SkPixelRef.h"
36#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4837#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4438
[email protected]418ed5ab2009-11-12 01:14:4939#include "third_party/WebKit/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2940
[email protected]fa7b1dc2010-06-23 17:53:0441using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3042using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2243using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0644using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2845using WebKit::WebNavigationPolicy;
46using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5947using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1848using WebKit::WebPopupType;
[email protected]b3f2b912009-04-09 16:18:5249using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1950using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5251using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2852using WebKit::WebTextDirection;
[email protected]fa7b1dc2010-06-23 17:53:0453using WebKit::WebTextInputType;
54using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1855using WebKit::WebWidget;
[email protected]62cb33cae2009-03-27 23:30:2256
[email protected]3e2b375b2010-04-07 17:03:1257RenderWidget::RenderWidget(RenderThreadBase* render_thread,
58 WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2959 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1160 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2961 opener_id_(MSG_ROUTING_NONE),
[email protected]8085dbc82008-09-26 22:53:4462 render_thread_(render_thread),
[email protected]659f73f2009-10-13 13:43:4263 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3564 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2965 next_paint_flags_(0),
[email protected]53d3f302009-12-21 04:42:0566 update_reply_pending_(false),
initial.commit09911bf2008-07-26 23:55:2967 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2968 is_hidden_(false),
69 needs_repainting_on_restore_(false),
70 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4971 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4872 closing_(false),
[email protected]fa7b1dc2010-06-23 17:53:0473 input_method_is_active_(false),
74 text_input_type_(WebKit::WebTextInputTypeNone),
[email protected]3e2b375b2010-04-07 17:03:1275 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4876 pending_window_rect_count_(0),
[email protected]edbcde932010-05-07 17:10:4677 suppress_next_char_events_(false),
78 is_gpu_rendering_active_(false) {
[email protected]8930d472009-02-21 08:05:2879 RenderProcess::current()->AddRefProcess();
[email protected]8085dbc82008-09-26 22:53:4480 DCHECK(render_thread_);
initial.commit09911bf2008-07-26 23:55:2981}
82
83RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:1184 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]b4d08452010-10-05 17:34:3585 if (current_paint_buf_) {
86 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
87 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:2988 }
[email protected]8930d472009-02-21 08:05:2889 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:2990}
91
[email protected]484955942010-08-19 16:13:1892// static
[email protected]8085dbc82008-09-26 22:53:4493RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]0ebf3872008-11-07 21:35:0394 RenderThreadBase* render_thread,
[email protected]3e2b375b2010-04-07 17:03:1295 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:2996 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]ad8e04a2010-11-01 04:16:2797 scoped_refptr<RenderWidget> widget(new RenderWidget(render_thread,
98 popup_type));
initial.commit09911bf2008-07-26 23:55:2999 widget->Init(opener_id); // adds reference
100 return widget;
101}
102
[email protected]484955942010-08-19 16:13:18103// static
104WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
105 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03106 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18107 break;
108 case WebKit::WebPopupTypeSelect:
109 case WebKit::WebPopupTypeSuggestion:
110 return WebPopupMenu::create(render_widget);
111 default:
112 NOTREACHED();
113 }
114 return NULL;
115}
116
[email protected]88efb7ec2009-07-14 16:32:59117void RenderWidget::ConfigureAsExternalPopupMenu(const WebPopupMenuInfo& info) {
118 popup_params_.reset(new ViewHostMsg_ShowPopup_Params);
119 popup_params_->item_height = info.itemHeight;
[email protected]c9e19792010-03-17 17:02:59120 popup_params_->item_font_size = info.itemFontSize;
[email protected]88efb7ec2009-07-14 16:32:59121 popup_params_->selected_item = info.selectedIndex;
122 for (size_t i = 0; i < info.items.size(); ++i)
123 popup_params_->popup_items.push_back(WebMenuItem(info.items[i]));
[email protected]b88ab032010-05-13 22:13:04124 popup_params_->right_aligned = info.rightAligned;
[email protected]88efb7ec2009-07-14 16:32:59125}
126
initial.commit09911bf2008-07-26 23:55:29127void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18128 DoInit(opener_id,
129 RenderWidget::CreateWebWidget(this),
130 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
131}
132
133
134void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06135 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18136 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29137 DCHECK(!webwidget_);
138
139 if (opener_id != MSG_ROUTING_NONE)
140 opener_id_ = opener_id;
141
[email protected]484955942010-08-19 16:13:18142 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29143
[email protected]484955942010-08-19 16:13:18144 bool result = render_thread_->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29145 if (result) {
[email protected]8085dbc82008-09-26 22:53:44146 render_thread_->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29147 // Take a reference on behalf of the RenderThread. This will be balanced
148 // when we receive ViewMsg_Close.
149 AddRef();
150 } else {
151 DCHECK(false);
152 }
153}
154
155// This is used to complete pending inits and non-pending inits. For non-
156// pending cases, the parent will be the same as the current parent. This
157// indicates we do not need to reparent or anything.
[email protected]18bcc3c2009-01-27 21:39:15158void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29159 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29160
161 host_window_ = parent_hwnd;
162
[email protected]6de74452009-02-25 18:04:59163 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29164}
165
166IPC_DEFINE_MESSAGE_MAP(RenderWidget)
167 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
168 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
169 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
170 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
171 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]53d3f302009-12-21 04:42:05172 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
initial.commit09911bf2008-07-26 23:55:29173 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
174 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
175 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
[email protected]fa7b1dc2010-06-23 17:53:04176 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
initial.commit09911bf2008-07-26 23:55:29177 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
[email protected]fa7b1dc2010-06-23 17:53:04178 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
[email protected]d65adb12010-04-28 17:26:49179 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
[email protected]ec7dc112008-08-06 05:30:12180 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
[email protected]07f953332009-03-25 04:31:11181 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
[email protected]2533ce12009-05-09 00:02:24182 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
initial.commit09911bf2008-07-26 23:55:29183 IPC_MESSAGE_UNHANDLED_ERROR()
184IPC_END_MESSAGE_MAP()
185
186bool RenderWidget::Send(IPC::Message* message) {
187 // Don't send any messages after the browser has told us to close.
188 if (closing_) {
189 delete message;
190 return false;
191 }
192
193 // If given a messsage without a routing ID, then assign our routing ID.
194 if (message->routing_id() == MSG_ROUTING_NONE)
195 message->set_routing_id(routing_id_);
196
[email protected]d3fc25652009-02-24 22:31:25197 return render_thread_->Send(message);
[email protected]8085dbc82008-09-26 22:53:44198}
199
initial.commit09911bf2008-07-26 23:55:29200// Got a response from the browser after the renderer decided to create a new
201// view.
[email protected]18bcc3c2009-01-27 21:39:15202void RenderWidget::OnCreatingNewAck(gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29203 DCHECK(routing_id_ != MSG_ROUTING_NONE);
204
205 CompleteInit(parent);
206}
207
208void RenderWidget::OnClose() {
209 if (closing_)
210 return;
211 closing_ = true;
212
213 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03214 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]8085dbc82008-09-26 22:53:44215 render_thread_->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03216 SetHidden(false);
217 }
initial.commit09911bf2008-07-26 23:55:29218
initial.commit09911bf2008-07-26 23:55:29219 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25220 // now. Post a task that only gets invoked when there are no nested message
221 // loops.
222 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
223 NewRunnableMethod(this, &RenderWidget::Close));
224
225 // Balances the AddRef taken when we called AddRoute.
226 Release();
initial.commit09911bf2008-07-26 23:55:29227}
228
[email protected]f21c613a2009-02-12 14:46:17229void RenderWidget::OnResize(const gfx::Size& new_size,
230 const gfx::Rect& resizer_rect) {
initial.commit09911bf2008-07-26 23:55:29231 // During shutdown we can just ignore this message.
232 if (!webwidget_)
233 return;
234
[email protected]e2356242010-11-16 22:33:03235 // We shouldn't be asked to resize to our current size.
236 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
237
[email protected]f21c613a2009-02-12 14:46:17238 // Remember the rect where the resize corner will be drawn.
239 resizer_rect_ = resizer_rect;
240
[email protected]e2356242010-11-16 22:33:03241 if (size_ == new_size)
242 return;
243
initial.commit09911bf2008-07-26 23:55:29244 // TODO(darin): We should not need to reset this here.
[email protected]bee16aab2009-08-26 15:55:03245 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29246 needs_repainting_on_restore_ = false;
247
initial.commit09911bf2008-07-26 23:55:29248 size_ = new_size;
249
250 // We should not be sent a Resize message if we have not ACK'd the previous
251 DCHECK(!next_paint_is_resize_ack());
252
[email protected]552e6002009-11-19 05:24:57253 paint_aggregator_.ClearPendingUpdate();
254
initial.commit09911bf2008-07-26 23:55:29255 // When resizing, we want to wait to paint before ACK'ing the resize. This
256 // ensures that we only resize as fast as we can paint. We only need to send
257 // an ACK if we are resized to a non-empty rect.
[email protected]4873c7d2009-07-16 06:36:28258 webwidget_->resize(new_size);
initial.commit09911bf2008-07-26 23:55:29259 if (!new_size.IsEmpty()) {
[email protected]f98d7e3c2010-09-13 22:30:46260 if (!webwidget_->isAcceleratedCompositingActive()) {
261 // Resize should have caused an invalidation of the entire view.
262 DCHECK(paint_aggregator_.HasPendingUpdate());
263 }
[email protected]2d5d09d52009-06-15 14:29:21264
initial.commit09911bf2008-07-26 23:55:29265 // We will send the Resize_ACK flag once we paint again.
266 set_next_paint_is_resize_ack();
267 }
268}
269
270void RenderWidget::OnWasHidden() {
271 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03272 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29273}
274
275void RenderWidget::OnWasRestored(bool needs_repainting) {
276 // During shutdown we can just ignore this message.
277 if (!webwidget_)
278 return;
279
280 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03281 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29282
283 if (!needs_repainting && !needs_repainting_on_restore_)
284 return;
285 needs_repainting_on_restore_ = false;
286
[email protected]d65adb12010-04-28 17:26:49287 // Tag the next paint as a restore ack, which is picked up by
288 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29289 set_next_paint_is_restore_ack();
290
291 // Generate a full repaint.
[email protected]4c0af8d2010-09-16 00:25:38292 if (!webwidget_->isAcceleratedCompositingActive()) {
[email protected]f98d7e3c2010-09-13 22:30:46293 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
294 } else {
295 scheduleComposite();
296 }
initial.commit09911bf2008-07-26 23:55:29297}
298
[email protected]53d3f302009-12-21 04:42:05299void RenderWidget::OnRequestMoveAck() {
300 DCHECK(pending_window_rect_count_);
301 pending_window_rect_count_--;
302}
303
304void RenderWidget::OnUpdateRectAck() {
305 DCHECK(update_reply_pending());
306 update_reply_pending_ = false;
307
[email protected]b4d08452010-10-05 17:34:35308 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
309 // have no current paint buffer.
310 if (current_paint_buf_) {
311 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
312 current_paint_buf_ = NULL;
313 }
314
[email protected]00c39612010-03-06 02:53:28315 // Notify subclasses.
316 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25317
initial.commit09911bf2008-07-26 23:55:29318 // Continue painting if necessary...
[email protected]552e6002009-11-19 05:24:57319 CallDoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29320}
321
initial.commit09911bf2008-07-26 23:55:29322void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
323 void* iter = NULL;
324
325 const char* data;
326 int data_length;
[email protected]5dd768212009-08-13 23:34:49327 handling_input_event_ = true;
328 if (!message.ReadData(&iter, &data, &data_length)) {
329 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29330 return;
[email protected]5dd768212009-08-13 23:34:49331 }
initial.commit09911bf2008-07-26 23:55:29332
333 const WebInputEvent* input_event =
334 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48335
336 bool is_keyboard_shortcut = false;
337 // is_keyboard_shortcut flag is only available for RawKeyDown events.
338 if (input_event->type == WebInputEvent::RawKeyDown)
339 message.ReadBool(&iter, &is_keyboard_shortcut);
340
initial.commit09911bf2008-07-26 23:55:29341 bool processed = false;
[email protected]867125a02009-12-10 06:01:48342 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
343 suppress_next_char_events_ = false;
344 if (webwidget_)
345 processed = webwidget_->handleInputEvent(*input_event);
346 }
347
348 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
349 // it's not processed by webkit, then we need to suppress the upcoming Char
350 // events.
351 if (!processed && is_keyboard_shortcut)
352 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29353
354 IPC::Message* response = new ViewHostMsg_HandleInputEvent_ACK(routing_id_);
355 response->WriteInt(input_event->type);
[email protected]e2824412009-02-27 01:57:05356 response->WriteBool(processed);
357
[email protected]353a34c2010-05-28 23:35:17358 if ((input_event->type == WebInputEvent::MouseMove ||
359 input_event->type == WebInputEvent::MouseWheel) &&
[email protected]552e6002009-11-19 05:24:57360 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24361 // We want to rate limit the input events in this case, so we'll wait for
362 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17363 if (pending_input_event_ack_.get()) {
364 // As two different kinds of events could cause us to postpone an ack
365 // we send it now, if we have one pending. The Browser should never
366 // send us the same kind of event we are delaying the ack for.
367 Send(pending_input_event_ack_.release());
368 }
[email protected]12fbad812009-09-01 18:21:24369 pending_input_event_ack_.reset(response);
370 } else {
371 Send(response);
372 }
373
[email protected]5dd768212009-08-13 23:34:49374 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48375
[email protected]867125a02009-12-10 06:01:48376 if (WebInputEvent::isKeyboardEventType(input_event->type))
[email protected]446705872009-09-10 07:22:48377 DidHandleKeyEvent();
[email protected]6a8ddba52010-09-05 04:38:06378 if (WebInputEvent::isMouseEventType(input_event->type))
379 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
initial.commit09911bf2008-07-26 23:55:29380}
381
382void RenderWidget::OnMouseCaptureLost() {
383 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28384 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29385}
386
387void RenderWidget::OnSetFocus(bool enable) {
388 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33389 if (webwidget_)
390 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29391}
392
393void RenderWidget::ClearFocus() {
394 // We may have got the focus from the browser before this gets processed, in
395 // which case we do not want to unfocus ourself.
396 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28397 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29398}
399
[email protected]2d5d09d52009-06-15 14:29:21400void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00401 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21402 skia::PlatformCanvas* canvas) {
[email protected]4fb66842009-12-04 21:41:00403 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21404
405 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00406 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
407 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03408
[email protected]699ab0d2009-04-23 23:19:14409 // If there is a custom background, tile it.
410 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14411 SkPaint paint;
412 SkShader* shader = SkShader::CreateBitmapShader(background_,
413 SkShader::kRepeat_TileMode,
414 SkShader::kRepeat_TileMode);
415 paint.setShader(shader)->unref();
[email protected]8860e4f52009-06-25 01:01:52416 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
[email protected]699ab0d2009-04-23 23:19:14417 canvas->drawPaint(paint);
[email protected]699ab0d2009-04-23 23:19:14418 }
419
[email protected]01b47232010-07-01 20:33:47420 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
initial.commit09911bf2008-07-26 23:55:29421
[email protected]4fb66842009-12-04 21:41:00422 PaintDebugBorder(rect, canvas);
423
initial.commit09911bf2008-07-26 23:55:29424 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]661eb9d2009-02-03 02:11:48425 canvas->getTopPlatformDevice().accessBitmap(false);
[email protected]4fb66842009-12-04 21:41:00426
427 canvas->restore();
428}
429
430void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
431 skia::PlatformCanvas* canvas) {
432 static bool kPaintBorder =
433 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
434 if (!kPaintBorder)
435 return;
436
[email protected]53d3f302009-12-21 04:42:05437 // Cycle through these colors to help distinguish new paint rects.
438 const SkColor colors[] = {
439 SkColorSetARGB(0x3F, 0xFF, 0, 0),
440 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
441 SkColorSetARGB(0x3F, 0, 0, 0xFF),
442 };
443 static int color_selector = 0;
444
[email protected]4fb66842009-12-04 21:41:00445 SkPaint paint;
446 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05447 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00448 paint.setStrokeWidth(1);
449
450 SkIRect irect;
451 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
452 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29453}
454
[email protected]552e6002009-11-19 05:24:57455void RenderWidget::CallDoDeferredUpdate() {
456 DoDeferredUpdate();
[email protected]12fbad812009-09-01 18:21:24457
[email protected]d22d8732010-05-04 19:24:42458 if (pending_input_event_ack_.get())
459 Send(pending_input_event_ack_.release());
[email protected]12fbad812009-09-01 18:21:24460}
461
[email protected]552e6002009-11-19 05:24:57462void RenderWidget::DoDeferredUpdate() {
463 if (!webwidget_ || !paint_aggregator_.HasPendingUpdate() ||
[email protected]53d3f302009-12-21 04:42:05464 update_reply_pending())
initial.commit09911bf2008-07-26 23:55:29465 return;
466
[email protected]552e6002009-11-19 05:24:57467 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29468 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57469 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29470 needs_repainting_on_restore_ = true;
471 return;
472 }
473
[email protected]f98d7e3c2010-09-13 22:30:46474 // Layout may generate more invalidation. It may also enable the
475 // GPU acceleration, so make sure to run layout before we send the
476 // GpuRenderingActivated message.
477 webwidget_->layout();
478
[email protected]edbcde932010-05-07 17:10:46479 // If we are using accelerated compositing then all the drawing
480 // to the associated window happens directly from the gpu process and the
481 // browser process shouldn't do any drawing.
482 // TODO(vangelis): Currently the accelerated compositing path relies on
483 // invalidating parts of the page so that we get a request to redraw.
484 // This needs to change to a model where the compositor updates the
485 // contents of the page independently and the browser process gets no
486 // longer involved.
487 if (webwidget_->isAcceleratedCompositingActive() !=
488 is_gpu_rendering_active_) {
489 is_gpu_rendering_active_ = webwidget_->isAcceleratedCompositingActive();
490 Send(new ViewHostMsg_GpuRenderingActivated(
491 routing_id_, is_gpu_rendering_active_));
492 }
493
[email protected]552e6002009-11-19 05:24:57494 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29495 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]552e6002009-11-19 05:24:57496 PaintAggregator::PendingUpdate update = paint_aggregator_.GetPendingUpdate();
497 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29498
[email protected]53d3f302009-12-21 04:42:05499 gfx::Rect scroll_damage = update.GetScrollDamage();
500 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29501
[email protected]ca4847f2010-09-24 05:39:15502 // A plugin may be able to do an optimized paint. First check this, in which
503 // case we can skip all of the bitmap generation and regular paint code.
504 TransportDIB::Id dib_id = TransportDIB::Id();
505 TransportDIB* dib = NULL;
[email protected]cef3362f2009-12-21 17:48:45506 std::vector<gfx::Rect> copy_rects;
[email protected]ca4847f2010-09-24 05:39:15507 gfx::Rect optimized_copy_rect, optimized_copy_location;
508 if (update.scroll_rect.IsEmpty() &&
509 !is_gpu_rendering_active_ &&
510 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
511 &optimized_copy_rect)) {
512 bounds = optimized_copy_location;
513 copy_rects.push_back(optimized_copy_rect);
514 dib_id = dib->id();
515 } else if (!is_gpu_rendering_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46516 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15517 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35518 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
519 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46520 if (!canvas.get()) {
521 NOTREACHED();
522 return;
523 }
[email protected]cef3362f2009-12-21 17:48:45524
[email protected]f98d7e3c2010-09-13 22:30:46525 // We may get back a smaller canvas than we asked for.
526 // TODO(darin): This seems like it could cause painting problems!
527 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
528 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
529 bounds.set_width(canvas->getDevice()->width());
530 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05531
[email protected]f98d7e3c2010-09-13 22:30:46532 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
533
534 // TODO(darin): Re-enable painting multiple damage rects once the
535 // page-cycler regressions are resolved. See bug 29589.
536 if (update.scroll_rect.IsEmpty()) {
537 update.paint_rects.clear();
538 update.paint_rects.push_back(bounds);
539 }
540
541 // The scroll damage is just another rectangle to paint and copy.
542 copy_rects.swap(update.paint_rects);
543 if (!scroll_damage.IsEmpty())
544 copy_rects.push_back(scroll_damage);
545
546 for (size_t i = 0; i < copy_rects.size(); ++i)
547 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]ca4847f2010-09-24 05:39:15548
[email protected]b4d08452010-10-05 17:34:35549 dib_id = current_paint_buf_->id();
[email protected]f98d7e3c2010-09-13 22:30:46550 } else { // Accelerated compositing path
551 // Begin painting.
552 bool finish = next_paint_is_resize_ack();
553 webwidget_->composite(finish);
554 }
555
556 // sending an ack to browser process that the paint is complete...
[email protected]53d3f302009-12-21 04:42:05557 ViewHostMsg_UpdateRect_Params params;
[email protected]36808ad2010-10-20 19:18:30558 params.bitmap = dib_id;
[email protected]53d3f302009-12-21 04:42:05559 params.bitmap_rect = bounds;
560 params.dx = update.scroll_delta.x();
561 params.dy = update.scroll_delta.y();
[email protected]b167ca662010-05-14 00:05:34562 if (is_gpu_rendering_active_) {
563 // If painting is done via the gpu process then we clear out all damage
564 // rects to save the browser process from doing unecessary work.
565 params.scroll_rect = gfx::Rect();
566 params.copy_rects.clear();
567 } else {
568 params.scroll_rect = update.scroll_rect;
569 params.copy_rects.swap(copy_rects); // TODO(darin): clip to bounds?
570 }
[email protected]53d3f302009-12-21 04:42:05571 params.view_size = size_;
[email protected]e2356242010-11-16 22:33:03572 params.resizer_rect = resizer_rect_;
[email protected]53d3f302009-12-21 04:42:05573 params.plugin_window_moves.swap(plugin_window_moves_);
574 params.flags = next_paint_flags_;
575
576 update_reply_pending_ = true;
577 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
578 next_paint_flags_ = 0;
579
[email protected]fa7b1dc2010-06-23 17:53:04580 UpdateInputMethod();
[email protected]00c39612010-03-06 02:53:28581
582 // Let derived classes know we've painted.
583 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29584}
585
586///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46587// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29588
[email protected]4873c7d2009-07-16 06:36:28589void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]f98d7e3c2010-09-13 22:30:46590 if (webwidget_->isAcceleratedCompositingActive()) {
591 // Drop invalidates on the floor when we are in compositing mode.
592 // TODO(nduca): Stop WebViewImpl from sending invalidates in the first
593 // place.
594 if (!(rect.x == 0 && rect.y == 0 &&
595 rect.width == 1 && rect.height == 1)) {
596 return;
597 }
598 }
599
[email protected]552e6002009-11-19 05:24:57600 // We only want one pending DoDeferredUpdate call at any time...
601 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29602
[email protected]552e6002009-11-19 05:24:57603 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48604 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57605 gfx::Rect damaged_rect = view_rect.Intersect(rect);
606 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29607 return;
608
[email protected]552e6002009-11-19 05:24:57609 paint_aggregator_.InvalidateRect(damaged_rect);
610
611 // We may not need to schedule another call to DoDeferredUpdate.
612 if (update_pending)
613 return;
614 if (!paint_aggregator_.HasPendingUpdate())
615 return;
[email protected]53d3f302009-12-21 04:42:05616 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57617 return;
618
619 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29620 // 1) Ensures that we call WebView::Paint without a bunch of other junk
621 // on the call stack.
622 // 2) Allows us to collect more damage rects before painting to help coalesce
623 // the work that we will need to do.
624 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57625 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29626}
627
[email protected]4873c7d2009-07-16 06:36:28628void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46629 // Drop scrolls on the floor when we are in compositing mode.
630 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
631 if (webwidget_->isAcceleratedCompositingActive())
632 return;
633
[email protected]552e6002009-11-19 05:24:57634 // We only want one pending DoDeferredUpdate call at any time...
635 bool update_pending = paint_aggregator_.HasPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29636
[email protected]552e6002009-11-19 05:24:57637 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48638 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57639 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
640 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29641 return;
642
[email protected]552e6002009-11-19 05:24:57643 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
644
645 // We may not need to schedule another call to DoDeferredUpdate.
646 if (update_pending)
647 return;
648 if (!paint_aggregator_.HasPendingUpdate())
649 return;
[email protected]53d3f302009-12-21 04:42:05650 if (update_reply_pending())
[email protected]552e6002009-11-19 05:24:57651 return;
652
653 // Perform updating asynchronously. This serves two purposes:
654 // 1) Ensures that we call WebView::Paint without a bunch of other junk
655 // on the call stack.
656 // 2) Allows us to collect more damage rects before painting to help coalesce
657 // the work that we will need to do.
initial.commit09911bf2008-07-26 23:55:29658 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]552e6002009-11-19 05:24:57659 this, &RenderWidget::CallDoDeferredUpdate));
initial.commit09911bf2008-07-26 23:55:29660}
661
[email protected]f98d7e3c2010-09-13 22:30:46662void RenderWidget::scheduleComposite() {
663 // TODO(nduca): replace with something a little less hacky. The reason this
664 // hack is still used is because the Invalidate-DoDeferredUpdate loop
665 // contains a lot of host-renderer synchronization logic that is still
666 // important for the accelerated compositing case. The option of simply
667 // duplicating all that code is less desirable than "faking out" the
668 // invalidation path using a magical damage rect.
[email protected]e2356242010-11-16 22:33:03669 didInvalidateRect(WebRect(0, 0, 1, 1));
[email protected]f98d7e3c2010-09-13 22:30:46670}
671
[email protected]4873c7d2009-07-16 06:36:28672void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:30673 // TODO(darin): Eliminate this temporary.
674 WebCursor cursor(cursor_info);
675
initial.commit09911bf2008-07-26 23:55:29676 // Only send a SetCursor message if we need to make a change.
677 if (!current_cursor_.IsEqual(cursor)) {
678 current_cursor_ = cursor;
679 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
680 }
681}
682
683// We are supposed to get a single call to Show for a newly created RenderWidget
684// that was created via RenderWidget::CreateWebView. So, we wait until this
685// point to dispatch the ShowWidget message.
686//
687// This method provides us with the information about how to display the newly
688// created RenderWidget (i.e., as a constrained popup or as a new tab).
689//
[email protected]4873c7d2009-07-16 06:36:28690void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:29691 DCHECK(!did_show_) << "received extraneous Show call";
692 DCHECK(routing_id_ != MSG_ROUTING_NONE);
693 DCHECK(opener_id_ != MSG_ROUTING_NONE);
694
695 if (!did_show_) {
696 did_show_ = true;
697 // NOTE: initial_pos_ may still have its default values at this point, but
698 // that's okay. It'll be ignored if as_popup is false, or the browser
699 // process will impose a default position otherwise.
[email protected]88efb7ec2009-07-14 16:32:59700 if (popup_params_.get()) {
701 popup_params_->bounds = initial_pos_;
702 Send(new ViewHostMsg_ShowPopup(routing_id_, *popup_params_));
703 popup_params_.reset();
704 } else {
705 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
706 }
[email protected]2533ce12009-05-09 00:02:24707 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:29708 }
709}
710
[email protected]4873c7d2009-07-16 06:36:28711void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:29712}
713
[email protected]4873c7d2009-07-16 06:36:28714void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:29715}
716
[email protected]2533ce12009-05-09 00:02:24717void RenderWidget::DoDeferredClose() {
718 Send(new ViewHostMsg_Close(routing_id_));
719}
720
[email protected]4873c7d2009-07-16 06:36:28721void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:29722 // If a page calls window.close() twice, we'll end up here twice, but that's
723 // OK. It is safe to send multiple Close messages.
724
[email protected]2533ce12009-05-09 00:02:24725 // Ask the RenderWidgetHost to initiate close. We could be called from deep
726 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
727 // could be closed before the JS finishes executing. So instead, post a
728 // message back to the message loop, which won't run until the JS is
729 // complete, and then the Close message can be sent.
[email protected]75ae4492009-07-10 00:05:15730 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]2533ce12009-05-09 00:02:24731 this, &RenderWidget::DoDeferredClose));
initial.commit09911bf2008-07-26 23:55:29732}
733
734void RenderWidget::Close() {
735 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:28736 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:29737 webwidget_ = NULL;
738 }
739}
740
[email protected]4873c7d2009-07-16 06:36:28741WebRect RenderWidget::windowRect() {
742 if (pending_window_rect_count_)
743 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24744
[email protected]b3f2b912009-04-09 16:18:52745 gfx::Rect rect;
746 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28747 return rect;
initial.commit09911bf2008-07-26 23:55:29748}
749
[email protected]4873c7d2009-07-16 06:36:28750void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:29751 if (did_show_) {
752 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:24753 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:29754 } else {
755 initial_pos_ = pos;
756 }
757}
758
[email protected]2533ce12009-05-09 00:02:24759void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
760 pending_window_rect_ = rect;
761 pending_window_rect_count_++;
762}
763
[email protected]4873c7d2009-07-16 06:36:28764WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:24765 if (pending_window_rect_count_) {
766 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
767 // the RootWindowRect is probably going to return wrong results since the
768 // browser may not have processed the Move yet. There isn't really anything
769 // good to do in this case, and it shouldn't happen - since this size is
770 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:28771 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:24772 }
773
[email protected]b3f2b912009-04-09 16:18:52774 gfx::Rect rect;
775 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:28776 return rect;
[email protected]d4547452008-08-28 18:36:37777}
778
[email protected]4873c7d2009-07-16 06:36:28779WebRect RenderWidget::windowResizerRect() {
780 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:19781}
782
[email protected]fa7b1dc2010-06-23 17:53:04783void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:03784 // To prevent this renderer process from sending unnecessary IPC messages to
785 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:04786 // only during the input method attached to the browser process is active.
787 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:29788}
789
[email protected]fa7b1dc2010-06-23 17:53:04790void RenderWidget::OnImeSetComposition(
791 const string16& text,
792 const std::vector<WebCompositionUnderline>& underlines,
793 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:28794 if (!webwidget_)
795 return;
[email protected]fa7b1dc2010-06-23 17:53:04796 if (!webwidget_->setComposition(
797 text, WebVector<WebCompositionUnderline>(underlines),
798 selection_start, selection_end)) {
799 // If we failed to set the composition text, then we need to let the browser
800 // process to cancel the input method's ongoing composition session, to make
801 // sure we are in a consistent state.
802 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:26803 }
[email protected]fa7b1dc2010-06-23 17:53:04804}
805
806void RenderWidget::OnImeConfirmComposition() {
807 if (webwidget_)
808 webwidget_->confirmComposition();
initial.commit09911bf2008-07-26 23:55:29809}
810
[email protected]948f7ab72010-05-28 23:48:08811// This message causes the renderer to render an image of the
812// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:49813void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:09814 int tag,
[email protected]948f7ab72010-05-28 23:48:08815 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:49816 const gfx::Size& desired_size) {
[email protected]45c6aad32010-11-11 04:46:25817 if (!webwidget_ || !TransportDIB::is_valid(dib_handle)) {
818 if (TransportDIB::is_valid(dib_handle)) {
819 // Close our unused handle.
820#if defined(OS_WIN)
821 ::CloseHandle(dib_handle);
822#elif defined(OS_MACOSX)
823 base::SharedMemory::CloseHandle(dib_handle);
824#endif
825 }
[email protected]d65adb12010-04-28 17:26:49826 return;
[email protected]45c6aad32010-11-11 04:46:25827 }
[email protected]d65adb12010-04-28 17:26:49828
[email protected]948f7ab72010-05-28 23:48:08829 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:49830 // If one of these is empty, then we just return the dib we were
831 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:09832 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:49833 return;
834 }
835
836 // Map the given DIB ID into this process, and unmap it at the end
837 // of this function.
[email protected]45c6aad32010-11-11 04:46:25838 scoped_ptr<TransportDIB> paint_at_size_buffer(
839 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:30840
841 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:49842 float x_scale = static_cast<float>(desired_size.width()) /
843 static_cast<float>(canvas_size.width());
844 float y_scale = static_cast<float>(desired_size.height()) /
845 static_cast<float>(canvas_size.height());
846
[email protected]ee8d6fd2010-05-26 17:05:48847 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49848 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
849 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:48850 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:49851
[email protected]36808ad2010-10-20 19:18:30852 scoped_ptr<skia::PlatformCanvas> canvas(
853 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
854 canvas_size.height()));
855 if (!canvas.get()) {
856 NOTREACHED();
857 return;
858 }
859
[email protected]d65adb12010-04-28 17:26:49860 // Reset bounds to what we actually received, but they should be the
861 // same.
862 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
863 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
864 bounds.set_width(canvas->getDevice()->width());
865 bounds.set_height(canvas->getDevice()->height());
866
867 canvas->save();
[email protected]948f7ab72010-05-28 23:48:08868 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:49869 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
870
[email protected]948f7ab72010-05-28 23:48:08871 // Have to make sure we're laid out at the right size before
872 // rendering.
873 gfx::Size old_size = webwidget_->size();
874 webwidget_->resize(page_size);
875 webwidget_->layout();
876
[email protected]d65adb12010-04-28 17:26:49877 // Paint the entire thing (using original bounds, not scaled bounds).
878 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
879 canvas->restore();
880
[email protected]948f7ab72010-05-28 23:48:08881 // Return the widget to its previous size.
882 webwidget_->resize(old_size);
883
[email protected]c88c9442010-07-19 18:55:09884 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:49885}
886
[email protected]ec7dc112008-08-06 05:30:12887void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
888 // During shutdown we can just ignore this message.
889 if (!webwidget_)
890 return;
891
892 set_next_paint_is_repaint_ack();
[email protected]f98d7e3c2010-09-13 22:30:46893 if (webwidget_->isAcceleratedCompositingActive()) {
894 scheduleComposite();
895 } else {
896 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
897 didInvalidateRect(repaint_rect);
898 }
[email protected]ec7dc112008-08-06 05:30:12899}
900
[email protected]4873c7d2009-07-16 06:36:28901void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:11902 if (!webwidget_)
903 return;
[email protected]4873c7d2009-07-16 06:36:28904 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:11905}
906
[email protected]ca4847f2010-09-24 05:39:15907bool RenderWidget::GetBitmapForOptimizedPluginPaint(
908 const gfx::Rect& paint_bounds,
909 TransportDIB** dib,
910 gfx::Rect* location,
911 gfx::Rect* clip) {
912 // Normal RenderWidgets don't support optimized plugin painting.
913 return false;
914}
915
[email protected]bee16aab2009-08-26 15:55:03916void RenderWidget::SetHidden(bool hidden) {
917 if (is_hidden_ == hidden)
918 return;
919
920 // The status has changed. Tell the RenderThread about it.
921 is_hidden_ = hidden;
922 if (is_hidden_)
923 render_thread_->WidgetHidden();
924 else
925 render_thread_->WidgetRestored();
926}
927
[email protected]699ab0d2009-04-23 23:19:14928void RenderWidget::SetBackground(const SkBitmap& background) {
929 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:46930
[email protected]699ab0d2009-04-23 23:19:14931 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:28932 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:14933}
934
[email protected]674741932009-02-04 23:44:46935bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:05936 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:46937}
938
939bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:05940 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:46941}
942
943void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:05944 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:46945}
946
947void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:05948 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:46949}
950
951void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:05952 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:46953}
954
[email protected]fa7b1dc2010-06-23 17:53:04955void RenderWidget::UpdateInputMethod() {
956 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:29957 return;
[email protected]fa7b1dc2010-06-23 17:53:04958
959 WebTextInputType new_type = WebKit::WebTextInputTypeNone;
960 WebRect new_caret_bounds;
961
962 if (webwidget_) {
963 new_type = webwidget_->textInputType();
964 new_caret_bounds = webwidget_->caretOrSelectionBounds();
initial.commit09911bf2008-07-26 23:55:29965 }
[email protected]fa7b1dc2010-06-23 17:53:04966
967 // Only sends text input type and caret bounds to the browser process if they
968 // are changed.
969 if (text_input_type_ != new_type || caret_bounds_ != new_caret_bounds) {
970 text_input_type_ = new_type;
971 caret_bounds_ = new_caret_bounds;
972 Send(new ViewHostMsg_ImeUpdateTextInputState(
973 routing_id(), new_type, new_caret_bounds));
initial.commit09911bf2008-07-26 23:55:29974 }
initial.commit09911bf2008-07-26 23:55:29975}
976
[email protected]4873c7d2009-07-16 06:36:28977WebScreenInfo RenderWidget::screenInfo() {
978 WebScreenInfo results;
979 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
980 return results;
981}
982
[email protected]fa7b1dc2010-06-23 17:53:04983void RenderWidget::resetInputMethod() {
984 if (!input_method_is_active_)
985 return;
986
987 // If the last text input type is not None, then we should finish any
988 // ongoing composition regardless of the new text input type.
989 if (text_input_type_ != WebKit::WebTextInputTypeNone) {
990 // If a composition text exists, then we need to let the browser process
991 // to cancel the input method's ongoing composition session.
992 if (webwidget_->confirmComposition())
993 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
994 }
995}
996
[email protected]f103ab72009-09-02 17:10:59997void RenderWidget::SchedulePluginMove(
998 const webkit_glue::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:29999 size_t i = 0;
1000 for (; i < plugin_window_moves_.size(); ++i) {
1001 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581002 if (move.rects_valid) {
1003 plugin_window_moves_[i] = move;
1004 } else {
1005 plugin_window_moves_[i].visible = move.visible;
1006 }
initial.commit09911bf2008-07-26 23:55:291007 break;
1008 }
1009 }
1010
1011 if (i == plugin_window_moves_.size())
1012 plugin_window_moves_.push_back(move);
1013}
[email protected]268654772009-08-06 23:02:041014
1015void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1016 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1017 i != plugin_window_moves_.end(); ++i) {
1018 if (i->window == window) {
1019 plugin_window_moves_.erase(i);
1020 break;
1021 }
1022 }
1023}