Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / browser / render_widget_host.h
bloba937f87e3f249fa9516ed37c475b700eb61f43d0
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.
5 #ifndef CONTENT_PUBLIC_BROWSER_RENDER_WIDGET_HOST_H_
6 #define CONTENT_PUBLIC_BROWSER_RENDER_WIDGET_HOST_H_
8 #include "base/callback.h"
9 #include "content/common/content_export.h"
10 #include "content/public/browser/native_web_keyboard_event.h"
11 #include "content/public/browser/readback_types.h"
12 #include "ipc/ipc_channel.h"
13 #include "ipc/ipc_sender.h"
14 #include "third_party/WebKit/public/web/WebInputEvent.h"
15 #include "third_party/WebKit/public/web/WebTextDirection.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/gfx/geometry/size.h"
18 #include "ui/surface/transport_dib.h"
20 #if defined(OS_MACOSX)
21 #include "skia/ext/platform_device.h"
22 #endif
24 class SkBitmap;
26 namespace gfx {
27 class Rect;
30 namespace blink {
31 class WebMouseEvent;
32 struct WebScreenInfo;
35 namespace content {
37 class RenderProcessHost;
38 class RenderWidgetHostImpl;
39 class RenderWidgetHostIterator;
40 class RenderWidgetHostView;
42 // A RenderWidgetHost manages the browser side of a browser<->renderer
43 // HWND connection. The HWND lives in the browser process, and
44 // windows events are sent over IPC to the corresponding object in the
45 // renderer. The renderer paints into shared memory, which we
46 // transfer to a backing store and blit to the screen when Windows
47 // sends us a WM_PAINT message.
49 // How Shutdown Works
51 // There are two situations in which this object, a RenderWidgetHost, can be
52 // instantiated:
54 // 1. By a WebContents as the communication conduit for a rendered web page.
55 // The WebContents instantiates a derived class: RenderViewHost.
56 // 2. By a WebContents as the communication conduit for a select widget. The
57 // WebContents instantiates the RenderWidgetHost directly.
59 // For every WebContents there are several objects in play that need to be
60 // properly destroyed or cleaned up when certain events occur.
62 // - WebContents - the WebContents itself, and its associated HWND.
63 // - RenderViewHost - representing the communication conduit with the child
64 // process.
65 // - RenderWidgetHostView - the view of the web page content, message handler,
66 // and plugin root.
68 // Normally, the WebContents contains a child RenderWidgetHostView that renders
69 // the contents of the loaded page. It has a WS_CLIPCHILDREN style so that it
70 // does no painting of its own.
72 // The lifetime of the RenderWidgetHostView is tied to the render process. If
73 // the render process dies, the RenderWidgetHostView goes away and all
74 // references to it must become nullptr.
76 // RenderViewHost (a RenderWidgetHost subclass) is the conduit used to
77 // communicate with the RenderView and is owned by the WebContents. If the
78 // render process crashes, the RenderViewHost remains and restarts the render
79 // process if needed to continue navigation.
81 // Some examples of how shutdown works:
83 // For a WebContents, its Destroy method tells the RenderViewHost to
84 // shut down the render process and die.
86 // When the render process is destroyed it destroys the View: the
87 // RenderWidgetHostView, which destroys its HWND and deletes that object.
89 // For select popups, the situation is a little different. The RenderWidgetHost
90 // associated with the select popup owns the view and itself (is responsible
91 // for destroying itself when the view is closed). The WebContents's only
92 // responsibility is to select popups is to create them when it is told to. When
93 // the View is destroyed via an IPC message (for when WebCore destroys the
94 // popup, e.g. if the user selects one of the options), or because
95 // WM_CANCELMODE is received by the view, the View schedules the destruction of
96 // the render process. However in this case since there's no WebContents
97 // container, when the render process is destroyed, the RenderWidgetHost just
98 // deletes itself, which is safe because no one else should have any references
99 // to it (the WebContents does not).
101 // It should be noted that the RenderViewHost, not the RenderWidgetHost,
102 // handles IPC messages relating to the render process going away, since the
103 // way a RenderViewHost (WebContents) handles the process dying is different to
104 // the way a select popup does. As such the RenderWidgetHostView handles these
105 // messages for select popups. This placement is more out of convenience than
106 // anything else. When the view is live, these messages are forwarded to it by
107 // the RenderWidgetHost's IPC message map.
108 class CONTENT_EXPORT RenderWidgetHost : public IPC::Sender {
109 public:
110 // Returns the RenderWidgetHost given its ID and the ID of its render process.
111 // Returns nullptr if the IDs do not correspond to a live RenderWidgetHost.
112 static RenderWidgetHost* FromID(int32 process_id, int32 routing_id);
114 // Returns an iterator to iterate over the global list of active render widget
115 // hosts.
116 static scoped_ptr<RenderWidgetHostIterator> GetRenderWidgetHosts();
118 ~RenderWidgetHost() override {}
120 // Update the text direction of the focused input element and notify it to a
121 // renderer process.
122 // These functions have two usage scenarios: changing the text direction
123 // from a menu (as Safari does), and; changing the text direction when a user
124 // presses a set of keys (as IE and Firefox do).
125 // 1. Change the text direction from a menu.
126 // In this scenario, we receive a menu event only once and we should update
127 // the text direction immediately when a user chooses a menu item. So, we
128 // should call both functions at once as listed in the following snippet.
129 // void RenderViewHost::SetTextDirection(WebTextDirection direction) {
130 // UpdateTextDirection(direction);
131 // NotifyTextDirection();
132 // }
133 // 2. Change the text direction when pressing a set of keys.
134 // Because of auto-repeat, we may receive the same key-press event many
135 // times while we presses the keys and it is nonsense to send the same IPC
136 // message every time when we receive a key-press event.
137 // To suppress the number of IPC messages, we just update the text direction
138 // when receiving a key-press event and send an IPC message when we release
139 // the keys as listed in the following snippet.
140 // if (key_event.type == WebKeyboardEvent::KEY_DOWN) {
141 // if (key_event.windows_key_code == 'A' &&
142 // key_event.modifiers == WebKeyboardEvent::CTRL_KEY) {
143 // UpdateTextDirection(dir);
144 // } else {
145 // CancelUpdateTextDirection();
146 // }
147 // } else if (key_event.type == WebKeyboardEvent::KEY_UP) {
148 // NotifyTextDirection();
149 // }
150 // Once we cancel updating the text direction, we have to ignore all
151 // succeeding UpdateTextDirection() requests until calling
152 // NotifyTextDirection(). (We may receive keydown events even after we
153 // canceled updating the text direction because of auto-repeat.)
154 // Note: we cannot undo this change for compatibility with Firefox and IE.
155 virtual void UpdateTextDirection(blink::WebTextDirection direction) = 0;
156 virtual void NotifyTextDirection() = 0;
158 virtual void Focus() = 0;
159 virtual void Blur() = 0;
161 // Sets whether the renderer should show controls in an active state. On all
162 // platforms except mac, that's the same as focused. On mac, the frontmost
163 // window will show active controls even if the focus is not in the web
164 // contents, but e.g. in the omnibox.
165 virtual void SetActive(bool active) = 0;
167 // Copies the given subset of the backing store, and passes the result as a
168 // bitmap to a callback.
170 // If |src_rect| is empty, the whole contents is copied. If non empty
171 // |accelerated_dst_size| is given and accelerated compositing is active, the
172 // content is shrunk so that it fits in |accelerated_dst_size|. If
173 // |accelerated_dst_size| is larger than the content size, the content is not
174 // resized. If |accelerated_dst_size| is empty, the size copied from the
175 // source contents is used. |callback| is invoked with true on success, false
176 // otherwise, along with a SkBitmap containing the copied pixel data.
178 // NOTE: |callback| is called synchronously if the backing store is available.
179 // When accelerated compositing is active, |callback| may be called
180 // asynchronously.
181 virtual void CopyFromBackingStore(const gfx::Rect& src_rect,
182 const gfx::Size& accelerated_dst_size,
183 ReadbackRequestCallback& callback,
184 const SkColorType color_type) = 0;
185 // Ensures that the view does not drop the backing store even when hidden.
186 virtual bool CanCopyFromBackingStore() = 0;
187 #if defined(OS_ANDROID)
188 virtual void LockBackingStore() = 0;
189 virtual void UnlockBackingStore() = 0;
190 #endif
192 // Forwards the given message to the renderer. These are called by
193 // the view when it has received a message.
194 virtual void ForwardMouseEvent(
195 const blink::WebMouseEvent& mouse_event) = 0;
196 virtual void ForwardWheelEvent(
197 const blink::WebMouseWheelEvent& wheel_event) = 0;
198 virtual void ForwardKeyboardEvent(
199 const NativeWebKeyboardEvent& key_event) = 0;
201 virtual RenderProcessHost* GetProcess() const = 0;
203 virtual int GetRoutingID() const = 0;
205 // Gets the View of this RenderWidgetHost. Can be nullptr, e.g. if the
206 // RenderWidget is being destroyed or the render process crashed. You should
207 // never cache this pointer since it can become nullptr if the renderer
208 // crashes, instead you should always ask for it using the accessor.
209 virtual RenderWidgetHostView* GetView() const = 0;
211 // Returns true if the renderer is loading, false if not.
212 virtual bool IsLoading() const = 0;
214 // Returns true if this is a RenderViewHost, false if not.
215 virtual bool IsRenderView() const = 0;
217 // Called to notify the RenderWidget that the resize rect has changed without
218 // the size of the RenderWidget itself changing.
219 virtual void ResizeRectChanged(const gfx::Rect& new_rect) = 0;
221 // Restart the active hang monitor timeout. Clears all existing timeouts and
222 // starts with a new one. This can be because the renderer has become
223 // active, the tab is being hidden, or the user has chosen to wait some more
224 // to give the tab a chance to become active and we don't want to display a
225 // warning too soon.
226 virtual void RestartHangMonitorTimeout() = 0;
228 virtual void SetIgnoreInputEvents(bool ignore_input_events) = 0;
230 // Called to notify the RenderWidget that it has been resized.
231 virtual void WasResized() = 0;
233 // Access to the implementation's IPC::Listener::OnMessageReceived. Intended
234 // only for test code.
236 // Add/remove a callback that can handle key presses without requiring focus.
237 typedef base::Callback<bool(const NativeWebKeyboardEvent&)>
238 KeyPressEventCallback;
239 virtual void AddKeyPressEventCallback(
240 const KeyPressEventCallback& callback) = 0;
241 virtual void RemoveKeyPressEventCallback(
242 const KeyPressEventCallback& callback) = 0;
244 // Add/remove a callback that can handle all kinds of mouse events.
245 typedef base::Callback<bool(const blink::WebMouseEvent&)> MouseEventCallback;
246 virtual void AddMouseEventCallback(const MouseEventCallback& callback) = 0;
247 virtual void RemoveMouseEventCallback(const MouseEventCallback& callback) = 0;
249 // Get the screen info corresponding to this render widget.
250 virtual void GetWebScreenInfo(blink::WebScreenInfo* result) = 0;
252 virtual SkColorType PreferredReadbackFormat() = 0;
254 protected:
255 friend class RenderWidgetHostImpl;
257 // Retrieves the implementation class. Intended only for code
258 // within content/. This method is necessary because
259 // RenderWidgetHost is the root of a diamond inheritance pattern, so
260 // subclasses inherit it virtually, which removes our ability to
261 // static_cast to the subclass.
262 virtual RenderWidgetHostImpl* AsRenderWidgetHostImpl() = 0;
265 } // namespace content
267 #endif // CONTENT_PUBLIC_BROWSER_RENDER_WIDGET_HOST_H_