// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_H_ #define CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_H_ #include #include #include "base/gfx/size.h" #include "base/scoped_ptr.h" #include "base/timer.h" #include "chrome/common/ipc_channel.h" #include "chrome/common/native_web_keyboard_event.h" #include "testing/gtest/include/gtest/gtest_prod.h" #include "webkit/glue/webtextdirection.h" namespace gfx { class Rect; } namespace WebKit { class WebInputEvent; class WebMouseEvent; class WebMouseWheelEvent; } class BackingStore; class PaintObserver; class RenderProcessHost; class RenderWidgetHostView; class TransportDIB; class WebCursor; struct ViewHostMsg_PaintRect_Params; struct ViewHostMsg_ScrollRect_Params; // This class manages the browser side of a browser<->renderer HWND connection. // The HWND lives in the browser process, and windows events are sent over // IPC to the corresponding object in the renderer. The renderer paints into // shared memory, which we transfer to a backing store and blit to the screen // when Windows sends us a WM_PAINT message. // // How Shutdown Works // // There are two situations in which this object, a RenderWidgetHost, can be // instantiated: // // 1. By a WebContents as the communication conduit for a rendered web page. // The WebContents instantiates a derived class: RenderViewHost. // 2. By a WebContents as the communication conduit for a select widget. The // WebContents instantiates the RenderWidgetHost directly. // // For every WebContents there are several objects in play that need to be // properly destroyed or cleaned up when certain events occur. // // - WebContents - the TabContents itself, and its associated HWND. // - RenderViewHost - representing the communication conduit with the child // process. // - RenderWidgetHostView - the view of the web page content, message handler, // and plugin root. // // Normally, the WebContents contains a child RenderWidgetHostView that renders // the contents of the loaded page. It has a WS_CLIPCHILDREN style so that it // does no painting of its own. // // The lifetime of the RenderWidgetHostView is tied to the render process. If // the render process dies, the RenderWidgetHostView goes away and all // references to it must become NULL. If the WebContents finds itself without a // RenderWidgetHostView, it paints Sad Tab instead. // // RenderViewHost (a RenderWidgetHost subclass) is the conduit used to // communicate with the RenderView and is owned by the WebContents. If the // render process crashes, the RenderViewHost remains and restarts the render // process if needed to continue navigation. // // The WebContents is itself owned by the NavigationController in which it // resides. // // Some examples of how shutdown works: // // When a tab is closed (either by the user, the web page calling window.close, // etc) the TabStrip destroys the associated NavigationController, which calls // Destroy on each TabContents it owns. // // For a WebContents, its Destroy method tells the RenderViewHost to // shut down the render process and die. // // When the render process is destroyed it destroys the View: the // RenderWidgetHostView, which destroys its HWND and deletes that object. // // For select popups, the situation is a little different. The RenderWidgetHost // associated with the select popup owns the view and itself (is responsible // for destroying itself when the view is closed). The WebContents's only // responsibility is to select popups is to create them when it is told to. When // the View is destroyed via an IPC message (for when WebCore destroys the // popup, e.g. if the user selects one of the options), or because // WM_CANCELMODE is received by the view, the View schedules the destruction of // the render process. However in this case since there's no WebContents // container, when the render process is destroyed, the RenderWidgetHost just // deletes itself, which is safe because no one else should have any references // to it (the WebContents does not). // // It should be noted that the RenderViewHost, not the RenderWidgetHost, // handles IPC messages relating to the render process going away, since the // way a RenderViewHost (WebContents) handles the process dying is different to // the way a select popup does. As such the RenderWidgetHostView handles these // messages for select popups. This placement is more out of convenience than // anything else. When the view is live, these messages are forwarded to it by // the RenderWidgetHost's IPC message map. // class RenderWidgetHost : public IPC::Channel::Listener { public: // An interface that gets called whenever a paint occurs. // Used in performance tests. class PaintObserver { public: virtual ~PaintObserver() {} virtual void RenderWidgetHostDidPaint(RenderWidgetHost* rwh) = 0; }; // routing_id can be MSG_ROUTING_NONE, in which case the next available // routing id is taken from the RenderProcessHost. RenderWidgetHost(RenderProcessHost* process, int routing_id); virtual ~RenderWidgetHost(); // Gets/Sets the View of this RenderWidgetHost. Can be NULL, e.g. if the // RenderWidget is being destroyed or the render process crashed. You should // never cache this pointer since it can become NULL if the renderer crashes, // instead you should always ask for it using the accessor. void set_view(RenderWidgetHostView* view) { view_ = view; } RenderWidgetHostView* view() const { return view_; } RenderProcessHost* process() const { return process_; } int routing_id() const { return routing_id_; } // Set the PaintObserver on this object. Takes ownership. void set_paint_observer(PaintObserver* paint_observer) { paint_observer_.reset(paint_observer); } // Called when a renderer object already been created for this host, and we // just need to be attached to it. Used for window.open,