[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 | |
[email protected] | 05d47875 | 2009-04-08 23:38:16 | [diff] [blame] | 5 | #ifndef CHROME_RENDERER_RENDER_WIDGET_H_ |
| 6 | #define CHROME_RENDERER_RENDER_WIDGET_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
| 8 | #include <vector> |
| 9 | #include "base/basictypes.h" |
[email protected] | 18bcc3c | 2009-01-27 21:39:15 | [diff] [blame] | 10 | #include "base/gfx/native_widget_types.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/gfx/point.h" |
| 12 | #include "base/gfx/rect.h" |
| 13 | #include "base/gfx/size.h" |
| 14 | #include "base/ref_counted.h" |
[email protected] | 67474193 | 2009-02-04 23:44:46 | [diff] [blame] | 15 | #include "base/shared_memory.h" |
[email protected] | 82e5ee8 | 2009-04-03 02:29:45 | [diff] [blame] | 16 | #include "chrome/common/ipc_channel.h" |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 17 | #include "chrome/renderer/render_process.h" |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 18 | #include "skia/ext/platform_canvas.h" |
[email protected] | d5282e7 | 2009-05-13 13:16:52 | [diff] [blame] | 19 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 20 | |
[email protected] | afdcf5c | 2009-05-10 20:30:41 | [diff] [blame] | 21 | #include "webkit/api/public/WebRect.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | #include "webkit/glue/webwidget_delegate.h" |
| 23 | #include "webkit/glue/webcursor.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 25 | class RenderThreadBase; |
[email protected] | 39008c0 | 2009-02-11 23:59:25 | [diff] [blame] | 26 | struct WebPluginGeometry; |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 27 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | // RenderWidget provides a communication bridge between a WebWidget and |
| 29 | // a RenderWidgetHost, the latter of which lives in a different process. |
| 30 | class RenderWidget : public IPC::Channel::Listener, |
| 31 | public IPC::Message::Sender, |
| 32 | virtual public WebWidgetDelegate, |
| 33 | public base::RefCounted<RenderWidget> { |
| 34 | public: |
| 35 | // Creates a new RenderWidget. The opener_id is the routing ID of the |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 36 | // RenderView that this widget lives inside. The render_thread is any |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 37 | // RenderThreadBase implementation, mostly commonly RenderThread::current(). |
[email protected] | 0ebf387 | 2008-11-07 21:35:03 | [diff] [blame] | 38 | static RenderWidget* Create(int32 opener_id, |
| 39 | RenderThreadBase* render_thread, |
[email protected] | cfd727f | 2009-01-09 20:21:11 | [diff] [blame] | 40 | bool activatable); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | |
| 42 | // The routing ID assigned by the RenderProcess. Will be MSG_ROUTING_NONE if |
| 43 | // not yet assigned a view ID, in which case, the process MUST NOT send |
| 44 | // messages with this ID to the parent. |
| 45 | int32 routing_id() const { |
| 46 | return routing_id_; |
| 47 | } |
| 48 | |
| 49 | // May return NULL when the window is closing. |
| 50 | WebWidget* webwidget() const { |
| 51 | return webwidget_; |
| 52 | } |
| 53 | |
| 54 | // Implementing RefCounting required for WebWidgetDelegate |
| 55 | virtual void AddRef() { |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 56 | base::RefCounted<RenderWidget>::AddRef(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | } |
| 58 | virtual void Release() { |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 59 | base::RefCounted<RenderWidget>::Release(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // IPC::Channel::Listener |
| 63 | virtual void OnMessageReceived(const IPC::Message& msg); |
| 64 | |
| 65 | // IPC::Message::Sender |
| 66 | virtual bool Send(IPC::Message* msg); |
| 67 | |
| 68 | // WebWidgetDelegate |
[email protected] | 18bcc3c | 2009-01-27 21:39:15 | [diff] [blame] | 69 | virtual gfx::NativeViewId GetContainingView(WebWidget* webwidget); |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 70 | virtual void DidInvalidateRect(WebWidget* webwidget, |
| 71 | const WebKit::WebRect& rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | virtual void DidScrollRect(WebWidget* webwidget, int dx, int dy, |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 73 | const WebKit::WebRect& clip_rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | virtual void Show(WebWidget* webwidget, WindowOpenDisposition disposition); |
[email protected] | 3f64e5af | 2009-04-10 03:03:09 | [diff] [blame] | 75 | virtual void ShowAsPopupWithItems(WebWidget* webwidget, |
| 76 | const WebKit::WebRect& bounds, |
| 77 | int item_height, |
| 78 | int selected_index, |
| 79 | const std::vector<WebMenuItem>& items); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | virtual void CloseWidgetSoon(WebWidget* webwidget); |
| 81 | virtual void Focus(WebWidget* webwidget); |
| 82 | virtual void Blur(WebWidget* webwidget); |
[email protected] | 3f64e5af | 2009-04-10 03:03:09 | [diff] [blame] | 83 | virtual void SetCursor(WebWidget* webwidget, const WebCursor& cursor); |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 84 | virtual void GetWindowRect(WebWidget* webwidget, WebKit::WebRect* rect); |
| 85 | virtual void SetWindowRect(WebWidget* webwidget, |
| 86 | const WebKit::WebRect& rect); |
| 87 | virtual void GetRootWindowRect(WebWidget* webwidget, WebKit::WebRect* rect); |
| 88 | virtual void GetRootWindowResizerRect(WebWidget* webwidget, |
| 89 | WebKit::WebRect* rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | virtual void DidMove(WebWidget* webwidget, const WebPluginGeometry& move); |
| 91 | virtual void RunModal(WebWidget* webwidget) {} |
[email protected] | 12456fa | 2009-04-01 23:07:19 | [diff] [blame] | 92 | virtual bool IsHidden(WebWidget* webwidget) { return is_hidden_; } |
| 93 | virtual WebKit::WebScreenInfo GetScreenInfo(WebWidget* webwidget); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | |
[email protected] | b4b967e | 2009-04-22 11:33:05 | [diff] [blame] | 95 | // Invalidates entire widget rect to generate a full repaint. |
| 96 | void GenerateFullRepaint(); |
| 97 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | // Close the underlying WebWidget. |
| 99 | void Close(); |
| 100 | |
| 101 | protected: |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 102 | // Friend RefCounted so that the dtor can be non-public. Using this class |
| 103 | // without ref-counting is an error. |
| 104 | friend class base::RefCounted<RenderWidget>; |
| 105 | |
[email protected] | cfd727f | 2009-01-09 20:21:11 | [diff] [blame] | 106 | RenderWidget(RenderThreadBase* render_thread, bool activatable); |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 107 | virtual ~RenderWidget(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | |
| 109 | // Initializes this view with the given opener. CompleteInit must be called |
| 110 | // later. |
| 111 | void Init(int32 opener_id); |
| 112 | |
| 113 | // Finishes creation of a pending view started with Init. |
[email protected] | 18bcc3c | 2009-01-27 21:39:15 | [diff] [blame] | 114 | void CompleteInit(gfx::NativeViewId parent); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | |
[email protected] | 661eb9d | 2009-02-03 02:11:48 | [diff] [blame] | 116 | // Paints the given rectangular region of the WebWidget into canvas (a |
| 117 | // shared memory segment returned by AllocPaintBuf on Windows). The caller |
| 118 | // must ensure that the given rect fits within the bounds of the WebWidget. |
| 119 | void PaintRect(const gfx::Rect& rect, skia::PlatformCanvas* canvas); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | |
| 121 | void DoDeferredPaint(); |
| 122 | void DoDeferredScroll(); |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 123 | void DoDeferredClose(); |
| 124 | void DoDeferredSetWindowRect(const WebKit::WebRect& pos); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | |
[email protected] | 699ab0d | 2009-04-23 23:19:14 | [diff] [blame] | 126 | // Set the background of the render widget to a bitmap. The bitmap will be |
| 127 | // tiled in both directions if it isn't big enough to fill the area. This is |
| 128 | // mainly intended to be used in conjuction with WebView::SetIsTransparent(). |
| 129 | virtual void SetBackground(const SkBitmap& bitmap); |
| 130 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | // RenderWidget IPC message handlers |
| 132 | void OnClose(); |
[email protected] | 18bcc3c | 2009-01-27 21:39:15 | [diff] [blame] | 133 | void OnCreatingNewAck(gfx::NativeViewId parent); |
[email protected] | 30f75e6 | 2009-02-25 22:01:00 | [diff] [blame] | 134 | virtual void OnResize(const gfx::Size& new_size, |
| 135 | const gfx::Rect& resizer_rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | void OnWasHidden(); |
| 137 | void OnWasRestored(bool needs_repainting); |
[email protected] | b7fce1f | 2008-08-14 05:01:07 | [diff] [blame] | 138 | void OnPaintRectAck(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | void OnScrollRectAck(); |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 140 | void OnRequestMoveAck(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | void OnHandleInputEvent(const IPC::Message& message); |
| 142 | void OnMouseCaptureLost(); |
| 143 | void OnSetFocus(bool enable); |
| 144 | void OnImeSetInputMode(bool is_active); |
| 145 | void OnImeSetComposition(int string_type, int cursor_position, |
| 146 | int target_start, int target_end, |
| 147 | const std::wstring& ime_string); |
[email protected] | ec7dc11 | 2008-08-06 05:30:12 | [diff] [blame] | 148 | void OnMsgRepaint(const gfx::Size& size_to_paint); |
[email protected] | 07f95333 | 2009-03-25 04:31:11 | [diff] [blame] | 149 | void OnSetTextDirection(int direction); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
| 151 | // True if a PaintRect_ACK message is pending. |
| 152 | bool paint_reply_pending() const { |
| 153 | return paint_reply_pending_; |
| 154 | } |
| 155 | |
| 156 | // True if a ScrollRect_ACK message is pending. |
| 157 | bool scroll_reply_pending() const { |
| 158 | return current_scroll_buf_ != NULL; |
| 159 | } |
| 160 | |
[email protected] | 67474193 | 2009-02-04 23:44:46 | [diff] [blame] | 161 | bool next_paint_is_resize_ack() const; |
| 162 | bool next_paint_is_restore_ack() const; |
| 163 | void set_next_paint_is_resize_ack(); |
| 164 | void set_next_paint_is_restore_ack(); |
| 165 | void set_next_paint_is_repaint_ack(); |
[email protected] | ec7dc11 | 2008-08-06 05:30:12 | [diff] [blame] | 166 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | // Called when a renderer process moves an input focus or updates the |
| 168 | // position of its caret. |
| 169 | // This function compares them with the previous values, and send them to |
| 170 | // the browser process only if they are updated. |
| 171 | // The browser process moves IME windows and context. |
| 172 | void UpdateIME(); |
| 173 | |
| 174 | // Tells the renderer it does not have focus. Used to prevent us from getting |
| 175 | // the focus on our own when the browser did not focus us. |
| 176 | void ClearFocus(); |
| 177 | |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 178 | // Set the pending window rect. |
| 179 | // Because the real render_widget is hosted in another process, there is |
| 180 | // a time period where we may have set a new window rect which has not yet |
| 181 | // been processed by the browser. So we maintain a pending window rect |
| 182 | // size. If JS code sets the WindowRect, and then immediately calls |
| 183 | // GetWindowRect() we'll use this pending window rect as the size. |
| 184 | void SetPendingWindowRect(const WebKit::WebRect& r); |
| 185 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | // Routing ID that allows us to communicate to the parent browser process |
| 187 | // RenderWidgetHost. When MSG_ROUTING_NONE, no messages may be sent. |
| 188 | int32 routing_id_; |
| 189 | |
[email protected] | c5b3b5e | 2009-02-13 06:41:11 | [diff] [blame] | 190 | // We are responsible for destroying this object via its Close method. |
| 191 | WebWidget* webwidget_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | |
| 193 | // Set to the ID of the view that initiated creating this view, if any. When |
| 194 | // the view was initiated by the browser (the common case), this will be |
| 195 | // MSG_ROUTING_NONE. This is used in determining ownership when opening |
| 196 | // child tabs. See RenderWidget::createWebViewWithRequest. |
| 197 | // |
| 198 | // This ID may refer to an invalid view if that view is closed before this |
| 199 | // view is. |
| 200 | int32 opener_id_; |
| 201 | |
[email protected] | 8085dbc8 | 2008-09-26 22:53:44 | [diff] [blame] | 202 | // The thread that does our IPC. |
| 203 | RenderThreadBase* render_thread_; |
| 204 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | // The position where this view should be initially shown. |
| 206 | gfx::Rect initial_pos_; |
| 207 | |
| 208 | // The window we are embedded within. TODO(darin): kill this. |
[email protected] | 18bcc3c | 2009-01-27 21:39:15 | [diff] [blame] | 209 | gfx::NativeViewId host_window_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | |
| 211 | // We store the current cursor object so we can avoid spamming SetCursor |
| 212 | // messages. |
| 213 | WebCursor current_cursor_; |
| 214 | // The size of the RenderWidget. |
| 215 | gfx::Size size_; |
| 216 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 217 | // Transport DIBs that are currently in use to transfer an image to the |
| 218 | // browser. |
| 219 | TransportDIB* current_paint_buf_; |
| 220 | TransportDIB* current_scroll_buf_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 221 | |
| 222 | // The smallest bounding rectangle that needs to be re-painted. This is non- |
| 223 | // empty if a paint event is pending. |
[email protected] | 2d5d09d5 | 2009-06-15 14:29:21 | [diff] [blame^] | 224 | gfx::Rect paint_rect_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | |
| 226 | // The clip rect for the pending scroll event. This is non-empty if a |
| 227 | // scroll event is pending. |
| 228 | gfx::Rect scroll_rect_; |
| 229 | |
[email protected] | f21c613a | 2009-02-12 14:46:17 | [diff] [blame] | 230 | // The area that must be reserved for drawing the resize corner. |
| 231 | gfx::Rect resizer_rect_; |
| 232 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | // The scroll delta for a pending scroll event. |
| 234 | gfx::Point scroll_delta_; |
| 235 | |
| 236 | // Flags for the next ViewHostMsg_PaintRect message. |
| 237 | int next_paint_flags_; |
| 238 | |
| 239 | // True if we are expecting a PaintRect_ACK message (i.e., that a PaintRect |
| 240 | // message has been sent). |
| 241 | bool paint_reply_pending_; |
| 242 | |
| 243 | // Set to true if we should ignore RenderWidget::Show calls. |
| 244 | bool did_show_; |
| 245 | |
| 246 | // Indicates that we shouldn't bother generated paint events. |
| 247 | bool is_hidden_; |
| 248 | |
| 249 | // Indicates that we should be repainted when restored. This flag is set to |
| 250 | // true if we receive an invalidation / scroll event from webkit while our |
| 251 | // is_hidden_ flag is set to true. This is used to force a repaint once we |
| 252 | // restore to account for the fact that our host would not know about the |
| 253 | // invalidation / scroll event(s) from webkit while we are hidden. |
| 254 | bool needs_repainting_on_restore_; |
| 255 | |
| 256 | // Indicates whether we have been focused/unfocused by the browser. |
| 257 | bool has_focus_; |
| 258 | |
| 259 | // True if we have requested this widget be closed. No more messages will |
| 260 | // be sent, except for a Close. |
| 261 | bool closing_; |
| 262 | |
| 263 | // Represents whether or not the IME of a browser process is active. |
| 264 | bool ime_is_active_; |
| 265 | |
| 266 | // Represents the status of the selected edit control sent to a browser |
| 267 | // process last time. |
| 268 | // When a renderer process finishes rendering a region, it retrieves: |
| 269 | // * The identifier of the selected edit control; |
| 270 | // * Whether or not the selected edit control requires IME, and; |
| 271 | // * The position of the caret (or cursor). |
| 272 | // If the above values is updated, a renderer process sends an IPC message |
| 273 | // to a browser process. A browser process uses these values to |
| 274 | // activate/deactivate IME and set the position of IME windows. |
| 275 | bool ime_control_enable_ime_; |
| 276 | int ime_control_x_; |
| 277 | int ime_control_y_; |
| 278 | bool ime_control_new_state_; |
| 279 | bool ime_control_updated_; |
[email protected] | 9f23f59 | 2008-11-17 08:36:34 | [diff] [blame] | 280 | bool ime_control_busy_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 281 | |
[email protected] | cfd727f | 2009-01-09 20:21:11 | [diff] [blame] | 282 | // Whether the window for this RenderWidget can be activated. |
| 283 | bool activatable_; |
[email protected] | 0ebf387 | 2008-11-07 21:35:03 | [diff] [blame] | 284 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 285 | // Holds all the needed plugin window moves for a scroll. |
| 286 | std::vector<WebPluginGeometry> plugin_window_moves_; |
| 287 | |
[email protected] | 699ab0d | 2009-04-23 23:19:14 | [diff] [blame] | 288 | // A custom background for the widget. |
| 289 | SkBitmap background_; |
| 290 | |
[email protected] | 2533ce1 | 2009-05-09 00:02:24 | [diff] [blame] | 291 | // While we are waiting for the browser to update window sizes, |
| 292 | // we track the pending size temporarily. |
| 293 | int pending_window_rect_count_; |
| 294 | WebKit::WebRect pending_window_rect_; |
| 295 | |
[email protected] | 05d47875 | 2009-04-08 23:38:16 | [diff] [blame] | 296 | DISALLOW_COPY_AND_ASSIGN(RenderWidget); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | }; |
| 298 | |
[email protected] | 05d47875 | 2009-04-08 23:38:16 | [diff] [blame] | 299 | #endif // CHROME_RENDERER_RENDER_WIDGET_H_ |