render_widget_host.h revision a02191e04bc25c4935f804f2c080ae28663d096d
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/native_web_keyboard_event.h" 11#include "ipc/ipc_channel.h" 12#include "ipc/ipc_sender.h" 13#include "third_party/WebKit/public/web/WebInputEvent.h" 14#include "third_party/WebKit/public/web/WebTextDirection.h" 15#include "third_party/skia/include/core/SkBitmap.h" 16#include "ui/gfx/size.h" 17#include "ui/surface/transport_dib.h" 18 19#if defined(OS_MACOSX) 20#include "skia/ext/platform_device.h" 21#endif 22 23class SkBitmap; 24 25namespace gfx { 26class Rect; 27} 28 29namespace blink { 30class WebMouseEvent; 31struct WebScreenInfo; 32} 33 34namespace content { 35 36class RenderProcessHost; 37class RenderWidgetHostImpl; 38class RenderWidgetHostIterator; 39class RenderWidgetHostView; 40 41// A RenderWidgetHost manages the browser side of a browser<->renderer 42// HWND connection. The HWND lives in the browser process, and 43// windows events are sent over IPC to the corresponding object in the 44// renderer. The renderer paints into shared memory, which we 45// transfer to a backing store and blit to the screen when Windows 46// sends us a WM_PAINT message. 47// 48// How Shutdown Works 49// 50// There are two situations in which this object, a RenderWidgetHost, can be 51// instantiated: 52// 53// 1. By a WebContents as the communication conduit for a rendered web page. 54// The WebContents instantiates a derived class: RenderViewHost. 55// 2. By a WebContents as the communication conduit for a select widget. The 56// WebContents instantiates the RenderWidgetHost directly. 57// 58// For every WebContents there are several objects in play that need to be 59// properly destroyed or cleaned up when certain events occur. 60// 61// - WebContents - the WebContents itself, and its associated HWND. 62// - RenderViewHost - representing the communication conduit with the child 63// process. 64// - RenderWidgetHostView - the view of the web page content, message handler, 65// and plugin root. 66// 67// Normally, the WebContents contains a child RenderWidgetHostView that renders 68// the contents of the loaded page. It has a WS_CLIPCHILDREN style so that it 69// does no painting of its own. 70// 71// The lifetime of the RenderWidgetHostView is tied to the render process. If 72// the render process dies, the RenderWidgetHostView goes away and all 73// references to it must become NULL. 74// 75// RenderViewHost (a RenderWidgetHost subclass) is the conduit used to 76// communicate with the RenderView and is owned by the WebContents. If the 77// render process crashes, the RenderViewHost remains and restarts the render 78// process if needed to continue navigation. 79// 80// Some examples of how shutdown works: 81// 82// For a WebContents, its Destroy method tells the RenderViewHost to 83// shut down the render process and die. 84// 85// When the render process is destroyed it destroys the View: the 86// RenderWidgetHostView, which destroys its HWND and deletes that object. 87// 88// For select popups, the situation is a little different. The RenderWidgetHost 89// associated with the select popup owns the view and itself (is responsible 90// for destroying itself when the view is closed). The WebContents's only 91// responsibility is to select popups is to create them when it is told to. When 92// the View is destroyed via an IPC message (for when WebCore destroys the 93// popup, e.g. if the user selects one of the options), or because 94// WM_CANCELMODE is received by the view, the View schedules the destruction of 95// the render process. However in this case since there's no WebContents 96// container, when the render process is destroyed, the RenderWidgetHost just 97// deletes itself, which is safe because no one else should have any references 98// to it (the WebContents does not). 99// 100// It should be noted that the RenderViewHost, not the RenderWidgetHost, 101// handles IPC messages relating to the render process going away, since the 102// way a RenderViewHost (WebContents) handles the process dying is different to 103// the way a select popup does. As such the RenderWidgetHostView handles these 104// messages for select popups. This placement is more out of convenience than 105// anything else. When the view is live, these messages are forwarded to it by 106// the RenderWidgetHost's IPC message map. 107class CONTENT_EXPORT RenderWidgetHost : public IPC::Sender { 108 public: 109 // Returns the size of all the backing stores used for rendering 110 static size_t BackingStoreMemorySize(); 111 112 // Returns the RenderWidgetHost given its ID and the ID of its render process. 113 // Returns NULL if the IDs do not correspond to a live RenderWidgetHost. 114 static RenderWidgetHost* FromID(int32 process_id, int32 routing_id); 115 116 // Returns an iterator to iterate over the global list of active render widget 117 // hosts. 118 static scoped_ptr<RenderWidgetHostIterator> GetRenderWidgetHosts(); 119 120 virtual ~RenderWidgetHost() {} 121 122 // Update the text direction of the focused input element and notify it to a 123 // renderer process. 124 // These functions have two usage scenarios: changing the text direction 125 // from a menu (as Safari does), and; changing the text direction when a user 126 // presses a set of keys (as IE and Firefox do). 127 // 1. Change the text direction from a menu. 128 // In this scenario, we receive a menu event only once and we should update 129 // the text direction immediately when a user chooses a menu item. So, we 130 // should call both functions at once as listed in the following snippet. 131 // void RenderViewHost::SetTextDirection(WebTextDirection direction) { 132 // UpdateTextDirection(direction); 133 // NotifyTextDirection(); 134 // } 135 // 2. Change the text direction when pressing a set of keys. 136 // Because of auto-repeat, we may receive the same key-press event many 137 // times while we presses the keys and it is nonsense to send the same IPC 138 // message every time when we receive a key-press event. 139 // To suppress the number of IPC messages, we just update the text direction 140 // when receiving a key-press event and send an IPC message when we release 141 // the keys as listed in the following snippet. 142 // if (key_event.type == WebKeyboardEvent::KEY_DOWN) { 143 // if (key_event.windows_key_code == 'A' && 144 // key_event.modifiers == WebKeyboardEvent::CTRL_KEY) { 145 // UpdateTextDirection(dir); 146 // } else { 147 // CancelUpdateTextDirection(); 148 // } 149 // } else if (key_event.type == WebKeyboardEvent::KEY_UP) { 150 // NotifyTextDirection(); 151 // } 152 // Once we cancel updating the text direction, we have to ignore all 153 // succeeding UpdateTextDirection() requests until calling 154 // NotifyTextDirection(). (We may receive keydown events even after we 155 // canceled updating the text direction because of auto-repeat.) 156 // Note: we cannot undo this change for compatibility with Firefox and IE. 157 virtual void UpdateTextDirection(blink::WebTextDirection direction) = 0; 158 virtual void NotifyTextDirection() = 0; 159 160 virtual void Focus() = 0; 161 virtual void Blur() = 0; 162 163 // Sets whether the renderer should show controls in an active state. On all 164 // platforms except mac, that's the same as focused. On mac, the frontmost 165 // window will show active controls even if the focus is not in the web 166 // contents, but e.g. in the omnibox. 167 virtual void SetActive(bool active) = 0; 168 169 // Copies the given subset of the backing store, and passes the result as a 170 // bitmap to a callback. 171 // 172 // If |src_rect| is empty, the whole contents is copied. If non empty 173 // |accelerated_dst_size| is given and accelerated compositing is active, the 174 // content is shrunk so that it fits in |accelerated_dst_size|. If 175 // |accelerated_dst_size| is larger than the content size, the content is not 176 // resized. If |accelerated_dst_size| is empty, the size copied from the 177 // source contents is used. |callback| is invoked with true on success, false 178 // otherwise, along with a SkBitmap containing the copied pixel data. 179 // 180 // NOTE: |callback| is called synchronously if the backing store is available. 181 // When accelerated compositing is active, |callback| may be called 182 // asynchronously. 183 virtual void CopyFromBackingStore( 184 const gfx::Rect& src_rect, 185 const gfx::Size& accelerated_dst_size, 186 const base::Callback<void(bool, const SkBitmap&)>& callback, 187 const SkBitmap::Config& bitmap_config) = 0; 188 // Ensures that the view does not drop the backing store even when hidden. 189 virtual bool CanCopyFromBackingStore() = 0; 190#if defined(OS_ANDROID) 191 virtual void LockBackingStore() = 0; 192 virtual void UnlockBackingStore() = 0; 193#endif 194#if defined(OS_MACOSX) 195 virtual gfx::Size GetBackingStoreSize() = 0; 196 virtual bool CopyFromBackingStoreToCGContext(const CGRect& dest_rect, 197 CGContextRef target) = 0; 198#endif 199 200 // Send a command to the renderer to turn on full accessibility. 201 virtual void EnableFullAccessibilityMode() = 0; 202 203 // Check whether this RenderWidget has full accessibility mode. 204 virtual bool IsFullAccessibilityModeForTesting() = 0; 205 206 // Send a command to the renderer to turn on tree only accessibility. 207 virtual void EnableTreeOnlyAccessibilityMode() = 0; 208 209 // Check whether this RenderWidget has tree-only accessibility mode. 210 virtual bool IsTreeOnlyAccessibilityModeForTesting() = 0; 211 212 // Relay a request from assistive technology to perform the default action 213 // on a given node. 214 virtual void AccessibilityDoDefaultAction(int object_id) = 0; 215 216 // Relay a request from assistive technology to set focus to a given node. 217 virtual void AccessibilitySetFocus(int object_id) = 0; 218 219 // Relay a request from assistive technology to make a given object 220 // visible by scrolling as many scrollable containers as necessary. 221 // In addition, if it's not possible to make the entire object visible, 222 // scroll so that the |subfocus| rect is visible at least. The subfocus 223 // rect is in local coordinates of the object itself. 224 virtual void AccessibilityScrollToMakeVisible( 225 int acc_obj_id, gfx::Rect subfocus) = 0; 226 227 // Relay a request from assistive technology to move a given object 228 // to a specific location, in the WebContents area coordinate space, i.e. 229 // (0, 0) is the top-left corner of the WebContents. 230 virtual void AccessibilityScrollToPoint(int acc_obj_id, gfx::Point point) = 0; 231 232 // Relay a request from assistive technology to set text selection. 233 virtual void AccessibilitySetTextSelection( 234 int acc_obj_id, int start_offset, int end_offset) = 0; 235 236 // Forwards the given message to the renderer. These are called by 237 // the view when it has received a message. 238 virtual void ForwardMouseEvent( 239 const blink::WebMouseEvent& mouse_event) = 0; 240 virtual void ForwardWheelEvent( 241 const blink::WebMouseWheelEvent& wheel_event) = 0; 242 virtual void ForwardKeyboardEvent( 243 const NativeWebKeyboardEvent& key_event) = 0; 244 245 virtual const gfx::Vector2d& GetLastScrollOffset() const = 0; 246 247 virtual RenderProcessHost* GetProcess() const = 0; 248 249 virtual int GetRoutingID() const = 0; 250 251 // Gets the View of this RenderWidgetHost. Can be NULL, e.g. if the 252 // RenderWidget is being destroyed or the render process crashed. You should 253 // never cache this pointer since it can become NULL if the renderer crashes, 254 // instead you should always ask for it using the accessor. 255 virtual RenderWidgetHostView* GetView() const = 0; 256 257 // Returns true if the renderer is loading, false if not. 258 virtual bool IsLoading() const = 0; 259 260 // Returns true if this is a RenderViewHost, false if not. 261 virtual bool IsRenderView() const = 0; 262 263 // Called to notify the RenderWidget that the resize rect has changed without 264 // the size of the RenderWidget itself changing. 265 virtual void ResizeRectChanged(const gfx::Rect& new_rect) = 0; 266 267 // Restart the active hang monitor timeout. Clears all existing timeouts and 268 // starts with a new one. This can be because the renderer has become 269 // active, the tab is being hidden, or the user has chosen to wait some more 270 // to give the tab a chance to become active and we don't want to display a 271 // warning too soon. 272 virtual void RestartHangMonitorTimeout() = 0; 273 274 virtual void SetIgnoreInputEvents(bool ignore_input_events) = 0; 275 276 // Stops loading the page. 277 virtual void Stop() = 0; 278 279 // Called to notify the RenderWidget that it has been resized. 280 virtual void WasResized() = 0; 281 282 // Access to the implementation's IPC::Listener::OnMessageReceived. Intended 283 // only for test code. 284 285 // Add/remove a callback that can handle key presses without requiring focus. 286 typedef base::Callback<bool(const NativeWebKeyboardEvent&)> 287 KeyPressEventCallback; 288 virtual void AddKeyPressEventCallback( 289 const KeyPressEventCallback& callback) = 0; 290 virtual void RemoveKeyPressEventCallback( 291 const KeyPressEventCallback& callback) = 0; 292 293 // Add/remove a callback that can handle all kinds of mouse events. 294 typedef base::Callback<bool(const blink::WebMouseEvent&)> MouseEventCallback; 295 virtual void AddMouseEventCallback(const MouseEventCallback& callback) = 0; 296 virtual void RemoveMouseEventCallback(const MouseEventCallback& callback) = 0; 297 298 // Get the screen info corresponding to this render widget. 299 virtual void GetWebScreenInfo(blink::WebScreenInfo* result) = 0; 300 301 // Grabs snapshot from renderer side and returns the bitmap to a callback. 302 // If |src_rect| is empty, the whole contents is copied. This is an expensive 303 // operation due to the IPC, but it can be used as a fallback method when 304 // CopyFromBackingStore fails due to the backing store not being available or, 305 // in composited mode, when the accelerated surface is not available to the 306 // browser side. 307 virtual void GetSnapshotFromRenderer( 308 const gfx::Rect& src_subrect, 309 const base::Callback<void(bool, const SkBitmap&)>& callback) = 0; 310 311 virtual SkBitmap::Config PreferredReadbackFormat() = 0; 312 313 protected: 314 friend class RenderWidgetHostImpl; 315 316 // Retrieves the implementation class. Intended only for code 317 // within content/. This method is necessary because 318 // RenderWidgetHost is the root of a diamond inheritance pattern, so 319 // subclasses inherit it virtually, which removes our ability to 320 // static_cast to the subclass. 321 virtual RenderWidgetHostImpl* AsRenderWidgetHostImpl() = 0; 322}; 323 324} // namespace content 325 326#endif // CONTENT_PUBLIC_BROWSER_RENDER_WIDGET_HOST_H_ 327