render_widget_host.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_PUBLIC_BROWSER_RENDER_WIDGET_HOST_H_
6#define CONTENT_PUBLIC_BROWSER_RENDER_WIDGET_HOST_H_
7
8#include "base/callback.h"
9#include "content/common/content_export.h"
10#include "content/public/browser/keyboard_listener.h"
11#include "content/public/browser/native_web_keyboard_event.h"
12#include "ipc/ipc_channel.h"
13#include "ipc/ipc_sender.h"
14#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
15#include "third_party/WebKit/Source/WebKit/chromium/public/WebTextDirection.h"
16#include "ui/gfx/size.h"
17#include "ui/surface/transport_dib.h"
18
19#if defined(TOOLKIT_GTK)
20#include "ui/base/x/x11_util.h"
21#elif defined(OS_MACOSX)
22#include "skia/ext/platform_device.h"
23#endif
24
25namespace gfx {
26class Rect;
27}
28
29namespace skia {
30class PlatformBitmap;
31}
32
33namespace content {
34
35class RenderProcessHost;
36class RenderWidgetHostImpl;
37class RenderWidgetHostView;
38
39// A RenderWidgetHost manages the browser side of a browser<->renderer
40// HWND connection.  The HWND lives in the browser process, and
41// windows events are sent over IPC to the corresponding object in the
42// renderer.  The renderer paints into shared memory, which we
43// transfer to a backing store and blit to the screen when Windows
44// sends us a WM_PAINT message.
45//
46// How Shutdown Works
47//
48// There are two situations in which this object, a RenderWidgetHost, can be
49// instantiated:
50//
51// 1. By a WebContents as the communication conduit for a rendered web page.
52//    The WebContents instantiates a derived class: RenderViewHost.
53// 2. By a WebContents as the communication conduit for a select widget. The
54//    WebContents instantiates the RenderWidgetHost directly.
55//
56// For every WebContents there are several objects in play that need to be
57// properly destroyed or cleaned up when certain events occur.
58//
59// - WebContents - the WebContents itself, and its associated HWND.
60// - RenderViewHost - representing the communication conduit with the child
61//   process.
62// - RenderWidgetHostView - the view of the web page content, message handler,
63//   and plugin root.
64//
65// Normally, the WebContents contains a child RenderWidgetHostView that renders
66// the contents of the loaded page. It has a WS_CLIPCHILDREN style so that it
67// does no painting of its own.
68//
69// The lifetime of the RenderWidgetHostView is tied to the render process. If
70// the render process dies, the RenderWidgetHostView goes away and all
71// references to it must become NULL.
72//
73// RenderViewHost (a RenderWidgetHost subclass) is the conduit used to
74// communicate with the RenderView and is owned by the WebContents. If the
75// render process crashes, the RenderViewHost remains and restarts the render
76// process if needed to continue navigation.
77//
78// Some examples of how shutdown works:
79//
80// For a WebContents, its Destroy method tells the RenderViewHost to
81// shut down the render process and die.
82//
83// When the render process is destroyed it destroys the View: the
84// RenderWidgetHostView, which destroys its HWND and deletes that object.
85//
86// For select popups, the situation is a little different. The RenderWidgetHost
87// associated with the select popup owns the view and itself (is responsible
88// for destroying itself when the view is closed). The WebContents's only
89// responsibility is to select popups is to create them when it is told to. When
90// the View is destroyed via an IPC message (for when WebCore destroys the
91// popup, e.g. if the user selects one of the options), or because
92// WM_CANCELMODE is received by the view, the View schedules the destruction of
93// the render process. However in this case since there's no WebContents
94// container, when the render process is destroyed, the RenderWidgetHost just
95// deletes itself, which is safe because no one else should have any references
96// to it (the WebContents does not).
97//
98// It should be noted that the RenderViewHost, not the RenderWidgetHost,
99// handles IPC messages relating to the render process going away, since the
100// way a RenderViewHost (WebContents) handles the process dying is different to
101// the way a select popup does. As such the RenderWidgetHostView handles these
102// messages for select popups. This placement is more out of convenience than
103// anything else. When the view is live, these messages are forwarded to it by
104// the RenderWidgetHost's IPC message map.
105class CONTENT_EXPORT RenderWidgetHost : public IPC::Sender {
106 public:
107  // Free all backing stores used for rendering to drop memory usage.
108  static void RemoveAllBackingStores();
109
110  // Returns the size of all the backing stores used for rendering
111  static size_t BackingStoreMemorySize();
112
113  virtual ~RenderWidgetHost() {}
114
115  // Edit operations.
116  virtual void Undo() = 0;
117  virtual void Redo() = 0;
118  virtual void Cut() = 0;
119  virtual void Copy() = 0;
120  virtual void CopyToFindPboard() = 0;
121  virtual void Paste() = 0;
122  virtual void PasteAndMatchStyle() = 0;
123  virtual void Delete() = 0;
124  virtual void SelectAll() = 0;
125
126  // Update the text direction of the focused input element and notify it to a
127  // renderer process.
128  // These functions have two usage scenarios: changing the text direction
129  // from a menu (as Safari does), and; changing the text direction when a user
130  // presses a set of keys (as IE and Firefox do).
131  // 1. Change the text direction from a menu.
132  // In this scenario, we receive a menu event only once and we should update
133  // the text direction immediately when a user chooses a menu item. So, we
134  // should call both functions at once as listed in the following snippet.
135  //   void RenderViewHost::SetTextDirection(WebTextDirection direction) {
136  //     UpdateTextDirection(direction);
137  //     NotifyTextDirection();
138  //   }
139  // 2. Change the text direction when pressing a set of keys.
140  // Because of auto-repeat, we may receive the same key-press event many
141  // times while we presses the keys and it is nonsense to send the same IPC
142  // message every time when we receive a key-press event.
143  // To suppress the number of IPC messages, we just update the text direction
144  // when receiving a key-press event and send an IPC message when we release
145  // the keys as listed in the following snippet.
146  //   if (key_event.type == WebKeyboardEvent::KEY_DOWN) {
147  //     if (key_event.windows_key_code == 'A' &&
148  //         key_event.modifiers == WebKeyboardEvent::CTRL_KEY) {
149  //       UpdateTextDirection(dir);
150  //     } else {
151  //       CancelUpdateTextDirection();
152  //     }
153  //   } else if (key_event.type == WebKeyboardEvent::KEY_UP) {
154  //     NotifyTextDirection();
155  //   }
156  // Once we cancel updating the text direction, we have to ignore all
157  // succeeding UpdateTextDirection() requests until calling
158  // NotifyTextDirection(). (We may receive keydown events even after we
159  // canceled updating the text direction because of auto-repeat.)
160  // Note: we cannot undo this change for compatibility with Firefox and IE.
161  virtual void UpdateTextDirection(WebKit::WebTextDirection direction) = 0;
162  virtual void NotifyTextDirection() = 0;
163
164  virtual void Blur() = 0;
165
166  // Copies the given subset of the backing store into the given (uninitialized)
167  // PlatformCanvas. If |src_rect| is empty, the whole contents is copied.
168  // If non empty |accelerated_dest_size| is given and accelerated compositing
169  // is active, the content is shrinked so that it fits in
170  // |accelerated_dest_size|. If |accelerated_dest_size| is larger than the
171  // contens size, the content is not resized. If |accelerated_dest_size| is
172  // empty, the size copied from the source contents is used.
173  // |callback| is invoked with true on success, false otherwise. |output| can
174  // be initialized even on failure.
175  // NOTE: |callback| is called synchronously if the backing store is available.
176  // When accelerated compositing is active, it is called asynchronously on Aura
177  // and synchronously on the other platforms.
178  virtual void CopyFromBackingStore(const gfx::Rect& src_rect,
179                                    const gfx::Size& accelerated_dest_size,
180                                    const base::Callback<void(bool)>& callback,
181                                    skia::PlatformBitmap* output) = 0;
182#if defined(TOOLKIT_GTK)
183  // Paint the backing store into the target's |dest_rect|.
184  virtual bool CopyFromBackingStoreToGtkWindow(const gfx::Rect& dest_rect,
185                                               GdkWindow* target) = 0;
186#elif defined(OS_MACOSX)
187  virtual gfx::Size GetBackingStoreSize() = 0;
188  virtual bool CopyFromBackingStoreToCGContext(const CGRect& dest_rect,
189                                               CGContextRef target) = 0;
190#endif
191
192  // Send a command to the renderer to turn on full accessibility.
193  virtual void EnableFullAccessibilityMode() = 0;
194
195  // Forwards the given message to the renderer. These are called by
196  // the view when it has received a message.
197  virtual void ForwardMouseEvent(
198      const WebKit::WebMouseEvent& mouse_event) = 0;
199  virtual void ForwardWheelEvent(
200      const WebKit::WebMouseWheelEvent& wheel_event) = 0;
201  virtual void ForwardKeyboardEvent(
202      const NativeWebKeyboardEvent& key_event) = 0;
203
204  virtual const gfx::Vector2d& GetLastScrollOffset() const = 0;
205
206  virtual RenderProcessHost* GetProcess() const = 0;
207
208  virtual int GetRoutingID() const = 0;
209
210  // Gets the View of this RenderWidgetHost. Can be NULL, e.g. if the
211  // RenderWidget is being destroyed or the render process crashed. You should
212  // never cache this pointer since it can become NULL if the renderer crashes,
213  // instead you should always ask for it using the accessor.
214  virtual RenderWidgetHostView* GetView() const = 0;
215
216  // Returns true if the renderer is loading, false if not.
217  virtual bool IsLoading() const = 0;
218
219  // Returns true if this is a RenderViewHost, false if not.
220  virtual bool IsRenderView() const = 0;
221
222  // This tells the renderer to paint into a bitmap and return it,
223  // regardless of whether the tab is hidden or not.  It resizes the
224  // web widget to match the |page_size| and then returns the bitmap
225  // scaled so it matches the |desired_size|, so that the scaling
226  // happens on the rendering thread.  When the bitmap is ready, the
227  // renderer sends a PaintAtSizeACK to this host, and a
228  // RENDER_WIDGET_HOST_DID_RECEIVE_PAINT_AT_SIZE_ACK notification is issued.
229  // Note that this bypasses most of the update logic that is normally invoked,
230  // and doesn't put the results into the backing store.
231  virtual void PaintAtSize(TransportDIB::Handle dib_handle,
232                           int tag,
233                           const gfx::Size& page_size,
234                           const gfx::Size& desired_size) = 0;
235
236  // Makes an IPC call to tell webkit to replace the currently selected word
237  // or a word around the cursor.
238  virtual void Replace(const string16& word) = 0;
239
240  // Called to notify the RenderWidget that the resize rect has changed without
241  // the size of the RenderWidget itself changing.
242  virtual void ResizeRectChanged(const gfx::Rect& new_rect) = 0;
243
244  // Restart the active hang monitor timeout. Clears all existing timeouts and
245  // starts with a new one.  This can be because the renderer has become
246  // active, the tab is being hidden, or the user has chosen to wait some more
247  // to give the tab a chance to become active and we don't want to display a
248  // warning too soon.
249  virtual void RestartHangMonitorTimeout() = 0;
250
251  virtual void SetIgnoreInputEvents(bool ignore_input_events) = 0;
252
253  // Stops loading the page.
254  virtual void Stop() = 0;
255
256  // Called to notify the RenderWidget that it has been resized.
257  virtual void WasResized() = 0;
258
259  // Access to the implementation's IPC::Listener::OnMessageReceived. Intended
260  // only for test code.
261
262  // Add a keyboard listener that can handle key presses without requiring
263  // focus.
264  virtual void AddKeyboardListener(KeyboardListener* listener) = 0;
265
266  // Remove a keyboard listener.
267  virtual void RemoveKeyboardListener(KeyboardListener* listener) = 0;
268
269  // Update the device scale factor.
270  virtual void SetDeviceScaleFactor(float scale) = 0;
271
272 protected:
273  friend class RenderWidgetHostImpl;
274
275  // Retrieves the implementation class.  Intended only for code
276  // within content/.  This method is necessary because
277  // RenderWidgetHost is the root of a diamond inheritance pattern, so
278  // subclasses inherit it virtually, which removes our ability to
279  // static_cast to the subclass.
280  virtual RenderWidgetHostImpl* AsRenderWidgetHostImpl() = 0;
281};
282
283}  // namespace content
284
285#endif  // CONTENT_PUBLIC_BROWSER_RENDER_WIDGET_HOST_H_
286