Home | History | Annotate | Download | only in browser
      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_PROCESS_HOST_H_
      6 #define CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/id_map.h"
     10 #include "base/process/kill.h"
     11 #include "base/process/process_handle.h"
     12 #include "base/supports_user_data.h"
     13 #include "content/common/content_export.h"
     14 #include "ipc/ipc_channel_proxy.h"
     15 #include "ipc/ipc_sender.h"
     16 #include "ui/gfx/native_widget_types.h"
     17 #include "ui/surface/transport_dib.h"
     18 
     19 class GURL;
     20 struct ViewMsg_SwapOut_Params;
     21 
     22 namespace base {
     23 class TimeDelta;
     24 }
     25 
     26 namespace content {
     27 class BrowserContext;
     28 class BrowserMessageFilter;
     29 class RenderProcessHostObserver;
     30 class RenderWidgetHost;
     31 class StoragePartition;
     32 
     33 typedef base::Thread* (*RendererMainThreadFactoryFunction)(
     34     const std::string& id);
     35 
     36 // Interface that represents the browser side of the browser <-> renderer
     37 // communication channel. There will generally be one RenderProcessHost per
     38 // renderer process.
     39 class CONTENT_EXPORT RenderProcessHost : public IPC::Sender,
     40                                          public IPC::Listener,
     41                                          public base::SupportsUserData {
     42  public:
     43   typedef IDMap<RenderProcessHost>::iterator iterator;
     44 
     45   // Details for RENDERER_PROCESS_CLOSED notifications.
     46   struct RendererClosedDetails {
     47     RendererClosedDetails(base::ProcessHandle handle,
     48                           base::TerminationStatus status,
     49                           int exit_code) {
     50       this->handle = handle;
     51       this->status = status;
     52       this->exit_code = exit_code;
     53     }
     54     base::ProcessHandle handle;
     55     base::TerminationStatus status;
     56     int exit_code;
     57   };
     58 
     59   // General functions ---------------------------------------------------------
     60 
     61   virtual ~RenderProcessHost() {}
     62 
     63   // Initialize the new renderer process, returning true on success. This must
     64   // be called once before the object can be used, but can be called after
     65   // that with no effect. Therefore, if the caller isn't sure about whether
     66   // the process has been created, it should just call Init().
     67   virtual bool Init() = 0;
     68 
     69   // Gets the next available routing id.
     70   virtual int GetNextRoutingID() = 0;
     71 
     72   // These methods add or remove listener for a specific message routing ID.
     73   // Used for refcounting, each holder of this object must AddRoute and
     74   // RemoveRoute. This object should be allocated on the heap; when no
     75   // listeners own it any more, it will delete itself.
     76   virtual void AddRoute(int32 routing_id, IPC::Listener* listener) = 0;
     77   virtual void RemoveRoute(int32 routing_id) = 0;
     78 
     79   // Add and remove observers for lifecycle events. The order in which
     80   // notifications are sent to observers is undefined. Observers must be sure to
     81   // remove the observer before they go away.
     82   virtual void AddObserver(RenderProcessHostObserver* observer) = 0;
     83   virtual void RemoveObserver(RenderProcessHostObserver* observer) = 0;
     84 
     85   // Called to wait for the next UpdateRect message for the specified render
     86   // widget.  Returns true if successful, and the msg out-param will contain a
     87   // copy of the received UpdateRect message.
     88   virtual bool WaitForBackingStoreMsg(int render_widget_id,
     89                                       const base::TimeDelta& max_delay,
     90                                       IPC::Message* msg) = 0;
     91 
     92   // Called when a received message cannot be decoded.
     93   virtual void ReceivedBadMessage() = 0;
     94 
     95   // Track the count of visible widgets. Called by listeners to register and
     96   // unregister visibility.
     97   virtual void WidgetRestored() = 0;
     98   virtual void WidgetHidden() = 0;
     99   virtual int VisibleWidgetCount() const = 0;
    100 
    101   // Indicates whether the current RenderProcessHost associated with a guest
    102   // renderer process.
    103   virtual bool IsGuest() const = 0;
    104 
    105   // Returns the storage partition associated with this process.
    106   //
    107   // TODO(nasko): Remove this function from the public API once
    108   // URLRequestContextGetter's creation is moved into StoragePartition.
    109   // http://crbug.com/158595
    110   virtual StoragePartition* GetStoragePartition() const = 0;
    111 
    112   // Try to shutdown the associated renderer process as fast as possible.
    113   // If this renderer has any RenderViews with unload handlers, then this
    114   // function does nothing.  The current implementation uses TerminateProcess.
    115   // Returns True if it was able to do fast shutdown.
    116   virtual bool FastShutdownIfPossible() = 0;
    117 
    118   // Returns true if fast shutdown was started for the renderer.
    119   virtual bool FastShutdownStarted() const = 0;
    120 
    121   // Dump the child process' handle table before shutting down.
    122   virtual void DumpHandles() = 0;
    123 
    124   // Returns the process object associated with the child process.  In certain
    125   // tests or single-process mode, this will actually represent the current
    126   // process.
    127   //
    128   // NOTE: this is not necessarily valid immediately after calling Init, as
    129   // Init starts the process asynchronously.  It's guaranteed to be valid after
    130   // the first IPC arrives.
    131   virtual base::ProcessHandle GetHandle() const = 0;
    132 
    133   // Transport DIB functions ---------------------------------------------------
    134 
    135   // Return the TransportDIB for the given id. On Linux, this can involve
    136   // mapping shared memory. On Mac, the shared memory is created in the browser
    137   // process and the cached metadata is returned. On Windows, this involves
    138   // duplicating the handle from the remote process.  The RenderProcessHost
    139   // still owns the returned DIB.
    140   virtual TransportDIB* GetTransportDIB(TransportDIB::Id dib_id) = 0;
    141 
    142   // Return the TransportDIB for the given id. In contrast to GetTransportDIB,
    143   // the caller owns the resulting TransportDIB.
    144   virtual TransportDIB* MapTransportDIB(TransportDIB::Id dib_id) = 0;
    145 
    146   // Returns the user browser context associated with this renderer process.
    147   virtual content::BrowserContext* GetBrowserContext() const = 0;
    148 
    149   // Returns whether this process is using the same StoragePartition as
    150   // |partition|.
    151   virtual bool InSameStoragePartition(StoragePartition* partition) const = 0;
    152 
    153   // Returns the unique ID for this child process. This can be used later in
    154   // a call to FromID() to get back to this object (this is used to avoid
    155   // sending non-threadsafe pointers to other threads).
    156   //
    157   // This ID will be unique for all child processes, including workers, plugins,
    158   // etc.
    159   virtual int GetID() const = 0;
    160 
    161   // Returns true iff channel_ has been set to non-NULL. Use this for checking
    162   // if there is connection or not. Virtual for mocking out for tests.
    163   virtual bool HasConnection() const = 0;
    164 
    165   // Call this to allow queueing of IPC messages that are sent before the
    166   // process is launched.
    167   virtual void EnableSendQueue() = 0;
    168 
    169   // Returns the renderer channel.
    170   virtual IPC::ChannelProxy* GetChannel() = 0;
    171 
    172   // Adds a message filter to the IPC channel.
    173   virtual void AddFilter(BrowserMessageFilter* filter) = 0;
    174 
    175   // Try to shutdown the associated render process as fast as possible
    176   virtual bool FastShutdownForPageCount(size_t count) = 0;
    177 
    178   // TODO(ananta)
    179   // Revisit whether the virtual functions declared from here on need to be
    180   // part of the interface.
    181   virtual void SetIgnoreInputEvents(bool ignore_input_events) = 0;
    182   virtual bool IgnoreInputEvents() const = 0;
    183 
    184   // Schedules the host for deletion and removes it from the all_hosts list.
    185   virtual void Cleanup() = 0;
    186 
    187   // Track the count of pending views that are being swapped back in.  Called
    188   // by listeners to register and unregister pending views to prevent the
    189   // process from exiting.
    190   virtual void AddPendingView() = 0;
    191   virtual void RemovePendingView() = 0;
    192 
    193   // Sets a flag indicating that the process can be abnormally terminated.
    194   virtual void SetSuddenTerminationAllowed(bool allowed) = 0;
    195   // Returns true if the process can be abnormally terminated.
    196   virtual bool SuddenTerminationAllowed() const = 0;
    197 
    198   // Returns how long the child has been idle. The definition of idle
    199   // depends on when a derived class calls mark_child_process_activity_time().
    200   // This is a rough indicator and its resolution should not be better than
    201   // 10 milliseconds.
    202   virtual base::TimeDelta GetChildProcessIdleTime() const = 0;
    203 
    204   // Signals that a compositing surface has been updated after a lost context
    205   // event, so that we can process requests from the renderer to create contexts
    206   // with that surface.
    207   virtual void SurfaceUpdated(int32 surface_id) = 0;
    208 
    209   // Called to resume the requests for a view created through window.open that
    210   // were initially blocked.
    211   virtual void ResumeRequestsForView(int route_id) = 0;
    212 
    213   // Static management functions -----------------------------------------------
    214 
    215   // Flag to run the renderer in process.  This is primarily
    216   // for debugging purposes.  When running "in process", the
    217   // browser maintains a single RenderProcessHost which communicates
    218   // to a RenderProcess which is instantiated in the same process
    219   // with the Browser.  All IPC between the Browser and the
    220   // Renderer is the same, it's just not crossing a process boundary.
    221 
    222   static bool run_renderer_in_process();
    223 
    224   // This also calls out to ContentBrowserClient::GetApplicationLocale and
    225   // modifies the current process' command line.
    226   static void SetRunRendererInProcess(bool value);
    227 
    228   // Allows iteration over all the RenderProcessHosts in the browser. Note
    229   // that each host may not be active, and therefore may have NULL channels.
    230   static iterator AllHostsIterator();
    231 
    232   // Returns the RenderProcessHost given its ID.  Returns NULL if the ID does
    233   // not correspond to a live RenderProcessHost.
    234   static RenderProcessHost* FromID(int render_process_id);
    235 
    236   // Returns whether the process-per-site model is in use (globally or just for
    237   // the current site), in which case we should ensure there is only one
    238   // RenderProcessHost per site for the entire browser context.
    239   static bool ShouldUseProcessPerSite(content::BrowserContext* browser_context,
    240                                       const GURL& url);
    241 
    242   // Returns true if the caller should attempt to use an existing
    243   // RenderProcessHost rather than creating a new one.
    244   static bool ShouldTryToUseExistingProcessHost(
    245       content::BrowserContext* browser_context, const GURL& site_url);
    246 
    247   // Get an existing RenderProcessHost associated with the given browser
    248   // context, if possible.  The renderer process is chosen randomly from
    249   // suitable renderers that share the same context and type (determined by the
    250   // site url).
    251   // Returns NULL if no suitable renderer process is available, in which case
    252   // the caller is free to create a new renderer.
    253   static RenderProcessHost* GetExistingProcessHost(
    254       content::BrowserContext* browser_context, const GURL& site_url);
    255 
    256   // Overrides the default heuristic for limiting the max renderer process
    257   // count.  This is useful for unit testing process limit behaviors.  It is
    258   // also used to allow a command line parameter to configure the max number of
    259   // renderer processes and should only be called once during startup.
    260   // A value of zero means to use the default heuristic.
    261   static void SetMaxRendererProcessCount(size_t count);
    262 
    263   // Returns the current max number of renderer processes used by the content
    264   // module.
    265   static size_t GetMaxRendererProcessCount();
    266 
    267   static void RegisterRendererMainThreadFactory(
    268       RendererMainThreadFactoryFunction create);
    269 };
    270 
    271 }  // namespace content.
    272 
    273 #endif  // CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
    274