[email protected] | 05d47875 | 2009-04-08 23:38:16 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/renderer/render_widget.h" |
| 6 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include "base/gfx/point.h" |
| 8 | #include "base/gfx/size.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/message_loop.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/scoped_ptr.h" |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | 67474193 | 2009-02-04 23:44:46 | [diff] [blame] | 13 | #include "chrome/common/render_messages.h" |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 14 | #include "chrome/common/transport_dib.h" |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 15 | #include "chrome/renderer/render_process.h" |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 16 | #include "skia/ext/platform_canvas.h" |
[email protected] | d5282e7 | 2009-05-13 13:16:52 | [diff] [blame] | 17 | #include "third_party/skia/include/core/SkShader.h" |
[email protected] | 7c51b0ee | 2009-07-08 21:49:30 | [diff] [blame] | 18 | #include "webkit/api/public/WebCursorInfo.h" |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 19 | #include "webkit/api/public/WebPopupMenu.h" |
[email protected] | 88efb7ec | 2009-07-14 16:32:59 | [diff] [blame] | 20 | #include "webkit/api/public/WebPopupMenuInfo.h" |
[email protected] | afdcf5c | 2009-05-10 20:30:41 | [diff] [blame] | 21 | #include "webkit/api/public/WebRect.h" |
| 22 | #include "webkit/api/public/WebScreenInfo.h" |
| 23 | #include "webkit/api/public/WebSize.h" |
[email protected] | 8c89e779 | 2009-08-19 21:18:34 | [diff] [blame] | 24 | #include "webkit/glue/webkit_glue.h" |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 25 | |
| 26 | #if defined(OS_POSIX) |
[email protected] | d5282e7 | 2009-05-13 13:16:52 | [diff] [blame] | 27 | #include "third_party/skia/include/core/SkPixelRef.h" |
| 28 | #include "third_party/skia/include/core/SkMallocPixelRef.h" |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 29 | #endif // defined(OS_POSIX) |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 30 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 31 | #include "webkit/api/public/WebWidget.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 33 | using WebKit::WebCompositionCommand; |
[email protected] | 7c51b0ee | 2009-07-08 21:49:30 | [diff] [blame] | 34 | using WebKit::WebCursorInfo; |
[email protected] | 62cb33cae | 2009-03-27 23:30:22 | [diff] [blame] | 35 | using WebKit::WebInputEvent; |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 36 | using WebKit::WebNavigationPolicy; |
| 37 | using WebKit::WebPopupMenu; |
[email protected] | 88efb7ec | 2009-07-14 16:32:59 | [diff] [blame] | 38 | using WebKit::WebPopupMenuInfo; |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 39 | using WebKit::WebRect; |
[email protected] | 12456fa | 2009-04-01 23:07:19 | [diff] [blame] | 40 | using WebKit::WebScreenInfo; |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 41 | using WebKit::WebSize; |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 42 | using WebKit::WebTextDirection; |
[email protected] | 62cb33cae | 2009-03-27 23:30:22 | [diff] [blame] | 43 | |
[email protected] | cfd727f | 2009-01-09 20:21:11 | [diff] [blame] | 44 | RenderWidget::RenderWidget(RenderThreadBase* render_thread, bool activatable) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | : routing_id_(MSG_ROUTING_NONE), |
[email protected] | c5b3b5e | 2009-02-13 06:41:11 | [diff] [blame] | 46 | webwidget_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | opener_id_(MSG_ROUTING_NONE), |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 48 | render_thread_(render_thread), |
[email protected] | 5a3b914 | 2009-08-28 21:03:17 | [diff] [blame] | 49 | host_window_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | current_paint_buf_(NULL), |
| 51 | current_scroll_buf_(NULL), |
| 52 | next_paint_flags_(0), |
| 53 | paint_reply_pending_(false), |
| 54 | did_show_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | is_hidden_(false), |
| 56 | needs_repainting_on_restore_(false), |
| 57 | has_focus_(false), |
[email protected] | 5dd76821 | 2009-08-13 23:34:49 | [diff] [blame] | 58 | handling_input_event_(false), |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 59 | closing_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | ime_is_active_(false), |
| 61 | ime_control_enable_ime_(true), |
| 62 | ime_control_x_(-1), |
| 63 | ime_control_y_(-1), |
| 64 | ime_control_new_state_(false), |
[email protected] | 0ebf387 | 2008-11-07 21:35:03 | [diff] [blame] | 65 | ime_control_updated_(false), |
[email protected] | 9f23f59 | 2008-11-17 08:36:34 | [diff] [blame] | 66 | ime_control_busy_(false), |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 67 | activatable_(activatable), |
| 68 | pending_window_rect_count_(0) { |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 69 | RenderProcess::current()->AddRefProcess(); |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 70 | DCHECK(render_thread_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | RenderWidget::~RenderWidget() { |
[email protected] | c5b3b5e | 2009-02-13 06:41:11 | [diff] [blame] | 74 | DCHECK(!webwidget_) << "Leaking our WebWidget!"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | if (current_paint_buf_) { |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 76 | RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | current_paint_buf_ = NULL; |
| 78 | } |
| 79 | if (current_scroll_buf_) { |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 80 | RenderProcess::current()->ReleaseTransportDIB(current_scroll_buf_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | current_scroll_buf_ = NULL; |
| 82 | } |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 83 | RenderProcess::current()->ReleaseProcess(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /*static*/ |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 87 | RenderWidget* RenderWidget::Create(int32 opener_id, |
[email protected] | 0ebf387 | 2008-11-07 21:35:03 | [diff] [blame] | 88 | RenderThreadBase* render_thread, |
[email protected] | cfd727f | 2009-01-09 20:21:11 | [diff] [blame] | 89 | bool activatable) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | DCHECK(opener_id != MSG_ROUTING_NONE); |
[email protected] | 0ebf387 | 2008-11-07 21:35:03 | [diff] [blame] | 91 | scoped_refptr<RenderWidget> widget = new RenderWidget(render_thread, |
[email protected] | cfd727f | 2009-01-09 20:21:11 | [diff] [blame] | 92 | activatable); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | widget->Init(opener_id); // adds reference |
| 94 | return widget; |
| 95 | } |
| 96 | |
[email protected] | 88efb7ec | 2009-07-14 16:32:59 | [diff] [blame] | 97 | void RenderWidget::ConfigureAsExternalPopupMenu(const WebPopupMenuInfo& info) { |
| 98 | popup_params_.reset(new ViewHostMsg_ShowPopup_Params); |
| 99 | popup_params_->item_height = info.itemHeight; |
| 100 | popup_params_->selected_item = info.selectedIndex; |
| 101 | for (size_t i = 0; i < info.items.size(); ++i) |
| 102 | popup_params_->popup_items.push_back(WebMenuItem(info.items[i])); |
| 103 | } |
| 104 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | void RenderWidget::Init(int32 opener_id) { |
| 106 | DCHECK(!webwidget_); |
| 107 | |
| 108 | if (opener_id != MSG_ROUTING_NONE) |
| 109 | opener_id_ = opener_id; |
| 110 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 111 | webwidget_ = WebPopupMenu::create(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 113 | bool result = render_thread_->Send( |
[email protected] | cfd727f | 2009-01-09 20:21:11 | [diff] [blame] | 114 | new ViewHostMsg_CreateWidget(opener_id, activatable_, &routing_id_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | if (result) { |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 116 | render_thread_->AddRoute(routing_id_, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | // Take a reference on behalf of the RenderThread. This will be balanced |
| 118 | // when we receive ViewMsg_Close. |
| 119 | AddRef(); |
| 120 | } else { |
| 121 | DCHECK(false); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // This is used to complete pending inits and non-pending inits. For non- |
| 126 | // pending cases, the parent will be the same as the current parent. This |
| 127 | // indicates we do not need to reparent or anything. |
[email protected] | 18bcc3c | 2009-01-27 21:39:15 | [diff] [blame] | 128 | void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | DCHECK(routing_id_ != MSG_ROUTING_NONE); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | |
| 131 | host_window_ = parent_hwnd; |
| 132 | |
[email protected] | 6de7445 | 2009-02-25 18:04:59 | [diff] [blame] | 133 | Send(new ViewHostMsg_RenderViewReady(routing_id_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | IPC_DEFINE_MESSAGE_MAP(RenderWidget) |
| 137 | IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose) |
| 138 | IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck) |
| 139 | IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize) |
| 140 | IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden) |
| 141 | IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored) |
| 142 | IPC_MESSAGE_HANDLER(ViewMsg_PaintRect_ACK, OnPaintRectAck) |
| 143 | IPC_MESSAGE_HANDLER(ViewMsg_ScrollRect_ACK, OnScrollRectAck) |
| 144 | IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent) |
| 145 | IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost) |
| 146 | IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus) |
| 147 | IPC_MESSAGE_HANDLER(ViewMsg_ImeSetInputMode, OnImeSetInputMode) |
| 148 | IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition) |
[email protected] | ec7dc11 | 2008-08-06 05:30:12 | [diff] [blame] | 149 | IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint) |
[email protected] | 07f95333 | 2009-03-25 04:31:11 | [diff] [blame] | 150 | IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection) |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 151 | IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | IPC_MESSAGE_UNHANDLED_ERROR() |
| 153 | IPC_END_MESSAGE_MAP() |
| 154 | |
| 155 | bool RenderWidget::Send(IPC::Message* message) { |
| 156 | // Don't send any messages after the browser has told us to close. |
| 157 | if (closing_) { |
| 158 | delete message; |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // If given a messsage without a routing ID, then assign our routing ID. |
| 163 | if (message->routing_id() == MSG_ROUTING_NONE) |
| 164 | message->set_routing_id(routing_id_); |
| 165 | |
[email protected] | d3fc2565 | 2009-02-24 22:31:25 | [diff] [blame] | 166 | return render_thread_->Send(message); |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 167 | } |
| 168 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | // Got a response from the browser after the renderer decided to create a new |
| 170 | // view. |
[email protected] | 18bcc3c | 2009-01-27 21:39:15 | [diff] [blame] | 171 | void RenderWidget::OnCreatingNewAck(gfx::NativeViewId parent) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | DCHECK(routing_id_ != MSG_ROUTING_NONE); |
| 173 | |
| 174 | CompleteInit(parent); |
| 175 | } |
| 176 | |
| 177 | void RenderWidget::OnClose() { |
| 178 | if (closing_) |
| 179 | return; |
| 180 | closing_ = true; |
| 181 | |
| 182 | // Browser correspondence is no longer needed at this point. |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 183 | if (routing_id_ != MSG_ROUTING_NONE) { |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 184 | render_thread_->RemoveRoute(routing_id_); |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 185 | SetHidden(false); |
| 186 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | // If there is a Send call on the stack, then it could be dangerous to close |
[email protected] | d3fc2565 | 2009-02-24 22:31:25 | [diff] [blame] | 189 | // now. Post a task that only gets invoked when there are no nested message |
| 190 | // loops. |
| 191 | MessageLoop::current()->PostNonNestableTask(FROM_HERE, |
| 192 | NewRunnableMethod(this, &RenderWidget::Close)); |
| 193 | |
| 194 | // Balances the AddRef taken when we called AddRoute. |
| 195 | Release(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | f21c613a | 2009-02-12 14:46:17 | [diff] [blame] | 198 | void RenderWidget::OnResize(const gfx::Size& new_size, |
| 199 | const gfx::Rect& resizer_rect) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | // During shutdown we can just ignore this message. |
| 201 | if (!webwidget_) |
| 202 | return; |
| 203 | |
[email protected] | f21c613a | 2009-02-12 14:46:17 | [diff] [blame] | 204 | // Remember the rect where the resize corner will be drawn. |
| 205 | resizer_rect_ = resizer_rect; |
| 206 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | // TODO(darin): We should not need to reset this here. |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 208 | SetHidden(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | needs_repainting_on_restore_ = false; |
| 210 | |
| 211 | // We shouldn't be asked to resize to our current size. |
| 212 | DCHECK(size_ != new_size); |
| 213 | size_ = new_size; |
| 214 | |
| 215 | // We should not be sent a Resize message if we have not ACK'd the previous |
| 216 | DCHECK(!next_paint_is_resize_ack()); |
| 217 | |
| 218 | // When resizing, we want to wait to paint before ACK'ing the resize. This |
| 219 | // ensures that we only resize as fast as we can paint. We only need to send |
| 220 | // an ACK if we are resized to a non-empty rect. |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 221 | webwidget_->resize(new_size); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | if (!new_size.IsEmpty()) { |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 223 | DCHECK(!paint_rect_.IsEmpty()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | |
| 225 | // This should have caused an invalidation of the entire view. The damaged |
| 226 | // rect could be larger than new_size if we are being made smaller. |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 227 | DCHECK_GE(paint_rect_.width(), new_size.width()); |
| 228 | DCHECK_GE(paint_rect_.height(), new_size.height()); |
| 229 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | // We will send the Resize_ACK flag once we paint again. |
| 231 | set_next_paint_is_resize_ack(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void RenderWidget::OnWasHidden() { |
| 236 | // Go into a mode where we stop generating paint and scrolling events. |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 237 | SetHidden(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void RenderWidget::OnWasRestored(bool needs_repainting) { |
| 241 | // During shutdown we can just ignore this message. |
| 242 | if (!webwidget_) |
| 243 | return; |
| 244 | |
| 245 | // See OnWasHidden |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 246 | SetHidden(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | |
| 248 | if (!needs_repainting && !needs_repainting_on_restore_) |
| 249 | return; |
| 250 | needs_repainting_on_restore_ = false; |
| 251 | |
| 252 | // Tag the next paint as a restore ack, which is picked up by DoDeferredPaint |
| 253 | // when it sends out the next PaintRect message. |
| 254 | set_next_paint_is_restore_ack(); |
| 255 | |
| 256 | // Generate a full repaint. |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 257 | didInvalidateRect(gfx::Rect(size_.width(), size_.height())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | b7fce1f | 2008-08-14 05:01:07 | [diff] [blame] | 260 | void RenderWidget::OnPaintRectAck() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 261 | DCHECK(paint_reply_pending()); |
| 262 | paint_reply_pending_ = false; |
[email protected] | b7fce1f | 2008-08-14 05:01:07 | [diff] [blame] | 263 | // If we sent a PaintRect message with a zero-sized bitmap, then |
| 264 | // we should have no current paint buf. |
| 265 | if (current_paint_buf_) { |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 266 | RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_); |
[email protected] | b7fce1f | 2008-08-14 05:01:07 | [diff] [blame] | 267 | current_paint_buf_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 268 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 269 | |
[email protected] | a2f6bc11 | 2009-06-27 16:27:25 | [diff] [blame] | 270 | // Notify subclasses |
| 271 | DidPaint(); |
| 272 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | // Continue painting if necessary... |
[email protected] | 12fbad81 | 2009-09-01 18:21:24 | [diff] [blame] | 274 | CallDoDeferredPaint(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | } |
| 276 | |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 277 | void RenderWidget::OnRequestMoveAck() { |
| 278 | DCHECK(pending_window_rect_count_); |
| 279 | pending_window_rect_count_--; |
| 280 | } |
| 281 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 282 | void RenderWidget::OnScrollRectAck() { |
| 283 | DCHECK(scroll_reply_pending()); |
| 284 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 285 | if (current_scroll_buf_) { |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 286 | RenderProcess::current()->ReleaseTransportDIB(current_scroll_buf_); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 287 | current_scroll_buf_ = NULL; |
| 288 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | |
| 290 | // Continue scrolling if necessary... |
[email protected] | 12fbad81 | 2009-09-01 18:21:24 | [diff] [blame] | 291 | CallDoDeferredScroll(); |
| 292 | } |
| 293 | |
| 294 | void RenderWidget::CallDoDeferredScroll() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 295 | DoDeferredScroll(); |
[email protected] | 12fbad81 | 2009-09-01 18:21:24 | [diff] [blame] | 296 | |
| 297 | if (pending_input_event_ack_.get()) { |
| 298 | Send(pending_input_event_ack_.get()); |
| 299 | pending_input_event_ack_.release(); |
| 300 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void RenderWidget::OnHandleInputEvent(const IPC::Message& message) { |
| 304 | void* iter = NULL; |
| 305 | |
| 306 | const char* data; |
| 307 | int data_length; |
[email protected] | 5dd76821 | 2009-08-13 23:34:49 | [diff] [blame] | 308 | handling_input_event_ = true; |
| 309 | if (!message.ReadData(&iter, &data, &data_length)) { |
| 310 | handling_input_event_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 311 | return; |
[email protected] | 5dd76821 | 2009-08-13 23:34:49 | [diff] [blame] | 312 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 313 | |
| 314 | const WebInputEvent* input_event = |
| 315 | reinterpret_cast<const WebInputEvent*>(data); |
| 316 | bool processed = false; |
| 317 | if (webwidget_) |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 318 | processed = webwidget_->handleInputEvent(*input_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | |
| 320 | IPC::Message* response = new ViewHostMsg_HandleInputEvent_ACK(routing_id_); |
| 321 | response->WriteInt(input_event->type); |
[email protected] | e282441 | 2009-02-27 01:57:05 | [diff] [blame] | 322 | response->WriteBool(processed); |
| 323 | |
[email protected] | 12fbad81 | 2009-09-01 18:21:24 | [diff] [blame] | 324 | if (input_event->type == WebInputEvent::MouseMove && |
| 325 | (!paint_rect_.IsEmpty() || !scroll_rect_.IsEmpty())) { |
| 326 | // We want to rate limit the input events in this case, so we'll wait for |
| 327 | // painting to finish before ACKing this message. |
| 328 | pending_input_event_ack_.reset(response); |
| 329 | } else { |
| 330 | Send(response); |
| 331 | } |
| 332 | |
[email protected] | 5dd76821 | 2009-08-13 23:34:49 | [diff] [blame] | 333 | handling_input_event_ = false; |
[email protected] | 44670587 | 2009-09-10 07:22:48 | [diff] [blame^] | 334 | |
| 335 | WebInputEvent::Type type = input_event->type; |
| 336 | if (type == WebInputEvent::RawKeyDown || type == WebInputEvent::KeyDown || |
| 337 | type == WebInputEvent::KeyUp || type == WebInputEvent::Char) |
| 338 | DidHandleKeyEvent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void RenderWidget::OnMouseCaptureLost() { |
| 342 | if (webwidget_) |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 343 | webwidget_->mouseCaptureLost(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void RenderWidget::OnSetFocus(bool enable) { |
| 347 | has_focus_ = enable; |
| 348 | if (webwidget_) |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 349 | webwidget_->setFocus(enable); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 350 | if (enable) { |
| 351 | // Force to retrieve the state of the focused widget to determine if we |
| 352 | // should activate IMEs next time when this process calls the UpdateIME() |
| 353 | // function. |
| 354 | ime_control_updated_ = true; |
| 355 | ime_control_new_state_ = true; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | void RenderWidget::ClearFocus() { |
| 360 | // We may have got the focus from the browser before this gets processed, in |
| 361 | // which case we do not want to unfocus ourself. |
| 362 | if (!has_focus_ && webwidget_) |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 363 | webwidget_->setFocus(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | } |
| 365 | |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 366 | void RenderWidget::PaintRect(const gfx::Rect& rect, |
| 367 | skia::PlatformCanvas* canvas) { |
| 368 | |
| 369 | // Bring the canvas into the coordinate system of the paint rect. |
| 370 | canvas->translate(static_cast<SkScalar>(-rect.x()), |
| 371 | static_cast<SkScalar>(-rect.y())); |
[email protected] | 96c3499a | 2009-05-02 18:31:03 | [diff] [blame] | 372 | |
[email protected] | 699ab0d | 2009-04-23 23:19:14 | [diff] [blame] | 373 | // If there is a custom background, tile it. |
| 374 | if (!background_.empty()) { |
[email protected] | 699ab0d | 2009-04-23 23:19:14 | [diff] [blame] | 375 | SkPaint paint; |
| 376 | SkShader* shader = SkShader::CreateBitmapShader(background_, |
| 377 | SkShader::kRepeat_TileMode, |
| 378 | SkShader::kRepeat_TileMode); |
| 379 | paint.setShader(shader)->unref(); |
[email protected] | 8860e4f5 | 2009-06-25 01:01:52 | [diff] [blame] | 380 | paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
[email protected] | 699ab0d | 2009-04-23 23:19:14 | [diff] [blame] | 381 | canvas->drawPaint(paint); |
[email protected] | 699ab0d | 2009-04-23 23:19:14 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | 8c89e779 | 2009-08-19 21:18:34 | [diff] [blame] | 384 | webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | |
| 386 | // Flush to underlying bitmap. TODO(darin): is this needed? |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 387 | canvas->getTopPlatformDevice().accessBitmap(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 388 | } |
| 389 | |
[email protected] | 12fbad81 | 2009-09-01 18:21:24 | [diff] [blame] | 390 | void RenderWidget::CallDoDeferredPaint() { |
| 391 | DoDeferredPaint(); |
| 392 | |
| 393 | if (pending_input_event_ack_.get()) { |
| 394 | Send(pending_input_event_ack_.get()); |
| 395 | pending_input_event_ack_.release(); |
| 396 | } |
| 397 | } |
| 398 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | void RenderWidget::DoDeferredPaint() { |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 400 | if (!webwidget_ || paint_reply_pending() || paint_rect_.IsEmpty()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | return; |
| 402 | |
| 403 | // When we are hidden, we want to suppress painting, but we still need to |
| 404 | // mark this DoDeferredPaint as complete. |
| 405 | if (is_hidden_ || size_.IsEmpty()) { |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 406 | paint_rect_ = gfx::Rect(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | needs_repainting_on_restore_ = true; |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | // Layout may generate more invalidation... |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 412 | webwidget_->layout(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | |
| 414 | // OK, save the current paint_rect to a local since painting may cause more |
| 415 | // invalidation. Some WebCore rendering objects only layout when painted. |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 416 | gfx::Rect damaged_rect = paint_rect_; |
| 417 | paint_rect_ = gfx::Rect(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 418 | |
| 419 | // Compute a buffer for painting and cache it. |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 420 | skia::PlatformCanvas* canvas = |
[email protected] | f09c718 | 2009-03-10 12:54:04 | [diff] [blame] | 421 | RenderProcess::current()->GetDrawingCanvas(¤t_paint_buf_, |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 422 | damaged_rect); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 423 | if (!canvas) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | NOTREACHED(); |
| 425 | return; |
| 426 | } |
| 427 | |
[email protected] | 955ee6d6 | 2009-06-17 21:53:03 | [diff] [blame] | 428 | // We may get back a smaller canvas than we asked for. |
| 429 | damaged_rect.set_width(canvas->getDevice()->width()); |
| 430 | damaged_rect.set_height(canvas->getDevice()->height()); |
| 431 | |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 432 | PaintRect(damaged_rect, canvas); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 433 | |
| 434 | ViewHostMsg_PaintRect_Params params; |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 435 | params.bitmap_rect = damaged_rect; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 436 | params.view_size = size_; |
| 437 | params.plugin_window_moves = plugin_window_moves_; |
| 438 | params.flags = next_paint_flags_; |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 439 | params.bitmap = current_paint_buf_->id(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 440 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 441 | delete canvas; |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 442 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 443 | plugin_window_moves_.clear(); |
| 444 | |
| 445 | paint_reply_pending_ = true; |
| 446 | Send(new ViewHostMsg_PaintRect(routing_id_, params)); |
| 447 | next_paint_flags_ = 0; |
| 448 | |
| 449 | UpdateIME(); |
| 450 | } |
| 451 | |
| 452 | void RenderWidget::DoDeferredScroll() { |
| 453 | if (!webwidget_ || scroll_reply_pending() || scroll_rect_.IsEmpty()) |
| 454 | return; |
| 455 | |
| 456 | // When we are hidden, we want to suppress scrolling, but we still need to |
| 457 | // mark this DoDeferredScroll as complete. |
| 458 | if (is_hidden_ || size_.IsEmpty()) { |
| 459 | scroll_rect_ = gfx::Rect(); |
| 460 | needs_repainting_on_restore_ = true; |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | // Layout may generate more invalidation, so we might have to bail on |
| 465 | // optimized scrolling... |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 466 | webwidget_->layout(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 467 | |
| 468 | if (scroll_rect_.IsEmpty()) |
| 469 | return; |
| 470 | |
| 471 | gfx::Rect damaged_rect; |
| 472 | |
| 473 | // Compute the region we will expose by scrolling, and paint that into a |
| 474 | // shared memory section. |
| 475 | if (scroll_delta_.x()) { |
| 476 | int dx = scroll_delta_.x(); |
| 477 | damaged_rect.set_y(scroll_rect_.y()); |
| 478 | damaged_rect.set_height(scroll_rect_.height()); |
| 479 | if (dx > 0) { |
| 480 | damaged_rect.set_x(scroll_rect_.x()); |
| 481 | damaged_rect.set_width(dx); |
| 482 | } else { |
| 483 | damaged_rect.set_x(scroll_rect_.right() + dx); |
| 484 | damaged_rect.set_width(-dx); |
| 485 | } |
| 486 | } else { |
| 487 | int dy = scroll_delta_.y(); |
| 488 | damaged_rect.set_x(scroll_rect_.x()); |
| 489 | damaged_rect.set_width(scroll_rect_.width()); |
| 490 | if (dy > 0) { |
| 491 | damaged_rect.set_y(scroll_rect_.y()); |
| 492 | damaged_rect.set_height(dy); |
| 493 | } else { |
| 494 | damaged_rect.set_y(scroll_rect_.bottom() + dy); |
| 495 | damaged_rect.set_height(-dy); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // In case the scroll offset exceeds the width/height of the scroll rect |
| 500 | damaged_rect = scroll_rect_.Intersect(damaged_rect); |
| 501 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 502 | skia::PlatformCanvas* canvas = |
[email protected] | f09c718 | 2009-03-10 12:54:04 | [diff] [blame] | 503 | RenderProcess::current()->GetDrawingCanvas(¤t_scroll_buf_, |
| 504 | damaged_rect); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 505 | if (!canvas) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 506 | NOTREACHED(); |
| 507 | return; |
| 508 | } |
| 509 | |
[email protected] | 955ee6d6 | 2009-06-17 21:53:03 | [diff] [blame] | 510 | // We may get back a smaller canvas than we asked for. |
| 511 | damaged_rect.set_width(canvas->getDevice()->width()); |
| 512 | damaged_rect.set_height(canvas->getDevice()->height()); |
| 513 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 514 | // Set these parameters before calling Paint, since that could result in |
| 515 | // further invalidates (uncommon). |
| 516 | ViewHostMsg_ScrollRect_Params params; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 517 | params.bitmap_rect = damaged_rect; |
| 518 | params.dx = scroll_delta_.x(); |
| 519 | params.dy = scroll_delta_.y(); |
| 520 | params.clip_rect = scroll_rect_; |
| 521 | params.view_size = size_; |
| 522 | params.plugin_window_moves = plugin_window_moves_; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 523 | params.bitmap = current_scroll_buf_->id(); |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 524 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 525 | plugin_window_moves_.clear(); |
| 526 | |
| 527 | // Mark the scroll operation as no longer pending. |
| 528 | scroll_rect_ = gfx::Rect(); |
| 529 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 530 | PaintRect(damaged_rect, canvas); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | Send(new ViewHostMsg_ScrollRect(routing_id_, params)); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 532 | delete canvas; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 533 | UpdateIME(); |
| 534 | } |
| 535 | |
| 536 | /////////////////////////////////////////////////////////////////////////////// |
| 537 | // WebWidgetDelegate |
| 538 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 539 | void RenderWidget::didInvalidateRect(const WebRect& rect) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | // We only want one pending DoDeferredPaint call at any time... |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 541 | bool paint_pending = !paint_rect_.IsEmpty(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 542 | |
| 543 | // If this invalidate overlaps with a pending scroll, then we have to |
| 544 | // downgrade to invalidating the scroll rect. |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 545 | if (gfx::Rect(rect).Intersects(scroll_rect_)) { |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 546 | paint_rect_ = paint_rect_.Union(scroll_rect_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 547 | scroll_rect_ = gfx::Rect(); |
| 548 | } |
| 549 | |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 550 | gfx::Rect view_rect(0, 0, size_.width(), size_.height()); |
| 551 | // TODO(iyengar) Investigate why we have painting issues when |
| 552 | // we ignore invalid regions outside the view. |
| 553 | // Ignore invalidates that occur outside the bounds of the view |
| 554 | // TODO(darin): maybe this should move into the paint code? |
| 555 | // paint_rect_ = view_rect.Intersect(paint_rect_.Union(rect)); |
| 556 | paint_rect_ = paint_rect_.Union(view_rect.Intersect(rect)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 557 | |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 558 | if (paint_rect_.IsEmpty() || paint_reply_pending() || paint_pending) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 559 | return; |
| 560 | |
| 561 | // Perform painting asynchronously. This serves two purposes: |
| 562 | // 1) Ensures that we call WebView::Paint without a bunch of other junk |
| 563 | // on the call stack. |
| 564 | // 2) Allows us to collect more damage rects before painting to help coalesce |
| 565 | // the work that we will need to do. |
| 566 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 12fbad81 | 2009-09-01 18:21:24 | [diff] [blame] | 567 | this, &RenderWidget::CallDoDeferredPaint)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 568 | } |
| 569 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 570 | void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) { |
[email protected] | f911f74 | 2009-02-27 02:17:02 | [diff] [blame] | 571 | if (dx != 0 && dy != 0) { |
| 572 | // We only support scrolling along one axis at a time. |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 573 | didScrollRect(0, dy, clip_rect); |
[email protected] | f911f74 | 2009-02-27 02:17:02 | [diff] [blame] | 574 | dy = 0; |
| 575 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 576 | |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame] | 577 | bool intersects_with_painting = paint_rect_.Intersects(clip_rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 578 | |
| 579 | // If we already have a pending scroll operation or if this scroll operation |
| 580 | // intersects the existing paint region, then just failover to invalidating. |
| 581 | if (!scroll_rect_.IsEmpty() || intersects_with_painting) { |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 582 | if (!intersects_with_painting && scroll_rect_ == gfx::Rect(clip_rect)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 583 | // OK, we can just update the scroll delta (requires same scrolling axis) |
| 584 | if (!dx && !scroll_delta_.x()) { |
| 585 | scroll_delta_.set_y(scroll_delta_.y() + dy); |
| 586 | return; |
| 587 | } |
| 588 | if (!dy && !scroll_delta_.y()) { |
| 589 | scroll_delta_.set_x(scroll_delta_.x() + dx); |
| 590 | return; |
| 591 | } |
| 592 | } |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 593 | didInvalidateRect(scroll_rect_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 594 | DCHECK(scroll_rect_.IsEmpty()); |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 595 | didInvalidateRect(clip_rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
| 599 | // We only want one pending DoDeferredScroll call at any time... |
| 600 | bool scroll_pending = !scroll_rect_.IsEmpty(); |
| 601 | |
| 602 | scroll_rect_ = clip_rect; |
| 603 | scroll_delta_.SetPoint(dx, dy); |
| 604 | |
| 605 | if (scroll_pending) |
| 606 | return; |
| 607 | |
| 608 | // Perform scrolling asynchronously since we need to call WebView::Paint |
| 609 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 12fbad81 | 2009-09-01 18:21:24 | [diff] [blame] | 610 | this, &RenderWidget::CallDoDeferredScroll)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 611 | } |
| 612 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 613 | void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) { |
[email protected] | 7c51b0ee | 2009-07-08 21:49:30 | [diff] [blame] | 614 | // TODO(darin): Eliminate this temporary. |
| 615 | WebCursor cursor(cursor_info); |
| 616 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 617 | // Only send a SetCursor message if we need to make a change. |
| 618 | if (!current_cursor_.IsEqual(cursor)) { |
| 619 | current_cursor_ = cursor; |
| 620 | Send(new ViewHostMsg_SetCursor(routing_id_, cursor)); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | // We are supposed to get a single call to Show for a newly created RenderWidget |
| 625 | // that was created via RenderWidget::CreateWebView. So, we wait until this |
| 626 | // point to dispatch the ShowWidget message. |
| 627 | // |
| 628 | // This method provides us with the information about how to display the newly |
| 629 | // created RenderWidget (i.e., as a constrained popup or as a new tab). |
| 630 | // |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 631 | void RenderWidget::show(WebNavigationPolicy) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 632 | DCHECK(!did_show_) << "received extraneous Show call"; |
| 633 | DCHECK(routing_id_ != MSG_ROUTING_NONE); |
| 634 | DCHECK(opener_id_ != MSG_ROUTING_NONE); |
| 635 | |
| 636 | if (!did_show_) { |
| 637 | did_show_ = true; |
| 638 | // NOTE: initial_pos_ may still have its default values at this point, but |
| 639 | // that's okay. It'll be ignored if as_popup is false, or the browser |
| 640 | // process will impose a default position otherwise. |
[email protected] | 88efb7ec | 2009-07-14 16:32:59 | [diff] [blame] | 641 | if (popup_params_.get()) { |
| 642 | popup_params_->bounds = initial_pos_; |
| 643 | Send(new ViewHostMsg_ShowPopup(routing_id_, *popup_params_)); |
| 644 | popup_params_.reset(); |
| 645 | } else { |
| 646 | Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_)); |
| 647 | } |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 648 | SetPendingWindowRect(initial_pos_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 652 | void RenderWidget::didFocus() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 653 | // Prevent the widget from stealing the focus if it does not have focus |
| 654 | // already. We do this by explicitely setting the focus to false again. |
| 655 | // We only let the browser focus the renderer. |
| 656 | if (!has_focus_ && webwidget_) { |
| 657 | MessageLoop::current()->PostTask(FROM_HERE, |
| 658 | NewRunnableMethod(this, &RenderWidget::ClearFocus)); |
| 659 | } |
| 660 | } |
| 661 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 662 | void RenderWidget::didBlur() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 663 | Send(new ViewHostMsg_Blur(routing_id_)); |
| 664 | } |
| 665 | |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 666 | void RenderWidget::DoDeferredClose() { |
| 667 | Send(new ViewHostMsg_Close(routing_id_)); |
| 668 | } |
| 669 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 670 | void RenderWidget::closeWidgetSoon() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 671 | // If a page calls window.close() twice, we'll end up here twice, but that's |
| 672 | // OK. It is safe to send multiple Close messages. |
| 673 | |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 674 | // Ask the RenderWidgetHost to initiate close. We could be called from deep |
| 675 | // in Javascript. If we ask the RendwerWidgetHost to close now, the window |
| 676 | // could be closed before the JS finishes executing. So instead, post a |
| 677 | // message back to the message loop, which won't run until the JS is |
| 678 | // complete, and then the Close message can be sent. |
[email protected] | 75ae449 | 2009-07-10 00:05:15 | [diff] [blame] | 679 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 680 | this, &RenderWidget::DoDeferredClose)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 681 | } |
| 682 | |
[email protected] | b4b967e | 2009-04-22 11:33:05 | [diff] [blame] | 683 | void RenderWidget::GenerateFullRepaint() { |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 684 | didInvalidateRect(gfx::Rect(size_.width(), size_.height())); |
[email protected] | b4b967e | 2009-04-22 11:33:05 | [diff] [blame] | 685 | } |
| 686 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 687 | void RenderWidget::Close() { |
| 688 | if (webwidget_) { |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 689 | webwidget_->close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 690 | webwidget_ = NULL; |
| 691 | } |
| 692 | } |
| 693 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 694 | WebRect RenderWidget::windowRect() { |
| 695 | if (pending_window_rect_count_) |
| 696 | return pending_window_rect_; |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 697 | |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 698 | gfx::Rect rect; |
| 699 | Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect)); |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 700 | return rect; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 701 | } |
| 702 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 703 | void RenderWidget::setWindowRect(const WebRect& pos) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 704 | if (did_show_) { |
| 705 | Send(new ViewHostMsg_RequestMove(routing_id_, pos)); |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 706 | SetPendingWindowRect(pos); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 707 | } else { |
| 708 | initial_pos_ = pos; |
| 709 | } |
| 710 | } |
| 711 | |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 712 | void RenderWidget::SetPendingWindowRect(const WebRect& rect) { |
| 713 | pending_window_rect_ = rect; |
| 714 | pending_window_rect_count_++; |
| 715 | } |
| 716 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 717 | WebRect RenderWidget::rootWindowRect() { |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 718 | if (pending_window_rect_count_) { |
| 719 | // NOTE(mbelshe): If there is a pending_window_rect_, then getting |
| 720 | // the RootWindowRect is probably going to return wrong results since the |
| 721 | // browser may not have processed the Move yet. There isn't really anything |
| 722 | // good to do in this case, and it shouldn't happen - since this size is |
| 723 | // only really needed for windowToScreen, which is only used for Popups. |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 724 | return pending_window_rect_; |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 725 | } |
| 726 | |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 727 | gfx::Rect rect; |
| 728 | Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect)); |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 729 | return rect; |
[email protected] | d454745 | 2008-08-28 18:36:37 | [diff] [blame] | 730 | } |
| 731 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 732 | WebRect RenderWidget::windowResizerRect() { |
| 733 | return resizer_rect_; |
[email protected] | c04b636 | 2008-11-21 18:54:19 | [diff] [blame] | 734 | } |
| 735 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 736 | void RenderWidget::OnImeSetInputMode(bool is_active) { |
[email protected] | c4bb35a | 2008-10-31 17:54:03 | [diff] [blame] | 737 | // To prevent this renderer process from sending unnecessary IPC messages to |
| 738 | // a browser process, we permit the renderer process to send IPC messages |
| 739 | // only during the IME attached to the browser process is active. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 740 | ime_is_active_ = is_active; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 741 | } |
| 742 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 743 | void RenderWidget::OnImeSetComposition(WebCompositionCommand command, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 744 | int cursor_position, |
| 745 | int target_start, int target_end, |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 746 | const string16& ime_string) { |
| 747 | if (!webwidget_) |
| 748 | return; |
| 749 | ime_control_busy_ = true; |
| 750 | webwidget_->handleCompositionEvent(command, cursor_position, |
| 751 | target_start, target_end, |
| 752 | ime_string); |
| 753 | ime_control_busy_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 754 | } |
| 755 | |
[email protected] | ec7dc11 | 2008-08-06 05:30:12 | [diff] [blame] | 756 | void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) { |
| 757 | // During shutdown we can just ignore this message. |
| 758 | if (!webwidget_) |
| 759 | return; |
| 760 | |
| 761 | set_next_paint_is_repaint_ack(); |
| 762 | gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height()); |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 763 | didInvalidateRect(repaint_rect); |
[email protected] | ec7dc11 | 2008-08-06 05:30:12 | [diff] [blame] | 764 | } |
| 765 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 766 | void RenderWidget::OnSetTextDirection(WebTextDirection direction) { |
[email protected] | 07f95333 | 2009-03-25 04:31:11 | [diff] [blame] | 767 | if (!webwidget_) |
| 768 | return; |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 769 | webwidget_->setTextDirection(direction); |
[email protected] | 07f95333 | 2009-03-25 04:31:11 | [diff] [blame] | 770 | } |
| 771 | |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 772 | void RenderWidget::SetHidden(bool hidden) { |
| 773 | if (is_hidden_ == hidden) |
| 774 | return; |
| 775 | |
| 776 | // The status has changed. Tell the RenderThread about it. |
| 777 | is_hidden_ = hidden; |
| 778 | if (is_hidden_) |
| 779 | render_thread_->WidgetHidden(); |
| 780 | else |
| 781 | render_thread_->WidgetRestored(); |
| 782 | } |
| 783 | |
[email protected] | 699ab0d | 2009-04-23 23:19:14 | [diff] [blame] | 784 | void RenderWidget::SetBackground(const SkBitmap& background) { |
| 785 | background_ = background; |
| 786 | // Generate a full repaint. |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 787 | didInvalidateRect(gfx::Rect(size_.width(), size_.height())); |
[email protected] | 699ab0d | 2009-04-23 23:19:14 | [diff] [blame] | 788 | } |
| 789 | |
[email protected] | 67474193 | 2009-02-04 23:44:46 | [diff] [blame] | 790 | bool RenderWidget::next_paint_is_resize_ack() const { |
| 791 | return ViewHostMsg_PaintRect_Flags::is_resize_ack(next_paint_flags_); |
| 792 | } |
| 793 | |
| 794 | bool RenderWidget::next_paint_is_restore_ack() const { |
| 795 | return ViewHostMsg_PaintRect_Flags::is_restore_ack(next_paint_flags_); |
| 796 | } |
| 797 | |
| 798 | void RenderWidget::set_next_paint_is_resize_ack() { |
| 799 | next_paint_flags_ |= ViewHostMsg_PaintRect_Flags::IS_RESIZE_ACK; |
| 800 | } |
| 801 | |
| 802 | void RenderWidget::set_next_paint_is_restore_ack() { |
| 803 | next_paint_flags_ |= ViewHostMsg_PaintRect_Flags::IS_RESTORE_ACK; |
| 804 | } |
| 805 | |
| 806 | void RenderWidget::set_next_paint_is_repaint_ack() { |
| 807 | next_paint_flags_ |= ViewHostMsg_PaintRect_Flags::IS_REPAINT_ACK; |
| 808 | } |
| 809 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 810 | void RenderWidget::UpdateIME() { |
| 811 | // If a browser process does not have IMEs, its IMEs are not active, or there |
| 812 | // are not any attached widgets. |
| 813 | // a renderer process does not have to retrieve information of the focused |
| 814 | // control or send notification messages to a browser process. |
| 815 | if (!ime_is_active_) { |
| 816 | return; |
| 817 | } |
[email protected] | 34f6bc1 | 2008-11-06 07:40:53 | [diff] [blame] | 818 | // Retrieve the caret position from the focused widget and verify we should |
| 819 | // enabled IMEs attached to the browser process. |
| 820 | bool enable_ime = false; |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 821 | WebRect caret_rect; |
[email protected] | 34f6bc1 | 2008-11-06 07:40:53 | [diff] [blame] | 822 | if (!webwidget_ || |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 823 | !webwidget_->queryCompositionStatus(&enable_ime, &caret_rect)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 824 | // There are not any editable widgets attached to this process. |
| 825 | // We should disable the IME to prevent it from sending CJK strings to |
| 826 | // non-editable widgets. |
| 827 | ime_control_updated_ = true; |
| 828 | ime_control_new_state_ = false; |
| 829 | } |
[email protected] | 9f23f59 | 2008-11-17 08:36:34 | [diff] [blame] | 830 | if (ime_control_new_state_ != enable_ime) { |
| 831 | ime_control_updated_ = true; |
| 832 | ime_control_new_state_ = enable_ime; |
| 833 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 834 | if (ime_control_updated_) { |
| 835 | // The input focus has been changed. |
| 836 | // Compare the current state with the updated state and choose actions. |
| 837 | if (ime_control_enable_ime_) { |
| 838 | if (ime_control_new_state_) { |
| 839 | // Case 1: a text input -> another text input |
| 840 | // Complete the current composition and notify the caret position. |
[email protected] | 34f6bc1 | 2008-11-06 07:40:53 | [diff] [blame] | 841 | Send(new ViewHostMsg_ImeUpdateStatus(routing_id(), |
| 842 | IME_COMPLETE_COMPOSITION, |
| 843 | caret_rect)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 844 | } else { |
| 845 | // Case 2: a text input -> a password input (or a static control) |
| 846 | // Complete the current composition and disable the IME. |
[email protected] | 34f6bc1 | 2008-11-06 07:40:53 | [diff] [blame] | 847 | Send(new ViewHostMsg_ImeUpdateStatus(routing_id(), IME_DISABLE, |
| 848 | caret_rect)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 849 | } |
| 850 | } else { |
| 851 | if (ime_control_new_state_) { |
| 852 | // Case 3: a password input (or a static control) -> a text input |
| 853 | // Enable the IME and notify the caret position. |
[email protected] | 34f6bc1 | 2008-11-06 07:40:53 | [diff] [blame] | 854 | Send(new ViewHostMsg_ImeUpdateStatus(routing_id(), |
| 855 | IME_COMPLETE_COMPOSITION, |
| 856 | caret_rect)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 857 | } else { |
| 858 | // Case 4: a password input (or a static contol) -> another password |
| 859 | // input (or another static control). |
| 860 | // The IME has been already disabled and we don't have to do anything. |
| 861 | } |
| 862 | } |
| 863 | } else { |
| 864 | // The input focus is not changed. |
| 865 | // Notify the caret position to a browser process only if it is changed. |
| 866 | if (ime_control_enable_ime_) { |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 867 | if (caret_rect.x != ime_control_x_ || |
| 868 | caret_rect.y != ime_control_y_) { |
[email protected] | 34f6bc1 | 2008-11-06 07:40:53 | [diff] [blame] | 869 | Send(new ViewHostMsg_ImeUpdateStatus(routing_id(), IME_MOVE_WINDOWS, |
| 870 | caret_rect)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | } |
| 874 | // Save the updated IME status to prevent from sending the same IPC messages. |
| 875 | ime_control_updated_ = false; |
| 876 | ime_control_enable_ime_ = ime_control_new_state_; |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 877 | ime_control_x_ = caret_rect.x; |
| 878 | ime_control_y_ = caret_rect.y; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 879 | } |
| 880 | |
[email protected] | 4873c7d | 2009-07-16 06:36:28 | [diff] [blame] | 881 | WebScreenInfo RenderWidget::screenInfo() { |
| 882 | WebScreenInfo results; |
| 883 | Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results)); |
| 884 | return results; |
| 885 | } |
| 886 | |
[email protected] | f103ab7 | 2009-09-02 17:10:59 | [diff] [blame] | 887 | void RenderWidget::SchedulePluginMove( |
| 888 | const webkit_glue::WebPluginGeometry& move) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 889 | size_t i = 0; |
| 890 | for (; i < plugin_window_moves_.size(); ++i) { |
| 891 | if (plugin_window_moves_[i].window == move.window) { |
[email protected] | 16f89d0 | 2009-08-26 17:17:58 | [diff] [blame] | 892 | if (move.rects_valid) { |
| 893 | plugin_window_moves_[i] = move; |
| 894 | } else { |
| 895 | plugin_window_moves_[i].visible = move.visible; |
| 896 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 897 | break; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | if (i == plugin_window_moves_.size()) |
| 902 | plugin_window_moves_.push_back(move); |
| 903 | } |
[email protected] | 26865477 | 2009-08-06 23:02:04 | [diff] [blame] | 904 | |
| 905 | void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) { |
| 906 | for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin(); |
| 907 | i != plugin_window_moves_.end(); ++i) { |
| 908 | if (i->window == window) { |
| 909 | plugin_window_moves_.erase(i); |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | } |