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